]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_io.cxx
merging in topic/makej
[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             int configToken;
191             if (tokens[1] == "socket")
192                 configToken = 7;
193             else if (tokens[1] == "file")
194                 configToken = 5;
195             else
196                 configToken = 6;
197             FGGeneric *generic = new FGGeneric( tokens[configToken] );
198             io = generic;
199         } else if ( protocol == "multiplay" ) {
200             if ( tokens.size() != 5 ) {
201                 SG_LOG( SG_IO, SG_ALERT, "Ignoring invalid --multiplay option "
202                         "(4 arguments expected: --multiplay=dir,hz,hostname,port)" );
203                 return NULL;
204             }
205             string dir = tokens[1];
206             string rate = tokens[2];
207             string host = tokens[3];
208             string port = tokens[4];
209             return new FGMultiplay(dir, atoi(rate.c_str()), host, atoi(port.c_str()));
210         } else {
211             return NULL;
212         }
213     }
214     catch (FGProtocolConfigError& err)
215     {
216         SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
217         delete io;
218         return 0;
219     }
220     
221     if (tokens.size() < 3) {
222       SG_LOG( SG_IO, SG_ALERT, "Incompatible number of network arguments.");
223       return NULL;
224     }
225     string medium = tokens[1];
226     SG_LOG( SG_IO, SG_INFO, "  medium = " << medium );
227
228     string direction = tokens[2];
229     io->set_direction( direction );
230     SG_LOG( SG_IO, SG_INFO, "  direction = " << direction );
231
232     string hertz_str = tokens[3];
233     double hertz = atof( hertz_str.c_str() );
234     io->set_hz( hertz );
235     SG_LOG( SG_IO, SG_INFO, "  hertz = " << hertz );
236
237     if ( medium == "serial" ) {
238         if ( tokens.size() < 5) {
239           SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for serial communications.");
240           return NULL;
241         }
242         // device name
243         string device = tokens[4];
244         SG_LOG( SG_IO, SG_INFO, "  device = " << device );
245
246         // baud
247         string baud = tokens[5];
248         SG_LOG( SG_IO, SG_INFO, "  baud = " << baud );
249
250         
251         SGSerial *ch = new SGSerial( device, baud );
252         io->set_io_channel( ch );
253     } else if ( medium == "file" ) {
254         // file name
255         if ( tokens.size() < 4) {
256           SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for file I/O.");
257           return NULL;
258         }
259           
260         string file = tokens[4];
261         SG_LOG( SG_IO, SG_INFO, "  file name = " << file );
262         int repeat = 1;
263         if (tokens.size() >= 7 && tokens[6] == "repeat") {
264             if (tokens.size() >= 8) {
265                 repeat = atoi(tokens[7].c_str());
266                 FGGeneric* generic = dynamic_cast<FGGeneric*>(io);
267                 if (generic)
268                     generic->setExitOnError(true);
269             } else {
270                 repeat = -1;
271             }
272         }
273         SGFile *ch = new SGFile( file, repeat );
274         io->set_io_channel( ch );
275     } else if ( medium == "socket" ) {
276         if ( tokens.size() < 6) {
277           SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for socket communications.");
278           return NULL;
279         }
280         string hostname = tokens[4];
281         string port = tokens[5];
282         string style = tokens[6];
283
284         SG_LOG( SG_IO, SG_INFO, "  hostname = " << hostname );
285         SG_LOG( SG_IO, SG_INFO, "  port = " << port );
286         SG_LOG( SG_IO, SG_INFO, "  style = " << style );
287
288         io->set_io_channel( new SGSocket( hostname, port, style ) );
289     }
290
291     return io;
292 }
293
294
295 // step through the port config streams (from fgOPTIONS) and setup
296 // serial port channels for each
297 void
298 FGIO::init()
299 {
300     // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
301     //         globals->get_channel_options_list()->size() << " requests." );
302
303     FGProtocol *p;
304
305     // we could almost do this in a single step except pushing a valid
306     // port onto the port list copies the structure and destroys the
307     // original, which closes the port and frees up the fd ... doh!!!
308
309     // parse the configuration strings and store the results in the
310     // appropriate FGIOChannel structures
311     typedef vector<string> container;
312     container::iterator i = globals->get_channel_options_list()->begin();
313     container::iterator end = globals->get_channel_options_list()->end();
314     for (; i != end; ++i )
315     {
316         p = parse_port_config( *i );
317         if ( p != NULL ) {
318             p->open();
319             io_channels.push_back( p );
320             if ( !p->is_enabled() ) {
321                 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
322                 exit(-1);
323             }
324         } else {
325             SG_LOG( SG_IO, SG_INFO, "I/O Channel parse failed." );
326         }
327     }
328 }
329
330
331 // process any IO channel work
332 void
333 FGIO::update( double delta_time_sec )
334 {
335     // cout << "processing I/O channels" << endl;
336     // cout << "  Elapsed time = " << delta_time_sec << endl;
337
338     typedef vector< FGProtocol* > container;
339     container::iterator i = io_channels.begin();
340     container::iterator end = io_channels.end();
341     for (; i != end; ++i ) {
342         FGProtocol* p = *i;
343
344         if ( p->is_enabled() ) {
345             p->dec_count_down( delta_time_sec );
346             double dt = 1 / p->get_hz();
347             if ( p->get_count_down() < 0.33 * dt ) {
348               p->process();
349               p->inc_count();
350               while ( p->get_count_down() < 0.33 * dt ) {
351                 p->inc_count_down( dt );
352               }
353               // double ave = elapsed_time / p->get_count();
354               // cout << "  ave rate = " << ave << endl;
355             }
356         }
357     }
358 }
359
360
361 void
362 FGIO::shutdown_all() {
363     FGProtocol *p;
364
365     // cout << "shutting down all I/O channels" << endl;
366
367     typedef vector< FGProtocol* > container;
368     container::iterator i = io_channels.begin();
369     container::iterator end = io_channels.end();
370     for (; i != end; ++i )
371     {
372         p = *i;
373
374         if ( p->is_enabled() ) {
375             p->close();
376         }
377     }
378 }
379
380 void
381 FGIO::bind()
382 {
383 }
384
385 void
386 FGIO::unbind()
387 {
388 }
389