X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FEnvironment%2Frealwx_ctrl.cxx;h=621fb11c3c829ba76c0c98214937c049aab5837f;hb=ed434d9967b0b23e4abb172d699ef956246f4bf9;hp=4b379a6574cfa6289d79598146dc0c811975c67d;hpb=141186a358d07141eac151c287761eed6d81b68c;p=flightgear.git diff --git a/src/Environment/realwx_ctrl.cxx b/src/Environment/realwx_ctrl.cxx index 4b379a657..621fb11c3 100644 --- a/src/Environment/realwx_ctrl.cxx +++ b/src/Environment/realwx_ctrl.cxx @@ -25,7 +25,6 @@ #endif #include "realwx_ctrl.hxx" -#include "tiedpropertylist.hxx" #include "metarproperties.hxx" #include "metarairportfilter.hxx" #include "fgmetar.hxx" @@ -34,15 +33,54 @@ #include #include +#include #include #if defined(ENABLE_THREADS) #include #include #endif +using simgear::PropertyList; namespace Environment { +/* -------------------------------------------------------------------------------- */ + +class LiveMetarProperties : public MetarProperties { +public: + LiveMetarProperties( SGPropertyNode_ptr rootNode ); + virtual ~LiveMetarProperties(); + virtual void update( double dt ); + + virtual double getTimeToLive() const { return _timeToLive; } + virtual void setTimeToLive( double value ) { _timeToLive = value; } +private: + double _timeToLive; + +}; + +typedef SGSharedPtr LiveMetarProperties_ptr; + +LiveMetarProperties::LiveMetarProperties( SGPropertyNode_ptr rootNode ) : + MetarProperties( rootNode ), + _timeToLive(0.0) +{ + _tiedProperties.Tie("time-to-live", &_timeToLive ); +} + +LiveMetarProperties::~LiveMetarProperties() +{ + _tiedProperties.Untie(); +} + +void LiveMetarProperties::update( double dt ) +{ + _timeToLive -= dt; + if( _timeToLive < 0.0 ) _timeToLive = 0.0; +} + +/* -------------------------------------------------------------------------------- */ + class BasicRealWxController : public RealWxController { public: @@ -55,6 +93,9 @@ public: protected: void bind(); void unbind(); + void update( double dt ); + + virtual void update( bool first, double dt ) = 0; long getMetarMaxAgeMin() const { return _max_age_n == NULL ? 0 : _max_age_n->getLongValue(); } @@ -65,8 +106,10 @@ protected: SGPropertyNode_ptr _max_age_n; bool _enabled; - TiedPropertyList _tiedProperties; - MetarProperties _metarProperties; + bool __enabled; + simgear::TiedPropertyList _tiedProperties; + typedef std::vector MetarPropertiesList; + MetarPropertiesList _metarProperties; }; /* -------------------------------------------------------------------------------- */ @@ -83,8 +126,18 @@ BasicRealWxController::BasicRealWxController( SGPropertyNode_ptr rootNode ) : _ground_elevation_n( fgGetNode( "/position/ground-elev-m", true )), _max_age_n( fgGetNode( "/environment/params/metar-max-age-min", false ) ), _enabled(true), - _metarProperties( fgGetNode( rootNode->getStringValue("metar", "/environment/metar"), true ) ) + __enabled(false) { + // at least instantiate MetarProperties for /environment/metar + _metarProperties.push_back( new LiveMetarProperties( + fgGetNode( rootNode->getStringValue("metar", "/environment/metar"), true ) ) ); + + PropertyList metars = rootNode->getChildren("metar"); + for( PropertyList::size_type i = 1; i < metars.size(); i++ ) { + SG_LOG( SG_ALL, SG_INFO, "Adding metar properties at " << metars[i]->getStringValue() ); + _metarProperties.push_back( new LiveMetarProperties( + fgGetNode( metars[i]->getStringValue(), true ))); + } } BasicRealWxController::~BasicRealWxController() @@ -93,11 +146,13 @@ BasicRealWxController::~BasicRealWxController() void BasicRealWxController::init() { + __enabled = false; update(0); // fetch data ASAP } void BasicRealWxController::reinit() { + __enabled = false; } void BasicRealWxController::bind() @@ -111,50 +166,90 @@ void BasicRealWxController::unbind() _tiedProperties.Untie(); } +void BasicRealWxController::update( double dt ) +{ + if( _enabled ) { + bool firstIteration = !__enabled; // first iteration after being enabled? + + // clock tick for every METAR in stock + for( MetarPropertiesList::iterator it = _metarProperties.begin(); + it != _metarProperties.end(); it++ ) { + // first round? All received METARs are outdated + if( firstIteration ) (*it)->setTimeToLive( 0.0 ); + (*it)->update(dt); + } + + update( firstIteration, dt ); + __enabled = true; + } else { + __enabled = false; + } +} + /* -------------------------------------------------------------------------------- */ class NoaaMetarRealWxController : public BasicRealWxController { public: NoaaMetarRealWxController( SGPropertyNode_ptr rootNode ); virtual ~NoaaMetarRealWxController(); - virtual void update (double delta_time_sec); + virtual void update (bool first, double delta_time_sec); + virtual void shutdown (); class MetarLoadRequest { public: - MetarLoadRequest( const string & stationId ) { - _stationId = stationId; - _proxyHost = fgGetNode("/sim/presets/proxy/host", true)->getStringValue(); - _proxyPort = fgGetNode("/sim/presets/proxy/port", true)->getStringValue(); - _proxyAuth = fgGetNode("/sim/presets/proxy/authentication", true)->getStringValue(); - } + MetarLoadRequest( const string & stationId ) : + _stationId(stationId), + _proxyHost(fgGetNode("/sim/presets/proxy/host", true)->getStringValue()), + _proxyPort(fgGetNode("/sim/presets/proxy/port", true)->getStringValue()), + _proxyAuth(fgGetNode("/sim/presets/proxy/authentication", true)->getStringValue()) + {} + MetarLoadRequest( const MetarLoadRequest & other ) : + _stationId(other._stationId), + _proxyHost(other._proxyAuth), + _proxyPort(other._proxyPort), + _proxyAuth(other._proxyAuth) + {} string _stationId; string _proxyHost; string _proxyPort; string _proxyAuth; private: }; + + class MetarLoadResponse { + public: + MetarLoadResponse( const string & stationId, const string metar ) { + _stationId = stationId; + _metar = metar; + } + MetarLoadResponse( const MetarLoadResponse & other ) { + _stationId = other._stationId; + _metar = other._metar; + } + string _stationId; + string _metar; + }; private: - double _metarTimeToLive; double _positionTimeToLive; - double _minimumRequestInterval; - - SGPropertyNode_ptr _metarDataNode; - SGPropertyNode_ptr _metarValidNode; - SGPropertyNode_ptr _metarStationIdNode; - + double _requestTimer; #if defined(ENABLE_THREADS) class MetarLoadThread : public OpenThreads::Thread { public: - MetarLoadThread( long maxAge ); - void requestMetar( const MetarLoadRequest & metarRequest ); - bool hasMetar() { return _responseQueue.size() > 0; } - string getMetar() { return _responseQueue.pop(); } - virtual void run(); + MetarLoadThread( long maxAge ); + virtual ~MetarLoadThread( ) { stop(); } + void requestMetar( const MetarLoadRequest & metarRequest, bool background = true ); + bool hasMetar() { return _responseQueue.size() > 0; } + MetarLoadResponse getMetar() { return _responseQueue.pop(); } + virtual void run(); + void stop(); private: + void fetch( const MetarLoadRequest & ); long _maxAge; + long _minRequestInterval; + volatile bool _stop; SGBlockingQueue _requestQueue; - SGBlockingQueue _responseQueue; + SGBlockingQueue _responseQueue; }; MetarLoadThread * _metarLoadThread; @@ -163,12 +258,8 @@ private: NoaaMetarRealWxController::NoaaMetarRealWxController( SGPropertyNode_ptr rootNode ) : BasicRealWxController(rootNode), - _metarTimeToLive(0.0), _positionTimeToLive(0.0), - _minimumRequestInterval(0.0), - _metarDataNode(_metarProperties.get_root_node()->getNode("data",true)), - _metarValidNode(_metarProperties.get_root_node()->getNode("valid",true)), - _metarStationIdNode(_metarProperties.get_root_node()->getNode("station-id",true)) + _requestTimer(0.0) { #if defined(ENABLE_THREADS) _metarLoadThread = new MetarLoadThread(getMetarMaxAgeMin()); @@ -176,44 +267,28 @@ NoaaMetarRealWxController::NoaaMetarRealWxController( SGPropertyNode_ptr rootNod #endif } -NoaaMetarRealWxController::~NoaaMetarRealWxController() +void NoaaMetarRealWxController::shutdown() { #if defined(ENABLE_THREADS) if( _metarLoadThread ) { - MetarLoadRequest request(""); - _metarLoadThread->requestMetar(request); - _metarLoadThread->join(); delete _metarLoadThread; + _metarLoadThread = NULL; } #endif // ENABLE_THREADS } -void NoaaMetarRealWxController::update( double dt ) +NoaaMetarRealWxController::~NoaaMetarRealWxController() { - if( !_enabled ) - return; - - if( _metarLoadThread->hasMetar() ) { - string metar = _metarLoadThread->getMetar(); - SG_LOG( SG_ALL, SG_ALERT, "NoaaMetarRwalWxController::update() received METAR " << metar ); - _metarDataNode->setStringValue( metar ); - } +} - _metarTimeToLive -= dt; +void NoaaMetarRealWxController::update( bool first, double dt ) +{ _positionTimeToLive -= dt; - _minimumRequestInterval -= dt; - - bool valid = _metarValidNode->getBoolValue(); - string stationId = valid ? _metarStationIdNode->getStringValue() : ""; - - - if( _metarTimeToLive <= 0.0 ) { - valid = false; - _metarTimeToLive = 900; - _positionTimeToLive = 0; - } + _requestTimer -= dt; - if( _positionTimeToLive <= 0.0 || valid == false ) { + if( _positionTimeToLive <= 0.0 ) { + // check nearest airport + SG_LOG(SG_ALL, SG_INFO, "NoaaMetarRealWxController::update(): (re) checking nearby airport with METAR" ); _positionTimeToLive = 60.0; SGGeod pos = SGGeod::fromDeg(_longitude_n->getDoubleValue(), _latitude_n->getDoubleValue()); @@ -224,78 +299,156 @@ void NoaaMetarRealWxController::update( double dt ) return; } - if( stationId != nearestAirport->ident() ) { - valid = false; - stationId = nearestAirport->ident(); + SG_LOG(SG_ALL, SG_INFO, + "NoaaMetarRealWxController::update(): nearest airport with METAR is: " << nearestAirport->ident() ); + + // if it has changed, invalidate the associated METAR + if( _metarProperties[0]->getStationId() != nearestAirport->ident() ) { + SG_LOG(SG_ALL, SG_INFO, + "NoaaMetarRealWxController::update(): nearest airport with METAR has changed. Old: '" << + _metarProperties[0]->getStationId() << + "', new: '" << nearestAirport->ident() << "'" ); + _metarProperties[0]->setStationId( nearestAirport->ident() ); + _metarProperties[0]->setTimeToLive( 0.0 ); } - } - - if( !valid ) { - if( _minimumRequestInterval <= 0 && stationId.length() > 0 ) { - MetarLoadRequest request( stationId ); - _metarLoadThread->requestMetar( request ); - _minimumRequestInterval = 10; + + if( _requestTimer <= 0.0 ) { + _requestTimer = 10.0; + + for( MetarPropertiesList::iterator it = _metarProperties.begin(); + it != _metarProperties.end(); it++ ) { + + if( (*it)->getTimeToLive() > 0.0 ) continue; + const std::string & stationId = (*it)->getStationId(); + if( stationId.empty() ) continue; + + SG_LOG(SG_ALL, SG_INFO, + "NoaaMetarRealWxController::update(): spawning load request for station-id '" << stationId << "'" ); + + MetarLoadRequest request( stationId ); + // load the metar for the nearest airport in the foreground if the fdm is uninitialized + // to make sure a metar is received + // before the automatic runway selection code runs. All subsequent calls + // run in the background + bool background = fgGetBool("/sim/fdm-initialized", false ) || it != _metarProperties.begin(); + _metarLoadThread->requestMetar( request, background ); } } + // pick all the received responses from the result queue and update the associated + // property tree + while( _metarLoadThread->hasMetar() ) { + MetarLoadResponse metar = _metarLoadThread->getMetar(); + SG_LOG( SG_ALL, SG_INFO, "NoaaMetarRwalWxController::update() received METAR for " << metar._stationId << ": " << metar._metar ); + for( MetarPropertiesList::iterator it = _metarProperties.begin(); + it != _metarProperties.end(); it++ ) { + if( (*it)->getStationId() != metar._stationId ) + continue; + (*it)->setTimeToLive(900); + (*it)->setMetar( metar._metar ); + } + } } /* -------------------------------------------------------------------------------- */ #if defined(ENABLE_THREADS) NoaaMetarRealWxController::MetarLoadThread::MetarLoadThread( long maxAge ) : - _maxAge(maxAge) + _maxAge(maxAge), + _minRequestInterval(2000), + _stop(false) { } -void NoaaMetarRealWxController::MetarLoadThread::requestMetar( const MetarLoadRequest & metarRequest ) +void NoaaMetarRealWxController::MetarLoadThread::requestMetar( const MetarLoadRequest & metarRequest, bool background ) { - if( _requestQueue.size() > 10 ) { - SG_LOG(SG_ALL,SG_ALERT, - "NoaaMetarRealWxController::MetarLoadThread::requestMetar() more than 10 outstanding METAR requests, dropping " - << metarRequest._stationId ); - return; + if( background ) { + if( _requestQueue.size() > 10 ) { + SG_LOG(SG_ALL,SG_ALERT, + "NoaaMetarRealWxController::MetarLoadThread::requestMetar() more than 10 outstanding METAR requests, dropping " + << metarRequest._stationId ); + return; + } + + _requestQueue.push( metarRequest ); + } else { + fetch( metarRequest ); } +} - _requestQueue.push( metarRequest ); +void NoaaMetarRealWxController::MetarLoadThread::stop() +{ + // set stop flag and wake up the thread with an empty request + _stop = true; + MetarLoadRequest request(""); + requestMetar(request); + join(); } void NoaaMetarRealWxController::MetarLoadThread::run() { + SGTimeStamp lastRun = SGTimeStamp::fromSec(0); for( ;; ) { - const MetarLoadRequest request = _requestQueue.pop(); + SGTimeStamp dt = SGTimeStamp::now() - lastRun; + + long delayMs = _minRequestInterval - dt.getSeconds() * 1000; + while (( delayMs > 0 ) && !_stop) + { + // sleep no more than 3 seconds at a time, otherwise shutdown response is too slow + long sleepMs = (delayMs>3000) ? 3000 : delayMs; + microSleep( sleepMs * 1000 ); + delayMs -= sleepMs; + } - if( request._stationId.size() == 0 ) + if (_stop) break; - SGSharedPtr result = NULL; + lastRun = SGTimeStamp::now(); + + const MetarLoadRequest request = _requestQueue.pop(); - try { - result = new FGMetar( request._stationId, request._proxyHost, request._proxyPort, request._proxyAuth ); - } catch (const sg_io_exception& e) { - SG_LOG( SG_GENERAL, SG_WARN, "NoaaMetarRealWxController::fetchMetar(): can't get METAR for " - << request._stationId << ":" << e.getFormattedMessage().c_str() ); - continue; - } + if (( request._stationId.size() == 0 ) || _stop) + break; - string reply = result->getData(); - std::replace(reply.begin(), reply.end(), '\n', ' '); - string metar = simgear::strutils::strip( reply ); + fetch( request ); + } +} - if( metar.empty() ) { - SG_LOG( SG_GENERAL, SG_WARN, "NoaaMetarRealWxController::fetchMetar(): dropping empty METAR for " - << request._stationId ); - } +void NoaaMetarRealWxController::MetarLoadThread::fetch( const MetarLoadRequest & request ) +{ + SGSharedPtr result = NULL; + + try { + result = new FGMetar( request._stationId, request._proxyHost, request._proxyPort, request._proxyAuth ); + _minRequestInterval = 2000; + } catch (const sg_io_exception& e) { + SG_LOG( SG_GENERAL, SG_WARN, "NoaaMetarRealWxController::fetchMetar(): can't get METAR for " + << request._stationId << ":" << e.getFormattedMessage().c_str() ); + _minRequestInterval += _minRequestInterval/2; + if( _minRequestInterval > 30000 ) + _minRequestInterval = 30000; + return; + } - if( _maxAge && result->getAge_min() > _maxAge ) { - SG_LOG( SG_GENERAL, SG_ALERT, "NoaaMetarRealWxController::fetchMetar(): dropping outdated METAR " - << metar ); - return; - } + string reply = result->getData(); + std::replace(reply.begin(), reply.end(), '\n', ' '); + string metar = simgear::strutils::strip( reply ); - _responseQueue.push( metar ); + if( metar.empty() ) { + SG_LOG( SG_GENERAL, SG_WARN, "NoaaMetarRealWxController::fetchMetar(): dropping empty METAR for " + << request._stationId ); + return; } + + if( _maxAge && result->getAge_min() > _maxAge ) { + SG_LOG( SG_GENERAL, SG_ALERT, "NoaaMetarRealWxController::fetchMetar(): dropping outdated METAR " + << metar ); + return; + } + + MetarLoadResponse response( request._stationId, metar ); + _responseQueue.push( response ); } #endif