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

Last change on this file since 1854 was 1850, checked in by mervart, 15 years ago

* empty log message *

File size: 6.5 KB
Line 
1/* -------------------------------------------------------------------------
2 * BKG NTRIP Client
3 * -------------------------------------------------------------------------
4 *
5 * Class: bncNetQueryV1
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
20#include "bncnetqueryv1.h"
21#include "bncsettings.h"
22
23using namespace std;
24
25#define BNCVERSION "1.7"
26
27// Constructor
28////////////////////////////////////////////////////////////////////////////
29bncNetQueryV1::bncNetQueryV1() {
30 _socket = 0;
31 _eventLoop = new QEventLoop(this);
32 _timeOut = 20000;
33}
34
35// Destructor
36////////////////////////////////////////////////////////////////////////////
37bncNetQueryV1::~bncNetQueryV1() {
38 delete _socket;
39 delete _eventLoop;
40}
41
42//
43////////////////////////////////////////////////////////////////////////////
44void bncNetQueryV1::stop() {
45 _eventLoop->quit();
46#ifndef sparc
47 if (_socket) {
48 _socket->abort();
49 }
50#endif
51 _status = finished;
52}
53
54//
55////////////////////////////////////////////////////////////////////////////
56void bncNetQueryV1::waitForRequestResult(const QUrl& url, QByteArray& outData){
57
58 delete _socket;
59 _socket = new QTcpSocket();
60
61 connect(_socket, SIGNAL(disconnected()), _eventLoop, SLOT(quit()));
62
63 startRequestPrivate(url, "", true);
64
65 QTimer::singleShot(10000, _eventLoop, SLOT(quit()));
66
67 _eventLoop->exec();
68
69 outData = _socket->readAll();
70
71 delete _socket; _socket = 0;
72
73 _status = finished;
74}
75
76//
77////////////////////////////////////////////////////////////////////////////
78void bncNetQueryV1::waitForReadyRead(QByteArray& outData) {
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)) {
87 QString errStr = _socket->errorString();
88 if (errStr.isEmpty()) {
89 errStr = "Read timeout";
90 }
91 delete _socket;
92 _socket = 0;
93 _status = error;
94 emit newMessage(_url.path().toAscii().replace(0,1,"")
95 + ": " + errStr.toAscii(), true);
96 return;
97 }
98 }
99 }
100}
101
102// Connect to Caster, send the Request
103////////////////////////////////////////////////////////////////////////////
104void bncNetQueryV1::startRequest(const QUrl& url, const QByteArray& gga) {
105 startRequestPrivate(url, gga, false);
106}
107
108// Connect to Caster, send the Request
109////////////////////////////////////////////////////////////////////////////
110void bncNetQueryV1::startRequestPrivate(const QUrl& url,
111 const QByteArray& gga,
112 bool sendRequestOnly) {
113
114 _status = running;
115
116 if (!sendRequestOnly) {
117 delete _socket;
118 _socket = new QTcpSocket();
119 }
120
121 // Default scheme and path
122 // -----------------------
123 _url = url;
124 if (_url.scheme().isEmpty()) {
125 _url.setScheme("http");
126 }
127 if (_url.path().isEmpty()) {
128 _url.setPath("/");
129 }
130
131 // Connect the Socket
132 // ------------------
133 bncSettings settings;
134 QString proxyHost = settings.value("proxyHost").toString();
135 int proxyPort = settings.value("proxyPort").toInt();
136
137 if ( proxyHost.isEmpty() ) {
138 _socket->connectToHost(_url.host(), _url.port());
139 }
140 else {
141 _socket->connectToHost(proxyHost, proxyPort);
142 }
143 if (!_socket->waitForConnected(_timeOut)) {
144 delete _socket;
145 _socket = 0;
146 _status = error;
147 return;
148 }
149
150 // Send Request
151 // ------------
152 QString uName = QUrl::fromPercentEncoding(_url.userName().toAscii());
153 QString passW = QUrl::fromPercentEncoding(_url.password().toAscii());
154 QByteArray userAndPwd;
155
156 if(!uName.isEmpty() || !passW.isEmpty()) {
157 userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
158 passW.toAscii()).toBase64() + "\r\n";
159 }
160
161 QByteArray reqStr;
162 if ( proxyHost.isEmpty() ) {
163 if (_url.path().indexOf("/") != 0) _url.setPath("/");
164 reqStr = "GET " + _url.path().toAscii() + " HTTP/1.0\r\n"
165 + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
166 + userAndPwd + "\r\n";
167 } else {
168 reqStr = "GET " + _url.toEncoded() + " HTTP/1.0\r\n"
169 + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
170 + "Host: " + _url.host().toAscii() + "\r\n"
171 + userAndPwd + "\r\n";
172 }
173
174 // NMEA string to handle VRS stream
175 // --------------------------------
176 if (!gga.isEmpty()) {
177 reqStr += gga + "\r\n";
178 }
179
180 _socket->write(reqStr, reqStr.length());
181
182 if (!_socket->waitForBytesWritten(_timeOut)) {
183 delete _socket;
184 _socket = 0;
185 _status = error;
186 emit newMessage(_url.path().toAscii().replace(0,1,"")
187 + ": Write timeout", true);
188 return;
189 }
190
191 // Read Caster Response
192 // --------------------
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) {
220 break;
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 }
232 }
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;
240 }
241 }
242 if (response.size() > 0) {
243 delete _socket;
244 _socket = 0;
245 _status = error;
246 emit newMessage(_url.path().toAscii().replace(0,1,"")
247 + ": Wrong caster response\n"
248 + response.join("").toAscii(), true);
249 }
250 }
251}
252
Note: See TracBrowser for help on using the repository browser.