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

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

* empty log message *

File size: 3.6 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 <iostream>
18#include <iomanip>
19
20#include "bncnetqueryv2.h"
21#include "bncapp.h"
22
23using namespace std;
24
25#define BNCVERSION "1.7"
26
27// Constructor
28////////////////////////////////////////////////////////////////////////////
29bncNetQueryV2::bncNetQueryV2() {
30 _manager = new QNetworkAccessManager(this);
31 _reply = 0;
32 _eventLoop = new QEventLoop(this);
33
34 _status = init;
35}
36
37// Destructor
38////////////////////////////////////////////////////////////////////////////
39bncNetQueryV2::~bncNetQueryV2() {
40 delete _eventLoop;
41 delete _reply;
42 delete _manager;
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
53void bncNetQueryV2::slotFinished() {
54 if (_status != error) {
55 _status = finished;
56 }
57}
58
59// Start request, block till the next read (public)
60////////////////////////////////////////////////////////////////////////////
61void bncNetQueryV2::startRequest(const QUrl& url, const QByteArray& gga) {
62 startRequest(url, gga, false);
63}
64
65// Start request
66////////////////////////////////////////////////////////////////////////////
67void bncNetQueryV2::startRequest(const QUrl& url, const QByteArray& gga,
68 bool full) {
69
70 _status = running;
71
72 // Default scheme and path
73 // -----------------------
74 QUrl urlLoc(url);
75 if (urlLoc.scheme().isEmpty()) {
76 urlLoc.setScheme("http");
77 }
78 if (urlLoc.path().isEmpty()) {
79 urlLoc.setPath("/");
80 }
81
82 // Network Request
83 // ---------------
84 QNetworkRequest request;
85 request.setUrl(urlLoc);
86 request.setRawHeader("Host" , urlLoc.host().toAscii());
87 request.setRawHeader("Ntrip-Version", "NTRIP/2.0");
88 request.setRawHeader("User-Agent" , "NTRIP BNC/1.7");
89 if (!urlLoc.userName().isEmpty()) {
90 request.setRawHeader("Authorization", "Basic " +
91 (urlLoc.userName() + ":" + urlLoc.password()).toAscii().toBase64());
92 }
93 request.setRawHeader("Connection" , "close");
94
95 _reply = _manager->get(request);
96
97 // Connect Signals
98 // ---------------
99 connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
100 connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(quit()));
101 if (!full) {
102 connect(_reply, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
103 }
104 connect(_reply, SIGNAL(error(QNetworkReply::NetworkError)),
105 this, SLOT(slotError(QNetworkReply::NetworkError)));
106}
107
108// Start Request, wait for its completion
109////////////////////////////////////////////////////////////////////////////
110void bncNetQueryV2::waitForRequestResult(const QUrl& url, QByteArray& outData) {
111
112 // Send Request
113 // ------------
114 startRequest(url, "", true);
115
116 // Wait Loop
117 // ---------
118 _eventLoop->exec();
119
120 // Copy Data and Return
121 // --------------------
122 outData = _reply->readAll();
123}
124
125// Wait for next data
126////////////////////////////////////////////////////////////////////////////
127void bncNetQueryV2::waitForReadyRead(QByteArray& outData) {
128
129 // Wait Loop
130 // ---------
131 if (!_reply->bytesAvailable()) {
132 _eventLoop->exec();
133 }
134
135 // Append Data
136 // -----------
137 outData.append(_reply->readAll());
138}
Note: See TracBrowser for help on using the repository browser.