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

Last change on this file since 1387 was 1380, 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 <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
31 connect(this, SIGNAL(newMessage(QByteArray,bool)),
32 (bncApp*) qApp, SLOT(slotMessage(const QByteArray,bool)));
33
34 _manager = new QNetworkAccessManager(this);
35 _reply = 0;
36 _eventLoop = new QEventLoop(this);
37
38 _status = init;
39}
40
41// Destructor
42////////////////////////////////////////////////////////////////////////////
43bncNetQueryV2::~bncNetQueryV2() {
44 delete _eventLoop;
45 delete _reply;
46 delete _manager;
47}
48
49// Error
50////////////////////////////////////////////////////////////////////////////
51void bncNetQueryV2::slotError(QNetworkReply::NetworkError) {
52 _status = error;
53 cout << "slotError " << endl;
54 emit newMessage("slotError " + _reply->errorString().toAscii(), true);
55 _eventLoop->quit();
56}
57
58void bncNetQueryV2::slotFinished() {
59 if (_status != error) {
60 _status = finished;
61 }
62 cout << "slotFinished" << endl;
63}
64
65void bncNetQueryV2::slotReadyRead() {
66 cout << "slotReadyRead" << endl;
67}
68
69// Start request, block till the next read (public)
70////////////////////////////////////////////////////////////////////////////
71void bncNetQueryV2::startRequest(const QUrl& url, const QByteArray& gga) {
72 startRequest(url, gga, false);
73}
74
75// Start request
76////////////////////////////////////////////////////////////////////////////
77void bncNetQueryV2::startRequest(const QUrl& url, const QByteArray& gga,
78 bool full) {
79
80 _status = running;
81
82 // Default scheme and path
83 // -----------------------
84 QUrl urlLoc(url);
85 if (urlLoc.scheme().isEmpty()) {
86 urlLoc.setScheme("http");
87 }
88 if (urlLoc.path().isEmpty()) {
89 urlLoc.setPath("/");
90 }
91
92 // Network Request
93 // ---------------
94 QNetworkRequest request;
95 request.setUrl(urlLoc);
96 request.setRawHeader("Host" , urlLoc.host().toAscii());
97 request.setRawHeader("Ntrip-Version", "NTRIP/2.0");
98 request.setRawHeader("User-Agent" , "NTRIP BNC/1.7");
99 if (!urlLoc.userName().isEmpty()) {
100 request.setRawHeader("Authorization", "Basic " +
101 (urlLoc.userName() + ":" + urlLoc.password()).toAscii().toBase64());
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(readyRead()), this, SLOT(slotReadyRead()));
111
112 connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(quit()));
113 if (!full) {
114 connect(_reply, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
115 }
116 connect(_reply, SIGNAL(error(QNetworkReply::NetworkError)),
117 this, SLOT(slotError(QNetworkReply::NetworkError)));
118}
119
120// Start Request, wait for its completion
121////////////////////////////////////////////////////////////////////////////
122void bncNetQueryV2::waitForRequestResult(const QUrl& url, QByteArray& outData) {
123
124 // Send Request
125 // ------------
126 startRequest(url, "", true);
127
128 // Wait Loop
129 // ---------
130 _eventLoop->exec();
131
132 // Copy Data and Return
133 // --------------------
134 outData = _reply->readAll();
135}
136
137// Wait for next data
138////////////////////////////////////////////////////////////////////////////
139void bncNetQueryV2::waitForReadyRead(QByteArray& outData) {
140
141 // Wait Loop
142 // ---------
143 if (!_reply->bytesAvailable()) {
144 _eventLoop->exec();
145 }
146
147 // Append Data
148 // -----------
149 outData.append(_reply->readAll());
150}
Note: See TracBrowser for help on using the repository browser.