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

Last change on this file since 194 was 194, checked in by mervart, 18 years ago

* empty log message *

File size: 6.7 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 = "GET " + hlp.toEncoded() +
86 " HTTP/1.0\r\n"
87 "User-Agent: NTRIP BNC 1.0\r\n"
88 "Authorization: Basic " +
89 userAndPwd.toBase64() + "\r\n\r\n";
90
91 msg += reqStr;
92
93 socket->write(reqStr, reqStr.length());
94
95 if (!socket->waitForBytesWritten(timeOut)) {
96 msg += "Write timeout\n";
97 delete socket;
98 return 0;
99 }
100
101 return socket;
102}
103
104// Init Run
105////////////////////////////////////////////////////////////////////////////
106t_irc bncGetThread::initRun() {
107
108 // Send the Request
109 // ----------------
110 QString msg;
111
112 _socket = bncGetThread::request(_mountPoint, _timeOut, msg);
113
114 //// emit(newMessage(msg.toAscii()));
115
116 if (!_socket) {
117 return failure;
118 }
119
120 // Read Caster Response
121 // --------------------
122 _socket->waitForReadyRead(_timeOut);
123 if (_socket->canReadLine()) {
124 QString line = _socket->readLine();
125 if (line.indexOf("Unauthorized") != -1) {
126 QStringList table;
127 bncTableDlg::getFullTable(_mountPoint.host(), _mountPoint.port(), table);
128 QString net;
129 QStringListIterator it(table);
130 while (it.hasNext()) {
131 QString line = it.next();
132 if (line.indexOf("STR") == 0) {
133 QStringList tags = line.split(";");
134 if (tags.at(1) == _staID) {
135 net = tags.at(7);
136 break;
137 }
138 }
139 }
140
141 QString reg;
142 it.toFront();
143 while (it.hasNext()) {
144 QString line = it.next();
145 if (line.indexOf("NET") == 0) {
146 QStringList tags = line.split(";");
147 if (tags.at(1) == net) {
148 reg = tags.at(7);
149 break;
150 }
151 }
152 }
153 emit(newMessage((_staID + ": Caster Response: " + line +
154 " Adjust User-ID and Password Register through"
155 "\n " + reg).toAscii()));
156 return fatal;
157 }
158 if (line.indexOf("ICY 200 OK") != 0) {
159 emit(newMessage((_staID + ": Wrong Caster Response:\n" + line).toAscii()));
160 return failure;
161 }
162 }
163 else {
164 emit(newMessage(_staID + ": Response Timeout"));
165 return failure;
166 }
167
168 // Instantiate the filter
169 // ----------------------
170 if (!_decoder) {
171 if (_format.indexOf("RTCM_2") != -1) {
172 emit(newMessage("Get Data: " + _staID + " in RTCM 2.x format"));
173 _decoder = new RTCM('A',true);
174 }
175 else if (_format.indexOf("RTCM_3") != -1) {
176 emit(newMessage("Get Data: " + _staID + " in RTCM 3.0 format"));
177 _decoder = new rtcm3();
178 }
179 else if (_format.indexOf("RTIGS") != -1) {
180 emit(newMessage("Get Data: " + _staID + " in RTIGS format"));
181 _decoder = new rtigs();
182 }
183 else {
184 emit(newMessage(_staID + ": Unknown data format " + _format));
185 exit(1);
186 }
187 }
188 return success;
189}
190
191// Run
192////////////////////////////////////////////////////////////////////////////
193void bncGetThread::run() {
194
195 if (initRun() == fatal) {
196 QThread::exit(1);
197 return;
198 }
199 else if ( initRun() != success ) {
200 emit(newMessage(_staID + ": initRun failed, reconnecting"));
201 tryReconnect();
202 }
203
204 // Read Incoming Data
205 // ------------------
206 while (true) {
207
208 if (_socket->state() != QAbstractSocket::ConnectedState) {
209 emit(newMessage(_staID + ": Socket not connected, reconnecting"));
210 tryReconnect();
211 }
212
213 _socket->waitForReadyRead(_timeOut);
214 qint64 nBytes = _socket->bytesAvailable();
215 if (nBytes > 0) {
216 char* data = new char[nBytes];
217 _socket->read(data, nBytes);
218 _decoder->Decode(data, nBytes);
219 delete data;
220 for (list<Observation*>::iterator it = _decoder->m_lObsList.begin();
221 it != _decoder->m_lObsList.end(); it++) {
222 emit newObs(_staID, *it);
223 }
224 _decoder->m_lObsList.clear();
225 }
226 else {
227 emit(newMessage(_staID + ": Data Timeout, reconnecting"));
228 tryReconnect();
229 }
230 }
231}
232
233// Exit
234////////////////////////////////////////////////////////////////////////////
235void bncGetThread::exit(int exitCode) {
236 if (exitCode!= 0) {
237 emit error(_staID);
238 }
239 QThread::exit(exitCode);
240 terminate();
241}
242
243// Try Re-Connect
244////////////////////////////////////////////////////////////////////////////
245void bncGetThread::tryReconnect() {
246 while (1) {
247 delete _socket; _socket = 0;
248 sleep(_nextSleep);
249 if ( initRun() == success ) {
250 break;
251 }
252 else {
253 _nextSleep *= 2;
254 if (_nextSleep > 128) {
255 _nextSleep = 128;
256 }
257 }
258 }
259 _nextSleep = 1;
260}
Note: See TracBrowser for help on using the repository browser.