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