source: ntrip/trunk/BNC/bncnetquery.cpp@ 1373

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

* empty log message *

File size: 4.0 KB
Line 
1/* -------------------------------------------------------------------------
2 * BKG NTRIP Client
3 * -------------------------------------------------------------------------
4 *
5 * Class: bncNetQuery
6 *
7 * Purpose: Blocking Network Requests
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 "bncnetquery.h"
21#include "bncapp.h"
22
23using namespace std;
24
25#define BNCVERSION "1.7"
26
27// Constructor
28////////////////////////////////////////////////////////////////////////////
29bncNetQuery::bncNetQuery() {
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
39// Destructor
40////////////////////////////////////////////////////////////////////////////
41bncNetQuery::~bncNetQuery() {
42 delete _eventLoop;
43 delete _reply;
44 delete _manager;
45}
46
47// Error
48////////////////////////////////////////////////////////////////////////////
49void bncNetQuery::slotError(QNetworkReply::NetworkError) {
50 cout << "slotError " << endl;
51 emit newMessage("slotError " + _reply->errorString().toAscii(), true);
52 _eventLoop->quit();
53}
54
55void bncNetQuery::slotFinished() {
56 cout << "slotFinished" << endl;
57}
58
59void bncNetQuery::slotReadyRead() {
60 cout << "slotReadyRead" << endl;
61}
62
63// Start request, block till the next read (public)
64////////////////////////////////////////////////////////////////////////////
65t_irc bncNetQuery::startRequest(const QUrl& url) {
66 return startRequest(url, true);
67}
68
69// Start request
70////////////////////////////////////////////////////////////////////////////
71t_irc bncNetQuery::startRequest(const QUrl& url, bool full) {
72
73 // Default scheme and path
74 // -----------------------
75 QUrl urlLoc(url);
76 if (urlLoc.scheme().isEmpty()) {
77 urlLoc.setScheme("http");
78 }
79 if (urlLoc.path().isEmpty()) {
80 urlLoc.setPath("/");
81 }
82
83 // Network Request
84 // ---------------
85 QNetworkRequest request;
86 request.setUrl(urlLoc);
87 request.setRawHeader("Host" , urlLoc.host().toAscii());
88 request.setRawHeader("Ntrip-Version", "NTRIP/2.0");
89 request.setRawHeader("User-Agent" , "NTRIP BNC/1.7");
90 if (!urlLoc.userName().isEmpty()) {
91 request.setRawHeader("Authorization", "Basic " +
92 (urlLoc.userName() + ":" + urlLoc.password()).toAscii().toBase64());
93 }
94 request.setRawHeader("Connection" , "close");
95
96 _reply = _manager->get(request);
97
98 // Connect Signals
99 // ---------------
100 connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
101 connect(_reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
102
103 connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(quit()));
104 if (!full) {
105 connect(_reply, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
106 }
107 connect(_reply, SIGNAL(error(QNetworkReply::NetworkError)),
108 this, SLOT(slotError(QNetworkReply::NetworkError)));
109
110 return success;
111}
112
113// Start Request, wait for its completion
114////////////////////////////////////////////////////////////////////////////
115t_irc bncNetQuery::waitForRequestResult(const QUrl& url, QByteArray& outData) {
116
117 // Send Request
118 // ------------
119 if (startRequest(url) != success) {
120 return failure;
121 }
122
123 // Wait Loop
124 // ---------
125 _eventLoop->exec();
126
127 // Copy Data and Return
128 // --------------------
129 outData = _reply->readAll();
130
131 if (_reply->error() == QNetworkReply::NoError) {
132 return success;
133 }
134 else {
135 return failure;
136 }
137}
138
139// Wait for next data
140////////////////////////////////////////////////////////////////////////////
141t_irc bncNetQuery::waitForReadyRead(QByteArray& outData) {
142
143 // Wait Loop
144 // ---------
145 if (!_reply->bytesAvailable()) {
146 _eventLoop->exec();
147 }
148
149 // Append Data and Return
150 // -----------------------
151 outData.append(_reply->readAll());
152
153 if (_reply->error() == QNetworkReply::NoError) {
154 return success;
155 }
156 else {
157 return failure;
158 }
159}
Note: See TracBrowser for help on using the repository browser.