source: ntrip/trunk/BNC/bncnetqueryv2.cpp@ 1629

Last change on this file since 1629 was 1584, checked in by mervart, 16 years ago

* empty log message *

File size: 5.4 KB
RevLine 
[1378]1/* -------------------------------------------------------------------------
2 * BKG NTRIP Client
3 * -------------------------------------------------------------------------
4 *
5 * Class: bncNetQueryV2
6 *
7 * Purpose: Blocking Network Requests (NTRIP Version 2)
8 *
9 * Author: L. Mervart
10 *
11 * Created: 27-Dec-2008
12 *
13 * Changes:
14 *
15 * -----------------------------------------------------------------------*/
16
[1583]17#include <iostream>
18
[1379]19#include "bncnetqueryv2.h"
[1535]20#include "bncsettings.h"
[1378]21
22#define BNCVERSION "1.7"
23
24// Constructor
25////////////////////////////////////////////////////////////////////////////
26bncNetQueryV2::bncNetQueryV2() {
27 _manager = new QNetworkAccessManager(this);
[1405]28 connect(_manager, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&,
29 QAuthenticator*)),
30 this, SLOT(slotProxyAuthenticationRequired(const QNetworkProxy&,
31 QAuthenticator*)));
[1378]32 _reply = 0;
33 _eventLoop = new QEventLoop(this);
[1583]34 _firstData = true;
[1378]35 _status = init;
36}
37
38// Destructor
39////////////////////////////////////////////////////////////////////////////
40bncNetQueryV2::~bncNetQueryV2() {
41 delete _eventLoop;
42 delete _reply;
43 delete _manager;
44}
45
[1391]46// Stop (quit even loop)
[1390]47////////////////////////////////////////////////////////////////////////////
48void bncNetQueryV2::stop() {
[1393]49 if (_reply) {
[1398]50 _reply->disconnect(SIGNAL(error(QNetworkReply::NetworkError)));
[1395]51 _reply->abort();
[1393]52 }
[1394]53 _eventLoop->quit();
[1398]54 _status = finished;
[1390]55}
56
[1378]57// Error
58////////////////////////////////////////////////////////////////////////////
59void bncNetQueryV2::slotError(QNetworkReply::NetworkError) {
60 _status = error;
[1460]61 emit newMessage("NetQuery: " + _reply->errorString().toAscii(), true);
[1394]62 _eventLoop->quit();
[1378]63}
64
[1389]65// End of Request
66////////////////////////////////////////////////////////////////////////////
[1378]67void bncNetQueryV2::slotFinished() {
68 if (_status != error) {
69 _status = finished;
70 }
71}
72
[1405]73//
74////////////////////////////////////////////////////////////////////////////
75void bncNetQueryV2::slotProxyAuthenticationRequired(const QNetworkProxy&,
76 QAuthenticator*) {
77 emit newMessage("slotProxyAuthenticationRequired", true);
78}
79
[1389]80// Start request, block till the next read
[1378]81////////////////////////////////////////////////////////////////////////////
[1380]82void bncNetQueryV2::startRequest(const QUrl& url, const QByteArray& gga) {
[1389]83 startRequestPrivate(url, gga, false);
[1378]84}
85
[1389]86// Start Request (Private Method)
[1378]87////////////////////////////////////////////////////////////////////////////
[1389]88void bncNetQueryV2::startRequestPrivate(const QUrl& url, const QByteArray& gga,
89 bool full) {
[1378]90
91 _status = running;
92
93 // Default scheme and path
94 // -----------------------
[1509]95 _url = url;
96 if (_url.scheme().isEmpty()) {
97 _url.setScheme("http");
[1378]98 }
[1509]99 if (_url.path().isEmpty()) {
100 _url.setPath("/");
[1378]101 }
102
[1404]103 // Proxy Settings
104 // --------------
[1535]105 bncSettings settings;
[1404]106 QString proxyHost = settings.value("proxyHost").toString();
107 int proxyPort = settings.value("proxyPort").toInt();
108
109 if (!proxyHost.isEmpty()) {
110 QNetworkProxy proxy(QNetworkProxy::HttpProxy, proxyHost, proxyPort);
111 _manager->setProxy(proxy);
112 }
113
[1378]114 // Network Request
115 // ---------------
116 QNetworkRequest request;
[1509]117 request.setUrl(_url);
118 request.setRawHeader("Host" , _url.host().toAscii());
[1463]119 request.setRawHeader("Ntrip-Version", "Ntrip/2.0");
[1378]120 request.setRawHeader("User-Agent" , "NTRIP BNC/1.7");
[1509]121 if (!_url.userName().isEmpty()) {
122 QString uName = QUrl::fromPercentEncoding(_url.userName().toAscii());
123 QString passW = QUrl::fromPercentEncoding(_url.password().toAscii());
[1378]124 request.setRawHeader("Authorization", "Basic " +
[1509]125 (uName + ":" + passW).toAscii().toBase64());
[1378]126 }
[1389]127 if (!gga.isEmpty()) {
128 request.setRawHeader("Ntrip-GGA", gga);
129 }
[1378]130 request.setRawHeader("Connection" , "close");
131
132 _reply = _manager->get(request);
133
134 // Connect Signals
135 // ---------------
136 connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
137 connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(quit()));
138 if (!full) {
139 connect(_reply, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
140 }
141 connect(_reply, SIGNAL(error(QNetworkReply::NetworkError)),
142 this, SLOT(slotError(QNetworkReply::NetworkError)));
143}
144
145// Start Request, wait for its completion
146////////////////////////////////////////////////////////////////////////////
147void bncNetQueryV2::waitForRequestResult(const QUrl& url, QByteArray& outData) {
148
149 // Send Request
150 // ------------
[1389]151 startRequestPrivate(url, "", true);
[1378]152
153 // Wait Loop
154 // ---------
[1394]155 _eventLoop->exec();
[1378]156
157 // Copy Data and Return
158 // --------------------
159 outData = _reply->readAll();
160}
161
162// Wait for next data
163////////////////////////////////////////////////////////////////////////////
164void bncNetQueryV2::waitForReadyRead(QByteArray& outData) {
165
166 // Wait Loop
167 // ---------
168 if (!_reply->bytesAvailable()) {
[1394]169 _eventLoop->exec();
[1378]170 }
171
172 // Append Data
173 // -----------
174 outData.append(_reply->readAll());
[1583]175
176 if (_firstData) {
177 _firstData = false;
178 if (outData.indexOf("SOURCETABLE") != -1) {
179 _reply->disconnect(SIGNAL(error(QNetworkReply::NetworkError)));
180 _reply->abort();
181 _eventLoop->quit();
182 _status = error;
[1584]183 emit newMessage("NetQuery: wrong Mountpoint", true);
[1583]184 }
185 }
[1378]186}
Note: See TracBrowser for help on using the repository browser.