]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_io.cxx
Adapt for latest SimGear changes and insert missing bracket
[flightgear.git] / src / Main / fg_io.cxx
1 // fg_io.cxx -- higher level I/O channel management routines
2 //
3 // Written by Curtis Olson, started November 1999.
4 //
5 // Copyright (C) 1999  Curtis L. Olson - http://www.flightgear.org/~curt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include <simgear/compiler.h>
28
29 #include <cstdlib>             // atoi()
30
31 #include <string>
32 #include <algorithm>
33
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/io/iochannel.hxx>
36 #include <simgear/io/sg_file.hxx>
37 #include <simgear/io/sg_serial.hxx>
38 #include <simgear/io/sg_socket.hxx>
39 #include <simgear/io/sg_socket_udp.hxx>
40 #include <simgear/math/sg_types.hxx>
41 #include <simgear/timing/timestamp.hxx>
42 #include <simgear/misc/strutils.hxx>
43
44 #include <Network/protocol.hxx>
45 #include <Network/ATC-Main.hxx>
46 #include <Network/atlas.hxx>
47 #include <Network/AV400.hxx>
48 #include <Network/AV400Sim.hxx>
49 #include <Network/AV400WSim.hxx>
50 #include <Network/garmin.hxx>
51 #include <Network/httpd.hxx>
52 #ifdef FG_JPEG_SERVER
53 #  include <Network/jpg-httpd.hxx>
54 #endif
55 #include <Network/joyclient.hxx>
56 #include <Network/jsclient.hxx>
57 #include <Network/native.hxx>
58 #include <Network/native_ctrls.hxx>
59 #include <Network/native_fdm.hxx>
60 #include <Network/native_gui.hxx>
61 #include <Network/opengc.hxx>
62 #include <Network/nmea.hxx>
63 #include <Network/props.hxx>
64 #include <Network/pve.hxx>
65 #include <Network/ray.hxx>
66 #include <Network/rul.hxx>
67 #include <Network/generic.hxx>
68 #include <Network/HTTPClient.hxx>
69
70 #ifdef FG_HAVE_HLA
71 #include <Network/HLA/hla.hxx>
72 #endif
73
74 #include "globals.hxx"
75 #include "fg_io.hxx"
76
77 using std::atoi;
78 using std::string;
79
80
81 FGIO::FGIO()
82 {
83 }
84
85
86 FGIO::~FGIO()
87 {
88
89 }
90
91
92 // configure a port based on the config string
93 FGProtocol*
94 FGIO::parse_port_config( const string& config )
95 {
96     SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config );
97
98     string_list tokens = simgear::strutils::split( config, "," );
99     if (tokens.empty())
100     {
101         SG_LOG( SG_IO, SG_ALERT,
102                 "Port configuration error: empty config string" );
103         return NULL;
104     }
105
106     string protocol = tokens[0];
107     SG_LOG( SG_IO, SG_INFO, "  protocol = " << protocol );
108
109     FGProtocol *io = NULL;
110
111     try
112     {
113         if ( protocol == "atcsim" ) {
114             FGATCMain *atcsim = new FGATCMain;
115             atcsim->set_hz( 30 );
116             if ( tokens.size() != 6 ) {
117                 SG_LOG( SG_IO, SG_ALERT, "Usage: --atcsim=[no-]pedals,"
118                         << "input0_config,input1_config,"
119                         << "output0_config,output1_config,file.nas" );
120                 delete atcsim;
121                 return NULL;
122             }
123             if ( tokens[1] == "no-pedals" ) {
124                 fgSetBool( "/input/atcsim/ignore-pedal-controls", true );
125             } else {
126                 fgSetBool( "/input/atcsim/ignore-pedal-controls", false );
127             }
128             atcsim->set_path_names(tokens[2], tokens[3], tokens[4], tokens[5]);
129             return atcsim;
130         } else if ( protocol == "atlas" ) {
131             FGAtlas *atlas = new FGAtlas;
132             io = atlas;
133         } else if ( protocol == "opengc" ) {
134             FGOpenGC *opengc = new FGOpenGC;
135             io = opengc;
136         } else if ( protocol == "AV400" ) {
137             FGAV400 *av400 = new FGAV400;
138             io = av400;
139         } else if ( protocol == "AV400Sim" ) {
140             FGAV400Sim *av400sim = new FGAV400Sim;
141             io = av400sim;
142         } else if ( protocol == "AV400WSimA" ) {
143             FGAV400WSimA *av400wsima = new FGAV400WSimA;
144             io = av400wsima;
145         } else if ( protocol == "AV400WSimB" ) {
146             FGAV400WSimB *av400wsimb = new FGAV400WSimB;
147             io = av400wsimb;
148         } else if ( protocol == "garmin" ) {
149             FGGarmin *garmin = new FGGarmin;
150             io = garmin;
151         } else if ( protocol == "httpd" ) {
152             // determine port
153             string port = tokens[1];
154             return new FGHttpd( atoi(port.c_str()) );
155 #ifdef FG_JPEG_SERVER
156         } else if ( protocol == "jpg-httpd" ) {
157             // determine port
158             string port = tokens[1];
159             return new FGJpegHttpd( atoi(port.c_str()) );
160 #endif
161         } else if ( protocol == "joyclient" ) {
162             FGJoyClient *joyclient = new FGJoyClient;
163             io = joyclient;
164         } else if ( protocol == "jsclient" ) {
165             FGJsClient *jsclient = new FGJsClient;
166             io = jsclient;
167         } else if ( protocol == "native" ) {
168             FGNative *native = new FGNative;
169             io = native;
170         } else if ( protocol == "native-ctrls" ) {
171             FGNativeCtrls *native_ctrls = new FGNativeCtrls;
172             io = native_ctrls;
173         } else if ( protocol == "native-fdm" ) {
174             FGNativeFDM *native_fdm = new FGNativeFDM;
175             io = native_fdm;
176         } else if ( protocol == "native-gui" ) {
177             FGNativeGUI *net_gui = new FGNativeGUI;
178             io = net_gui;
179         } else if ( protocol == "nmea" ) {
180             FGNMEA *nmea = new FGNMEA;
181             io = nmea;
182         } else if ( protocol == "props" || protocol == "telnet" ) {
183             io = new FGProps( tokens );
184             return io;
185         } else if ( protocol == "pve" ) {
186             FGPVE *pve = new FGPVE;
187             io = pve;
188         } else if ( protocol == "ray" ) {
189             FGRAY *ray = new FGRAY;
190             io = ray;
191         } else if ( protocol == "rul" ) {
192             FGRUL *rul = new FGRUL;
193             io = rul;
194         } else if ( protocol == "generic" ) {
195             FGGeneric *generic = new FGGeneric( tokens );
196             if (!generic->getInitOk())
197             {
198                 // failed to initialize (i.e. invalid configuration)
199                 delete generic;
200                 return NULL;
201             }
202             io = generic;
203         } else if ( protocol == "multiplay" ) {
204             if ( tokens.size() != 5 ) {
205                 SG_LOG( SG_IO, SG_ALERT, "Ignoring invalid --multiplay option "
206                         "(4 arguments expected: --multiplay=dir,hz,hostname,port)" );
207                 return NULL;
208             }
209             string dir = tokens[1];
210             int rate = atoi(tokens[2].c_str());
211             string host = tokens[3];
212
213             short port = atoi(tokens[4].c_str());
214
215             // multiplay used to be handled by an FGProtocol, but no longer. This code
216             // retains compatibility with existing command-line syntax
217             fgSetInt("/sim/multiplay/tx-rate-hz", rate);
218             if (dir == "in") {
219                 fgSetInt("/sim/multiplay/rxport", port);
220                 fgSetString("/sim/multiplay/rxhost", host.c_str());
221             } else if (dir == "out") {
222                 fgSetInt("/sim/multiplay/txport", port);
223                 fgSetString("/sim/multiplay/txhost", host.c_str());
224             }
225
226             return NULL;
227         }
228 #ifdef FG_HAVE_HLA
229         else if ( protocol == "hla" ) {
230             return new FGHLA(tokens);
231         }
232 #endif
233         else {
234             return NULL;
235         }
236     }
237     catch (FGProtocolConfigError& err)
238     {
239         SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
240         delete io;
241         return NULL;
242     }
243
244     if (tokens.size() < 3) {
245         SG_LOG( SG_IO, SG_ALERT, "Incompatible number of network arguments.");
246         delete io;
247         return NULL;
248     }
249     string medium = tokens[1];
250     SG_LOG( SG_IO, SG_INFO, "  medium = " << medium );
251
252     string direction = tokens[2];
253     io->set_direction( direction );
254     SG_LOG( SG_IO, SG_INFO, "  direction = " << direction );
255
256     string hertz_str = tokens[3];
257     double hertz = atof( hertz_str.c_str() );
258     io->set_hz( hertz );
259     SG_LOG( SG_IO, SG_INFO, "  hertz = " << hertz );
260
261     if ( medium == "serial" ) {
262         if ( tokens.size() < 5) {
263             SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for serial communications.");
264             delete io;
265             return NULL;
266         }
267         // device name
268         string device = tokens[4];
269         SG_LOG( SG_IO, SG_INFO, "  device = " << device );
270
271         // baud
272         string baud = tokens[5];
273         SG_LOG( SG_IO, SG_INFO, "  baud = " << baud );
274
275
276         SGSerial *ch = new SGSerial( device, baud );
277         io->set_io_channel( ch );
278
279         if ( protocol == "AV400WSimB" ) {
280             if ( tokens.size() < 7 ) {
281                 SG_LOG( SG_IO, SG_ALERT, "Missing second hz for AV400WSimB.");
282                 delete io;
283                 return NULL;
284             }
285             FGAV400WSimB *fgavb = static_cast<FGAV400WSimB*>(io);
286             string hz2_str = tokens[6];
287             double hz2 = atof(hz2_str.c_str());
288             fgavb->set_hz2(hz2);
289         }
290     } else if ( medium == "file" ) {
291         // file name
292         if ( tokens.size() < 4) {
293             SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for file I/O.");
294             delete io;
295             return NULL;
296         }
297
298         string file = tokens[4];
299         SG_LOG( SG_IO, SG_INFO, "  file name = " << file );
300         int repeat = 1;
301         if (tokens.size() >= 7 && tokens[6] == "repeat") {
302             if (tokens.size() >= 8) {
303                 repeat = atoi(tokens[7].c_str());
304                 FGGeneric* generic = dynamic_cast<FGGeneric*>(io);
305                 if (generic)
306                     generic->setExitOnError(true);
307             } else {
308                 repeat = -1;
309             }
310         }
311         SGFile *ch = new SGFile( file, repeat );
312         io->set_io_channel( ch );
313     } else if ( medium == "socket" ) {
314         if ( tokens.size() < 6) {
315             SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for socket communications.");
316             delete io;
317             return NULL;
318         }
319         string hostname = tokens[4];
320         string port = tokens[5];
321         string style = tokens[6];
322
323         SG_LOG( SG_IO, SG_INFO, "  hostname = " << hostname );
324         SG_LOG( SG_IO, SG_INFO, "  port = " << port );
325         SG_LOG( SG_IO, SG_INFO, "  style = " << style );
326
327         io->set_io_channel( new SGSocket( hostname, port, style ) );
328     }
329
330     return io;
331 }
332
333
334 // step through the port config streams (from fgOPTIONS) and setup
335 // serial port channels for each
336 void
337 FGIO::init()
338 {
339     // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
340     //         globals->get_channel_options_list()->size() << " requests." );
341
342     _realDeltaTime = fgGetNode("/sim/time/delta-realtime-sec");
343
344     // we could almost do this in a single step except pushing a valid
345     // port onto the port list copies the structure and destroys the
346     // original, which closes the port and frees up the fd ... doh!!!
347
348     string_list::iterator i = globals->get_channel_options_list()->begin();
349     string_list::iterator end = globals->get_channel_options_list()->end();
350     for (; i != end; ++i ) {
351         add_channel( *i );
352     } // of channel options iteration
353 }
354
355 // add another I/O channel
356 void FGIO::add_channel(const string& config)
357 {
358     // parse the configuration string and store the results in the
359     // appropriate FGIOChannel structure
360     FGProtocol *p = parse_port_config( config );
361     if (!p)
362     {
363         return;
364     }
365
366     p->open();
367     if ( !p->is_enabled() ) {
368         SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
369         delete p;
370         return;
371     }
372
373     io_channels.push_back( p );
374 }
375
376 void
377 FGIO::reinit()
378 {
379 }
380
381 // process any IO channel work
382 void
383 FGIO::update( double /* delta_time_sec */ )
384 {
385     if (FGHTTPClient::haveInstance()) {
386         FGHTTPClient::instance()->update();
387     }
388     
389     // use wall-clock, not simulation, delta time, so that network
390     // protocols update when the simulation is paused
391     // see http://code.google.com/p/flightgear-bugs/issues/detail?id=125
392     double delta_time_sec = _realDeltaTime->getDoubleValue();
393
394     ProtocolVec::iterator i = io_channels.begin();
395     ProtocolVec::iterator end = io_channels.end();
396     for (; i != end; ++i ) {
397         FGProtocol* p = *i;
398         if (!p->is_enabled()) {
399             continue;
400         }
401
402         p->dec_count_down( delta_time_sec );
403         double dt = 1 / p->get_hz();
404         if ( p->get_count_down() < 0.33 * dt ) {
405             p->process();
406             p->inc_count();
407             while ( p->get_count_down() < 0.33 * dt ) {
408                 p->inc_count_down( dt );
409             }
410         } // of channel processing
411     } // of io_channels iteration
412 }
413
414 void
415 FGIO::shutdown()
416 {
417     ProtocolVec::iterator i = io_channels.begin();
418     ProtocolVec::iterator end = io_channels.end();
419     for (; i != end; ++i )
420     {
421         FGProtocol *p = *i;
422         if ( p->is_enabled() ) {
423             p->close();
424         }
425
426         delete p;
427     }
428
429     io_channels.clear();
430 }
431
432 void
433 FGIO::bind()
434 {
435 }
436
437 void
438 FGIO::unbind()
439 {
440 }