]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_io.cxx
Fix bug #125
[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/garmin.hxx>
50 #include <Network/httpd.hxx>
51 #ifdef FG_JPEG_SERVER
52 #  include <Network/jpg-httpd.hxx>
53 #endif
54 #include <Network/joyclient.hxx>
55 #include <Network/jsclient.hxx>
56 #include <Network/native.hxx>
57 #include <Network/native_ctrls.hxx>
58 #include <Network/native_fdm.hxx>
59 #include <Network/native_gui.hxx>
60 #include <Network/opengc.hxx>
61 #include <Network/nmea.hxx>
62 #include <Network/props.hxx>
63 #include <Network/pve.hxx>
64 #include <Network/ray.hxx>
65 #include <Network/rul.hxx>
66 #include <Network/generic.hxx>
67 #include <Network/multiplay.hxx>
68
69 #include "globals.hxx"
70 #include "fg_io.hxx"
71
72 using std::atoi;
73 using std::string;
74
75
76 FGIO::FGIO()
77 {
78 }
79
80
81
82
83
84 FGIO::~FGIO()
85 {
86     
87 }
88
89
90 // configure a port based on the config string
91 FGProtocol*
92 FGIO::parse_port_config( const string& config )
93 {
94     SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config );
95
96     string_list tokens = simgear::strutils::split( config, "," );
97     if (tokens.empty())
98     {
99         SG_LOG( SG_IO, SG_ALERT,
100                 "Port configuration error: empty config string" );
101         return 0;
102     }
103
104     string protocol = tokens[0];
105     SG_LOG( SG_IO, SG_INFO, "  protocol = " << protocol );
106
107     FGProtocol *io = 0;
108
109     try
110     {
111         if ( protocol == "atcsim" ) {
112             FGATCMain *atcsim = new FGATCMain;
113             atcsim->set_hz( 30 );
114             if ( tokens.size() != 6 ) {
115                 SG_LOG( SG_IO, SG_ALERT, "Usage: --atcsim=[no-]pedals,"
116                         << "input0_config,input1_config,"
117                         << "output0_config,output1_config,file.nas" );
118                 delete atcsim;
119                 return NULL;
120             }
121             if ( tokens[1] == "no-pedals" ) {
122                 fgSetBool( "/input/atcsim/ignore-pedal-controls", true );
123             } else {
124                 fgSetBool( "/input/atcsim/ignore-pedal-controls", false );
125             }
126             atcsim->set_path_names(tokens[2], tokens[3], tokens[4], tokens[5]);
127             return atcsim;
128         } else if ( protocol == "atlas" ) {
129             FGAtlas *atlas = new FGAtlas;
130             io = atlas;
131         } else if ( protocol == "opengc" ) {
132             // char wait;
133             // printf("Parsed opengc\n"); cin >> wait;
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 == "garmin" ) {
143             FGGarmin *garmin = new FGGarmin;
144             io = garmin;
145         } else if ( protocol == "httpd" ) {
146             // determine port
147             string port = tokens[1];
148             return new FGHttpd( atoi(port.c_str()) );
149 #ifdef FG_JPEG_SERVER
150         } else if ( protocol == "jpg-httpd" ) {
151             // determine port
152             string port = tokens[1];
153             return new FGJpegHttpd( atoi(port.c_str()) );
154 #endif
155         } else if ( protocol == "joyclient" ) {
156             FGJoyClient *joyclient = new FGJoyClient;
157             io = joyclient;
158         } else if ( protocol == "jsclient" ) {
159             FGJsClient *jsclient = new FGJsClient;
160             io = jsclient;
161         } else if ( protocol == "native" ) {
162             FGNative *native = new FGNative;
163             io = native;
164         } else if ( protocol == "native-ctrls" ) {
165             FGNativeCtrls *native_ctrls = new FGNativeCtrls;
166             io = native_ctrls;
167         } else if ( protocol == "native-fdm" ) {
168             FGNativeFDM *native_fdm = new FGNativeFDM;
169             io = native_fdm;
170         } else if ( protocol == "native-gui" ) {
171             FGNativeGUI *net_gui = new FGNativeGUI;
172             io = net_gui;
173         } else if ( protocol == "nmea" ) {
174             FGNMEA *nmea = new FGNMEA;
175             io = nmea;
176         } else if ( protocol == "props" || protocol == "telnet" ) {
177             io = new FGProps( tokens );
178             return io;
179         } else if ( protocol == "pve" ) {
180             FGPVE *pve = new FGPVE;
181             io = pve;
182         } else if ( protocol == "ray" ) {
183             FGRAY *ray = new FGRAY;
184             io = ray;
185         } else if ( protocol == "rul" ) {
186             FGRUL *rul = new FGRUL;
187             io = rul;
188         } else if ( protocol == "generic" ) {
189             size_t configToken;
190             if (tokens[1] == "socket") {
191                 configToken = 7;
192             } else if (tokens[1] == "file") {
193                 configToken = 5;
194             } else {
195                 configToken = 6;
196             }
197
198             if (configToken >= tokens.size()) {
199                 SG_LOG( SG_IO, SG_ALERT, "Not enough tokens passed for the generic protocol.");
200                 return NULL;
201             }
202
203             FGGeneric *generic = new FGGeneric( tokens );
204             io = generic;
205         } else if ( protocol == "multiplay" ) {
206             if ( tokens.size() != 5 ) {
207                 SG_LOG( SG_IO, SG_ALERT, "Ignoring invalid --multiplay option "
208                         "(4 arguments expected: --multiplay=dir,hz,hostname,port)" );
209                 return NULL;
210             }
211             string dir = tokens[1];
212             string rate = tokens[2];
213             string host = tokens[3];
214             string port = tokens[4];
215             return new FGMultiplay(dir, atoi(rate.c_str()), host, atoi(port.c_str()));
216         } else {
217             return NULL;
218         }
219     }
220     catch (FGProtocolConfigError& err)
221     {
222         SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
223         delete io;
224         return 0;
225     }
226     
227     if (tokens.size() < 3) {
228       SG_LOG( SG_IO, SG_ALERT, "Incompatible number of network arguments.");
229       return NULL;
230     }
231     string medium = tokens[1];
232     SG_LOG( SG_IO, SG_INFO, "  medium = " << medium );
233
234     string direction = tokens[2];
235     io->set_direction( direction );
236     SG_LOG( SG_IO, SG_INFO, "  direction = " << direction );
237
238     string hertz_str = tokens[3];
239     double hertz = atof( hertz_str.c_str() );
240     io->set_hz( hertz );
241     SG_LOG( SG_IO, SG_INFO, "  hertz = " << hertz );
242
243     if ( medium == "serial" ) {
244         if ( tokens.size() < 5) {
245           SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for serial communications.");
246           return NULL;
247         }
248         // device name
249         string device = tokens[4];
250         SG_LOG( SG_IO, SG_INFO, "  device = " << device );
251
252         // baud
253         string baud = tokens[5];
254         SG_LOG( SG_IO, SG_INFO, "  baud = " << baud );
255
256         
257         SGSerial *ch = new SGSerial( device, baud );
258         io->set_io_channel( ch );
259     } else if ( medium == "file" ) {
260         // file name
261         if ( tokens.size() < 4) {
262           SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for file I/O.");
263           return NULL;
264         }
265           
266         string file = tokens[4];
267         SG_LOG( SG_IO, SG_INFO, "  file name = " << file );
268         int repeat = 1;
269         if (tokens.size() >= 7 && tokens[6] == "repeat") {
270             if (tokens.size() >= 8) {
271                 repeat = atoi(tokens[7].c_str());
272                 FGGeneric* generic = dynamic_cast<FGGeneric*>(io);
273                 if (generic)
274                     generic->setExitOnError(true);
275             } else {
276                 repeat = -1;
277             }
278         }
279         SGFile *ch = new SGFile( file, repeat );
280         io->set_io_channel( ch );
281     } else if ( medium == "socket" ) {
282         if ( tokens.size() < 6) {
283           SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for socket communications.");
284           return NULL;
285         }
286         string hostname = tokens[4];
287         string port = tokens[5];
288         string style = tokens[6];
289
290         SG_LOG( SG_IO, SG_INFO, "  hostname = " << hostname );
291         SG_LOG( SG_IO, SG_INFO, "  port = " << port );
292         SG_LOG( SG_IO, SG_INFO, "  style = " << style );
293
294         io->set_io_channel( new SGSocket( hostname, port, style ) );
295     }
296
297     return io;
298 }
299
300
301 // step through the port config streams (from fgOPTIONS) and setup
302 // serial port channels for each
303 void
304 FGIO::init()
305 {
306     // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
307     //         globals->get_channel_options_list()->size() << " requests." );
308
309     _realDeltaTime = fgGetNode("/sim/time/delta-realtime-sec");
310     
311     FGProtocol *p;
312
313     // we could almost do this in a single step except pushing a valid
314     // port onto the port list copies the structure and destroys the
315     // original, which closes the port and frees up the fd ... doh!!!
316
317     // parse the configuration strings and store the results in the
318     // appropriate FGIOChannel structures
319     string_list::iterator i = globals->get_channel_options_list()->begin();
320     string_list::iterator end = globals->get_channel_options_list()->end();
321     for (; i != end; ++i )
322     {
323         p = parse_port_config( *i );
324         if ( p != NULL ) {
325             p->open();
326             io_channels.push_back( p );
327             if ( !p->is_enabled() ) {
328                 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
329                 exit(-1);
330             }
331         } else {
332             SG_LOG( SG_IO, SG_INFO, "I/O Channel parse failed." );
333         }
334     }
335 }
336
337 void
338 FGIO::reinit()
339 {
340 }
341
342 // process any IO channel work
343 void
344 FGIO::update( double /* delta_time_sec */ )
345 {
346   // use wall-clock, not simulation, delta time, so that network
347   // protocols update when the simulation is paused
348   // see http://code.google.com/p/flightgear-bugs/issues/detail?id=125
349     double delta_time_sec = _realDeltaTime->getDoubleValue();
350
351     ProtocolVec::iterator i = io_channels.begin();
352     ProtocolVec::iterator end = io_channels.end();
353     for (; i != end; ++i ) {
354       FGProtocol* p = *i;
355       if (!p->is_enabled()) {
356         continue;
357       }
358       
359             p->dec_count_down( delta_time_sec );
360             double dt = 1 / p->get_hz();
361             if ( p->get_count_down() < 0.33 * dt ) {
362               p->process();
363               p->inc_count();
364               while ( p->get_count_down() < 0.33 * dt ) {
365           p->inc_count_down( dt );
366               }
367             } // of channel processing
368     } // of io_channels iteration
369 }
370
371 void
372 FGIO::shutdown()
373 {
374     FGProtocol *p;
375
376     ProtocolVec::iterator i = io_channels.begin();
377     ProtocolVec::iterator end = io_channels.end();
378     for (; i != end; ++i )
379     {
380       p = *i;
381       if ( p->is_enabled() ) {
382           p->close();
383       }
384       
385       delete p;
386     }
387
388   io_channels.clear();
389 }
390
391 void
392 FGIO::bind()
393 {
394 }
395
396 void
397 FGIO::unbind()
398 {
399 }
400