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