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

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