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

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

* empty log message *

File size: 3.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 _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();
44 _reply->close();
45 }
46 _eventLoop->quit();
47}
48
49// Error
50////////////////////////////////////////////////////////////////////////////
51void bncNetQueryV2::slotError(QNetworkReply::NetworkError) {
52 _status = error;
53 emit newMessage(_reply->errorString().toAscii(), true);
54 _eventLoop->quit();
55}
56
57// End of Request
58////////////////////////////////////////////////////////////////////////////
59void bncNetQueryV2::slotFinished() {
60 if (_status != error) {
61 _status = finished;
62 }
63}
64
65// Start request, block till the next read
66////////////////////////////////////////////////////////////////////////////
67void bncNetQueryV2::startRequest(const QUrl& url, const QByteArray& gga) {
68 startRequestPrivate(url, gga, false);
69}
70
71// Start Request (Private Method)
72////////////////////////////////////////////////////////////////////////////
73void bncNetQueryV2::startRequestPrivate(const QUrl& url, const QByteArray& gga,
74 bool full) {
75
76 _status = running;
77
78 // Default scheme and path
79 // -----------------------
80 QUrl urlLoc(url);
81 if (urlLoc.scheme().isEmpty()) {
82 urlLoc.setScheme("http");
83 }
84 if (urlLoc.path().isEmpty()) {
85 urlLoc.setPath("/");
86 }
87
88 // Network Request
89 // ---------------
90 QNetworkRequest request;
91 request.setUrl(urlLoc);
92 request.setRawHeader("Host" , urlLoc.host().toAscii());
93 request.setRawHeader("Ntrip-Version", "NTRIP/2.0");
94 request.setRawHeader("User-Agent" , "NTRIP BNC/1.7");
95 if (!urlLoc.userName().isEmpty()) {
96 request.setRawHeader("Authorization", "Basic " +
97 (urlLoc.userName() + ":" + urlLoc.password()).toAscii().toBase64());
98 }
99 if (!gga.isEmpty()) {
100 request.setRawHeader("Ntrip-GGA", gga);
101 }
102 request.setRawHeader("Connection" , "close");
103
104 _reply = _manager->get(request);
105
106 // Connect Signals
107 // ---------------
108 connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
109 connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(quit()));
110 if (!full) {
111 connect(_reply, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
112 }
113 connect(_reply, SIGNAL(error(QNetworkReply::NetworkError)),
114 this, SLOT(slotError(QNetworkReply::NetworkError)));
115}
116
117// Start Request, wait for its completion
118////////////////////////////////////////////////////////////////////////////
119void bncNetQueryV2::waitForRequestResult(const QUrl& url, QByteArray& outData) {
120
121 // Send Request
122 // ------------
123 startRequestPrivate(url, "", true);
124
125 // Wait Loop
126 // ---------
127 _eventLoop->exec();
128
129 // Copy Data and Return
130 // --------------------
131 outData = _reply->readAll();
132}
133
134// Wait for next data
135////////////////////////////////////////////////////////////////////////////
136void bncNetQueryV2::waitForReadyRead(QByteArray& outData) {
137
138 // Wait Loop
139 // ---------
140 if (!_reply->bytesAvailable()) {
141 _eventLoop->exec();
142 }
143
144 // Append Data
145 // -----------
146 outData.append(_reply->readAll());
147}
Note: See TracBrowser for help on using the repository browser.