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.
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/io/lowlevel.hxx> // endian tests
29 #include <simgear/io/sg_netBuffer.hxx>
31 #include <Main/fg_props.hxx>
32 #include <Network/native_ctrls.hxx>
33 #include <Network/native_fdm.hxx>
35 #include "ExternalNet.hxx"
38 class HTTPClient : public simgear::NetBufferChannel
46 HTTPClient ( const char* host, int port, const char* path ) :
53 ::snprintf (buffer, 256, "GET %s HTTP/1.0\r\n\r\n", path );
54 bufferSend(buffer, strlen(buffer) ) ;
59 virtual void handleBufferRead (simgear::NetBuffer& buffer)
61 const char* s = buffer.getData();
67 printf("after buffer.remove()\n");
71 bool isDone() const { return done; }
72 bool isDone( long usec ) const {
73 if ( start + SGTimeStamp::fromUSec(usec) < SGTimeStamp::now() ) {
81 FGExternalNet::FGExternalNet( double dt, string host, int dop, int dip, int cp )
92 /////////////////////////////////////////////////////////
93 // Setup client udp connection (sends data to remote fdm)
95 if ( ! data_client.open( false ) ) {
96 SG_LOG( SG_FLIGHT, SG_ALERT, "Error opening client data channel" );
101 data_client.setBlocking( false );
103 if ( data_client.connect( fdm_host.c_str(), data_out_port ) == -1 ) {
104 printf("error connecting to %s:%d\n", fdm_host.c_str(), data_out_port);
108 /////////////////////////////////////////////////////////
109 // Setup server udp connection (for receiving data)
111 if ( ! data_server.open( false ) ) {
112 SG_LOG( SG_FLIGHT, SG_ALERT, "Error opening client server channel" );
117 data_server.setBlocking( false );
119 // allowed to read from a broadcast addr
120 // data_server.setBroadcast( true );
122 // if we bind to fdm_host = "" then we accept messages from
124 if ( data_server.bind( "", data_in_port ) == -1 ) {
125 printf("error binding to port %d\n", data_in_port);
131 FGExternalNet::~FGExternalNet() {
137 // Initialize the ExternalNet flight model, dt is the time increment
138 // for each subsequent iteration through the EOM
139 void FGExternalNet::init() {
140 // cout << "FGExternalNet::init()" << endl;
142 // Explicitly call the superclass's
143 // init method first.
146 double lon = fgGetDouble( "/sim/presets/longitude-deg" );
147 double lat = fgGetDouble( "/sim/presets/latitude-deg" );
148 double alt = fgGetDouble( "/sim/presets/altitude-ft" );
149 double ground = get_Runway_altitude_m();
150 double heading = fgGetDouble("/sim/presets/heading-deg");
151 double speed = fgGetDouble( "/sim/presets/airspeed-kt" );
156 sprintf( cmd, "/longitude-deg?value=%.8f", lon );
157 http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
158 while ( !http->isDone(1000000) ) http->poll(0);
161 sprintf( cmd, "/latitude-deg?value=%.8f", lat );
162 http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
163 while ( !http->isDone(1000000) ) http->poll(0);
166 sprintf( cmd, "/altitude-ft?value=%.8f", alt );
167 http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
168 while ( !http->isDone(1000000) ) http->poll(0);
171 sprintf( cmd, "/ground-m?value=%.8f", ground );
172 http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
173 while ( !http->isDone(1000000) ) http->poll(0);
176 sprintf( cmd, "/speed-kts?value=%.8f", speed );
177 http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
178 while ( !http->isDone(1000000) ) http->poll(0);
181 sprintf( cmd, "/heading-deg?value=%.8f", heading );
182 http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
183 while ( !http->isDone(1000000) ) http->poll(0);
186 SG_LOG( SG_IO, SG_INFO, "before sending reset command." );
188 if( fgGetBool("/sim/presets/onground") ) {
189 sprintf( cmd, "/reset?value=ground" );
191 sprintf( cmd, "/reset?value=air" );
193 http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
194 while ( !http->isDone(1000000) ) http->poll(0);
197 SG_LOG( SG_IO, SG_INFO, "Remote FDM init() finished." );
201 // Run an iteration of the EOM.
202 void FGExternalNet::update( double dt ) {
209 // Send control positions to remote fdm
210 length = sizeof(ctrls);
211 FGProps2NetCtrls( &ctrls, true, true );
212 if ( data_client.send( (char *)(& ctrls), length, 0 ) != length ) {
213 SG_LOG( SG_IO, SG_DEBUG, "Error writing data." );
215 SG_LOG( SG_IO, SG_DEBUG, "wrote control data." );
218 // Read next set of FDM data (blocking enabled to maintain 'sync')
219 length = sizeof(fdm);
220 while ( (result = data_server.recv( (char *)(& fdm), length, 0)) >= 0 ) {
221 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
222 FGNetFDM2Props( &fdm );