source: ntrip/trunk/BNC/bncgetthread.cpp@ 200

Last change on this file since 200 was 200, checked in by weber, 18 years ago

* empty log message *

File size: 7.0 KB
Line 
1
2/* -------------------------------------------------------------------------
3 * BKG NTRIP Client
4 * -------------------------------------------------------------------------
5 *
6 * Class: bncGetThread
7 *
8 * Purpose: Thread that retrieves data from NTRIP caster
9 *
10 * Author: L. Mervart
11 *
12 * Created: 24-Dec-2005
13 *
14 * Changes:
15 *
16 * -----------------------------------------------------------------------*/
17
18#include <QFile>
19#include <QTextStream>
20#include <QtNetwork>
21
22#include "bncgetthread.h"
23#include "bnctabledlg.h"
24
25#include "RTCM/RTCM.h"
26#include "RTCM3/rtcm3.h"
27#include "RTIGS/rtigs.h"
28
29using namespace std;
30
31// Constructor
32////////////////////////////////////////////////////////////////////////////
33bncGetThread::bncGetThread(const QUrl& mountPoint, const QByteArray& format) {
34 _decoder = 0;
35 _mountPoint = mountPoint;
36 _staID = mountPoint.path().mid(1).toAscii();
37 _format = format;
38 _socket = 0;
39 _timeOut = 20*1000; // 20 seconds
40 _nextSleep = 1; // 1 second
41}
42
43// Destructor
44////////////////////////////////////////////////////////////////////////////
45bncGetThread::~bncGetThread() {
46 delete _socket;
47 delete _decoder;
48}
49
50// Connect to Caster, send the Request (static)
51////////////////////////////////////////////////////////////////////////////
52QTcpSocket* bncGetThread::request(const QUrl& mountPoint, int timeOut,
53 QString& msg) {
54
55 // Connect the Socket
56 // ------------------
57 QSettings settings;
58 QString proxyHost = settings.value("proxyHost").toString();
59 int proxyPort = settings.value("proxyPort").toInt();
60
61 QTcpSocket* socket = new QTcpSocket();
62 if ( proxyHost.isEmpty() ) {
63 socket->connectToHost(mountPoint.host(), mountPoint.port());
64 }
65 else {
66 socket->connectToHost(proxyHost, proxyPort);
67 }
68 if (!socket->waitForConnected(timeOut)) {
69 msg += "Connect timeout\n";
70 delete socket;
71 return 0;
72 }
73
74 // Send Request
75 // ------------
76 QByteArray userAndPwd = mountPoint.userName().toAscii() + ":" +
77 mountPoint.password().toAscii();
78
79 QUrl hlp;
80 hlp.setScheme("http");
81 hlp.setHost(mountPoint.host());
82 hlp.setPort(mountPoint.port());
83 hlp.setPath(mountPoint.path());
84
85 QByteArray reqStr;
86 if ( proxyHost.isEmpty() ) {
87 if (hlp.path().indexOf("/") != 0) hlp.setPath("/");
88 reqStr = "GET " + hlp.path().toAscii() +
89 " HTTP/1.0\r\n"
90 "User-Agent: NTRIP BNC 1.0\r\n"
91 "Authorization: Basic " +
92 userAndPwd.toBase64() + "\r\n\r\n";
93 } else
94 {
95 reqStr = "GET " + hlp.toEncoded() +
96 " HTTP/1.0\r\n"
97 "User-Agent: NTRIP BNC 1.0\r\n"
98 "Authorization: Basic " +
99 userAndPwd.toBase64() + "\r\n\r\n";
100 }
101
102 msg += reqStr;
103
104 socket->write(reqStr, reqStr.length());
105
106 if (!socket->waitForBytesWritten(timeOut)) {
107 msg += "Write timeout\n";
108 delete socket;
109 return 0;
110 }
111
112 return socket;
113}
114
115// Init Run
116////////////////////////////////////////////////////////////////////////////
117t_irc bncGetThread::initRun() {
118
119 // Send the Request
120 // ----------------
121 QString msg;
122
123 _socket = bncGetThread::request(_mountPoint, _timeOut, msg);
124
125 //// emit(newMessage(msg.toAscii()));
126
127 if (!_socket) {
128 return failure;
129 }
130
131 // Read Caster Response
132 // --------------------
133 _socket->waitForReadyRead(_timeOut);
134 if (_socket->canReadLine()) {
135 QString line = _socket->readLine();
136 if (line.indexOf("Unauthorized") != -1) {
137 QStringList table;
138 bncTableDlg::getFullTable(_mountPoint.host(), _mountPoint.port(), table);
139 QString net;
140 QStringListIterator it(table);
141 while (it.hasNext()) {
142 QString line = it.next();
143 if (line.indexOf("STR") == 0) {
144 QStringList tags = line.split(";");
145 if (tags.at(1) == _staID) {
146 net = tags.at(7);
147 break;
148 }
149 }
150 }
151
152 QString reg;
153 it.toFront();
154 while (it.hasNext()) {
155 QString line = it.next();
156 if (line.indexOf("NET") == 0) {
157 QStringList tags = line.split(";");
158 if (tags.at(1) == net) {
159 reg = tags.at(7);
160 break;
161 }
162 }
163 }
164 emit(newMessage((_staID + ": Caster Response: " + line +
165 " Adjust User-ID and Password Register, see"
166 "\n " + reg).toAscii()));
167 return fatal;
168 }
169 if (line.indexOf("ICY 200 OK") != 0) {
170 emit(newMessage((_staID + ": Wrong Caster Response:\n" + line).toAscii()));
171 return failure;
172 }
173 }
174 else {
175 emit(newMessage(_staID + ": Response Timeout"));
176 return failure;
177 }
178
179 // Instantiate the filter
180 // ----------------------
181 if (!_decoder) {
182 if (_format.indexOf("RTCM_2") != -1) {
183 emit(newMessage("Get Data: " + _staID + " in RTCM 2.x format"));
184 _decoder = new RTCM('A',true);
185 }
186 else if (_format.indexOf("RTCM_3") != -1) {
187 emit(newMessage("Get Data: " + _staID + " in RTCM 3.0 format"));
188 _decoder = new rtcm3();
189 }
190 else if (_format.indexOf("RTIGS") != -1) {
191 emit(newMessage("Get Data: " + _staID + " in RTIGS format"));
192 _decoder = new rtigs();
193 }
194 else {
195 emit(newMessage(_staID + ": Unknown data format " + _format));
196 exit(1);
197 }
198 }
199 return success;
200}
201
202// Run
203////////////////////////////////////////////////////////////////////////////
204void bncGetThread::run() {
205
206 if (initRun() == fatal) {
207 QThread::exit(1);
208 return;
209 }
210 else if ( initRun() != success ) {
211 emit(newMessage(_staID + ": initRun failed, reconnecting"));
212 tryReconnect();
213 }
214
215 // Read Incoming Data
216 // ------------------
217 while (true) {
218
219 if (_socket->state() != QAbstractSocket::ConnectedState) {
220 emit(newMessage(_staID + ": Socket not connected, reconnecting"));
221 tryReconnect();
222 }
223
224 _socket->waitForReadyRead(_timeOut);
225 qint64 nBytes = _socket->bytesAvailable();
226 if (nBytes > 0) {
227 char* data = new char[nBytes];
228 _socket->read(data, nBytes);
229 _decoder->Decode(data, nBytes);
230 delete data;
231 for (list<Observation*>::iterator it = _decoder->m_lObsList.begin();
232 it != _decoder->m_lObsList.end(); it++) {
233 emit newObs(_staID, *it);
234 }
235 _decoder->m_lObsList.clear();
236 }
237 else {
238 emit(newMessage(_staID + ": Data Timeout, reconnecting"));
239 tryReconnect();
240 }
241 }
242}
243
244// Exit
245////////////////////////////////////////////////////////////////////////////
246void bncGetThread::exit(int exitCode) {
247 if (exitCode!= 0) {
248 emit error(_staID);
249 }
250 QThread::exit(exitCode);
251 terminate();
252}
253
254// Try Re-Connect
255////////////////////////////////////////////////////////////////////////////
256void bncGetThread::tryReconnect() {
257 while (1) {
258 delete _socket; _socket = 0;
259 sleep(_nextSleep);
260 if ( initRun() == success ) {
261 break;
262 }
263 else {
264 _nextSleep *= 2;
265 if (_nextSleep > 128) {
266 _nextSleep = 128;
267 }
268 }
269 }
270 _nextSleep = 1;
271}
Note: See TracBrowser for help on using the repository browser.