source: ntrip/trunk/BNC/bncnetqueryv1.cpp@ 2060

Last change on this file since 2060 was 2011, checked in by mervart, 15 years ago

* empty log message *

File size: 6.5 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
[1850]68 outData = _socket->readAll();
69
[1848]70 delete _socket; _socket = 0;
71
[1850]72 _status = finished;
[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;
[1645]93 emit newMessage(_url.path().toAscii().replace(0,1,"")
94 + ": " + errStr.toAscii(), 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
[1848]107// Connect to Caster, send the Request
108////////////////////////////////////////////////////////////////////////////
109void bncNetQueryV1::startRequestPrivate(const QUrl& url,
110 const QByteArray& gga,
111 bool sendRequestOnly) {
112
[1385]113 _status = running;
114
[1848]115 if (!sendRequestOnly) {
116 delete _socket;
117 _socket = new QTcpSocket();
118 }
[1382]119
[1385]120 // Default scheme and path
121 // -----------------------
[1500]122 _url = url;
123 if (_url.scheme().isEmpty()) {
124 _url.setScheme("http");
[1385]125 }
[1500]126 if (_url.path().isEmpty()) {
127 _url.setPath("/");
[1385]128 }
129
[1382]130 // Connect the Socket
131 // ------------------
[1535]132 bncSettings settings;
[1382]133 QString proxyHost = settings.value("proxyHost").toString();
134 int proxyPort = settings.value("proxyPort").toInt();
135
136 if ( proxyHost.isEmpty() ) {
[1500]137 _socket->connectToHost(_url.host(), _url.port());
[1382]138 }
139 else {
140 _socket->connectToHost(proxyHost, proxyPort);
141 }
[1500]142 if (!_socket->waitForConnected(_timeOut)) {
[1382]143 delete _socket;
144 _socket = 0;
[1385]145 _status = error;
146 return;
[1382]147 }
148
149 // Send Request
150 // ------------
[1500]151 QString uName = QUrl::fromPercentEncoding(_url.userName().toAscii());
152 QString passW = QUrl::fromPercentEncoding(_url.password().toAscii());
[1382]153 QByteArray userAndPwd;
154
[1385]155 if(!uName.isEmpty() || !passW.isEmpty()) {
[1382]156 userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
157 passW.toAscii()).toBase64() + "\r\n";
158 }
159
160 QByteArray reqStr;
161 if ( proxyHost.isEmpty() ) {
[1500]162 if (_url.path().indexOf("/") != 0) _url.setPath("/");
163 reqStr = "GET " + _url.path().toAscii() + " HTTP/1.0\r\n"
[1382]164 + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
165 + userAndPwd + "\r\n";
166 } else {
[1500]167 reqStr = "GET " + _url.toEncoded() + " HTTP/1.0\r\n"
[1382]168 + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
[1500]169 + "Host: " + _url.host().toAscii() + "\r\n"
[1382]170 + userAndPwd + "\r\n";
171 }
172
173 // NMEA string to handle VRS stream
174 // --------------------------------
[1385]175 if (!gga.isEmpty()) {
176 reqStr += gga + "\r\n";
[1382]177 }
178
179 _socket->write(reqStr, reqStr.length());
180
[1500]181 if (!_socket->waitForBytesWritten(_timeOut)) {
[1382]182 delete _socket;
183 _socket = 0;
[1385]184 _status = error;
[1645]185 emit newMessage(_url.path().toAscii().replace(0,1,"")
186 + ": Write timeout", true);
[1498]187 return;
[1382]188 }
[1498]189
190 // Read Caster Response
191 // --------------------
[1848]192 if (!sendRequestOnly) {
193 bool proxyResponse = false;
194 QStringList response;
195 while (true) {
196 if (_socket->canReadLine()) {
197 QString line = _socket->readLine();
198
199 if (line.indexOf("ICY 200 OK") == -1 &&
200 line.indexOf("HTTP") != -1 &&
201 line.indexOf("200 OK") != -1 ) {
202 proxyResponse = true;
203 }
204
205 if (!proxyResponse && !line.trimmed().isEmpty()) {
206 response.push_back(line);
207 }
208
209 if (line.trimmed().isEmpty()) {
210 if (proxyResponse) {
211 proxyResponse = false;
212 }
213 else {
214 break;
215 }
216 }
217
218 if (line.indexOf("Unauthorized") != -1) {
[1593]219 break;
[1848]220 }
221
222 if (!proxyResponse &&
223 line.indexOf("200 OK") != -1 &&
224 line.indexOf("SOURCETABLE") == -1) {
225 response.clear();
226 if (_socket->canReadLine()) {
227 _socket->readLine();
228 }
229 break;
230 }
[1593]231 }
[1848]232 else if (!_socket->waitForReadyRead(_timeOut)) {
233 delete _socket;
234 _socket = 0;
235 _status = error;
236 emit newMessage(_url.path().toAscii().replace(0,1,"")
237 + ": Response timeout", true);
238 return;
[1593]239 }
[1591]240 }
[1848]241 if (response.size() > 0) {
[1593]242 delete _socket;
243 _socket = 0;
244 _status = error;
[1645]245 emit newMessage(_url.path().toAscii().replace(0,1,"")
[1848]246 + ": Wrong caster response\n"
247 + response.join("").toAscii(), true);
[1593]248 }
[1498]249 }
[1382]250}
251
Note: See TracBrowser for help on using the repository browser.