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

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

* empty log message *

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