14#include <simgear/debug/logstream.hxx>
15#include <simgear/io/lowlevel.hxx>
16#include <simgear/io/sg_netBuffer.hxx>
30 simgear::NetChannelPoller poller;
33 HTTPClient(
const char* host,
int port,
const char* path) : done(false)
39 ::snprintf(buffer, 299,
"GET %s HTTP/1.0\r\n\r\n", path);
42 bufferSend(buffer, strlen(buffer));
44 poller.addChannel(
this);
50 const char* s = buffer.getData();
64 if (start + SGTimeStamp::fromUSec(usec) < SGTimeStamp::now()) {
91 if (!data_client.open(
false)) {
92 SG_LOG(SG_FLIGHT, SG_ALERT,
"Error opening client data channel");
97 data_client.setBlocking(
false);
99 if (data_client.connect(fdm_host.c_str(), data_out_port) == -1) {
100 printf(
"error connecting to %s:%d\n", fdm_host.c_str(), data_out_port);
107 if (!data_server.open(
false)) {
108 SG_LOG(SG_FLIGHT, SG_ALERT,
"Error opening client server channel");
113 data_server.setBlocking(
false);
120 if (data_server.bind(
"", data_in_port) == -1) {
121 printf(
"error binding to port %d\n", data_in_port);
144 double lon =
fgGetDouble(
"/sim/presets/longitude-deg");
145 double lat =
fgGetDouble(
"/sim/presets/latitude-deg");
146 double alt =
fgGetDouble(
"/sim/presets/altitude-ft");
148 double heading =
fgGetDouble(
"/sim/presets/heading-deg");
149 double speed =
fgGetDouble(
"/sim/presets/airspeed-kt");
154 snprintf(cmd, 256,
"/longitude-deg?value=%.8f", lon);
155 http =
new HTTPClient(fdm_host.c_str(), cmd_port, cmd);
159 snprintf(cmd, 256,
"/latitude-deg?value=%.8f", lat);
160 http =
new HTTPClient(fdm_host.c_str(), cmd_port, cmd);
164 snprintf(cmd, 256,
"/altitude-ft?value=%.8f", alt);
165 http =
new HTTPClient(fdm_host.c_str(), cmd_port, cmd);
169 snprintf(cmd, 256,
"/ground-m?value=%.8f", ground);
170 http =
new HTTPClient(fdm_host.c_str(), cmd_port, cmd);
174 snprintf(cmd, 256,
"/speed-kts?value=%.8f", speed);
175 http =
new HTTPClient(fdm_host.c_str(), cmd_port, cmd);
179 snprintf(cmd, 256,
"/heading-deg?value=%.8f", heading);
180 http =
new HTTPClient(fdm_host.c_str(), cmd_port, cmd);
184 SG_LOG(SG_IO, SG_INFO,
"before sending reset command.");
186 if (
fgGetBool(
"/sim/presets/onground")) {
187 snprintf(cmd, 256,
"/reset?value=ground");
189 snprintf(cmd, 256,
"/reset?value=air");
191 http =
new HTTPClient(fdm_host.c_str(), cmd_port, cmd);
195 SG_LOG(SG_IO, SG_INFO,
"Remote FDM init() finished.");
209 length =
sizeof(ctrls);
211 if (data_client.send((
char*)(&ctrls), length, 0) != length) {
212 SG_LOG(SG_IO, SG_DEBUG,
"Error writing data.");
214 SG_LOG(SG_IO, SG_DEBUG,
"wrote control data.");
218 length =
sizeof(fdm);
219 while ((result = data_server.recv((
char*)(&fdm), length, 0)) >= 0) {
220 SG_LOG(SG_IO, SG_DEBUG,
"Success reading data.");
227SGSubsystemMgr::Registrant<FGExternalNet> registrantFGExternalNet;
void update(double dt) override
FGExternalNet(double dt, std::string host, int dop, int dip, int cp)
void common_init()
Initialize the state of the FDM.
double get_Runway_altitude_m() const
HTTPClient(const char *host, int port, const char *path)
bool isDone(long usec) const
virtual void handleBufferRead(simgear::NetBuffer &buffer)
void FGProps2Ctrls< FGNetCtrls >(SGPropertyNode *props, FGNetCtrls *net, bool honor_freezes, bool net_byte_order)
void FGFDM2Props< FGNetFDM >(SGPropertyNode *props, FGNetFDM *net, bool net_byte_order)
bool fgGetBool(char const *name, bool def)
Get a bool value for a property.
double fgGetDouble(const char *name, double defaultValue)
Get a double value for a property.