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

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

* empty log message *

File size: 6.7 KB
RevLine 
[35]1
2/* -------------------------------------------------------------------------
[93]3 * BKG NTRIP Client
[35]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"
[192]23#include "bnctabledlg.h"
[65]24
[35]25#include "RTCM/RTCM.h"
[65]26#include "RTCM3/rtcm3.h"
[61]27#include "RTIGS/rtigs.h"
[35]28
29using namespace std;
30
31// Constructor
32////////////////////////////////////////////////////////////////////////////
[88]33bncGetThread::bncGetThread(const QUrl& mountPoint, const QByteArray& format) {
[136]34 _decoder = 0;
[35]35 _mountPoint = mountPoint;
[89]36 _staID = mountPoint.path().mid(1).toAscii();
[59]37 _format = format;
[35]38 _socket = 0;
[181]39 _timeOut = 20*1000; // 20 seconds
[138]40 _nextSleep = 1; // 1 second
[35]41}
42
43// Destructor
44////////////////////////////////////////////////////////////////////////////
45bncGetThread::~bncGetThread() {
46 delete _socket;
[136]47 delete _decoder;
[35]48}
49
50// Connect to Caster, send the Request (static)
51////////////////////////////////////////////////////////////////////////////
[136]52QTcpSocket* bncGetThread::request(const QUrl& mountPoint, int timeOut,
53 QString& msg) {
[35]54
[88]55 // Connect the Socket
56 // ------------------
57 QSettings settings;
58 QString proxyHost = settings.value("proxyHost").toString();
59 int proxyPort = settings.value("proxyPort").toInt();
60
[35]61 QTcpSocket* socket = new QTcpSocket();
62 if ( proxyHost.isEmpty() ) {
[88]63 socket->connectToHost(mountPoint.host(), mountPoint.port());
[35]64 }
65 else {
66 socket->connectToHost(proxyHost, proxyPort);
67 }
68 if (!socket->waitForConnected(timeOut)) {
[82]69 msg += "Connect timeout\n";
[35]70 delete socket;
71 return 0;
72 }
73
74 // Send Request
75 // ------------
[88]76 QByteArray userAndPwd = mountPoint.userName().toAscii() + ":" +
77 mountPoint.password().toAscii();
[92]78
79 QUrl hlp;
80 hlp.setScheme("http");
81 hlp.setHost(mountPoint.host());
82 hlp.setPort(mountPoint.port());
83 hlp.setPath(mountPoint.path());
84
[202]85 QByteArray reqStr = "GET " + hlp.path().toAscii() +
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";
[35]90
[82]91 msg += reqStr;
[35]92
93 socket->write(reqStr, reqStr.length());
94
95 if (!socket->waitForBytesWritten(timeOut)) {
[82]96 msg += "Write timeout\n";
[35]97 delete socket;
98 return 0;
99 }
100
101 return socket;
102}
103
[136]104// Init Run
[35]105////////////////////////////////////////////////////////////////////////////
[138]106t_irc bncGetThread::initRun() {
[35]107
108 // Send the Request
109 // ----------------
[82]110 QString msg;
[88]111
[136]112 _socket = bncGetThread::request(_mountPoint, _timeOut, msg);
[88]113
[193]114 //// emit(newMessage(msg.toAscii()));
[82]115
[35]116 if (!_socket) {
[138]117 return failure;
[35]118 }
119
120 // Read Caster Response
121 // --------------------
[136]122 _socket->waitForReadyRead(_timeOut);
[35]123 if (_socket->canReadLine()) {
124 QString line = _socket->readLine();
[146]125 if (line.indexOf("Unauthorized") != -1) {
[192]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 }
[194]153 emit(newMessage((_staID + ": Caster Response: " + line +
[200]154 " Adjust User-ID and Password Register, see"
[194]155 "\n " + reg).toAscii()));
[192]156 return fatal;
[146]157 }
[35]158 if (line.indexOf("ICY 200 OK") != 0) {
[190]159 emit(newMessage((_staID + ": Wrong Caster Response:\n" + line).toAscii()));
[144]160 return failure;
[35]161 }
162 }
163 else {
[190]164 emit(newMessage(_staID + ": Response Timeout"));
[138]165 return failure;
[35]166 }
167
168 // Instantiate the filter
169 // ----------------------
[136]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 {
[190]184 emit(newMessage(_staID + ": Unknown data format " + _format));
[138]185 exit(1);
[136]186 }
[60]187 }
[138]188 return success;
[136]189}
[59]190
[136]191// Run
192////////////////////////////////////////////////////////////////////////////
193void bncGetThread::run() {
194
[192]195 if (initRun() == fatal) {
196 QThread::exit(1);
197 return;
198 }
199 else if ( initRun() != success ) {
[190]200 emit(newMessage(_staID + ": initRun failed, reconnecting"));
[138]201 tryReconnect();
202 }
[136]203
[35]204 // Read Incoming Data
205 // ------------------
206 while (true) {
[145]207
208 if (_socket->state() != QAbstractSocket::ConnectedState) {
[190]209 emit(newMessage(_staID + ": Socket not connected, reconnecting"));
[145]210 tryReconnect();
211 }
212
[136]213 _socket->waitForReadyRead(_timeOut);
[35]214 qint64 nBytes = _socket->bytesAvailable();
215 if (nBytes > 0) {
216 char* data = new char[nBytes];
217 _socket->read(data, nBytes);
[136]218 _decoder->Decode(data, nBytes);
[35]219 delete data;
[136]220 for (list<Observation*>::iterator it = _decoder->m_lObsList.begin();
221 it != _decoder->m_lObsList.end(); it++) {
[88]222 emit newObs(_staID, *it);
[35]223 }
[136]224 _decoder->m_lObsList.clear();
[35]225 }
226 else {
[190]227 emit(newMessage(_staID + ": Data Timeout, reconnecting"));
[136]228 tryReconnect();
[35]229 }
230 }
231}
232
233// Exit
234////////////////////////////////////////////////////////////////////////////
235void bncGetThread::exit(int exitCode) {
236 if (exitCode!= 0) {
[88]237 emit error(_staID);
[35]238 }
239 QThread::exit(exitCode);
[148]240 terminate();
[35]241}
[82]242
[136]243// Try Re-Connect
244////////////////////////////////////////////////////////////////////////////
245void bncGetThread::tryReconnect() {
[138]246 while (1) {
247 delete _socket; _socket = 0;
248 sleep(_nextSleep);
249 if ( initRun() == success ) {
250 break;
251 }
252 else {
253 _nextSleep *= 2;
[181]254 if (_nextSleep > 128) {
255 _nextSleep = 128;
[152]256 }
[138]257 }
258 }
259 _nextSleep = 1;
[136]260}
Note: See TracBrowser for help on using the repository browser.