]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/metar_main.cxx
Crash-fix: mat-lib is now reference-counted.
[flightgear.git] / src / Main / metar_main.cxx
index e63e6763f95c4d36fe268e6987626a58feb8b727..d97ed1a6fae0f634bd51235a2463a898c7aa977d 100644 (file)
 #include <cstdlib>
 #include <cstdio>
 
-#include <simgear/debug/logstream.hxx>
+#include <boost/algorithm/string.hpp>
+
 #include <simgear/environment/metar.hxx>
 #include <simgear/structure/exception.hxx>
 
 #include <simgear/io/HTTPClient.hxx>
-#include <simgear/io/HTTPRequest.hxx>
+#include <simgear/io/HTTPMemoryRequest.hxx>
 #include <simgear/io/raw_socket.hxx>
 #include <simgear/timing/timestamp.hxx>
 
 using namespace std;
 using namespace simgear;
 
-class MetarRequest : public HTTP::Request
-{
-public:
-    bool complete;
-    bool failed;
-    string metarData;
-    bool fromProxy;
-    
-    MetarRequest(const std::string& stationId) : 
-        HTTP::Request("http://weather.noaa.gov/pub/data/observations/metar/stations/" + stationId + ".TXT"),
-        complete(false),
-        failed(false)
-    {
-        fromProxy = false;
-    }
-    
-protected:
-    
-    virtual void responseHeader(const string& key, const string& value)
-    {
-        if (key == "x-metarproxy") {
-            fromProxy = true;
-        }
-    }
-
-    virtual void gotBodyData(const char* s, int n)
-    {
-        metarData += string(s, n);
-    }
-
-    virtual void responseComplete()
-    {
-        if (responseCode() == 200) {
-            complete = true;
-        } else {
-            SG_LOG(SG_ENVIRONMENT, SG_WARN, "metar download failed:" << url() << ": reason:" << responseReason());
-            failed = true;
-        }
-    }
-};
-
 // text color
 #if defined(__linux__) || defined(__sun) || defined(__CYGWIN__) \
     || defined( __FreeBSD__ ) || defined ( sgi )
@@ -330,7 +290,7 @@ void printReport(SGMetar *m)
                        surface.push_back(buf);
                }
 
-               if (surface.size()) {
+               if (! surface.empty()) {
                        vector<string>::iterator rwysurf = surface.begin();
                        for (i = 0; rwysurf != surface.end(); rwysurf++, i++) {
                                if (i)
@@ -577,8 +537,15 @@ int main(int argc, char *argv[])
                                shown = true;
                        }
 
-                       try {
-                MetarRequest* mr = new MetarRequest(argv[i]);
+                       try
+                       {
+              static const std::string NOAA_BASE_URL =
+                "http://weather.noaa.gov/pub/data/observations/metar/stations/";
+                HTTP::MemoryRequest* mr = new HTTP::MemoryRequest
+                (
+                    NOAA_BASE_URL
+                  + boost::to_upper_copy<std::string>(argv[i]) + ".TXT"
+                );
                 HTTP::Request_ptr own(mr);
                 http.makeRequest(mr);
                 
@@ -586,16 +553,24 @@ int main(int argc, char *argv[])
                 SGTimeStamp start(SGTimeStamp::now());
                 while (start.elapsedMSec() <  8000) {
                     http.update();
-                    if (mr->complete || mr->failed) {
+                    if( mr->isComplete() )
                         break;
-                    }
                     SGTimeStamp::sleepForMSec(1);
                 }
                 
-                if (!mr->complete) {
-                    throw sg_io_exception("metar download failed (or timed out)");
+                if( !mr->isComplete() )
+                  throw sg_io_exception("metar download timed out");
+                if( mr->responseCode() != 200 )
+                {
+                  std::cerr << "metar download failed: "
+                            << mr->url()
+                            << " (" << mr->responseCode()
+                            << " " << mr->responseReason() << ")"
+                            << std::endl;
+                  throw sg_io_exception("metar download failed");
                 }
-                               SGMetar *m = new SGMetar(mr->metarData);
+
+                               SGMetar *m = new SGMetar(mr->responseBody());
                                
                                //SGMetar *m = new SGMetar("2004/01/11 01:20\nLOWG 110120Z AUTO VRB01KT 0050 1600N R35/0600 FG M06/M06 Q1019 88//////\n");