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

Last change on this file since 3850 was 2309, 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
[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;
[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"
[2288]165 + "Host: " + _url.host().toAscii() + "\r\n"
[1382]166 + userAndPwd + "\r\n";
167 } else {
[1500]168 reqStr = "GET " + _url.toEncoded() + " HTTP/1.0\r\n"
[1382]169 + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
[1500]170 + "Host: " + _url.host().toAscii() + "\r\n"
[1382]171 + userAndPwd + "\r\n";
172 }
173
174 // NMEA string to handle VRS stream
175 // --------------------------------
[1385]176 if (!gga.isEmpty()) {
177 reqStr += gga + "\r\n";
[1382]178 }
179
180 _socket->write(reqStr, reqStr.length());
181
[1500]182 if (!_socket->waitForBytesWritten(_timeOut)) {
[1382]183 delete _socket;
184 _socket = 0;
[1385]185 _status = error;
[1645]186 emit newMessage(_url.path().toAscii().replace(0,1,"")
187 + ": Write timeout", true);
[1498]188 return;
[1382]189 }
[1498]190
191 // Read Caster Response
192 // --------------------
[1848]193 if (!sendRequestOnly) {
194 bool proxyResponse = false;
195 QStringList response;
196 while (true) {
197 if (_socket->canReadLine()) {
198 QString line = _socket->readLine();
199
200 if (line.indexOf("ICY 200 OK") == -1 &&
201 line.indexOf("HTTP") != -1 &&
202 line.indexOf("200 OK") != -1 ) {
203 proxyResponse = true;
204 }
205
206 if (!proxyResponse && !line.trimmed().isEmpty()) {
207 response.push_back(line);
208 }
209
210 if (line.trimmed().isEmpty()) {
211 if (proxyResponse) {
212 proxyResponse = false;
213 }
214 else {
215 break;
216 }
217 }
218
219 if (line.indexOf("Unauthorized") != -1) {
[1593]220 break;
[1848]221 }
222
223 if (!proxyResponse &&
224 line.indexOf("200 OK") != -1 &&
225 line.indexOf("SOURCETABLE") == -1) {
226 response.clear();
227 if (_socket->canReadLine()) {
228 _socket->readLine();
229 }
230 break;
231 }
[1593]232 }
[1848]233 else if (!_socket->waitForReadyRead(_timeOut)) {
234 delete _socket;
235 _socket = 0;
236 _status = error;
237 emit newMessage(_url.path().toAscii().replace(0,1,"")
238 + ": Response timeout", true);
239 return;
[1593]240 }
[1591]241 }
[1848]242 if (response.size() > 0) {
[1593]243 delete _socket;
244 _socket = 0;
245 _status = error;
[1645]246 emit newMessage(_url.path().toAscii().replace(0,1,"")
[1848]247 + ": Wrong caster response\n"
248 + response.join("").toAscii(), true);
[1593]249 }
[1498]250 }
[1382]251}
252
Note: See TracBrowser for help on using the repository browser.