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

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

* empty log message *

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