]> git.mxchange.org Git - flightgear.git/blob - src/FDM/ExternalNet/ExternalNet.cxx
Work with revised NetChannel poll() API.
[flightgear.git] / src / FDM / ExternalNet / ExternalNet.cxx
1 // ExternalNet.hxx -- an net interface to an external flight dynamics model
2 //
3 // Written by Curtis Olson, started November 2001.
4 //
5 // Copyright (C) 2001  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 <cstring>
28
29 #include <simgear/debug/logstream.hxx>
30 #include <simgear/io/lowlevel.hxx> // endian tests
31 #include <simgear/io/sg_netBuffer.hxx>
32
33 #include <Main/fg_props.hxx>
34 #include <Network/native_ctrls.hxx>
35 #include <Network/native_fdm.hxx>
36
37 #include "ExternalNet.hxx"
38
39
40 class HTTPClient : public simgear::NetBufferChannel
41 {
42
43     bool done;
44     SGTimeStamp start;
45     simgear::NetChannelPoller poller;
46 public:
47
48     HTTPClient ( const char* host, int port, const char* path ) :
49         done( false )
50     {
51         open ();
52         connect (host, port);
53
54   char buffer[256];
55   ::snprintf (buffer, 256, "GET %s HTTP/1.0\r\n\r\n", path );
56         bufferSend(buffer, strlen(buffer) ) ;
57
58         poller.addChannel(this);
59         start.stamp();
60     }
61
62     virtual void handleBufferRead (simgear::NetBuffer& buffer)
63     {
64         const char* s = buffer.getData();
65         while (*s)
66             fputc(*s++,stdout);
67
68         printf("done\n");
69         buffer.remove();
70         printf("after buffer.remove()\n");
71         done = true;
72     }
73
74     bool isDone() const { return done; }
75     bool isDone( long usec ) const { 
76         if ( start + SGTimeStamp::fromUSec(usec) < SGTimeStamp::now() ) {
77             return true;
78         } else {
79             return done;
80         }
81     }
82     
83     void poll(int timeout)
84     {
85         poller.poll(timeout);
86     }
87 };
88
89 FGExternalNet::FGExternalNet( double dt, string host, int dop, int dip, int cp )
90 {
91 //     set_delta_t( dt );
92
93     valid = true;
94
95     data_in_port = dip;
96     data_out_port = dop;
97     cmd_port = cp;
98     fdm_host = host;
99
100     /////////////////////////////////////////////////////////
101     // Setup client udp connection (sends data to remote fdm)
102
103     if ( ! data_client.open( false ) ) {
104         SG_LOG( SG_FLIGHT, SG_ALERT, "Error opening client data channel" );
105         valid = false;
106     }
107
108     // fire and forget
109     data_client.setBlocking( false );
110
111     if ( data_client.connect( fdm_host.c_str(), data_out_port ) == -1 ) {
112         printf("error connecting to %s:%d\n", fdm_host.c_str(), data_out_port);
113         valid = false;
114     }
115
116     /////////////////////////////////////////////////////////
117     // Setup server udp connection (for receiving data)
118
119     if ( ! data_server.open( false ) ) {
120         SG_LOG( SG_FLIGHT, SG_ALERT, "Error opening client server channel" );
121         valid = false;
122     }
123
124     // disable blocking
125     data_server.setBlocking( false );
126
127     // allowed to read from a broadcast addr
128     // data_server.setBroadcast( true );
129
130     // if we bind to fdm_host = "" then we accept messages from
131     // anyone.
132     if ( data_server.bind( "", data_in_port ) == -1 ) {
133         printf("error binding to port %d\n", data_in_port);
134         valid = false;
135     }
136 }
137
138
139 FGExternalNet::~FGExternalNet() {
140     data_client.close();
141     data_server.close();
142 }
143
144
145 // Initialize the ExternalNet flight model, dt is the time increment
146 // for each subsequent iteration through the EOM
147 void FGExternalNet::init() {
148     // cout << "FGExternalNet::init()" << endl;
149
150     // Explicitly call the superclass's
151     // init method first.
152     common_init();
153
154     double lon = fgGetDouble( "/sim/presets/longitude-deg" );
155     double lat = fgGetDouble( "/sim/presets/latitude-deg" );
156     double alt = fgGetDouble( "/sim/presets/altitude-ft" );
157     double ground = get_Runway_altitude_m();
158     double heading = fgGetDouble("/sim/presets/heading-deg");
159     double speed = fgGetDouble( "/sim/presets/airspeed-kt" );
160
161     char cmd[256];
162
163     HTTPClient *http;
164     sprintf( cmd, "/longitude-deg?value=%.8f", lon );
165     http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
166     while ( !http->isDone(1000000) ) http->poll(0);
167     delete http;
168
169     sprintf( cmd, "/latitude-deg?value=%.8f", lat );
170     http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
171     while ( !http->isDone(1000000) ) http->poll(0);
172     delete http;
173
174     sprintf( cmd, "/altitude-ft?value=%.8f", alt );
175     http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
176     while ( !http->isDone(1000000) ) http->poll(0);
177     delete http;
178
179     sprintf( cmd, "/ground-m?value=%.8f", ground );
180     http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
181     while ( !http->isDone(1000000) ) http->poll(0);
182     delete http;
183
184     sprintf( cmd, "/speed-kts?value=%.8f", speed );
185     http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
186     while ( !http->isDone(1000000) ) http->poll(0);
187     delete http;
188
189     sprintf( cmd, "/heading-deg?value=%.8f", heading );
190     http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
191     while ( !http->isDone(1000000) ) http->poll(0);
192     delete http;
193
194     SG_LOG( SG_IO, SG_INFO, "before sending reset command." );
195
196     if( fgGetBool("/sim/presets/onground") ) {
197       sprintf( cmd, "/reset?value=ground" );
198     } else {
199       sprintf( cmd, "/reset?value=air" );
200     }
201     http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
202     while ( !http->isDone(1000000) ) http->poll(0);
203     delete http;
204
205     SG_LOG( SG_IO, SG_INFO, "Remote FDM init() finished." );
206 }
207
208
209 // Run an iteration of the EOM.
210 void FGExternalNet::update( double dt ) {
211     int length;
212     int result;
213
214     if (is_suspended())
215       return;
216
217     // Send control positions to remote fdm
218     length = sizeof(ctrls);
219     FGProps2NetCtrls( &ctrls, true, true );
220     if ( data_client.send( (char *)(& ctrls), length, 0 ) != length ) {
221         SG_LOG( SG_IO, SG_DEBUG, "Error writing data." );
222     } else {
223         SG_LOG( SG_IO, SG_DEBUG, "wrote control data." );
224     }
225
226     // Read next set of FDM data (blocking enabled to maintain 'sync')
227     length = sizeof(fdm);
228     while ( (result = data_server.recv( (char *)(& fdm), length, 0)) >= 0 ) {
229         SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
230         FGNetFDM2Props( &fdm );
231     }
232 }