1 | /* -------------------------------------------------------------------------
|
---|
2 | * BKG NTRIP Client
|
---|
3 | * -------------------------------------------------------------------------
|
---|
4 | *
|
---|
5 | * Class: bncNetQueryV2
|
---|
6 | *
|
---|
7 | * Purpose: Blocking Network Requests (NTRIP Version 2)
|
---|
8 | *
|
---|
9 | * Author: L. Mervart
|
---|
10 | *
|
---|
11 | * Created: 27-Dec-2008
|
---|
12 | *
|
---|
13 | * Changes:
|
---|
14 | *
|
---|
15 | * -----------------------------------------------------------------------*/
|
---|
16 |
|
---|
17 | #include <iostream>
|
---|
18 |
|
---|
19 | #include "bncnetqueryv2.h"
|
---|
20 | #include "bncsettings.h"
|
---|
21 | #include "bncversion.h"
|
---|
22 |
|
---|
23 | // Constructor
|
---|
24 | ////////////////////////////////////////////////////////////////////////////
|
---|
25 | bncNetQueryV2::bncNetQueryV2() {
|
---|
26 | _manager = new QNetworkAccessManager(this);
|
---|
27 | connect(_manager, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&,
|
---|
28 | QAuthenticator*)),
|
---|
29 | this, SLOT(slotProxyAuthenticationRequired(const QNetworkProxy&,
|
---|
30 | QAuthenticator*)));
|
---|
31 | _reply = 0;
|
---|
32 | _eventLoop = new QEventLoop(this);
|
---|
33 | _firstData = true;
|
---|
34 | _status = init;
|
---|
35 | }
|
---|
36 |
|
---|
37 | // Destructor
|
---|
38 | ////////////////////////////////////////////////////////////////////////////
|
---|
39 | bncNetQueryV2::~bncNetQueryV2() {
|
---|
40 | delete _eventLoop;
|
---|
41 | delete _reply;
|
---|
42 | delete _manager;
|
---|
43 | }
|
---|
44 |
|
---|
45 | // Stop (quit event loop)
|
---|
46 | ////////////////////////////////////////////////////////////////////////////
|
---|
47 | void bncNetQueryV2::stop() {
|
---|
48 | if (_reply) {
|
---|
49 | _reply->abort();
|
---|
50 | }
|
---|
51 | _eventLoop->quit();
|
---|
52 | _status = finished;
|
---|
53 | }
|
---|
54 |
|
---|
55 | // End of Request
|
---|
56 | ////////////////////////////////////////////////////////////////////////////
|
---|
57 | void bncNetQueryV2::slotFinished() {
|
---|
58 | _eventLoop->quit();
|
---|
59 | if (_reply && _reply->error() != QNetworkReply::NoError) {
|
---|
60 | _status = error;
|
---|
61 | emit newMessage(_url.path().toAscii().replace(0,1,"") +
|
---|
62 | ": NetQueryV2: server replied: " +
|
---|
63 | _reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray(),
|
---|
64 | true);
|
---|
65 | }
|
---|
66 | else {
|
---|
67 | _status = finished;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | //
|
---|
72 | ////////////////////////////////////////////////////////////////////////////
|
---|
73 | void bncNetQueryV2::slotProxyAuthenticationRequired(const QNetworkProxy&,
|
---|
74 | QAuthenticator*) {
|
---|
75 | emit newMessage("slotProxyAuthenticationRequired", true);
|
---|
76 | }
|
---|
77 |
|
---|
78 | // Start request, block till the next read
|
---|
79 | ////////////////////////////////////////////////////////////////////////////
|
---|
80 | void bncNetQueryV2::startRequest(const QUrl& url, const QByteArray& gga) {
|
---|
81 | startRequestPrivate(url, gga, false);
|
---|
82 | }
|
---|
83 |
|
---|
84 | // Start Request (Private Method)
|
---|
85 | ////////////////////////////////////////////////////////////////////////////
|
---|
86 | void bncNetQueryV2::startRequestPrivate(const QUrl& url, const QByteArray& gga,
|
---|
87 | bool full) {
|
---|
88 |
|
---|
89 | _status = running;
|
---|
90 |
|
---|
91 | // Default scheme and path
|
---|
92 | // -----------------------
|
---|
93 | _url = url;
|
---|
94 | if (_url.scheme().isEmpty()) {
|
---|
95 | _url.setScheme("http");
|
---|
96 | }
|
---|
97 | if (_url.path().isEmpty()) {
|
---|
98 | _url.setPath("/");
|
---|
99 | }
|
---|
100 |
|
---|
101 | // Proxy Settings
|
---|
102 | // --------------
|
---|
103 | bncSettings settings;
|
---|
104 | QString proxyHost = settings.value("proxyHost").toString();
|
---|
105 | int proxyPort = settings.value("proxyPort").toInt();
|
---|
106 |
|
---|
107 | if (!proxyHost.isEmpty()) {
|
---|
108 | QNetworkProxy proxy(QNetworkProxy::HttpProxy, proxyHost, proxyPort);
|
---|
109 | _manager->setProxy(proxy);
|
---|
110 | }
|
---|
111 |
|
---|
112 | // Network Request
|
---|
113 | // ---------------
|
---|
114 | QNetworkRequest request;
|
---|
115 | request.setUrl(_url);
|
---|
116 | request.setRawHeader("Host" , _url.host().toAscii());
|
---|
117 | request.setRawHeader("Ntrip-Version", "Ntrip/2.0");
|
---|
118 | request.setRawHeader("User-Agent" , "NTRIP BNC/"BNCVERSION);
|
---|
119 | if (!_url.userName().isEmpty()) {
|
---|
120 | QString uName = QUrl::fromPercentEncoding(_url.userName().toAscii());
|
---|
121 | QString passW = QUrl::fromPercentEncoding(_url.password().toAscii());
|
---|
122 | request.setRawHeader("Authorization", "Basic " +
|
---|
123 | (uName + ":" + passW).toAscii().toBase64());
|
---|
124 | }
|
---|
125 | if (!gga.isEmpty()) {
|
---|
126 | request.setRawHeader("Ntrip-GGA", gga);
|
---|
127 | }
|
---|
128 | request.setRawHeader("Connection" , "close");
|
---|
129 |
|
---|
130 | delete _reply;
|
---|
131 | _reply = _manager->get(request);
|
---|
132 |
|
---|
133 | // Connect Signals
|
---|
134 | // ---------------
|
---|
135 | connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
|
---|
136 | connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(quit()));
|
---|
137 | if (!full) {
|
---|
138 | connect(_reply, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | // Start Request, wait for its completion
|
---|
143 | ////////////////////////////////////////////////////////////////////////////
|
---|
144 | void bncNetQueryV2::waitForRequestResult(const QUrl& url, QByteArray& outData) {
|
---|
145 |
|
---|
146 | // Send Request
|
---|
147 | // ------------
|
---|
148 | startRequestPrivate(url, "", true);
|
---|
149 |
|
---|
150 | // Wait Loop
|
---|
151 | // ---------
|
---|
152 | _eventLoop->exec();
|
---|
153 |
|
---|
154 | // Copy Data and Return
|
---|
155 | // --------------------
|
---|
156 | outData = _reply->readAll();
|
---|
157 | }
|
---|
158 |
|
---|
159 | // Wait for next data
|
---|
160 | ////////////////////////////////////////////////////////////////////////////
|
---|
161 | void bncNetQueryV2::waitForReadyRead(QByteArray& outData) {
|
---|
162 |
|
---|
163 | // Wait Loop
|
---|
164 | // ---------
|
---|
165 | if (!_reply->bytesAvailable()) {
|
---|
166 | _eventLoop->exec();
|
---|
167 | }
|
---|
168 |
|
---|
169 | // Check NTRIPv2 error code
|
---|
170 | // ------------------------
|
---|
171 | if (_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
|
---|
172 | _reply->abort();
|
---|
173 | }
|
---|
174 |
|
---|
175 | // Append Data
|
---|
176 | // -----------
|
---|
177 | else {
|
---|
178 | outData.append(_reply->readAll());
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|