+/* -------------------------------------------------------------------------------- */
+
+class FGHTTPClient : public simgear::HTTP::Client {
+public:
+ FGHTTPClient();
+};
+
+FGHTTPClient::FGHTTPClient()
+{
+ string proxyHost(fgGetString("/sim/presets/proxy/host"));
+ int proxyPort(fgGetInt("/sim/presets/proxy/port"));
+ string proxyAuth(fgGetString("/sim/presets/proxy/auth"));
+
+ if (!proxyHost.empty()) {
+ setProxy(proxyHost, proxyPort, proxyAuth);
+ }
+}
+
+/* -------------------------------------------------------------------------------- */
+
+class MetarDataHandler {
+public:
+ virtual void handleMetarData( const std::string & data ) = 0;
+};
+
+class MetarRequester {
+public:
+ virtual void requestMetar( MetarDataHandler * metarDataHandler, const std::string & id ) = 0;
+};
+
+/* -------------------------------------------------------------------------------- */
+
+class LiveMetarProperties : public MetarProperties, MetarDataHandler {
+public:
+ LiveMetarProperties( SGPropertyNode_ptr rootNode, MetarRequester * metarRequester );
+ virtual ~LiveMetarProperties();
+ virtual void update( double dt );
+
+ virtual double getTimeToLive() const { return _timeToLive; }
+ virtual void setTimeToLive( double value ) { _timeToLive = value; }
+
+ // implementation of MetarDataHandler
+ virtual void handleMetarData( const std::string & data );
+
+ static const unsigned MAX_POLLING_INTERVAL_SECONDS = 10;
+ static const unsigned DEFAULT_TIME_TO_LIVE_SECONDS = 900;
+
+private:
+ double _timeToLive;
+ double _pollingTimer;
+ MetarRequester * _metarRequester;
+};
+
+typedef SGSharedPtr<LiveMetarProperties> LiveMetarProperties_ptr;
+
+LiveMetarProperties::LiveMetarProperties( SGPropertyNode_ptr rootNode, MetarRequester * metarRequester ) :
+ MetarProperties( rootNode ),
+ _timeToLive(0.0),
+ _pollingTimer(0.0),
+ _metarRequester(metarRequester)
+{
+ _tiedProperties.Tie("time-to-live", &_timeToLive );
+}
+
+LiveMetarProperties::~LiveMetarProperties()
+{
+ _tiedProperties.Untie();
+}
+
+void LiveMetarProperties::update( double dt )
+{
+ _timeToLive -= dt;
+ _pollingTimer -= dt;
+ if( _timeToLive < 0.0 ) {
+ _timeToLive = 0.0;
+ std::string stationId = getStationId();
+ if( stationId.empty() ) return;
+ if( _pollingTimer > 0.0 ) return;
+ _metarRequester->requestMetar( this, stationId );
+ _pollingTimer = MAX_POLLING_INTERVAL_SECONDS;
+ }
+}
+
+void LiveMetarProperties::handleMetarData( const std::string & data )
+{
+ SG_LOG( SG_ENVIRONMENT, SG_INFO, "LiveMetarProperties::handleMetarData() received METAR for " << getStationId() << ": " << data );
+ _timeToLive = DEFAULT_TIME_TO_LIVE_SECONDS;
+ setMetar( data );
+}
+
+/* -------------------------------------------------------------------------------- */