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

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

* empty log message *

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