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