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

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