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

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