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