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

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

* empty log message *

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