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