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

Last change on this file since 1463 was 1463, checked in by weber, 15 years ago

* empty log message *

File size: 4.9 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 QUrl urlLoc(url);
93 if (urlLoc.scheme().isEmpty()) {
94 urlLoc.setScheme("http");
95 }
96 if (urlLoc.path().isEmpty()) {
97 urlLoc.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(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 }
122 if (!gga.isEmpty()) {
123 request.setRawHeader("Ntrip-GGA", gga);
124 }
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 // ------------
146 startRequestPrivate(url, "", true);
147
148 // Wait Loop
149 // ---------
150 _eventLoop->exec();
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()) {
164 _eventLoop->exec();
165 }
166
167 // Append Data
168 // -----------
169 outData.append(_reply->readAll());
170}
Note: See TracBrowser for help on using the repository browser.