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