]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_io.cxx
Nick "Lethe" WARNE: prevent crash caused by improper use of --multiplay
[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 <stdlib.h>             // atoi()
30
31 #include STL_STRING
32
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/io/iochannel.hxx>
35 #include <simgear/io/sg_file.hxx>
36 #include <simgear/io/sg_serial.hxx>
37 #include <simgear/io/sg_socket.hxx>
38 #include <simgear/io/sg_socket_udp.hxx>
39 #include <simgear/math/sg_types.hxx>
40 #include <simgear/timing/timestamp.hxx>
41 #include <simgear/misc/strutils.hxx>
42
43 #include <Network/protocol.hxx>
44 #include <Network/ATC-Main.hxx>
45 #include <Network/atlas.hxx>
46 #include <Network/AV400.hxx>
47 #include <Network/garmin.hxx>
48 #include <Network/httpd.hxx>
49 #ifdef FG_JPEG_SERVER
50 #  include <Network/jpg-httpd.hxx>
51 #endif
52 #include <Network/joyclient.hxx>
53 #include <Network/jsclient.hxx>
54 #include <Network/native.hxx>
55 #include <Network/native_ctrls.hxx>
56 #include <Network/native_fdm.hxx>
57 #include <Network/native_gui.hxx>
58 #include <Network/opengc.hxx>
59 #include <Network/nmea.hxx>
60 #include <Network/props.hxx>
61 #include <Network/pve.hxx>
62 #include <Network/ray.hxx>
63 #include <Network/rul.hxx>
64 #include <Network/generic.hxx>
65 #include <Network/multiplay.hxx>
66
67 #include "globals.hxx"
68 #include "fg_io.hxx"
69
70 SG_USING_STD(string);
71
72
73 FGIO::FGIO()
74 {
75 }
76
77 #include STL_ALGORITHM
78 SG_USING_STD(for_each);
79
80 static void delete_ptr( FGProtocol* p ) { delete p; }
81
82 FGIO::~FGIO()
83 {
84     shutdown_all();
85     for_each( io_channels.begin(), io_channels.end(), delete_ptr );
86 }
87
88
89 // configure a port based on the config string
90 FGProtocol*
91 FGIO::parse_port_config( const string& config )
92 {
93     SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config );
94
95     vector<string> tokens = simgear::strutils::split( config, "," );
96     if (tokens.empty())
97     {
98         SG_LOG( SG_IO, SG_ALERT,
99                 "Port configuration error: empty config string" );
100         return 0;
101     }
102
103     string protocol = tokens[0];
104     SG_LOG( SG_IO, SG_INFO, "  protocol = " << protocol );
105
106     FGProtocol *io = 0;
107
108     try
109     {
110         if ( protocol == "atcsim" ) {
111             FGATCMain *atcsim = new FGATCMain;
112             atcsim->set_hz( 30 );
113             if ( tokens.size() != 6 ) {
114                 SG_LOG( SG_IO, SG_ALERT, "Usage: --atcsim=[no-]pedals,"
115                         << "input0_config,input1_config,"
116                         << "output0_config,output1_config,file.nas" );
117                 delete atcsim;
118                 return NULL;
119             }
120             if ( tokens[1] == "no-pedals" ) {
121                 fgSetBool( "/input/atcsim/ignore-pedal-controls", true );
122             } else {
123                 fgSetBool( "/input/atcsim/ignore-pedal-controls", false );
124             }
125             atcsim->set_path_names(tokens[2], tokens[3], tokens[4], tokens[5]);
126             return atcsim;
127         } else if ( protocol == "atlas" ) {
128             FGAtlas *atlas = new FGAtlas;
129             io = atlas;
130         } else if ( protocol == "opengc" ) {
131             // char wait;
132             // printf("Parsed opengc\n"); cin >> wait;
133             FGOpenGC *opengc = new FGOpenGC;
134             io = opengc;
135         } else if ( protocol == "AV400" ) {
136             FGAV400 *av400 = new FGAV400;
137             io = av400;
138         } else if ( protocol == "garmin" ) {
139             FGGarmin *garmin = new FGGarmin;
140             io = garmin;
141         } else if ( protocol == "httpd" ) {
142             // determine port
143             string port = tokens[1];
144             return new FGHttpd( atoi(port.c_str()) );
145 #ifdef FG_JPEG_SERVER
146         } else if ( protocol == "jpg-httpd" ) {
147             // determine port
148             string port = tokens[1];
149             return new FGJpegHttpd( atoi(port.c_str()) );
150 #endif
151         } else if ( protocol == "joyclient" ) {
152             FGJoyClient *joyclient = new FGJoyClient;
153             io = joyclient;
154         } else if ( protocol == "jsclient" ) {
155             FGJsClient *jsclient = new FGJsClient;
156             io = jsclient;
157         } else if ( protocol == "native" ) {
158             FGNative *native = new FGNative;
159             io = native;
160         } else if ( protocol == "native-ctrls" ) {
161             FGNativeCtrls *native_ctrls = new FGNativeCtrls;
162             io = native_ctrls;
163         } else if ( protocol == "native-fdm" ) {
164             FGNativeFDM *native_fdm = new FGNativeFDM;
165             io = native_fdm;
166         } else if ( protocol == "native-gui" ) {
167             FGNativeGUI *net_gui = new FGNativeGUI;
168             io = net_gui;
169         } else if ( protocol == "nmea" ) {
170             FGNMEA *nmea = new FGNMEA;
171             io = nmea;
172         } else if ( protocol == "props" || protocol == "telnet" ) {
173             io = new FGProps( tokens );
174             return io;
175         } else if ( protocol == "pve" ) {
176             FGPVE *pve = new FGPVE;
177             io = pve;
178         } else if ( protocol == "ray" ) {
179             FGRAY *ray = new FGRAY;
180             io = ray;
181         } else if ( protocol == "rul" ) {
182             FGRUL *rul = new FGRUL;
183             io = rul;
184         } else if ( protocol == "generic" ) {
185             int n = 6;
186             if (tokens[1] == "socket")  n++;
187             else if (tokens[1] == "file") n--;
188             FGGeneric *generic = new FGGeneric( tokens[n] );
189             io = generic;
190         } else if ( protocol == "multiplay" ) {
191             if ( tokens.size() != 5 ) {
192                 SG_LOG( SG_IO, SG_ALERT, "Ignoring invalid --multiplay option "
193                         "(4 arguments expected: --multiplay=dir,hz,hostname,port)" );
194                 return NULL;
195             }
196             string dir = tokens[1];
197             string rate = tokens[2];
198             string host = tokens[3];
199             string port = tokens[4];
200             return new FGMultiplay(dir, atoi(rate.c_str()), host, atoi(port.c_str()));
201         } else {
202             return NULL;
203         }
204     }
205     catch (FGProtocolConfigError& err)
206     {
207         SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
208         delete io;
209         return 0;
210     }
211     
212     if (tokens.size() < 3) {
213       SG_LOG( SG_IO, SG_ALERT, "Incompatible number of network arguments.");
214       return NULL;
215     }
216     string medium = tokens[1];
217     SG_LOG( SG_IO, SG_INFO, "  medium = " << medium );
218
219     string direction = tokens[2];
220     io->set_direction( direction );
221     SG_LOG( SG_IO, SG_INFO, "  direction = " << direction );
222
223     string hertz_str = tokens[3];
224     double hertz = atof( hertz_str.c_str() );
225     io->set_hz( hertz );
226     SG_LOG( SG_IO, SG_INFO, "  hertz = " << hertz );
227
228     if ( medium == "serial" ) {
229         if ( tokens.size() < 5) {
230           SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for serial communications.");
231           return NULL;
232         }
233         // device name
234         string device = tokens[4];
235         SG_LOG( SG_IO, SG_INFO, "  device = " << device );
236
237         // baud
238         string baud = tokens[5];
239         SG_LOG( SG_IO, SG_INFO, "  baud = " << baud );
240
241         SGSerial *ch = new SGSerial( device, baud );
242         io->set_io_channel( ch );
243     } else if ( medium == "file" ) {
244         // file name
245         if ( tokens.size() < 4) {
246           SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for file I/O.");
247           return NULL;
248         }
249           
250         string file = tokens[4];
251         SG_LOG( SG_IO, SG_INFO, "  file name = " << file );
252
253         SGFile *ch = new SGFile( file );
254         io->set_io_channel( ch );
255     } else if ( medium == "socket" ) {
256         if ( tokens.size() < 6) {
257           SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for socket communications.");
258           return NULL;
259         }
260         string hostname = tokens[4];
261         string port = tokens[5];
262         string style = tokens[6];
263
264         SG_LOG( SG_IO, SG_INFO, "  hostname = " << hostname );
265         SG_LOG( SG_IO, SG_INFO, "  port = " << port );
266         SG_LOG( SG_IO, SG_INFO, "  style = " << style );
267
268         io->set_io_channel( new SGSocket( hostname, port, style ) );
269     }
270
271     return io;
272 }
273
274
275 // step through the port config streams (from fgOPTIONS) and setup
276 // serial port channels for each
277 void
278 FGIO::init()
279 {
280     // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
281     //         globals->get_channel_options_list()->size() << " requests." );
282
283     FGProtocol *p;
284
285     // we could almost do this in a single step except pushing a valid
286     // port onto the port list copies the structure and destroys the
287     // original, which closes the port and frees up the fd ... doh!!!
288
289     // parse the configuration strings and store the results in the
290     // appropriate FGIOChannel structures
291     typedef vector<string> container;
292     container::iterator i = globals->get_channel_options_list()->begin();
293     container::iterator end = globals->get_channel_options_list()->end();
294     for (; i != end; ++i )
295     {
296         p = parse_port_config( *i );
297         if ( p != NULL ) {
298             p->open();
299             io_channels.push_back( p );
300             if ( !p->is_enabled() ) {
301                 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
302                 exit(-1);
303             }
304         } else {
305             SG_LOG( SG_IO, SG_INFO, "I/O Channel parse failed." );
306         }
307     }
308 }
309
310
311 // process any IO channel work
312 void
313 FGIO::update( double delta_time_sec )
314 {
315     // cout << "processing I/O channels" << endl;
316     // cout << "  Elapsed time = " << delta_time_sec << endl;
317
318     typedef vector< FGProtocol* > container;
319     container::iterator i = io_channels.begin();
320     container::iterator end = io_channels.end();
321     for (; i != end; ++i ) {
322         FGProtocol* p = *i;
323
324         if ( p->is_enabled() ) {
325             p->dec_count_down( delta_time_sec );
326             double dt = 1 / p->get_hz();
327             if ( p->get_count_down() < 0.33 * dt ) {
328               p->process();
329               p->inc_count();
330               while ( p->get_count_down() < 0.33 * dt ) {
331                 p->inc_count_down( dt );
332               }
333               // double ave = elapsed_time / p->get_count();
334               // cout << "  ave rate = " << ave << endl;
335             }
336         }
337     }
338 }
339
340
341 void
342 FGIO::shutdown_all() {
343     FGProtocol *p;
344
345     // cout << "shutting down all I/O channels" << endl;
346
347     typedef vector< FGProtocol* > container;
348     container::iterator i = io_channels.begin();
349     container::iterator end = io_channels.end();
350     for (; i != end; ++i )
351     {
352         p = *i;
353
354         if ( p->is_enabled() ) {
355             p->close();
356         }
357     }
358 }
359
360 void
361 FGIO::bind()
362 {
363 }
364
365 void
366 FGIO::unbind()
367 {
368 }
369