]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_io.cxx
If preset-commit occurs during init, skip most re-init logic.
[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
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/AV400Sim.hxx>
48 #include <Network/garmin.hxx>
49 #include <Network/httpd.hxx>
50 #ifdef FG_JPEG_SERVER
51 #  include <Network/jpg-httpd.hxx>
52 #endif
53 #include <Network/joyclient.hxx>
54 #include <Network/jsclient.hxx>
55 #include <Network/native.hxx>
56 #include <Network/native_ctrls.hxx>
57 #include <Network/native_fdm.hxx>
58 #include <Network/native_gui.hxx>
59 #include <Network/opengc.hxx>
60 #include <Network/nmea.hxx>
61 #include <Network/props.hxx>
62 #include <Network/pve.hxx>
63 #include <Network/ray.hxx>
64 #include <Network/rul.hxx>
65 #include <Network/generic.hxx>
66 #include <Network/multiplay.hxx>
67
68 #include "globals.hxx"
69 #include "fg_io.hxx"
70
71 using std::atoi;
72 using std::string;
73
74
75 FGIO::FGIO()
76 {
77 }
78
79 #include <algorithm>
80 using std::for_each;
81
82 static void delete_ptr( FGProtocol* p ) { delete p; }
83
84 FGIO::~FGIO()
85 {
86     shutdown_all();
87     for_each( io_channels.begin(), io_channels.end(), delete_ptr );
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     vector<string> 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 0;
103     }
104
105     string protocol = tokens[0];
106     SG_LOG( SG_IO, SG_INFO, "  protocol = " << protocol );
107
108     FGProtocol *io = 0;
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             // char wait;
134             // printf("Parsed opengc\n"); cin >> wait;
135             FGOpenGC *opengc = new FGOpenGC;
136             io = opengc;
137         } else if ( protocol == "AV400" ) {
138             FGAV400 *av400 = new FGAV400;
139             io = av400;
140         } else if ( protocol == "AV400Sim" ) {
141             FGAV400Sim *av400sim = new FGAV400Sim;
142             io = av400sim;
143         } else if ( protocol == "garmin" ) {
144             FGGarmin *garmin = new FGGarmin;
145             io = garmin;
146         } else if ( protocol == "httpd" ) {
147             // determine port
148             string port = tokens[1];
149             return new FGHttpd( atoi(port.c_str()) );
150 #ifdef FG_JPEG_SERVER
151         } else if ( protocol == "jpg-httpd" ) {
152             // determine port
153             string port = tokens[1];
154             return new FGJpegHttpd( atoi(port.c_str()) );
155 #endif
156         } else if ( protocol == "joyclient" ) {
157             FGJoyClient *joyclient = new FGJoyClient;
158             io = joyclient;
159         } else if ( protocol == "jsclient" ) {
160             FGJsClient *jsclient = new FGJsClient;
161             io = jsclient;
162         } else if ( protocol == "native" ) {
163             FGNative *native = new FGNative;
164             io = native;
165         } else if ( protocol == "native-ctrls" ) {
166             FGNativeCtrls *native_ctrls = new FGNativeCtrls;
167             io = native_ctrls;
168         } else if ( protocol == "native-fdm" ) {
169             FGNativeFDM *native_fdm = new FGNativeFDM;
170             io = native_fdm;
171         } else if ( protocol == "native-gui" ) {
172             FGNativeGUI *net_gui = new FGNativeGUI;
173             io = net_gui;
174         } else if ( protocol == "nmea" ) {
175             FGNMEA *nmea = new FGNMEA;
176             io = nmea;
177         } else if ( protocol == "props" || protocol == "telnet" ) {
178             io = new FGProps( tokens );
179             return io;
180         } else if ( protocol == "pve" ) {
181             FGPVE *pve = new FGPVE;
182             io = pve;
183         } else if ( protocol == "ray" ) {
184             FGRAY *ray = new FGRAY;
185             io = ray;
186         } else if ( protocol == "rul" ) {
187             FGRUL *rul = new FGRUL;
188             io = rul;
189         } else if ( protocol == "generic" ) {
190             size_t configToken;
191             if (tokens[1] == "socket") {
192                 configToken = 7;
193             } else if (tokens[1] == "file") {
194                 configToken = 5;
195             } else {
196                 configToken = 6;
197             }
198
199             if (configToken >= tokens.size()) {
200                 SG_LOG( SG_IO, SG_ALERT, "Not enough tokens passed for the generic protocol.");
201                 return NULL;
202             }
203
204             FGGeneric *generic = new FGGeneric( tokens );
205             io = generic;
206         } else if ( protocol == "multiplay" ) {
207             if ( tokens.size() != 5 ) {
208                 SG_LOG( SG_IO, SG_ALERT, "Ignoring invalid --multiplay option "
209                         "(4 arguments expected: --multiplay=dir,hz,hostname,port)" );
210                 return NULL;
211             }
212             string dir = tokens[1];
213             string rate = tokens[2];
214             string host = tokens[3];
215             string port = tokens[4];
216             return new FGMultiplay(dir, atoi(rate.c_str()), host, atoi(port.c_str()));
217         } else {
218             return NULL;
219         }
220     }
221     catch (FGProtocolConfigError& err)
222     {
223         SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
224         delete io;
225         return 0;
226     }
227     
228     if (tokens.size() < 3) {
229       SG_LOG( SG_IO, SG_ALERT, "Incompatible number of network arguments.");
230       return NULL;
231     }
232     string medium = tokens[1];
233     SG_LOG( SG_IO, SG_INFO, "  medium = " << medium );
234
235     string direction = tokens[2];
236     io->set_direction( direction );
237     SG_LOG( SG_IO, SG_INFO, "  direction = " << direction );
238
239     string hertz_str = tokens[3];
240     double hertz = atof( hertz_str.c_str() );
241     io->set_hz( hertz );
242     SG_LOG( SG_IO, SG_INFO, "  hertz = " << hertz );
243
244     if ( medium == "serial" ) {
245         if ( tokens.size() < 5) {
246           SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for serial communications.");
247           return NULL;
248         }
249         // device name
250         string device = tokens[4];
251         SG_LOG( SG_IO, SG_INFO, "  device = " << device );
252
253         // baud
254         string baud = tokens[5];
255         SG_LOG( SG_IO, SG_INFO, "  baud = " << baud );
256
257         
258         SGSerial *ch = new SGSerial( device, baud );
259         io->set_io_channel( ch );
260     } else if ( medium == "file" ) {
261         // file name
262         if ( tokens.size() < 4) {
263           SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for file I/O.");
264           return NULL;
265         }
266           
267         string file = tokens[4];
268         SG_LOG( SG_IO, SG_INFO, "  file name = " << file );
269         int repeat = 1;
270         if (tokens.size() >= 7 && tokens[6] == "repeat") {
271             if (tokens.size() >= 8) {
272                 repeat = atoi(tokens[7].c_str());
273                 FGGeneric* generic = dynamic_cast<FGGeneric*>(io);
274                 if (generic)
275                     generic->setExitOnError(true);
276             } else {
277                 repeat = -1;
278             }
279         }
280         SGFile *ch = new SGFile( file, repeat );
281         io->set_io_channel( ch );
282     } else if ( medium == "socket" ) {
283         if ( tokens.size() < 6) {
284           SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for socket communications.");
285           return NULL;
286         }
287         string hostname = tokens[4];
288         string port = tokens[5];
289         string style = tokens[6];
290
291         SG_LOG( SG_IO, SG_INFO, "  hostname = " << hostname );
292         SG_LOG( SG_IO, SG_INFO, "  port = " << port );
293         SG_LOG( SG_IO, SG_INFO, "  style = " << style );
294
295         io->set_io_channel( new SGSocket( hostname, port, style ) );
296     }
297
298     return io;
299 }
300
301
302 // step through the port config streams (from fgOPTIONS) and setup
303 // serial port channels for each
304 void
305 FGIO::init()
306 {
307     // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
308     //         globals->get_channel_options_list()->size() << " requests." );
309
310     FGProtocol *p;
311
312     // we could almost do this in a single step except pushing a valid
313     // port onto the port list copies the structure and destroys the
314     // original, which closes the port and frees up the fd ... doh!!!
315
316     // parse the configuration strings and store the results in the
317     // appropriate FGIOChannel structures
318     typedef vector<string> container;
319     container::iterator i = globals->get_channel_options_list()->begin();
320     container::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
343 // process any IO channel work
344 void
345 FGIO::update( double delta_time_sec )
346 {
347     // cout << "processing I/O channels" << endl;
348     // cout << "  Elapsed time = " << delta_time_sec << endl;
349
350     typedef vector< FGProtocol* > container;
351     container::iterator i = io_channels.begin();
352     container::iterator end = io_channels.end();
353     for (; i != end; ++i ) {
354         FGProtocol* p = *i;
355
356         if ( p->is_enabled() ) {
357             p->dec_count_down( delta_time_sec );
358             double dt = 1 / p->get_hz();
359             if ( p->get_count_down() < 0.33 * dt ) {
360               p->process();
361               p->inc_count();
362               while ( p->get_count_down() < 0.33 * dt ) {
363                 p->inc_count_down( dt );
364               }
365               // double ave = elapsed_time / p->get_count();
366               // cout << "  ave rate = " << ave << endl;
367             }
368         }
369     }
370 }
371
372
373 void
374 FGIO::shutdown_all() {
375     FGProtocol *p;
376
377     // cout << "shutting down all I/O channels" << endl;
378
379     typedef vector< FGProtocol* > container;
380     container::iterator i = io_channels.begin();
381     container::iterator end = io_channels.end();
382     for (; i != end; ++i )
383     {
384         p = *i;
385
386         if ( p->is_enabled() ) {
387             p->close();
388         }
389     }
390 }
391
392 void
393 FGIO::bind()
394 {
395 }
396
397 void
398 FGIO::unbind()
399 {
400 }
401