1 // ExternalNet.hxx -- an net interface to an external flight dynamics model
3 // Written by Curtis Olson, started November 2001.
5 // Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
29 #include <simgear/debug/logstream.hxx>
30 #include <simgear/io/lowlevel.hxx> // endian tests
31 #include <simgear/io/sg_netBuffer.hxx>
33 #include <Main/fg_props.hxx>
34 #include <Network/native_ctrls.hxx>
35 #include <Network/native_fdm.hxx>
37 #include "ExternalNet.hxx"
40 class HTTPClient : public simgear::NetBufferChannel
45 simgear::NetChannelPoller poller;
48 HTTPClient ( const char* host, int port, const char* path ) :
55 ::snprintf (buffer, 256, "GET %s HTTP/1.0\r\n\r\n", path );
56 bufferSend(buffer, strlen(buffer) ) ;
58 poller.addChannel(this);
62 virtual void handleBufferRead (simgear::NetBuffer& buffer)
64 const char* s = buffer.getData();
70 printf("after buffer.remove()\n");
74 bool isDone() const { return done; }
75 bool isDone( long usec ) const {
76 if ( start + SGTimeStamp::fromUSec(usec) < SGTimeStamp::now() ) {
83 void poll(int timeout)
89 FGExternalNet::FGExternalNet( double dt, string host, int dop, int dip, int cp )
100 /////////////////////////////////////////////////////////
101 // Setup client udp connection (sends data to remote fdm)
103 if ( ! data_client.open( false ) ) {
104 SG_LOG( SG_FLIGHT, SG_ALERT, "Error opening client data channel" );
109 data_client.setBlocking( false );
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);
116 /////////////////////////////////////////////////////////
117 // Setup server udp connection (for receiving data)
119 if ( ! data_server.open( false ) ) {
120 SG_LOG( SG_FLIGHT, SG_ALERT, "Error opening client server channel" );
125 data_server.setBlocking( false );
127 // allowed to read from a broadcast addr
128 // data_server.setBroadcast( true );
130 // if we bind to fdm_host = "" then we accept messages from
132 if ( data_server.bind( "", data_in_port ) == -1 ) {
133 printf("error binding to port %d\n", data_in_port);
139 FGExternalNet::~FGExternalNet() {
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;
150 // Explicitly call the superclass's
151 // init method first.
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" );
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);
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);
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);
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);
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);
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);
194 SG_LOG( SG_IO, SG_INFO, "before sending reset command." );
196 if( fgGetBool("/sim/presets/onground") ) {
197 sprintf( cmd, "/reset?value=ground" );
199 sprintf( cmd, "/reset?value=air" );
201 http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
202 while ( !http->isDone(1000000) ) http->poll(0);
205 SG_LOG( SG_IO, SG_INFO, "Remote FDM init() finished." );
209 // Run an iteration of the EOM.
210 void FGExternalNet::update( double dt ) {
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." );
223 SG_LOG( SG_IO, SG_DEBUG, "wrote control data." );
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 );