]> git.mxchange.org Git - flightgear.git/blobdiff - src/Environment/environment_ctrl.cxx
Don't restore initial screen geometry because there is nothing in fg_os* to resize...
[flightgear.git] / src / Environment / environment_ctrl.cxx
index 5625cd666c94eef69bdb97ad36929f171cacea1c..b6e8db5864fc0b01564f1b2d4eedb255ae334f0e 100644 (file)
 //
 // You should have received a copy of the GNU General Public License
 // along with this program; if not, write to the Free Software
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 // $Id$
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
 #include <simgear/debug/logstream.hxx>
 
 #include <stdlib.h>
@@ -326,10 +330,12 @@ FGMetarEnvironmentCtrl::FGMetarEnvironmentCtrl ()
       proxy_auth( fgGetNode("/sim/presets/proxy/authentication", true) ),
       metar_max_age( fgGetNode("/environment/params/metar-max-age-min", true) ),
       _error_count( 0 ),
+      _stale_count( 0 ),
       _dt( 0.0 ),
-      _error_dt( 0.0 )
+      _error_dt( 0.0 ),
+      last_apt(0)
 {
-#if defined(ENABLE_THREADS) && ENABLE_THREADS
+#if defined(ENABLE_THREADS)
     thread = new MetarThread(this);
     thread->start( 1 );
 #endif // ENABLE_THREADS
@@ -337,9 +343,8 @@ FGMetarEnvironmentCtrl::FGMetarEnvironmentCtrl ()
 
 FGMetarEnvironmentCtrl::~FGMetarEnvironmentCtrl ()
 {
-#if defined(ENABLE_THREADS) && ENABLE_THREADS
-   thread->cancel();
-   thread->join();
+#if defined(ENABLE_THREADS)
+   thread_stop();
 #endif // ENABLE_THREADS
 
    delete env;
@@ -399,7 +404,7 @@ FGMetarEnvironmentCtrl::init ()
     // Don't check max age during init so that we don't loop over a lot
     // of airports metar if there is a problem.
     // The update() calls will find a correct metar if things went wrong here
-    metar_max_age->setLongValue(60 * 24 * 7);
+    metar_max_age->setLongValue(0);
 
     while ( !found_metar && (_error_count < 3) ) {
         const FGAirport* a = globals->get_airports()
@@ -411,7 +416,7 @@ FGMetarEnvironmentCtrl::init ()
             if ( result.m != NULL ) {
                 SG_LOG( SG_GENERAL, SG_INFO, "closest station w/ metar = "
                         << a->getId());
-                last_apt = *a;
+                last_apt = a;
                 _icao = a->getId();
                 search_elapsed = 0.0;
                 fetch_elapsed = 0.0;
@@ -469,13 +474,13 @@ FGMetarEnvironmentCtrl::update(double delta_time_sec)
                              latitude->getDoubleValue(),
                              true );
         if ( a ) {
-            if ( last_apt.getId() != a->getId()
+            if ( !last_apt || last_apt->getId() != a->getId()
                  || fetch_elapsed > same_station_interval_sec )
             {
                 SG_LOG( SG_GENERAL, SG_INFO, "closest station w/ metar = "
                         << a->getId());
                 request_queue.push( a->getId() );
-                last_apt = *a;
+                last_apt = a;
                 _icao = a->getId();
                 search_elapsed = 0.0;
                 fetch_elapsed = 0.0;
@@ -490,7 +495,7 @@ FGMetarEnvironmentCtrl::update(double delta_time_sec)
         }
     }
 
-#if defined(ENABLE_THREADS) && ENABLE_THREADS
+#if !defined(ENABLE_THREADS)
     // No loader thread running so manually fetch the data
     string id = "";
     while ( !request_queue.empty() ) {
@@ -564,20 +569,27 @@ FGMetarEnvironmentCtrl::fetch_data( const string &icao )
         result.m = new FGMetar( icao, host, port, auth);
 
         long max_age = metar_max_age->getLongValue();
-        if (max_age && result.m->getAge_min() > max_age) {
-            SG_LOG( SG_GENERAL, SG_WARN, "METAR data too old");
+        long age = result.m->getAge_min();
+        if (max_age &&  age > max_age) {
+            SG_LOG( SG_GENERAL, SG_WARN, "METAR data too old (" << age << " min).");
             delete result.m;
             result.m = NULL;
-        }
+
+            if (++_stale_count > 10) {
+                _error_count = 1000;
+                throw sg_io_exception("More than 10 stale METAR messages in a row."
+                        " Check your system time!");
+            }
+        } else
+            _stale_count = 0;
 
     } catch (const sg_io_exception& e) {
         SG_LOG( SG_GENERAL, SG_WARN, "Error fetching live weather data: "
                 << e.getFormattedMessage().c_str() );
-#if defined(ENABLE_THREADS) && ENABLE_THREADS
+#if defined(ENABLE_THREADS)
         if (_error_count++ >= 3) {
            SG_LOG( SG_GENERAL, SG_WARN, "Stop fetching data permanently.");
-           thread->cancel();
-           thread->join();
+           thread_stop();
         }
 #endif
 
@@ -692,37 +704,26 @@ FGMetarEnvironmentCtrl::update_metar_properties( const FGMetar *m )
 }
 
 
-#if defined(ENABLE_THREADS) && ENABLE_THREADS
-/**
- *
- */
+#if defined(ENABLE_THREADS)
+void
+FGMetarEnvironmentCtrl::thread_stop()
+{
+    request_queue.push( string() );    // ask thread to terminate
+    thread->join();
+}
+
 void
 FGMetarEnvironmentCtrl::MetarThread::run()
 {
-    pthread_cleanup_push( metar_cleanup_handler, fetcher );
     while ( true )
     {
-        set_cancel( SGThread::CANCEL_DISABLE );
-
         string icao = fetcher->request_queue.pop();
+        if (icao.empty())
+            return;
         SG_LOG( SG_GENERAL, SG_INFO, "Thread: fetch metar data = " << icao );
         FGMetarResult result = fetcher->fetch_data( icao );
-
-        set_cancel( SGThread::CANCEL_DEFERRED );
-
         fetcher->result_queue.push( result );
     }
-    pthread_cleanup_pop(1);
-}
-
-/**
- * Ensure mutex is unlocked.
- */
-void
-metar_cleanup_handler( void* arg )
-{
-    FGMetarEnvironmentCtrl* fetcher = (FGMetarEnvironmentCtrl*) arg;
-    fetcher->mutex.unlock();
 }
 #endif // ENABLE_THREADS