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