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

Last change on this file since 1525 was 1509, checked in by mervart, 16 years ago

* empty log message *

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