X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FMain%2Fmetar_main.cxx;h=40883409ed609a5a29396f44d8dfd90fa61e20a1;hb=52e5a9abe83882ca946429f7d8e71894d5852d8f;hp=0f79ec83cbc6c562d67a1f510e8325555eaac10b;hpb=ded8b8f34e871cd9f35453d0f0ae7c7cbbabf85d;p=flightgear.git diff --git a/src/Main/metar_main.cxx b/src/Main/metar_main.cxx index 0f79ec83c..40883409e 100644 --- a/src/Main/metar_main.cxx +++ b/src/Main/metar_main.cxx @@ -16,22 +16,75 @@ // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software -// Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // // $Id$ #include #include +#include #include +#include +#include +#include + +#include #include #include #include +#include +#include +#include +#include + 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/" + boost::to_upper_copy(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__) \ +#if defined(__linux__) || defined(__sun) || defined(__CYGWIN__) \ || defined( __FreeBSD__ ) || defined ( sgi ) # define R "\033[31;1m" // red # define G "\033[32;1m" // green @@ -75,11 +128,11 @@ const char *azimuthName(double d) double rnd(double r, int g = 0) { double f = pow(10.0, g); - return f * rint(r / f); + return f * floor(r / f + 0.5); } -ostream& operator<<(ostream& s, SGMetarVisibility& v) +ostream& operator<<(ostream& s, const SGMetarVisibility& v) { ostringstream buf; int m = v.getModifier(); @@ -154,7 +207,7 @@ void printReport(SGMetar *m) // directed visibility - SGMetarVisibility *dirvis = m->getDirVisibility(); + const SGMetarVisibility *dirvis = m->getDirVisibility(); for (i = 0; i < 8; i++, dirvis++) if (dirvis->getVisibility_m() != NaN) cout << "\t\t\t" << *dirvis << endl; @@ -269,7 +322,7 @@ void printReport(SGMetar *m) if ((s = rwy.getExtentString()) && strlen(s)) surface.push_back(s); if ((d = rwy.getDepth()) != NaN) { - sprintf(buf, "%.0lf mm", d * 1000.0); + sprintf(buf, "%.1lf mm", d * 1000.0); surface.push_back(buf); } if ((s = rwy.getFrictionString()) && strlen(s)) @@ -279,7 +332,7 @@ void printReport(SGMetar *m) surface.push_back(buf); } - if (surface.size()) { + if (! surface.empty()) { vector::iterator rwysurf = surface.begin(); for (i = 0; rwysurf != surface.end(); rwysurf++, i++) { if (i) @@ -484,7 +537,6 @@ void usage() ); } - int main(int argc, char *argv[]) { bool report = true; @@ -499,6 +551,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,7 +580,25 @@ 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::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->complete || mr->failed) { + break; + } + SGTimeStamp::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) {