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

Last change on this file since 3059 was 2012, checked in by mervart, 15 years ago

* empty log message *

File size: 5.2 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"
[2011]21#include "bncversion.h"
[1378]22
23// Constructor
24////////////////////////////////////////////////////////////////////////////
25bncNetQueryV2::bncNetQueryV2() {
26 _manager = new QNetworkAccessManager(this);
[1405]27 connect(_manager, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&,
28 QAuthenticator*)),
29 this, SLOT(slotProxyAuthenticationRequired(const QNetworkProxy&,
30 QAuthenticator*)));
[1378]31 _reply = 0;
32 _eventLoop = new QEventLoop(this);
[1583]33 _firstData = true;
[1378]34 _status = init;
35}
36
37// Destructor
38////////////////////////////////////////////////////////////////////////////
39bncNetQueryV2::~bncNetQueryV2() {
40 delete _eventLoop;
41 delete _reply;
42 delete _manager;
43}
44
[1713]45// Stop (quit event loop)
[1390]46////////////////////////////////////////////////////////////////////////////
47void bncNetQueryV2::stop() {
[1393]48 if (_reply) {
[1395]49 _reply->abort();
[1393]50 }
[1394]51 _eventLoop->quit();
[1398]52 _status = finished;
[1390]53}
54
[1389]55// End of Request
56////////////////////////////////////////////////////////////////////////////
[1378]57void bncNetQueryV2::slotFinished() {
[1704]58 _eventLoop->quit();
59 if (_reply && _reply->error() != QNetworkReply::NoError) {
60 _status = error;
61 emit newMessage(_url.path().toAscii().replace(0,1,"") +
[1713]62 ": NetQueryV2: server replied: " +
[1704]63 _reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray(),
64 true);
65 }
66 else {
[1378]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 // ---------------
[1716]114 QNetworkRequest request;
115 request.setUrl(_url);
116 request.setRawHeader("Host" , _url.host().toAscii());
117 request.setRawHeader("Ntrip-Version", "Ntrip/2.0");
[2012]118 request.setRawHeader("User-Agent" , "NTRIP BNC/"BNCVERSION);
[1509]119 if (!_url.userName().isEmpty()) {
120 QString uName = QUrl::fromPercentEncoding(_url.userName().toAscii());
121 QString passW = QUrl::fromPercentEncoding(_url.password().toAscii());
[1716]122 request.setRawHeader("Authorization", "Basic " +
[1509]123 (uName + ":" + passW).toAscii().toBase64());
[1378]124 }
[1389]125 if (!gga.isEmpty()) {
[1716]126 request.setRawHeader("Ntrip-GGA", gga);
[1389]127 }
[1716]128 request.setRawHeader("Connection" , "close");
[1378]129
[1716]130 delete _reply;
131 _reply = _manager->get(request);
[1378]132
133 // Connect Signals
134 // ---------------
135 connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
136 connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(quit()));
137 if (!full) {
138 connect(_reply, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
139 }
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
[1701]169 // Check NTRIPv2 error code
170 // ------------------------
171 if (_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
172 _reply->abort();
173 }
174
[1378]175 // Append Data
176 // -----------
[1701]177 else {
178 outData.append(_reply->readAll());
[1583]179 }
[1378]180}
[1712]181
Note: See TracBrowser for help on using the repository browser.