26
26
#include <string.h>
30
#include <simgear/debug/logstream.hxx>
31
#include <boost/algorithm/string.hpp>
31
33
#include <simgear/environment/metar.hxx>
32
34
#include <simgear/structure/exception.hxx>
34
36
#include <simgear/io/HTTPClient.hxx>
35
#include <simgear/io/HTTPRequest.hxx>
37
#include <simgear/io/HTTPMemoryRequest.hxx>
36
38
#include <simgear/io/raw_socket.hxx>
37
39
#include <simgear/timing/timestamp.hxx>
39
41
using namespace std;
40
42
using namespace simgear;
42
class MetarRequest : public HTTP::Request
50
MetarRequest(const std::string& stationId) :
51
HTTP::Request("http://weather.noaa.gov/pub/data/observations/metar/stations/" + stationId + ".TXT"),
60
virtual void responseHeader(const string& key, const string& value)
62
if (key == "x-metarproxy") {
67
virtual void gotBodyData(const char* s, int n)
69
metarData += string(s, n);
72
virtual void responseComplete()
74
if (responseCode() == 200) {
77
SG_LOG(SG_ENVIRONMENT, SG_WARN, "metar download failed:" << url() << ": reason:" << responseReason());
84
45
#if defined(__linux__) || defined(__sun) || defined(__CYGWIN__) \
85
46
|| defined( __FreeBSD__ ) || defined ( sgi )
580
MetarRequest* mr = new MetarRequest(argv[i]);
542
static const std::string NOAA_BASE_URL =
543
"http://weather.noaa.gov/pub/data/observations/metar/stations/";
544
HTTP::MemoryRequest* mr = new HTTP::MemoryRequest
547
+ boost::to_upper_copy<std::string>(argv[i]) + ".TXT"
581
549
HTTP::Request_ptr own(mr);
582
550
http.makeRequest(mr);
585
553
SGTimeStamp start(SGTimeStamp::now());
586
554
while (start.elapsedMSec() < 8000) {
588
if (mr->complete || mr->failed) {
556
if( mr->isComplete() )
591
558
SGTimeStamp::sleepForMSec(1);
595
throw sg_io_exception("metar download failed (or timed out)");
561
if( !mr->isComplete() )
562
throw sg_io_exception("metar download timed out");
563
if( mr->responseCode() != 200 )
565
std::cerr << "metar download failed: "
567
<< " (" << mr->responseCode()
568
<< " " << mr->responseReason() << ")"
570
throw sg_io_exception("metar download failed");
597
SGMetar *m = new SGMetar(mr->metarData);
573
SGMetar *m = new SGMetar(mr->responseBody());
599
575
//SGMetar *m = new SGMetar("2004/01/11 01:20\nLOWG 110120Z AUTO VRB01KT 0050 1600N R35/0600 FG M06/M06 Q1019 88//////\n");