]> git.mxchange.org Git - flightgear.git/commitdiff
Bring metar helper into CMake world, and use new HTTP code.
authorJames Turner <zakalawe@mac.com>
Fri, 21 Oct 2011 11:48:42 +0000 (12:48 +0100)
committerJames Turner <zakalawe@mac.com>
Fri, 21 Oct 2011 11:48:42 +0000 (12:48 +0100)
src/Main/CMakeLists.txt
src/Main/metar_main.cxx

index 8398ff65008693cb33d278c0712814a3dd5cce11..565ec65b1754af7a3b28c05b7a8a9e7b667989f0 100644 (file)
@@ -84,3 +84,13 @@ target_link_libraries(fgfs
 )
 
 install(TARGETS fgfs RUNTIME DESTINATION bin)
+
+add_executable(metar metar_main.cxx)
+target_link_libraries(metar
+       ${SIMGEAR_LIBRARIES}
+       ${SIMGEAR_CORE_LIBRARY_DEPENDENCIES}
+       ${PLATFORM_LIBS}
+)
+
+install(TARGETS metar RUNTIME DESTINATION bin)
+
index 04edbcab15b5cfa4c060c929f5c4a4fe8eeaee28..a80c1da2c0200be9a79427274288bbbbe13b880f 100644 (file)
 #include <simgear/environment/metar.hxx>
 #include <simgear/structure/exception.hxx>
 
+#include <simgear/io/HTTPClient.hxx>
+#include <simgear/io/HTTPRequest.hxx>
+#include <simgear/timing/timestamp.hxx>
+#include <simgear/misc/sg_sleep.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__) \
@@ -501,6 +548,9 @@ int main(int argc, char *argv[])
        string proxy_host, proxy_port;
        getproxy(proxy_host, proxy_port);
 
+    HTTP::Client http;
+    http.setProxy(proxy_host, atoi(proxy_port.c_str()));
+    
        for (int i = 1; i < argc; i++) {
                if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help"))
                        usage();
@@ -525,7 +575,24 @@ int main(int argc, char *argv[])
                        }
 
                        try {
-                               SGMetar *m = new SGMetar(argv[i], proxy_host, proxy_port, "", time(0));
+                MetarRequest* mr = new MetarRequest(argv[i]);
+                http.makeRequest(mr);
+                
+            // spin until the request completes, fails or times out
+                SGTimeStamp start(SGTimeStamp::now());
+                while (start.elapsedMSec() <  8000) {
+                    http.update();
+                    if (mr->complete || mr->failed) {
+                        break;
+                    }
+                    sleepForMSec(1);
+                }
+                
+                if (!mr->complete) {
+                    throw sg_io_exception("metar download failed (or timed out)");
+                }
+                               SGMetar *m = new SGMetar(mr->metarData);
+                               
                                //SGMetar *m = new SGMetar("2004/01/11 01:20\nLOWG 110120Z AUTO VRB01KT 0050 1600N R35/0600 FG M06/M06 Q1019 88//////\n");
 
                                if (verbose) {