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