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
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) ) ;
61 virtual void handleBufferRead (simgear::NetBuffer& buffer)
63 const char* s = buffer.getData();
69 printf("after buffer.remove()\n");
73 bool isDone() const { return done; }
74 bool isDone( long usec ) const {
75 if ( start + SGTimeStamp::fromUSec(usec) < SGTimeStamp::now() ) {
83 FGExternalNet::FGExternalNet( double dt, string host, int dop, int dip, int cp )
94 /////////////////////////////////////////////////////////
95 // Setup client udp connection (sends data to remote fdm)
97 if ( ! data_client.open( false ) ) {
98 SG_LOG( SG_FLIGHT, SG_ALERT, "Error opening client data channel" );
103 data_client.setBlocking( false );
105 if ( data_client.connect( fdm_host.c_str(), data_out_port ) == -1 ) {
106 printf("error connecting to %s:%d\n", fdm_host.c_str(), data_out_port);
110 /////////////////////////////////////////////////////////
111 // Setup server udp connection (for receiving data)
113 if ( ! data_server.open( false ) ) {
114 SG_LOG( SG_FLIGHT, SG_ALERT, "Error opening client server channel" );
119 data_server.setBlocking( false );
121 // allowed to read from a broadcast addr
122 // data_server.setBroadcast( true );
124 // if we bind to fdm_host = "" then we accept messages from
126 if ( data_server.bind( "", data_in_port ) == -1 ) {
127 printf("error binding to port %d\n", data_in_port);
133 FGExternalNet::~FGExternalNet() {
139 // Initialize the ExternalNet flight model, dt is the time increment
140 // for each subsequent iteration through the EOM
141 void FGExternalNet::init() {
142 // cout << "FGExternalNet::init()" << endl;
144 // Explicitly call the superclass's
145 // init method first.
148 double lon = fgGetDouble( "/sim/presets/longitude-deg" );
149 double lat = fgGetDouble( "/sim/presets/latitude-deg" );
150 double alt = fgGetDouble( "/sim/presets/altitude-ft" );
151 double ground = get_Runway_altitude_m();
152 double heading = fgGetDouble("/sim/presets/heading-deg");
153 double speed = fgGetDouble( "/sim/presets/airspeed-kt" );
158 sprintf( cmd, "/longitude-deg?value=%.8f", lon );
159 http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
160 while ( !http->isDone(1000000) ) http->poll(0);
163 sprintf( cmd, "/latitude-deg?value=%.8f", lat );
164 http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
165 while ( !http->isDone(1000000) ) http->poll(0);
168 sprintf( cmd, "/altitude-ft?value=%.8f", alt );
169 http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
170 while ( !http->isDone(1000000) ) http->poll(0);
173 sprintf( cmd, "/ground-m?value=%.8f", ground );
174 http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
175 while ( !http->isDone(1000000) ) http->poll(0);
178 sprintf( cmd, "/speed-kts?value=%.8f", speed );
179 http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
180 while ( !http->isDone(1000000) ) http->poll(0);
183 sprintf( cmd, "/heading-deg?value=%.8f", heading );
184 http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
185 while ( !http->isDone(1000000) ) http->poll(0);
188 SG_LOG( SG_IO, SG_INFO, "before sending reset command." );
190 if( fgGetBool("/sim/presets/onground") ) {
191 sprintf( cmd, "/reset?value=ground" );
193 sprintf( cmd, "/reset?value=air" );
195 http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
196 while ( !http->isDone(1000000) ) http->poll(0);
199 SG_LOG( SG_IO, SG_INFO, "Remote FDM init() finished." );
203 // Run an iteration of the EOM.
204 void FGExternalNet::update( double dt ) {
211 // Send control positions to remote fdm
212 length = sizeof(ctrls);
213 FGProps2NetCtrls( &ctrls, true, true );
214 if ( data_client.send( (char *)(& ctrls), length, 0 ) != length ) {
215 SG_LOG( SG_IO, SG_DEBUG, "Error writing data." );
217 SG_LOG( SG_IO, SG_DEBUG, "wrote control data." );
220 // Read next set of FDM data (blocking enabled to maintain 'sync')
221 length = sizeof(fdm);
222 while ( (result = data_server.recv( (char *)(& fdm), length, 0)) >= 0 ) {
223 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
224 FGNetFDM2Props( &fdm );