]> git.mxchange.org Git - simgear.git/blob - simgear/io/http_repo_sync.cxx
HTTP repository testing tool.
[simgear.git] / simgear / io / http_repo_sync.cxx
1
2 #include <cstdio>
3 #include <cstring>
4 #include <signal.h>
5
6 #include <iostream>
7 #include <boost/foreach.hpp>
8
9
10 #include <simgear/io/sg_file.hxx>
11 #include <simgear/io/HTTPClient.hxx>
12 #include <simgear/io/HTTPRepository.hxx>
13 #include <simgear/misc/strutils.hxx>
14 #include <simgear/timing/timestamp.hxx>
15 #include <simgear/misc/sg_dir.hxx>
16 #include <simgear/debug/logstream.hxx>
17
18 using namespace simgear;
19 using std::cout;
20 using std::endl;
21 using std::cerr;
22 using std::string;
23
24 int main(int argc, char* argv[])
25 {
26     HTTP::Client cl;
27     string proxy, proxyAuth;
28     string_list headers;
29     string url;
30
31     sglog().setLogLevels( SG_ALL, SG_INFO );
32
33     for (int a=0; a<argc;++a) {
34         if (argv[a][0] == '-') {
35             if (!strcmp(argv[a], "--proxy")) {
36                 proxy = argv[++a];
37             } else if (!strcmp(argv[a], "--auth")) {
38                 proxyAuth = argv[++a];
39             }
40         } else { // of argument starts with a hyphen
41             url = argv[a];
42         }
43     } // of arguments iteration
44
45     if (!proxy.empty()) {
46         int colonPos = proxy.find(':');
47         string proxyHost = proxy;
48         int proxyPort = 8800;
49         if (colonPos >= 0) {
50             proxyHost = proxy.substr(0, colonPos);
51             proxyPort = strutils::to_int(proxy.substr(colonPos + 1));
52         }
53
54         cl.setProxy(proxyHost, proxyPort, proxyAuth);
55     }
56
57 #ifndef WIN32
58     signal(SIGPIPE, SIG_IGN);
59 #endif
60
61     if (url.empty()) {
62         cerr << "no URL argument specificed" << endl;
63         return EXIT_FAILURE;
64     }
65
66     SGPath rootPath = simgear::Dir::current().path();
67     HTTPRepository* repo = new HTTPRepository(rootPath, &cl);
68     repo->setBaseUrl(url);
69     repo->update();
70
71     while (repo->isDoingSync()) {
72         cl.update();
73         SGTimeStamp::sleepForMSec(100);
74     }
75
76     if (repo->failure() != AbstractRepository::REPO_NO_ERROR) {
77         cerr << "got response:" << repo->failure() << endl;
78         return EXIT_FAILURE;
79     }
80
81     return EXIT_SUCCESS;
82 }