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

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

* empty log message *

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