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

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

* empty log message *

File size: 5.6 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 event loop)
47////////////////////////////////////////////////////////////////////////////
48void bncNetQueryV2::stop() {
49 if (_reply) {
50 _reply->abort();
51 }
52 _eventLoop->quit();
53 _status = finished;
54}
55
56// End of Request
57////////////////////////////////////////////////////////////////////////////
58void bncNetQueryV2::slotFinished() {
59 _eventLoop->quit();
60 if (_reply && _reply->error() != QNetworkReply::NoError) {
61 _status = error;
62 emit newMessage(_url.path().toAscii().replace(0,1,"") +
63 ": NetQueryV2: server replied: " +
64 _reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray(),
65 true);
66 }
67 else {
68 _status = finished;
69 }
70}
71
72//
73////////////////////////////////////////////////////////////////////////////
74void bncNetQueryV2::slotProxyAuthenticationRequired(const QNetworkProxy&,
75 QAuthenticator*) {
76 emit newMessage("slotProxyAuthenticationRequired", true);
77}
78
79// Start request, block till the next read
80////////////////////////////////////////////////////////////////////////////
81void bncNetQueryV2::startRequest(const QUrl& url, const QByteArray& gga) {
82 startRequestPrivate(url, gga, false);
83}
84
85// Start Request (Private Method)
86////////////////////////////////////////////////////////////////////////////
87void bncNetQueryV2::startRequestPrivate(const QUrl& url, const QByteArray& gga,
88 bool full) {
89
90 _status = running;
91
92 // Default scheme and path
93 // -----------------------
94 _url = url;
95 if (_url.scheme().isEmpty()) {
96 _url.setScheme("http");
97 }
98 if (_url.path().isEmpty()) {
99 _url.setPath("/");
100 }
101
102 // Proxy Settings
103 // --------------
104 bncSettings settings;
105 QString proxyHost = settings.value("proxyHost").toString();
106 int proxyPort = settings.value("proxyPort").toInt();
107
108 if (!proxyHost.isEmpty()) {
109 QNetworkProxy proxy(QNetworkProxy::HttpProxy, proxyHost, proxyPort);
110 _manager->setProxy(proxy);
111 }
112
113 // Network Request
114 // ---------------
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 this->startGet(full);
131}
132
133// Start Get HTTP Method
134////////////////////////////////////////////////////////////////////////////
135void bncNetQueryV2::startGet(bool full) {
136
137 if (_reply) {
138 _reply->abort();
139 delete _reply;
140 }
141
142 _reply = _manager->get(_request);
143
144 // Connect Signals
145 // ---------------
146 connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
147 connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(quit()));
148 if (!full) {
149 connect(_reply, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
150 }
151}
152
153// Start Request, wait for its completion
154////////////////////////////////////////////////////////////////////////////
155void bncNetQueryV2::waitForRequestResult(const QUrl& url, QByteArray& outData) {
156
157 // Send Request
158 // ------------
159 startRequestPrivate(url, "", true);
160
161 // Wait Loop
162 // ---------
163 _eventLoop->exec();
164
165 // Copy Data and Return
166 // --------------------
167 outData = _reply->readAll();
168}
169
170// Wait for next data
171////////////////////////////////////////////////////////////////////////////
172void bncNetQueryV2::waitForReadyRead(QByteArray& outData) {
173
174 // Wait Loop
175 // ---------
176 if (!_reply->bytesAvailable()) {
177 _eventLoop->exec();
178 }
179
180 // Check NTRIPv2 error code
181 // ------------------------
182 if (_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
183 _reply->abort();
184 }
185
186 // Append Data
187 // -----------
188 else {
189 outData.append(_reply->readAll());
190 }
191}
192
193// Send NMEA String
194////////////////////////////////////////////////////////////////////////////
195void bncNetQueryV2::sendNMEA(const QByteArray& gga) {
196 if (!gga.isEmpty()) {
197 _request.setRawHeader("Ntrip-GGA", gga);
198 }
199 this->startGet(false);
200}
Note: See TracBrowser for help on using the repository browser.