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

Last change on this file since 1551 was 1535, checked in by mervart, 15 years ago

* empty log message *

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