source: ntrip/trunk/BNC/src/bncnetqueryv1.cpp@ 10043

Last change on this file since 10043 was 8203, checked in by stoecker, 7 years ago

see #105 - some changes for Qt5

File size: 7.9 KB
RevLine 
[1382]1/* -------------------------------------------------------------------------
2 * BKG NTRIP Client
3 * -------------------------------------------------------------------------
4 *
[1383]5 * Class: bncNetQueryV1
[1382]6 *
7 * Purpose: Blocking Network Requests (NTRIP Version 1)
8 *
9 * Author: L. Mervart
10 *
11 * Created: 27-Dec-2008
12 *
13 * Changes:
14 *
15 * -----------------------------------------------------------------------*/
16
17#include <iostream>
18#include <iomanip>
19
[1385]20#include "bncnetqueryv1.h"
[1535]21#include "bncsettings.h"
[2011]22#include "bncversion.h"
[1382]23
24using namespace std;
25
26// Constructor
27////////////////////////////////////////////////////////////////////////////
[1383]28bncNetQueryV1::bncNetQueryV1() {
[1848]29 _socket = 0;
30 _eventLoop = new QEventLoop(this);
31 _timeOut = 20000;
[1382]32}
33
34// Destructor
35////////////////////////////////////////////////////////////////////////////
[1383]36bncNetQueryV1::~bncNetQueryV1() {
[1382]37 delete _socket;
[1848]38 delete _eventLoop;
[1382]39}
40
41//
42////////////////////////////////////////////////////////////////////////////
[1390]43void bncNetQueryV1::stop() {
[1848]44 _eventLoop->quit();
[1408]45#ifndef sparc
46 if (_socket) {
47 _socket->abort();
48 }
49#endif
[1403]50 _status = finished;
[1390]51}
52
53//
54////////////////////////////////////////////////////////////////////////////
[1848]55void bncNetQueryV1::waitForRequestResult(const QUrl& url, QByteArray& outData){
56
57 delete _socket;
58 _socket = new QTcpSocket();
59
[1850]60 connect(_socket, SIGNAL(disconnected()), _eventLoop, SLOT(quit()));
[1848]61
62 startRequestPrivate(url, "", true);
63
[1850]64 QTimer::singleShot(10000, _eventLoop, SLOT(quit()));
[1849]65
[1848]66 _eventLoop->exec();
67
[2309]68 if (_socket) {
69 outData = _socket->readAll();
70 delete _socket; _socket = 0;
71 _status = finished;
72 }
[1382]73}
74
75//
76////////////////////////////////////////////////////////////////////////////
[1384]77void bncNetQueryV1::waitForReadyRead(QByteArray& outData) {
[1500]78 if (_socket && _socket->state() == QAbstractSocket::ConnectedState) {
79 while (true) {
80 int nBytes = _socket->bytesAvailable();
81 if (nBytes > 0) {
82 outData = _socket->readAll();
83 return;
84 }
85 else if (!_socket->waitForReadyRead(_timeOut)) {
[1593]86 QString errStr = _socket->errorString();
87 if (errStr.isEmpty()) {
88 errStr = "Read timeout";
89 }
[1500]90 delete _socket;
91 _socket = 0;
92 _status = error;
[8203]93 emit newMessage(_url.path().toLatin1().replace(0,1,"")
94 + ": " + errStr.toLatin1(), true);
[1500]95 return;
96 }
[1402]97 }
98 }
[1382]99}
100
101// Connect to Caster, send the Request
102////////////////////////////////////////////////////////////////////////////
[1384]103void bncNetQueryV1::startRequest(const QUrl& url, const QByteArray& gga) {
[1848]104 startRequestPrivate(url, gga, false);
105}
[1382]106
[6787]107
108// Already connected to Caster, send another Request
109////////////////////////////////////////////////////////////////////////////
110void bncNetQueryV1::keepAliveRequest(const QUrl& url, const QByteArray& gga) {
111
112 _status = running;
113
114 // Default scheme and path
115 // -----------------------
116 _url = url;
117 if (_url.scheme().isEmpty()) {
118 _url.setScheme("http");
119 }
120 if (_url.path().isEmpty()) {
121 _url.setPath("/");
122 }
123
124 // Connect the Socket
125 // ------------------
126 bncSettings settings;
127 QString proxyHost = settings.value("proxyHost").toString();
128 int proxyPort = settings.value("proxyPort").toInt();
129
130 if ( proxyHost.isEmpty() ) {
131 _socket->connectToHost(_url.host(), _url.port());
132 }
133 else {
134 _socket->connectToHost(proxyHost, proxyPort);
135 }
136 if (!_socket->waitForConnected(_timeOut)) {
137 delete _socket;
138 _socket = 0;
139 _status = error;
140 return;
141 }
142
143 // Send Request
144 // ------------
145 QByteArray reqStr;
146
147 // NMEA string to handle VRS stream
148 // --------------------------------
149 if (!gga.isEmpty()) {
150 reqStr += gga + "\r\n";
151 }
152
153 _socket->write(reqStr, reqStr.length());
154
155 if (!_socket->waitForBytesWritten(_timeOut)) {
156 delete _socket;
157 _socket = 0;
158 _status = error;
[8203]159 emit newMessage(_url.path().toLatin1().replace(0,1,"")
[6787]160 + ": Write timeout", true);
161 return;
162 }
163
164}
165
[1848]166// Connect to Caster, send the Request
167////////////////////////////////////////////////////////////////////////////
168void bncNetQueryV1::startRequestPrivate(const QUrl& url,
169 const QByteArray& gga,
170 bool sendRequestOnly) {
171
[1385]172 _status = running;
173
[1848]174 if (!sendRequestOnly) {
175 delete _socket;
176 _socket = new QTcpSocket();
177 }
[1382]178
[1385]179 // Default scheme and path
180 // -----------------------
[1500]181 _url = url;
182 if (_url.scheme().isEmpty()) {
183 _url.setScheme("http");
[1385]184 }
[1500]185 if (_url.path().isEmpty()) {
186 _url.setPath("/");
[1385]187 }
188
[1382]189 // Connect the Socket
190 // ------------------
[1535]191 bncSettings settings;
[1382]192 QString proxyHost = settings.value("proxyHost").toString();
193 int proxyPort = settings.value("proxyPort").toInt();
194
195 if ( proxyHost.isEmpty() ) {
[1500]196 _socket->connectToHost(_url.host(), _url.port());
[1382]197 }
198 else {
199 _socket->connectToHost(proxyHost, proxyPort);
200 }
[1500]201 if (!_socket->waitForConnected(_timeOut)) {
[1382]202 delete _socket;
203 _socket = 0;
[1385]204 _status = error;
205 return;
[1382]206 }
207
208 // Send Request
209 // ------------
[8203]210 QString uName = QUrl::fromPercentEncoding(_url.userName().toLatin1());
211 QString passW = QUrl::fromPercentEncoding(_url.password().toLatin1());
[1382]212 QByteArray userAndPwd;
213
[1385]214 if(!uName.isEmpty() || !passW.isEmpty()) {
[8203]215 userAndPwd = "Authorization: Basic " + (uName.toLatin1() + ":" +
216 passW.toLatin1()).toBase64() + "\r\n";
[1382]217 }
218
219 QByteArray reqStr;
220 if ( proxyHost.isEmpty() ) {
[1500]221 if (_url.path().indexOf("/") != 0) _url.setPath("/");
[8203]222 reqStr = "GET " + _url.path().toLatin1() + " HTTP/1.0\r\n"
[6571]223 + "User-Agent: NTRIP BNC/" BNCVERSION " (" BNC_OS ")\r\n"
[8203]224 + "Host: " + _url.host().toLatin1() + "\r\n"
[1382]225 + userAndPwd + "\r\n";
226 } else {
[1500]227 reqStr = "GET " + _url.toEncoded() + " HTTP/1.0\r\n"
[6571]228 + "User-Agent: NTRIP BNC/" BNCVERSION " (" BNC_OS ")\r\n"
[8203]229 + "Host: " + _url.host().toLatin1() + "\r\n"
[1382]230 + userAndPwd + "\r\n";
231 }
232
233 // NMEA string to handle VRS stream
234 // --------------------------------
[1385]235 if (!gga.isEmpty()) {
236 reqStr += gga + "\r\n";
[1382]237 }
238
239 _socket->write(reqStr, reqStr.length());
240
[1500]241 if (!_socket->waitForBytesWritten(_timeOut)) {
[1382]242 delete _socket;
243 _socket = 0;
[1385]244 _status = error;
[8203]245 emit newMessage(_url.path().toLatin1().replace(0,1,"")
[1645]246 + ": Write timeout", true);
[1498]247 return;
[1382]248 }
[1498]249
250 // Read Caster Response
251 // --------------------
[1848]252 if (!sendRequestOnly) {
253 bool proxyResponse = false;
254 QStringList response;
255 while (true) {
256 if (_socket->canReadLine()) {
257 QString line = _socket->readLine();
258
259 if (line.indexOf("ICY 200 OK") == -1 &&
260 line.indexOf("HTTP") != -1 &&
261 line.indexOf("200 OK") != -1 ) {
262 proxyResponse = true;
263 }
264
265 if (!proxyResponse && !line.trimmed().isEmpty()) {
266 response.push_back(line);
267 }
268
269 if (line.trimmed().isEmpty()) {
270 if (proxyResponse) {
271 proxyResponse = false;
272 }
273 else {
274 break;
275 }
276 }
277
278 if (line.indexOf("Unauthorized") != -1) {
[1593]279 break;
[1848]280 }
281
282 if (!proxyResponse &&
283 line.indexOf("200 OK") != -1 &&
284 line.indexOf("SOURCETABLE") == -1) {
285 response.clear();
286 if (_socket->canReadLine()) {
287 _socket->readLine();
288 }
289 break;
290 }
[1593]291 }
[1848]292 else if (!_socket->waitForReadyRead(_timeOut)) {
293 delete _socket;
294 _socket = 0;
295 _status = error;
[8203]296 emit newMessage(_url.path().toLatin1().replace(0,1,"")
[1848]297 + ": Response timeout", true);
298 return;
[1593]299 }
[1591]300 }
[1848]301 if (response.size() > 0) {
[1593]302 delete _socket;
303 _socket = 0;
304 _status = error;
[8203]305 emit newMessage(_url.path().toLatin1().replace(0,1,"")
[1848]306 + ": Wrong caster response\n"
[8203]307 + response.join("").toLatin1(), true);
[1593]308 }
[1498]309 }
[1382]310}
311
Note: See TracBrowser for help on using the repository browser.