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

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

* empty log message *

File size: 4.9 KB
RevLine 
[1378]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
[1379]17#include "bncnetqueryv2.h"
[1378]18
19#define BNCVERSION "1.7"
20
21// Constructor
22////////////////////////////////////////////////////////////////////////////
23bncNetQueryV2::bncNetQueryV2() {
24 _manager = new QNetworkAccessManager(this);
[1405]25 connect(_manager, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&,
26 QAuthenticator*)),
27 this, SLOT(slotProxyAuthenticationRequired(const QNetworkProxy&,
28 QAuthenticator*)));
[1378]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
[1391]43// Stop (quit even loop)
[1390]44////////////////////////////////////////////////////////////////////////////
45void bncNetQueryV2::stop() {
[1393]46 if (_reply) {
[1398]47 _reply->disconnect(SIGNAL(error(QNetworkReply::NetworkError)));
[1395]48 _reply->abort();
[1393]49 }
[1394]50 _eventLoop->quit();
[1398]51 _status = finished;
[1390]52}
53
[1378]54// Error
55////////////////////////////////////////////////////////////////////////////
56void bncNetQueryV2::slotError(QNetworkReply::NetworkError) {
57 _status = error;
[1388]58 emit newMessage(_reply->errorString().toAscii(), true);
[1394]59 _eventLoop->quit();
[1378]60}
61
[1389]62// End of Request
63////////////////////////////////////////////////////////////////////////////
[1378]64void bncNetQueryV2::slotFinished() {
65 if (_status != error) {
66 _status = finished;
67 }
68}
69
[1405]70//
71////////////////////////////////////////////////////////////////////////////
72void bncNetQueryV2::slotProxyAuthenticationRequired(const QNetworkProxy&,
73 QAuthenticator*) {
74 emit newMessage("slotProxyAuthenticationRequired", true);
75}
76
[1389]77// Start request, block till the next read
[1378]78////////////////////////////////////////////////////////////////////////////
[1380]79void bncNetQueryV2::startRequest(const QUrl& url, const QByteArray& gga) {
[1389]80 startRequestPrivate(url, gga, false);
[1378]81}
82
[1389]83// Start Request (Private Method)
[1378]84////////////////////////////////////////////////////////////////////////////
[1389]85void bncNetQueryV2::startRequestPrivate(const QUrl& url, const QByteArray& gga,
86 bool full) {
[1378]87
88 _status = running;
89
90 // Default scheme and path
91 // -----------------------
92 QUrl urlLoc(url);
93 if (urlLoc.scheme().isEmpty()) {
94 urlLoc.setScheme("http");
95 }
96 if (urlLoc.path().isEmpty()) {
97 urlLoc.setPath("/");
98 }
99
[1404]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
[1378]111 // Network Request
112 // ---------------
113 QNetworkRequest request;
114 request.setUrl(urlLoc);
115 request.setRawHeader("Host" , urlLoc.host().toAscii());
116 request.setRawHeader("Ntrip-Version", "NTRIP/2.0");
117 request.setRawHeader("User-Agent" , "NTRIP BNC/1.7");
118 if (!urlLoc.userName().isEmpty()) {
119 request.setRawHeader("Authorization", "Basic " +
120 (urlLoc.userName() + ":" + urlLoc.password()).toAscii().toBase64());
121 }
[1389]122 if (!gga.isEmpty()) {
123 request.setRawHeader("Ntrip-GGA", gga);
124 }
[1378]125 request.setRawHeader("Connection" , "close");
126
127 _reply = _manager->get(request);
128
129 // Connect Signals
130 // ---------------
131 connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
132 connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(quit()));
133 if (!full) {
134 connect(_reply, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
135 }
136 connect(_reply, SIGNAL(error(QNetworkReply::NetworkError)),
137 this, SLOT(slotError(QNetworkReply::NetworkError)));
138}
139
140// Start Request, wait for its completion
141////////////////////////////////////////////////////////////////////////////
142void bncNetQueryV2::waitForRequestResult(const QUrl& url, QByteArray& outData) {
143
144 // Send Request
145 // ------------
[1389]146 startRequestPrivate(url, "", true);
[1378]147
148 // Wait Loop
149 // ---------
[1394]150 _eventLoop->exec();
[1378]151
152 // Copy Data and Return
153 // --------------------
154 outData = _reply->readAll();
155}
156
157// Wait for next data
158////////////////////////////////////////////////////////////////////////////
159void bncNetQueryV2::waitForReadyRead(QByteArray& outData) {
160
161 // Wait Loop
162 // ---------
163 if (!_reply->bytesAvailable()) {
[1394]164 _eventLoop->exec();
[1378]165 }
166
167 // Append Data
168 // -----------
169 outData.append(_reply->readAll());
170}
Note: See TracBrowser for help on using the repository browser.