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

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

* empty log message *

File size: 5.1 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 "bncnetqueryv2.h"
18#include "bncsettings.h"
19
20#define BNCVERSION "1.7"
21
22// Constructor
23////////////////////////////////////////////////////////////////////////////
24bncNetQueryV2::bncNetQueryV2() {
25 _manager = new QNetworkAccessManager(this);
26 connect(_manager, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&,
27 QAuthenticator*)),
28 this, SLOT(slotProxyAuthenticationRequired(const QNetworkProxy&,
29 QAuthenticator*)));
30 _reply = 0;
31 _eventLoop = new QEventLoop(this);
32
33 _status = init;
34}
35
36// Destructor
37////////////////////////////////////////////////////////////////////////////
38bncNetQueryV2::~bncNetQueryV2() {
39 delete _eventLoop;
40 delete _reply;
41 delete _manager;
42}
43
44// Stop (quit even loop)
45////////////////////////////////////////////////////////////////////////////
46void bncNetQueryV2::stop() {
47 if (_reply) {
48 _reply->disconnect(SIGNAL(error(QNetworkReply::NetworkError)));
49 _reply->abort();
50 }
51 _eventLoop->quit();
52 _status = finished;
53}
54
55// Error
56////////////////////////////////////////////////////////////////////////////
57void bncNetQueryV2::slotError(QNetworkReply::NetworkError) {
58 _status = error;
59 emit newMessage("NetQuery: " + _reply->errorString().toAscii(), true);
60 _eventLoop->quit();
61}
62
63// End of Request
64////////////////////////////////////////////////////////////////////////////
65void bncNetQueryV2::slotFinished() {
66 if (_status != error) {
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/1.7");
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 _reply = _manager->get(request);
131
132 // Connect Signals
133 // ---------------
134 connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
135 connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(quit()));
136 if (!full) {
137 connect(_reply, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
138 }
139 connect(_reply, SIGNAL(error(QNetworkReply::NetworkError)),
140 this, SLOT(slotError(QNetworkReply::NetworkError)));
141}
142
143// Start Request, wait for its completion
144////////////////////////////////////////////////////////////////////////////
145void bncNetQueryV2::waitForRequestResult(const QUrl& url, QByteArray& outData) {
146
147 // Send Request
148 // ------------
149 startRequestPrivate(url, "", true);
150
151 // Wait Loop
152 // ---------
153 _eventLoop->exec();
154
155 // Copy Data and Return
156 // --------------------
157 outData = _reply->readAll();
158}
159
160// Wait for next data
161////////////////////////////////////////////////////////////////////////////
162void bncNetQueryV2::waitForReadyRead(QByteArray& outData) {
163
164 // Wait Loop
165 // ---------
166 if (!_reply->bytesAvailable()) {
167 _eventLoop->exec();
168 }
169
170 // Append Data
171 // -----------
172 outData.append(_reply->readAll());
173}
Note: See TracBrowser for help on using the repository browser.