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

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