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

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

* empty log message *

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