]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_io.cxx
Moved some of the low level scene graph construction code over to simgear.
[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             if ( tokens.size() > 1 ) {
112                 if ( tokens[1] == "no-rudder" ) {
113                     atc610x->set_use_rudder( false );
114                 }
115             }
116             return atc610x;
117         } else if ( protocol == "atlas" ) {
118             FGAtlas *atlas = new FGAtlas;
119             io = atlas;
120         } else if ( protocol == "opengc" ) {
121             // char wait;
122             // printf("Parsed opengc\n"); cin >> wait;
123             FGOpenGC *opengc = new FGOpenGC;
124             io = opengc;
125         } else if ( protocol == "garmin" ) {
126             FGGarmin *garmin = new FGGarmin;
127             io = garmin;
128         } else if ( protocol == "httpd" ) {
129             // determine port
130             string port = tokens[1];
131             return new FGHttpd( atoi(port.c_str()) );
132 #ifdef FG_JPEG_SERVER
133         } else if ( protocol == "jpg-httpd" ) {
134             // determine port
135             string port = tokens[1];
136             return new FGJpegHttpd( atoi(port.c_str()) );
137 #endif
138         } else if ( protocol == "joyclient" ) {
139             FGJoyClient *joyclient = new FGJoyClient;
140             io = joyclient;
141         } else if ( protocol == "native" ) {
142             FGNative *native = new FGNative;
143             io = native;
144         } else if ( protocol == "native-ctrls" ) {
145             FGNativeCtrls *native_ctrls = new FGNativeCtrls;
146             io = native_ctrls;
147         } else if ( protocol == "native-fdm" ) {
148             FGNativeFDM *native_fdm = new FGNativeFDM;
149             io = native_fdm;
150         } else if ( protocol == "native-gui" ) {
151             FGNativeGUI *net_gui = new FGNativeGUI;
152             io = net_gui;
153         } else if ( protocol == "nmea" ) {
154             FGNMEA *nmea = new FGNMEA;
155             io = nmea;
156         } else if ( protocol == "props" || protocol == "telnet" ) {
157             io = new FGProps( tokens );
158             return io;
159         } else if ( protocol == "pve" ) {
160             FGPVE *pve = new FGPVE;
161             io = pve;
162         } else if ( protocol == "ray" ) {
163             FGRAY *ray = new FGRAY;
164             io = ray;
165         } else if ( protocol == "rul" ) {
166             FGRUL *rul = new FGRUL;
167             io = rul;
168         } else if ( protocol == "generic" ) {
169             FGGeneric *generic = new FGGeneric( tokens[6] );
170             io = generic;
171
172 #ifdef FG_MPLAYER_AS
173         } else if ( protocol == "multiplay" ) {\
174             //Determine dir, rate, host & port
175             string dir = tokens[1];
176             string rate = tokens[2];
177             string host = tokens[3];
178             string port = tokens[4];
179             return new FGMultiplay(dir, atoi(rate.c_str()), host, atoi(port.c_str()));
180 #endif
181
182         } else {
183             return NULL;
184         }
185     }
186     catch (FGProtocolConfigError& err)
187     {
188         SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
189         delete io;
190         return 0;
191     }
192
193     string medium = tokens[1];
194     SG_LOG( SG_IO, SG_INFO, "  medium = " << medium );
195
196     string direction = tokens[2];
197     io->set_direction( direction );
198     SG_LOG( SG_IO, SG_INFO, "  direction = " << direction );
199
200     string hertz_str = tokens[3];
201     double hertz = atof( hertz_str.c_str() );
202     io->set_hz( hertz );
203     SG_LOG( SG_IO, SG_INFO, "  hertz = " << hertz );
204
205     if ( medium == "serial" ) {
206         // device name
207         string device = tokens[4];
208         SG_LOG( SG_IO, SG_INFO, "  device = " << device );
209
210         // baud
211         string baud = tokens[5];
212         SG_LOG( SG_IO, SG_INFO, "  baud = " << baud );
213
214         SGSerial *ch = new SGSerial( device, baud );
215         io->set_io_channel( ch );
216     } else if ( medium == "file" ) {
217         // file name
218         string file = tokens[4];
219         SG_LOG( SG_IO, SG_INFO, "  file name = " << file );
220
221         SGFile *ch = new SGFile( file );
222         io->set_io_channel( ch );
223     } else if ( medium == "socket" ) {
224         string hostname = tokens[4];
225         string port = tokens[5];
226         string style = tokens[6];
227
228         SG_LOG( SG_IO, SG_INFO, "  hostname = " << hostname );
229         SG_LOG( SG_IO, SG_INFO, "  port = " << port );
230         SG_LOG( SG_IO, SG_INFO, "  style = " << style );
231
232         io->set_io_channel( new SGSocket( hostname, port, style ) );
233     }
234
235     return io;
236 }
237
238
239 // step through the port config streams (from fgOPTIONS) and setup
240 // serial port channels for each
241 void
242 FGIO::init()
243 {
244     // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
245     //         globals->get_channel_options_list()->size() << " requests." );
246
247     FGProtocol *p;
248
249     // we could almost do this in a single step except pushing a valid
250     // port onto the port list copies the structure and destroys the
251     // original, which closes the port and frees up the fd ... doh!!!
252
253     // parse the configuration strings and store the results in the
254     // appropriate FGIOChannel structures
255     typedef vector<string> container;
256     container::iterator i = globals->get_channel_options_list()->begin();
257     container::iterator end = globals->get_channel_options_list()->end();
258     for (; i != end; ++i )
259     {
260         p = parse_port_config( *i );
261         if ( p != NULL ) {
262             p->open();
263             io_channels.push_back( p );
264             if ( !p->is_enabled() ) {
265                 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
266                 exit(-1);
267             }
268         } else {
269             SG_LOG( SG_IO, SG_INFO, "I/O Channel parse failed." );
270         }
271     }
272 }
273
274
275 // process any IO channel work
276 void
277 FGIO::update( double delta_time_sec )
278 {
279     // cout << "processing I/O channels" << endl;
280     // cout << "  Elapsed time = " << delta_time_sec << endl;
281
282     typedef vector< FGProtocol* > container;
283     container::iterator i = io_channels.begin();
284     container::iterator end = io_channels.end();
285     for (; i != end; ++i ) {
286         FGProtocol* p = *i;
287
288         if ( p->is_enabled() ) {
289             p->dec_count_down( delta_time_sec );
290             double dt = 1 / p->get_hz();
291             if ( p->get_count_down() < 0.33 * dt ) {
292               p->process();
293               p->inc_count();
294               while ( p->get_count_down() < 0.33 * dt ) {
295                 p->inc_count_down( dt );
296               }
297               // double ave = elapsed_time / p->get_count();
298               // cout << "  ave rate = " << ave << endl;
299             }
300         }
301     }
302 }
303
304
305 void
306 FGIO::shutdown_all() {
307     FGProtocol *p;
308
309     // cout << "processing I/O channels" << endl;
310
311     typedef vector< FGProtocol* > container;
312     container::iterator i = io_channels.begin();
313     container::iterator end = io_channels.end();
314     for (; i != end; ++i )
315     {
316         p = *i;
317
318         if ( p->is_enabled() ) {
319             p->close();
320         }
321     }
322 }
323
324 void
325 FGIO::bind()
326 {
327 }
328
329 void
330 FGIO::unbind()
331 {
332 }
333