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

Last change on this file since 1705 was 1704, checked in by mervart, 16 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"
[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) {
[1395]50 _reply->abort();
[1393]51 }
[1394]52 _eventLoop->quit();
[1398]53 _status = finished;
[1390]54}
55
[1389]56// End of Request
57////////////////////////////////////////////////////////////////////////////
[1378]58void bncNetQueryV2::slotFinished() {
[1704]59 _eventLoop->quit();
60 if (_reply && _reply->error() != QNetworkReply::NoError) {
61 _status = error;
62 emit newMessage(_url.path().toAscii().replace(0,1,"") +
63 ": NetQuery, Error - server replied: " +
64 _reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray(),
65 true);
66 }
67 else {
[1378]68 _status = finished;
69 }
70}
71
[1405]72//
73////////////////////////////////////////////////////////////////////////////
74void bncNetQueryV2::slotProxyAuthenticationRequired(const QNetworkProxy&,
75 QAuthenticator*) {
76 emit newMessage("slotProxyAuthenticationRequired", true);
77}
78
[1389]79// Start request, block till the next read
[1378]80////////////////////////////////////////////////////////////////////////////
[1380]81void bncNetQueryV2::startRequest(const QUrl& url, const QByteArray& gga) {
[1389]82 startRequestPrivate(url, gga, false);
[1378]83}
84
[1389]85// Start Request (Private Method)
[1378]86////////////////////////////////////////////////////////////////////////////
[1389]87void bncNetQueryV2::startRequestPrivate(const QUrl& url, const QByteArray& gga,
88 bool full) {
[1378]89
90 _status = running;
91
92 // Default scheme and path
93 // -----------------------
[1509]94 _url = url;
95 if (_url.scheme().isEmpty()) {
96 _url.setScheme("http");
[1378]97 }
[1509]98 if (_url.path().isEmpty()) {
99 _url.setPath("/");
[1378]100 }
101
[1404]102 // Proxy Settings
103 // --------------
[1535]104 bncSettings settings;
[1404]105 QString proxyHost = settings.value("proxyHost").toString();
106 int proxyPort = settings.value("proxyPort").toInt();
107
108 if (!proxyHost.isEmpty()) {
109 QNetworkProxy proxy(QNetworkProxy::HttpProxy, proxyHost, proxyPort);
110 _manager->setProxy(proxy);
111 }
112
[1378]113 // Network Request
114 // ---------------
115 QNetworkRequest request;
[1509]116 request.setUrl(_url);
117 request.setRawHeader("Host" , _url.host().toAscii());
[1463]118 request.setRawHeader("Ntrip-Version", "Ntrip/2.0");
[1378]119 request.setRawHeader("User-Agent" , "NTRIP BNC/1.7");
[1509]120 if (!_url.userName().isEmpty()) {
121 QString uName = QUrl::fromPercentEncoding(_url.userName().toAscii());
122 QString passW = QUrl::fromPercentEncoding(_url.password().toAscii());
[1378]123 request.setRawHeader("Authorization", "Basic " +
[1509]124 (uName + ":" + passW).toAscii().toBase64());
[1378]125 }
[1389]126 if (!gga.isEmpty()) {
127 request.setRawHeader("Ntrip-GGA", gga);
128 }
[1378]129 request.setRawHeader("Connection" , "close");
130
131 _reply = _manager->get(request);
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}
Note: See TracBrowser for help on using the repository browser.