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

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

* empty log message *

File size: 4.0 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 // Network Request
90 // ---------------
91 QNetworkRequest request;
92 request.setUrl(urlLoc);
93 request.setRawHeader("Host" , urlLoc.host().toAscii());
94 request.setRawHeader("Ntrip-Version", "NTRIP/2.0");
95 request.setRawHeader("User-Agent" , "NTRIP BNC/1.7");
96 if (!urlLoc.userName().isEmpty()) {
97 request.setRawHeader("Authorization", "Basic " +
98 (urlLoc.userName() + ":" + urlLoc.password()).toAscii().toBase64());
99 }
100 if (!gga.isEmpty()) {
101 request.setRawHeader("Ntrip-GGA", gga);
102 }
103 request.setRawHeader("Connection" , "close");
104
105 _reply = _manager->get(request);
106
107 // Connect Signals
108 // ---------------
109 connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
110 connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(quit()));
111 if (!full) {
112 connect(_reply, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
113 }
114 connect(_reply, SIGNAL(error(QNetworkReply::NetworkError)),
115 this, SLOT(slotError(QNetworkReply::NetworkError)));
116}
117
118// Start Request, wait for its completion
119////////////////////////////////////////////////////////////////////////////
120void bncNetQueryV2::waitForRequestResult(const QUrl& url, QByteArray& outData) {
121
122 // Send Request
123 // ------------
124 startRequestPrivate(url, "", true);
125
126 // Wait Loop
127 // ---------
128 _eventLoop->exec();
129
130 // Copy Data and Return
131 // --------------------
132 outData = _reply->readAll();
133}
134
135// Wait for next data
136////////////////////////////////////////////////////////////////////////////
137void bncNetQueryV2::waitForReadyRead(QByteArray& outData) {
138
139 // Wait Loop
140 // ---------
141 if (!_reply->bytesAvailable()) {
142 _eventLoop->exec();
143 }
144
145 // Append Data
146 // -----------
147 outData.append(_reply->readAll());
148}
Note: See TracBrowser for help on using the repository browser.