source: ntrip/trunk/BNC/bncnetqueryv0.cpp@ 1620

Last change on this file since 1620 was 1620, checked in by weber, 15 years ago

* empty log message *

File size: 4.2 KB
Line 
1/* -------------------------------------------------------------------------
2 * BKG NTRIP Client
3 * -------------------------------------------------------------------------
4 *
5 * Class: bncNetQueryV0
6 *
7 * Purpose: Blocking Network Requests (NTRIP Version 1)
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 "bncnetqueryv0.h"
21#include "bncsettings.h"
22
23using namespace std;
24
25#define BNCVERSION "1.7"
26
27// Constructor
28////////////////////////////////////////////////////////////////////////////
29bncNetQueryV0::bncNetQueryV0() {
30 _socket = 0;
31 _timeOut = 20000;
32}
33
34// Destructor
35////////////////////////////////////////////////////////////////////////////
36bncNetQueryV0::~bncNetQueryV0() {
37 delete _socket;
38}
39
40//
41////////////////////////////////////////////////////////////////////////////
42void bncNetQueryV0::stop() {
43#ifndef sparc
44 if (_socket) {
45 _socket->abort();
46 }
47#endif
48 _status = finished;
49}
50
51//
52////////////////////////////////////////////////////////////////////////////
53void bncNetQueryV0::waitForRequestResult(const QUrl&, QByteArray&) {
54}
55
56//
57////////////////////////////////////////////////////////////////////////////
58void bncNetQueryV0::waitForReadyRead(QByteArray& outData) {
59 if (_socket && _socket->state() == QAbstractSocket::ConnectedState) {
60 while (true) {
61 int nBytes = _socket->bytesAvailable();
62 if (nBytes > 0) {
63 outData = _socket->readAll();
64 return;
65 }
66 else if (!_socket->waitForReadyRead(_timeOut)) {
67 delete _socket;
68 _socket = 0;
69 _status = error;
70 emit newMessage(_url.path().toAscii() + " read timeout", true);
71 return;
72 }
73 }
74 }
75}
76
77// Connect to Caster, send the Request
78////////////////////////////////////////////////////////////////////////////
79void bncNetQueryV0::startRequest(const QUrl& url, const QByteArray& gga) {
80
81 _status = running;
82
83 delete _socket;
84 _socket = new QTcpSocket();
85
86 // Default scheme and path
87 // -----------------------
88 _url = url;
89 if (_url.scheme().isEmpty()) {
90 _url.setScheme("http");
91 }
92 if (_url.path().isEmpty()) {
93 _url.setPath("/");
94 }
95
96 // Connect the Socket
97 // ------------------
98 bncSettings settings;
99
100 _socket->connectToHost(_url.host(), _url.port());
101 if (!_socket->waitForConnected(_timeOut)) {
102 delete _socket;
103 _socket = 0;
104 _status = error;
105 return;
106 }
107
108 // Send Request
109 // ------------
110 QString uName = QUrl::fromPercentEncoding(_url.userName().toAscii());
111 QString passW = QUrl::fromPercentEncoding(_url.password().toAscii());
112 QByteArray userAndPwd;
113
114 if(!uName.isEmpty() || !passW.isEmpty()) {
115 userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
116 passW.toAscii()).toBase64() + "\r\n";
117 }
118
119 QByteArray reqStr;
120 if (_url.path().indexOf("/") != 0) _url.setPath("/");
121 reqStr = "GET " + _url.path().toAscii() + " HTTP/1.0\r\n"
122 + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
123 + userAndPwd + "\r\n";
124
125 // NMEA string to handle VRS stream
126 // --------------------------------
127 if (!gga.isEmpty()) {
128 reqStr += gga + "\r\n";
129 }
130
131 _socket->write(reqStr, reqStr.length());
132
133 if (!_socket->waitForBytesWritten(_timeOut)) {
134 delete _socket;
135 _socket = 0;
136 _status = error;
137 emit newMessage(_url.path().toAscii() + " write timeout", true);
138 return;
139 }
140
141 // Read Caster Response
142 // --------------------
143 QStringList response;
144 while (true) {
145 if (!_socket->waitForReadyRead(_timeOut)) {
146 delete _socket;
147 _socket = 0;
148 _status = error;
149 emit newMessage(_url.path().toAscii() + " read timeout", true);
150 return;
151 }
152 if (_socket->canReadLine()) {
153 QString line = _socket->readLine();
154 response.push_back(line);
155 if (line.trimmed().isEmpty()) {
156 break;
157 }
158 if (line.indexOf("200 OK") == -1) { // != weber
159 response.clear();
160 break;
161 }
162 }
163 }
164 if (response.size() > 0) {
165 delete _socket;
166 _socket = 0;
167 _status = error;
168 emit newMessage(_url.path().toAscii() + " wrong caster response\n" +
169 response.join("\n").toAscii(), true);
170 }
171}
172
Note: See TracBrowser for help on using the repository browser.