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