]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_io.cxx
Support loading PropertyList data remotely, as part of #450. Along the way, centralis...
[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
87
88
89 FGIO::~FGIO()
90 {
91
92 }
93
94
95 // configure a port based on the config string
96 FGProtocol*
97 FGIO::parse_port_config( const string& config )
98 {
99     SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config );
100
101     string_list tokens = simgear::strutils::split( config, "," );
102     if (tokens.empty())
103     {
104         SG_LOG( SG_IO, SG_ALERT,
105                 "Port configuration error: empty config string" );
106         return 0;
107     }
108
109     string protocol = tokens[0];
110     SG_LOG( SG_IO, SG_INFO, "  protocol = " << protocol );
111
112     FGProtocol *io = 0;
113
114     try
115     {
116         if ( protocol == "atcsim" ) {
117             FGATCMain *atcsim = new FGATCMain;
118             atcsim->set_hz( 30 );
119             if ( tokens.size() != 6 ) {
120                 SG_LOG( SG_IO, SG_ALERT, "Usage: --atcsim=[no-]pedals,"
121                         << "input0_config,input1_config,"
122                         << "output0_config,output1_config,file.nas" );
123                 delete atcsim;
124                 return NULL;
125             }
126             if ( tokens[1] == "no-pedals" ) {
127                 fgSetBool( "/input/atcsim/ignore-pedal-controls", true );
128             } else {
129                 fgSetBool( "/input/atcsim/ignore-pedal-controls", false );
130             }
131             atcsim->set_path_names(tokens[2], tokens[3], tokens[4], tokens[5]);
132             return atcsim;
133         } else if ( protocol == "atlas" ) {
134             FGAtlas *atlas = new FGAtlas;
135             io = atlas;
136         } else if ( protocol == "opengc" ) {
137             // char wait;
138             // printf("Parsed opengc\n"); cin >> wait;
139             FGOpenGC *opengc = new FGOpenGC;
140             io = opengc;
141         } else if ( protocol == "AV400" ) {
142             FGAV400 *av400 = new FGAV400;
143             io = av400;
144         } else if ( protocol == "AV400Sim" ) {
145             FGAV400Sim *av400sim = new FGAV400Sim;
146             io = av400sim;
147         } else if ( protocol == "AV400WSimA" ) {
148             FGAV400WSimA *av400wsima = new FGAV400WSimA;
149             io = av400wsima;
150         } else if ( protocol == "AV400WSimB" ) {
151             FGAV400WSimB *av400wsimb = new FGAV400WSimB;
152             io = av400wsimb;
153         } else if ( protocol == "garmin" ) {
154             FGGarmin *garmin = new FGGarmin;
155             io = garmin;
156         } else if ( protocol == "httpd" ) {
157             // determine port
158             string port = tokens[1];
159             return new FGHttpd( atoi(port.c_str()) );
160 #ifdef FG_JPEG_SERVER
161         } else if ( protocol == "jpg-httpd" ) {
162             // determine port
163             string port = tokens[1];
164             return new FGJpegHttpd( atoi(port.c_str()) );
165 #endif
166         } else if ( protocol == "joyclient" ) {
167             FGJoyClient *joyclient = new FGJoyClient;
168             io = joyclient;
169         } else if ( protocol == "jsclient" ) {
170             FGJsClient *jsclient = new FGJsClient;
171             io = jsclient;
172         } else if ( protocol == "native" ) {
173             FGNative *native = new FGNative;
174             io = native;
175         } else if ( protocol == "native-ctrls" ) {
176             FGNativeCtrls *native_ctrls = new FGNativeCtrls;
177             io = native_ctrls;
178         } else if ( protocol == "native-fdm" ) {
179             FGNativeFDM *native_fdm = new FGNativeFDM;
180             io = native_fdm;
181         } else if ( protocol == "native-gui" ) {
182             FGNativeGUI *net_gui = new FGNativeGUI;
183             io = net_gui;
184         } else if ( protocol == "nmea" ) {
185             FGNMEA *nmea = new FGNMEA;
186             io = nmea;
187         } else if ( protocol == "props" || protocol == "telnet" ) {
188             io = new FGProps( tokens );
189             return io;
190         } else if ( protocol == "pve" ) {
191             FGPVE *pve = new FGPVE;
192             io = pve;
193         } else if ( protocol == "ray" ) {
194             FGRAY *ray = new FGRAY;
195             io = ray;
196         } else if ( protocol == "rul" ) {
197             FGRUL *rul = new FGRUL;
198             io = rul;
199         } else if ( protocol == "generic" ) {
200             size_t configToken;
201             if (tokens[1] == "socket") {
202                 configToken = 7;
203             } else if (tokens[1] == "file") {
204                 configToken = 5;
205             } else {
206                 configToken = 6;
207             }
208
209             if (configToken >= tokens.size()) {
210                 SG_LOG( SG_IO, SG_ALERT, "Not enough tokens passed for the generic protocol.");
211                 return NULL;
212             }
213
214             FGGeneric *generic = new FGGeneric( tokens );
215             io = generic;
216         } else if ( protocol == "multiplay" ) {
217             if ( tokens.size() != 5 ) {
218                 SG_LOG( SG_IO, SG_ALERT, "Ignoring invalid --multiplay option "
219                         "(4 arguments expected: --multiplay=dir,hz,hostname,port)" );
220                 return NULL;
221             }
222             string dir = tokens[1];
223             int rate = atoi(tokens[2].c_str());
224             string host = tokens[3];
225
226              short port = atoi(tokens[4].c_str());
227
228         // multiplay used to be handled by an FGProtocol, but no longer. This code
229         // retains compatability with existing command-line syntax
230           fgSetInt("/sim/multiplay/tx-rate-hz", rate);
231           if (dir == "in") {
232             fgSetInt("/sim/multiplay/rxport", port);
233             fgSetString("/sim/multiplay/rxhost", host.c_str());
234           } else if (dir == "out") {
235             fgSetInt("/sim/multiplay/txport", port);
236             fgSetString("/sim/multiplay/txhost", host.c_str());
237           }
238
239           return NULL;
240 #ifdef FG_HAVE_HLA
241         } else if ( protocol == "hla" ) {
242             return new FGHLA(tokens);
243 #endif
244         } else {
245             return NULL;
246         }
247     }
248     catch (FGProtocolConfigError& err)
249     {
250         SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
251         delete io;
252         return 0;
253     }
254
255     if (tokens.size() < 3) {
256       SG_LOG( SG_IO, SG_ALERT, "Incompatible number of network arguments.");
257       return NULL;
258     }
259     string medium = tokens[1];
260     SG_LOG( SG_IO, SG_INFO, "  medium = " << medium );
261
262     string direction = tokens[2];
263     io->set_direction( direction );
264     SG_LOG( SG_IO, SG_INFO, "  direction = " << direction );
265
266     string hertz_str = tokens[3];
267     double hertz = atof( hertz_str.c_str() );
268     io->set_hz( hertz );
269     SG_LOG( SG_IO, SG_INFO, "  hertz = " << hertz );
270
271     if ( medium == "serial" ) {
272         if ( tokens.size() < 5) {
273           SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for serial communications.");
274           return NULL;
275         }
276         // device name
277         string device = tokens[4];
278         SG_LOG( SG_IO, SG_INFO, "  device = " << device );
279
280         // baud
281         string baud = tokens[5];
282         SG_LOG( SG_IO, SG_INFO, "  baud = " << baud );
283
284
285         SGSerial *ch = new SGSerial( device, baud );
286         io->set_io_channel( ch );
287
288         if ( protocol == "AV400WSimB" ) {
289             if ( tokens.size() < 7 ) {
290                 SG_LOG( SG_IO, SG_ALERT, "Missing second hz for AV400WSimB.");
291                 return NULL;
292             }
293             FGAV400WSimB *fgavb = static_cast<FGAV400WSimB*>(io);
294             string hz2_str = tokens[6];
295             double hz2 = atof(hz2_str.c_str());
296             fgavb->set_hz2(hz2);
297         }
298     } else if ( medium == "file" ) {
299         // file name
300         if ( tokens.size() < 4) {
301           SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for file I/O.");
302           return NULL;
303         }
304
305         string file = tokens[4];
306         SG_LOG( SG_IO, SG_INFO, "  file name = " << file );
307         int repeat = 1;
308         if (tokens.size() >= 7 && tokens[6] == "repeat") {
309             if (tokens.size() >= 8) {
310                 repeat = atoi(tokens[7].c_str());
311                 FGGeneric* generic = dynamic_cast<FGGeneric*>(io);
312                 if (generic)
313                     generic->setExitOnError(true);
314             } else {
315                 repeat = -1;
316             }
317         }
318         SGFile *ch = new SGFile( file, repeat );
319         io->set_io_channel( ch );
320     } else if ( medium == "socket" ) {
321         if ( tokens.size() < 6) {
322           SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for socket communications.");
323           return NULL;
324         }
325         string hostname = tokens[4];
326         string port = tokens[5];
327         string style = tokens[6];
328
329         SG_LOG( SG_IO, SG_INFO, "  hostname = " << hostname );
330         SG_LOG( SG_IO, SG_INFO, "  port = " << port );
331         SG_LOG( SG_IO, SG_INFO, "  style = " << style );
332
333         io->set_io_channel( new SGSocket( hostname, port, style ) );
334     }
335
336     return io;
337 }
338
339
340 // step through the port config streams (from fgOPTIONS) and setup
341 // serial port channels for each
342 void
343 FGIO::init()
344 {
345     // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
346     //         globals->get_channel_options_list()->size() << " requests." );
347
348     _realDeltaTime = fgGetNode("/sim/time/delta-realtime-sec");
349
350     FGProtocol *p;
351
352     // we could almost do this in a single step except pushing a valid
353     // port onto the port list copies the structure and destroys the
354     // original, which closes the port and frees up the fd ... doh!!!
355
356     // parse the configuration strings and store the results in the
357     // appropriate FGIOChannel structures
358     string_list::iterator i = globals->get_channel_options_list()->begin();
359     string_list::iterator end = globals->get_channel_options_list()->end();
360     for (; i != end; ++i ) {
361       p = parse_port_config( *i );
362       if (!p) {
363         continue;
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         continue;
371       }
372
373       io_channels.push_back( p );
374     } // of channel options iteration
375 }
376
377 void
378 FGIO::reinit()
379 {
380 }
381
382 // process any IO channel work
383 void
384 FGIO::update( double /* delta_time_sec */ )
385 {
386     if (FGHTTPClient::haveInstance()) {
387         FGHTTPClient::instance()->update();
388     }
389     
390   // use wall-clock, not simulation, delta time, so that network
391   // protocols update when the simulation is paused
392   // see http://code.google.com/p/flightgear-bugs/issues/detail?id=125
393     double delta_time_sec = _realDeltaTime->getDoubleValue();
394
395     ProtocolVec::iterator i = io_channels.begin();
396     ProtocolVec::iterator end = io_channels.end();
397     for (; i != end; ++i ) {
398       FGProtocol* p = *i;
399       if (!p->is_enabled()) {
400         continue;
401       }
402
403             p->dec_count_down( delta_time_sec );
404             double dt = 1 / p->get_hz();
405             if ( p->get_count_down() < 0.33 * dt ) {
406               p->process();
407               p->inc_count();
408               while ( p->get_count_down() < 0.33 * dt ) {
409           p->inc_count_down( dt );
410               }
411             } // of channel processing
412     } // of io_channels iteration
413 }
414
415 void
416 FGIO::shutdown()
417 {
418     FGProtocol *p;
419
420     ProtocolVec::iterator i = io_channels.begin();
421     ProtocolVec::iterator end = io_channels.end();
422     for (; i != end; ++i )
423     {
424       p = *i;
425       if ( p->is_enabled() ) {
426           p->close();
427       }
428
429       delete p;
430     }
431
432   io_channels.clear();
433 }
434
435 void
436 FGIO::bind()
437 {
438 }
439
440 void
441 FGIO::unbind()
442 {
443 }
444