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

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