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

Last change on this file since 1523 was 1509, checked in by mervart, 16 years ago

* empty log message *

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