From 379e7a2fd56efc65887b55e419723903447d462d Mon Sep 17 00:00:00 2001 From: James Turner Date: Mon, 19 Nov 2012 11:30:12 +0000 Subject: [PATCH] Make HTTPClient a proper subsystem. Avoid some statics, and incidentally make the proxy host/port settings dynamic (re-init the subsystem to update proxy settings, woo). --- src/Environment/realwx_ctrl.cxx | 5 +++- src/Main/fg_commands.cxx | 9 +++++-- src/Main/fg_init.cxx | 4 ++- src/Main/fg_io.cxx | 5 ---- src/Network/HTTPClient.cxx | 45 ++++++++++++++++++++------------- src/Network/HTTPClient.hxx | 19 ++++++++++---- 6 files changed, 56 insertions(+), 31 deletions(-) diff --git a/src/Environment/realwx_ctrl.cxx b/src/Environment/realwx_ctrl.cxx index fcc73550a..6be99082e 100644 --- a/src/Environment/realwx_ctrl.cxx +++ b/src/Environment/realwx_ctrl.cxx @@ -432,7 +432,10 @@ void NoaaMetarRealWxController::requestMetar( MetarDataHandler * metarDataHandle SG_LOG(SG_ENVIRONMENT, SG_INFO, "NoaaMetarRealWxController::update(): spawning load request for station-id '" << upperId << "'" ); - FGHTTPClient::instance()->makeRequest(new NoaaMetarGetRequest(metarDataHandler, upperId)); + FGHTTPClient* http = static_cast(globals->get_subsystem("http")); + if (http) { + http->makeRequest(new NoaaMetarGetRequest(metarDataHandler, upperId)); + } } /* -------------------------------------------------------------------------------- */ diff --git a/src/Main/fg_commands.cxx b/src/Main/fg_commands.cxx index 746664f26..a406a226b 100644 --- a/src/Main/fg_commands.cxx +++ b/src/Main/fg_commands.cxx @@ -1383,6 +1383,12 @@ protected: static bool do_load_xml_from_url(const SGPropertyNode * arg) { + FGHTTPClient* http = static_cast(globals->get_subsystem("http")); + if (!http) { + SG_LOG(SG_IO, SG_ALERT, "xmlhttprequest: HTTP client not running"); + return false; + } + std::string url(arg->getStringValue("url")); if (url.empty()) return false; @@ -1406,8 +1412,7 @@ do_load_xml_from_url(const SGPropertyNode * arg) if (arg->hasValue("status")) req->setStatusProp(fgGetNode(arg->getStringValue("status"), true)); - FGHTTPClient::instance()->makeRequest(req); - + http->makeRequest(req); return true; } diff --git a/src/Main/fg_init.cxx b/src/Main/fg_init.cxx index 4abac9a31..716ae9514 100644 --- a/src/Main/fg_init.cxx +++ b/src/Main/fg_init.cxx @@ -100,6 +100,7 @@ #include #include #include +#include #include "fg_init.hxx" #include "fg_io.hxx" @@ -622,7 +623,8 @@ void fgCreateSubsystems() { // Initialize the Input-Output subsystem //////////////////////////////////////////////////////////////////// globals->add_subsystem( "io", new FGIO ); - + globals->add_subsystem( "http", new FGHTTPClient ); + //////////////////////////////////////////////////////////////////// // Create and register the logger. //////////////////////////////////////////////////////////////////// diff --git a/src/Main/fg_io.cxx b/src/Main/fg_io.cxx index db6289ae3..3b2d0b93b 100644 --- a/src/Main/fg_io.cxx +++ b/src/Main/fg_io.cxx @@ -65,7 +65,6 @@ #include #include #include -#include #ifdef FG_HAVE_HLA #include @@ -382,10 +381,6 @@ FGIO::reinit() void FGIO::update( double /* delta_time_sec */ ) { - if (FGHTTPClient::haveInstance()) { - FGHTTPClient::instance()->update(); - } - // use wall-clock, not simulation, delta time, so that network // protocols update when the simulation is paused // see http://code.google.com/p/flightgear-bugs/issues/detail?id=125 diff --git a/src/Network/HTTPClient.cxx b/src/Network/HTTPClient.cxx index ca293d83c..b86f42d82 100644 --- a/src/Network/HTTPClient.cxx +++ b/src/Network/HTTPClient.cxx @@ -21,30 +21,41 @@ #include "HTTPClient.hxx" #include
+#include -static FGHTTPClient* static_instance = NULL; +FGHTTPClient::FGHTTPClient() +{ +} -FGHTTPClient* FGHTTPClient::instance() +FGHTTPClient::~FGHTTPClient() { - if (!static_instance) { - static_instance = new FGHTTPClient; - } - - return static_instance; } -bool FGHTTPClient::haveInstance() +void FGHTTPClient::init() { - return (static_instance != NULL); + _http.reset(new simgear::HTTP::Client); + + std::string proxyHost(fgGetString("/sim/presets/proxy/host")); + int proxyPort(fgGetInt("/sim/presets/proxy/port")); + std::string proxyAuth(fgGetString("/sim/presets/proxy/auth")); + + if (!proxyHost.empty()) { + _http->setProxy(proxyHost, proxyPort, proxyAuth); + } } -FGHTTPClient::FGHTTPClient() +void FGHTTPClient::shutdown() { - std::string proxyHost(fgGetString("/sim/presets/proxy/host")); - int proxyPort(fgGetInt("/sim/presets/proxy/port")); - std::string proxyAuth(fgGetString("/sim/presets/proxy/auth")); - - if (!proxyHost.empty()) { - setProxy(proxyHost, proxyPort, proxyAuth); - } + _http.reset(); } + +void FGHTTPClient::update(double dt) +{ + SG_UNUSED(dt); + _http->update(); +} + +void FGHTTPClient::makeRequest(const simgear::HTTP::Request_ptr& req) +{ + _http->makeRequest(req); +} \ No newline at end of file diff --git a/src/Network/HTTPClient.hxx b/src/Network/HTTPClient.hxx index b36027e7f..f97cebbd3 100644 --- a/src/Network/HTTPClient.hxx +++ b/src/Network/HTTPClient.hxx @@ -21,15 +21,24 @@ #ifndef FG_HTTP_CLIENT_HXX #define FG_HTTP_CLIENT_HXX +#include #include +#include -class FGHTTPClient : public simgear::HTTP::Client { +class FGHTTPClient : public SGSubsystem +{ public: - static FGHTTPClient* instance(); - - static bool haveInstance(); -private: FGHTTPClient(); + virtual ~FGHTTPClient(); + + void makeRequest(const simgear::HTTP::Request_ptr& req); + + virtual void init(); + virtual void shutdown(); + virtual void update(double dt); + +private: + std::auto_ptr _http; }; #endif // FG_HTTP_CLIENT_HXX -- 2.39.5