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