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

Last change on this file since 1983 was 1850, 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"
[1382]22
23using namespace std;
24
25#define BNCVERSION "1.7"
26
27// Constructor
28////////////////////////////////////////////////////////////////////////////
[1383]29bncNetQueryV1::bncNetQueryV1() {
[1848]30 _socket = 0;
31 _eventLoop = new QEventLoop(this);
32 _timeOut = 20000;
[1382]33}
34
35// Destructor
36////////////////////////////////////////////////////////////////////////////
[1383]37bncNetQueryV1::~bncNetQueryV1() {
[1382]38 delete _socket;
[1848]39 delete _eventLoop;
[1382]40}
41
42//
43////////////////////////////////////////////////////////////////////////////
[1390]44void bncNetQueryV1::stop() {
[1848]45 _eventLoop->quit();
[1408]46#ifndef sparc
47 if (_socket) {
48 _socket->abort();
49 }
50#endif
[1403]51 _status = finished;
[1390]52}
53
54//
55////////////////////////////////////////////////////////////////////////////
[1848]56void bncNetQueryV1::waitForRequestResult(const QUrl& url, QByteArray& outData){
57
58 delete _socket;
59 _socket = new QTcpSocket();
60
[1850]61 connect(_socket, SIGNAL(disconnected()), _eventLoop, SLOT(quit()));
[1848]62
63 startRequestPrivate(url, "", true);
64
[1850]65 QTimer::singleShot(10000, _eventLoop, SLOT(quit()));
[1849]66
[1848]67 _eventLoop->exec();
68
[1850]69 outData = _socket->readAll();
70
[1848]71 delete _socket; _socket = 0;
72
[1850]73 _status = finished;
[1382]74}
75
76//
77////////////////////////////////////////////////////////////////////////////
[1384]78void bncNetQueryV1::waitForReadyRead(QByteArray& outData) {
[1500]79 if (_socket && _socket->state() == QAbstractSocket::ConnectedState) {
80 while (true) {
81 int nBytes = _socket->bytesAvailable();
82 if (nBytes > 0) {
83 outData = _socket->readAll();
84 return;
85 }
86 else if (!_socket->waitForReadyRead(_timeOut)) {
[1593]87 QString errStr = _socket->errorString();
88 if (errStr.isEmpty()) {
89 errStr = "Read timeout";
90 }
[1500]91 delete _socket;
92 _socket = 0;
93 _status = error;
[1645]94 emit newMessage(_url.path().toAscii().replace(0,1,"")
95 + ": " + errStr.toAscii(), true);
[1500]96 return;
97 }
[1402]98 }
99 }
[1382]100}
101
102// Connect to Caster, send the Request
103////////////////////////////////////////////////////////////////////////////
[1384]104void bncNetQueryV1::startRequest(const QUrl& url, const QByteArray& gga) {
[1848]105 startRequestPrivate(url, gga, false);
106}
[1382]107
[1848]108// Connect to Caster, send the Request
109////////////////////////////////////////////////////////////////////////////
110void bncNetQueryV1::startRequestPrivate(const QUrl& url,
111 const QByteArray& gga,
112 bool sendRequestOnly) {
113
[1385]114 _status = running;
115
[1848]116 if (!sendRequestOnly) {
117 delete _socket;
118 _socket = new QTcpSocket();
119 }
[1382]120
[1385]121 // Default scheme and path
122 // -----------------------
[1500]123 _url = url;
124 if (_url.scheme().isEmpty()) {
125 _url.setScheme("http");
[1385]126 }
[1500]127 if (_url.path().isEmpty()) {
128 _url.setPath("/");
[1385]129 }
130
[1382]131 // Connect the Socket
132 // ------------------
[1535]133 bncSettings settings;
[1382]134 QString proxyHost = settings.value("proxyHost").toString();
135 int proxyPort = settings.value("proxyPort").toInt();
136
137 if ( proxyHost.isEmpty() ) {
[1500]138 _socket->connectToHost(_url.host(), _url.port());
[1382]139 }
140 else {
141 _socket->connectToHost(proxyHost, proxyPort);
142 }
[1500]143 if (!_socket->waitForConnected(_timeOut)) {
[1382]144 delete _socket;
145 _socket = 0;
[1385]146 _status = error;
147 return;
[1382]148 }
149
150 // Send Request
151 // ------------
[1500]152 QString uName = QUrl::fromPercentEncoding(_url.userName().toAscii());
153 QString passW = QUrl::fromPercentEncoding(_url.password().toAscii());
[1382]154 QByteArray userAndPwd;
155
[1385]156 if(!uName.isEmpty() || !passW.isEmpty()) {
[1382]157 userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
158 passW.toAscii()).toBase64() + "\r\n";
159 }
160
161 QByteArray reqStr;
162 if ( proxyHost.isEmpty() ) {
[1500]163 if (_url.path().indexOf("/") != 0) _url.setPath("/");
164 reqStr = "GET " + _url.path().toAscii() + " HTTP/1.0\r\n"
[1382]165 + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
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.