]> git.mxchange.org Git - flightgear.git/commitdiff
Make HTTPClient a proper subsystem.
authorJames Turner <zakalawe@mac.com>
Mon, 19 Nov 2012 11:30:12 +0000 (11:30 +0000)
committerJames Turner <zakalawe@mac.com>
Mon, 19 Nov 2012 11:30:12 +0000 (11:30 +0000)
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
src/Main/fg_commands.cxx
src/Main/fg_init.cxx
src/Main/fg_io.cxx
src/Network/HTTPClient.cxx
src/Network/HTTPClient.hxx

index fcc73550a5e090da6af3372c3b5f5f6d0a190cce..6be99082e6fa942bb03933a7545df663be8ed514 100644 (file)
@@ -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<FGHTTPClient*>(globals->get_subsystem("http"));
+    if (http) {
+        http->makeRequest(new NoaaMetarGetRequest(metarDataHandler, upperId));
+    }
 }
 
 /* -------------------------------------------------------------------------------- */
index 746664f2608ca7d255d1b2029d9f4bc4d51c2b7e..a406a226bf895b03b4bca49564ce1bda1be44818 100644 (file)
@@ -1383,6 +1383,12 @@ protected:
 static bool
 do_load_xml_from_url(const SGPropertyNode * arg)
 {
+    FGHTTPClient* http = static_cast<FGHTTPClient*>(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;
 }
 
index 4abac9a31a498d28c2d3f5979581387282eee2df..716ae9514c6cf865eb1c83b4949ac3c39a6649ff 100644 (file)
 #include <Navaids/NavDataCache.hxx>
 #include <Instrumentation/HUD/HUD.hxx>
 #include <Cockpit/cockpitDisplayManager.hxx>
+#include <Network/HTTPClient.hxx>
 
 #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.
     ////////////////////////////////////////////////////////////////////
index db6289ae30bdc66b8bd31c995a95f469c16cf66a..3b2d0b93b82e3b0ff8dcf16a14e53d8acc522a23 100644 (file)
@@ -65,7 +65,6 @@
 #include <Network/ray.hxx>
 #include <Network/rul.hxx>
 #include <Network/generic.hxx>
-#include <Network/HTTPClient.hxx>
 
 #ifdef FG_HAVE_HLA
 #include <Network/HLA/hla.hxx>
@@ -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
index ca293d83ccb4df26691419a3755d872ff11c7acb..b86f42d82b0090efeab34b6ffa27514b599ef544 100644 (file)
 #include "HTTPClient.hxx"
 
 #include <Main/fg_props.hxx>
+#include <simgear/sg_inlines.h>
 
-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
index b36027e7fcd86eaf2e689714372fa1bbb61e3043..f97cebbd3252ea58b0fcf1bef6174111ad8c55f0 100644 (file)
 #ifndef FG_HTTP_CLIENT_HXX
 #define FG_HTTP_CLIENT_HXX
 
+#include <simgear/structure/subsystem_mgr.hxx>
 #include <simgear/io/HTTPClient.hxx>
+#include <memory>
 
-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<simgear::HTTP::Client> _http;
 };
 
 #endif // FG_HTTP_CLIENT_HXX