]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/metar_main.cxx
Set "/sim/fghome-readonly" to false also for Windows.
[flightgear.git] / src / Main / metar_main.cxx
index d150b65267097f027dce01302e0db4d10a01f766..d97ed1a6fae0f634bd51235a2463a898c7aa977d 100644 (file)
 #include <iostream>
 #include <string.h>
 #include <time.h>
+#include <cstdlib>
+#include <cstdio>
+
+#include <boost/algorithm/string.hpp>
 
-#include <simgear/debug/logstream.hxx>
 #include <simgear/environment/metar.hxx>
 #include <simgear/structure/exception.hxx>
 
+#include <simgear/io/HTTPClient.hxx>
+#include <simgear/io/HTTPMemoryRequest.hxx>
+#include <simgear/io/raw_socket.hxx>
+#include <simgear/timing/timestamp.hxx>
+
 using namespace std;
+using namespace simgear;
 
 // text color
 #if defined(__linux__) || defined(__sun) || defined(__CYGWIN__) \
@@ -281,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)
@@ -500,6 +509,11 @@ int main(int argc, char *argv[])
        string proxy_host, proxy_port;
        getproxy(proxy_host, proxy_port);
 
+  Socket::initSockets();
+  
+    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();
@@ -523,8 +537,41 @@ int main(int argc, char *argv[])
                                shown = true;
                        }
 
-                       try {
-                               SGMetar *m = new SGMetar(argv[i], proxy_host, proxy_port, "", time(0));
+                       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);
+                
+            // spin until the request completes, fails or times out
+                SGTimeStamp start(SGTimeStamp::now());
+                while (start.elapsedMSec() <  8000) {
+                    http.update();
+                    if( mr->isComplete() )
+                        break;
+                    SGTimeStamp::sleepForMSec(1);
+                }
+                
+                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->responseBody());
+                               
                                //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) {