]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_io.cxx
Return an error when not enough tokens are parsed for the generic protocol, that...
[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     unsigned num_tokens = tokens.size();
106     string protocol = tokens[0];
107     SG_LOG( SG_IO, SG_INFO, "  protocol = " << protocol );
108
109     FGProtocol *io = 0;
110
111     try
112     {
113         if ( protocol == "atcsim" ) {
114             FGATCMain *atcsim = new FGATCMain;
115             atcsim->set_hz( 30 );
116             if ( tokens.size() != 6 ) {
117                 SG_LOG( SG_IO, SG_ALERT, "Usage: --atcsim=[no-]pedals,"
118                         << "input0_config,input1_config,"
119                         << "output0_config,output1_config,file.nas" );
120                 delete atcsim;
121                 return NULL;
122             }
123             if ( tokens[1] == "no-pedals" ) {
124                 fgSetBool( "/input/atcsim/ignore-pedal-controls", true );
125             } else {
126                 fgSetBool( "/input/atcsim/ignore-pedal-controls", false );
127             }
128             atcsim->set_path_names(tokens[2], tokens[3], tokens[4], tokens[5]);
129             return atcsim;
130         } else if ( protocol == "atlas" ) {
131             FGAtlas *atlas = new FGAtlas;
132             io = atlas;
133         } else if ( protocol == "opengc" ) {
134             // char wait;
135             // printf("Parsed opengc\n"); cin >> wait;
136             FGOpenGC *opengc = new FGOpenGC;
137             io = opengc;
138         } else if ( protocol == "AV400" ) {
139             FGAV400 *av400 = new FGAV400;
140             io = av400;
141         } else if ( protocol == "AV400Sim" ) {
142             FGAV400Sim *av400sim = new FGAV400Sim;
143             io = av400sim;
144         } else if ( protocol == "garmin" ) {
145             FGGarmin *garmin = new FGGarmin;
146             io = garmin;
147         } else if ( protocol == "httpd" ) {
148             // determine port
149             string port = tokens[1];
150             return new FGHttpd( atoi(port.c_str()) );
151 #ifdef FG_JPEG_SERVER
152         } else if ( protocol == "jpg-httpd" ) {
153             // determine port
154             string port = tokens[1];
155             return new FGJpegHttpd( atoi(port.c_str()) );
156 #endif
157         } else if ( protocol == "joyclient" ) {
158             FGJoyClient *joyclient = new FGJoyClient;
159             io = joyclient;
160         } else if ( protocol == "jsclient" ) {
161             FGJsClient *jsclient = new FGJsClient;
162             io = jsclient;
163         } else if ( protocol == "native" ) {
164             FGNative *native = new FGNative;
165             io = native;
166         } else if ( protocol == "native-ctrls" ) {
167             FGNativeCtrls *native_ctrls = new FGNativeCtrls;
168             io = native_ctrls;
169         } else if ( protocol == "native-fdm" ) {
170             FGNativeFDM *native_fdm = new FGNativeFDM;
171             io = native_fdm;
172         } else if ( protocol == "native-gui" ) {
173             FGNativeGUI *net_gui = new FGNativeGUI;
174             io = net_gui;
175         } else if ( protocol == "nmea" ) {
176             FGNMEA *nmea = new FGNMEA;
177             io = nmea;
178         } else if ( protocol == "props" || protocol == "telnet" ) {
179             io = new FGProps( tokens );
180             return io;
181         } else if ( protocol == "pve" ) {
182             FGPVE *pve = new FGPVE;
183             io = pve;
184         } else if ( protocol == "ray" ) {
185             FGRAY *ray = new FGRAY;
186             io = ray;
187         } else if ( protocol == "rul" ) {
188             FGRUL *rul = new FGRUL;
189             io = rul;
190         } else if ( protocol == "generic" ) {
191             int configToken;
192             if (tokens[1] == "socket")
193                 configToken = 7;
194             else if (tokens[1] == "file")
195                 configToken = 5;
196             else
197                 configToken = 6;
198             if (configToken < num_tokens)
199             {
200                FGGeneric *generic = new FGGeneric( tokens[configToken] );
201                io = generic;
202             }
203             else
204             {
205                SG_LOG( SG_IO, SG_ALERT, "Not enough tokens passed for the generic protocol.");
206                return NULL;
207             }
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         } else {
220             return NULL;
221         }
222     }
223     catch (FGProtocolConfigError& err)
224     {
225         SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
226         delete io;
227         return 0;
228     }
229     
230     if (tokens.size() < 3) {
231       SG_LOG( SG_IO, SG_ALERT, "Incompatible number of network arguments.");
232       return NULL;
233     }
234     string medium = tokens[1];
235     SG_LOG( SG_IO, SG_INFO, "  medium = " << medium );
236
237     string direction = tokens[2];
238     io->set_direction( direction );
239     SG_LOG( SG_IO, SG_INFO, "  direction = " << direction );
240
241     string hertz_str = tokens[3];
242     double hertz = atof( hertz_str.c_str() );
243     io->set_hz( hertz );
244     SG_LOG( SG_IO, SG_INFO, "  hertz = " << hertz );
245
246     if ( medium == "serial" ) {
247         if ( tokens.size() < 5) {
248           SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for serial communications.");
249           return NULL;
250         }
251         // device name
252         string device = tokens[4];
253         SG_LOG( SG_IO, SG_INFO, "  device = " << device );
254
255         // baud
256         string baud = tokens[5];
257         SG_LOG( SG_IO, SG_INFO, "  baud = " << baud );
258
259         
260         SGSerial *ch = new SGSerial( device, baud );
261         io->set_io_channel( ch );
262     } else if ( medium == "file" ) {
263         // file name
264         if ( tokens.size() < 4) {
265           SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for file I/O.");
266           return NULL;
267         }
268           
269         string file = tokens[4];
270         SG_LOG( SG_IO, SG_INFO, "  file name = " << file );
271         int repeat = 1;
272         if (tokens.size() >= 7 && tokens[6] == "repeat") {
273             if (tokens.size() >= 8) {
274                 repeat = atoi(tokens[7].c_str());
275                 FGGeneric* generic = dynamic_cast<FGGeneric*>(io);
276                 if (generic)
277                     generic->setExitOnError(true);
278             } else {
279                 repeat = -1;
280             }
281         }
282         SGFile *ch = new SGFile( file, repeat );
283         io->set_io_channel( ch );
284     } else if ( medium == "socket" ) {
285         if ( tokens.size() < 6) {
286           SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for socket communications.");
287           return NULL;
288         }
289         string hostname = tokens[4];
290         string port = tokens[5];
291         string style = tokens[6];
292
293         SG_LOG( SG_IO, SG_INFO, "  hostname = " << hostname );
294         SG_LOG( SG_IO, SG_INFO, "  port = " << port );
295         SG_LOG( SG_IO, SG_INFO, "  style = " << style );
296
297         io->set_io_channel( new SGSocket( hostname, port, style ) );
298     }
299
300     return io;
301 }
302
303
304 // step through the port config streams (from fgOPTIONS) and setup
305 // serial port channels for each
306 void
307 FGIO::init()
308 {
309     // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
310     //         globals->get_channel_options_list()->size() << " requests." );
311
312     FGProtocol *p;
313
314     // we could almost do this in a single step except pushing a valid
315     // port onto the port list copies the structure and destroys the
316     // original, which closes the port and frees up the fd ... doh!!!
317
318     // parse the configuration strings and store the results in the
319     // appropriate FGIOChannel structures
320     typedef vector<string> container;
321     container::iterator i = globals->get_channel_options_list()->begin();
322     container::iterator end = globals->get_channel_options_list()->end();
323     for (; i != end; ++i )
324     {
325         p = parse_port_config( *i );
326         if ( p != NULL ) {
327             p->open();
328             io_channels.push_back( p );
329             if ( !p->is_enabled() ) {
330                 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
331                 exit(-1);
332             }
333         } else {
334             SG_LOG( SG_IO, SG_INFO, "I/O Channel parse failed." );
335         }
336     }
337 }
338
339
340 // process any IO channel work
341 void
342 FGIO::update( double delta_time_sec )
343 {
344     // cout << "processing I/O channels" << endl;
345     // cout << "  Elapsed time = " << delta_time_sec << endl;
346
347     typedef vector< FGProtocol* > container;
348     container::iterator i = io_channels.begin();
349     container::iterator end = io_channels.end();
350     for (; i != end; ++i ) {
351         FGProtocol* p = *i;
352
353         if ( p->is_enabled() ) {
354             p->dec_count_down( delta_time_sec );
355             double dt = 1 / p->get_hz();
356             if ( p->get_count_down() < 0.33 * dt ) {
357               p->process();
358               p->inc_count();
359               while ( p->get_count_down() < 0.33 * dt ) {
360                 p->inc_count_down( dt );
361               }
362               // double ave = elapsed_time / p->get_count();
363               // cout << "  ave rate = " << ave << endl;
364             }
365         }
366     }
367 }
368
369
370 void
371 FGIO::shutdown_all() {
372     FGProtocol *p;
373
374     // cout << "shutting down all I/O channels" << endl;
375
376     typedef vector< FGProtocol* > container;
377     container::iterator i = io_channels.begin();
378     container::iterator end = io_channels.end();
379     for (; i != end; ++i )
380     {
381         p = *i;
382
383         if ( p->is_enabled() ) {
384             p->close();
385         }
386     }
387 }
388
389 void
390 FGIO::bind()
391 {
392 }
393
394 void
395 FGIO::unbind()
396 {
397 }
398