]> git.mxchange.org Git - flightgear.git/blob - src/FDM/ExternalNet/ExternalNet.cxx
Merge branch 'next' of gitorious.org:fg/flightgear into next
[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
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         start.stamp();
59     }
60
61     virtual void handleBufferRead (simgear::NetBuffer& buffer)
62     {
63         const char* s = buffer.getData();
64         while (*s)
65             fputc(*s++,stdout);
66
67         printf("done\n");
68         buffer.remove();
69         printf("after buffer.remove()\n");
70         done = true;
71     }
72
73     bool isDone() const { return done; }
74     bool isDone( long usec ) const { 
75         if ( start + SGTimeStamp::fromUSec(usec) < SGTimeStamp::now() ) {
76             return true;
77         } else {
78             return done;
79         }
80     }
81 };
82
83 FGExternalNet::FGExternalNet( double dt, string host, int dop, int dip, int cp )
84 {
85 //     set_delta_t( dt );
86
87     valid = true;
88
89     data_in_port = dip;
90     data_out_port = dop;
91     cmd_port = cp;
92     fdm_host = host;
93
94     /////////////////////////////////////////////////////////
95     // Setup client udp connection (sends data to remote fdm)
96
97     if ( ! data_client.open( false ) ) {
98         SG_LOG( SG_FLIGHT, SG_ALERT, "Error opening client data channel" );
99         valid = false;
100     }
101
102     // fire and forget
103     data_client.setBlocking( false );
104
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);
107         valid = false;
108     }
109
110     /////////////////////////////////////////////////////////
111     // Setup server udp connection (for receiving data)
112
113     if ( ! data_server.open( false ) ) {
114         SG_LOG( SG_FLIGHT, SG_ALERT, "Error opening client server channel" );
115         valid = false;
116     }
117
118     // disable blocking
119     data_server.setBlocking( false );
120
121     // allowed to read from a broadcast addr
122     // data_server.setBroadcast( true );
123
124     // if we bind to fdm_host = "" then we accept messages from
125     // anyone.
126     if ( data_server.bind( "", data_in_port ) == -1 ) {
127         printf("error binding to port %d\n", data_in_port);
128         valid = false;
129     }
130 }
131
132
133 FGExternalNet::~FGExternalNet() {
134     data_client.close();
135     data_server.close();
136 }
137
138
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;
143
144     // Explicitly call the superclass's
145     // init method first.
146     common_init();
147
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" );
154
155     char cmd[256];
156
157     HTTPClient *http;
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);
161     delete http;
162
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);
166     delete http;
167
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);
171     delete http;
172
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);
176     delete http;
177
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);
181     delete http;
182
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);
186     delete http;
187
188     SG_LOG( SG_IO, SG_INFO, "before sending reset command." );
189
190     if( fgGetBool("/sim/presets/onground") ) {
191       sprintf( cmd, "/reset?value=ground" );
192     } else {
193       sprintf( cmd, "/reset?value=air" );
194     }
195     http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
196     while ( !http->isDone(1000000) ) http->poll(0);
197     delete http;
198
199     SG_LOG( SG_IO, SG_INFO, "Remote FDM init() finished." );
200 }
201
202
203 // Run an iteration of the EOM.
204 void FGExternalNet::update( double dt ) {
205     int length;
206     int result;
207
208     if (is_suspended())
209       return;
210
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." );
216     } else {
217         SG_LOG( SG_IO, SG_DEBUG, "wrote control data." );
218     }
219
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 );
225     }
226 }