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).
This commit is contained in:
parent
be3f94f63a
commit
379e7a2fd5
6 changed files with 62 additions and 37 deletions
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------------- */
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -100,6 +100,7 @@
|
|||
#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.
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -21,30 +21,41 @@
|
|||
#include "HTTPClient.hxx"
|
||||
|
||||
#include <Main/fg_props.hxx>
|
||||
|
||||
static FGHTTPClient* static_instance = NULL;
|
||||
|
||||
FGHTTPClient* FGHTTPClient::instance()
|
||||
{
|
||||
if (!static_instance) {
|
||||
static_instance = new FGHTTPClient;
|
||||
}
|
||||
|
||||
return static_instance;
|
||||
}
|
||||
|
||||
bool FGHTTPClient::haveInstance()
|
||||
{
|
||||
return (static_instance != NULL);
|
||||
}
|
||||
#include <simgear/sg_inlines.h>
|
||||
|
||||
FGHTTPClient::FGHTTPClient()
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
FGHTTPClient::~FGHTTPClient()
|
||||
{
|
||||
}
|
||||
|
||||
void FGHTTPClient::init()
|
||||
{
|
||||
_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);
|
||||
}
|
||||
}
|
||||
|
||||
void FGHTTPClient::shutdown()
|
||||
{
|
||||
_http.reset();
|
||||
}
|
||||
|
||||
void FGHTTPClient::update(double dt)
|
||||
{
|
||||
SG_UNUSED(dt);
|
||||
_http->update();
|
||||
}
|
||||
|
||||
void FGHTTPClient::makeRequest(const simgear::HTTP::Request_ptr& req)
|
||||
{
|
||||
_http->makeRequest(req);
|
||||
}
|
|
@ -21,15 +21,24 @@
|
|||
#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
|
||||
|
|
Loading…
Add table
Reference in a new issue