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

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

* empty log message *

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