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

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

* empty log message *

File size: 5.6 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
24#include "RTCM/RTCM.h"
25#include "RTCM3/rtcm3.h"
26#include "RTIGS/rtigs.h"
27
28using namespace std;
29
30// Constructor
31////////////////////////////////////////////////////////////////////////////
32bncGetThread::bncGetThread(const QUrl& mountPoint, const QByteArray& format) {
33 _decoder = 0;
34 _mountPoint = mountPoint;
35 _staID = mountPoint.path().mid(1).toAscii();
36 _format = format;
37 _socket = 0;
38 _timeOut = 10*1000; // 10 seconds
39 _nextSleep = 1; // 1 second
40}
41
42// Destructor
43////////////////////////////////////////////////////////////////////////////
44bncGetThread::~bncGetThread() {
45 delete _socket;
46 delete _decoder;
47}
48
49// Connect to Caster, send the Request (static)
50////////////////////////////////////////////////////////////////////////////
51QTcpSocket* bncGetThread::request(const QUrl& mountPoint, int timeOut,
52 QString& msg) {
53
54 // Connect the Socket
55 // ------------------
56 QSettings settings;
57 QString proxyHost = settings.value("proxyHost").toString();
58 int proxyPort = settings.value("proxyPort").toInt();
59
60 QTcpSocket* socket = new QTcpSocket();
61 if ( proxyHost.isEmpty() ) {
62 socket->connectToHost(mountPoint.host(), mountPoint.port());
63 }
64 else {
65 socket->connectToHost(proxyHost, proxyPort);
66 }
67 if (!socket->waitForConnected(timeOut)) {
68 msg += "Connect timeout\n";
69 delete socket;
70 return 0;
71 }
72
73 // Send Request
74 // ------------
75 QByteArray userAndPwd = mountPoint.userName().toAscii() + ":" +
76 mountPoint.password().toAscii();
77
78 QUrl hlp;
79 hlp.setScheme("http");
80 hlp.setHost(mountPoint.host());
81 hlp.setPort(mountPoint.port());
82 hlp.setPath(mountPoint.path());
83
84 QByteArray reqStr = "GET " + hlp.toEncoded() +
85 " HTTP/1.0\r\n"
86 "User-Agent: NTRIP BNC 1.0\r\n"
87 "Authorization: Basic " +
88 userAndPwd.toBase64() + "\r\n\r\n";
89
90 msg += reqStr;
91
92 socket->write(reqStr, reqStr.length());
93
94 if (!socket->waitForBytesWritten(timeOut)) {
95 msg += "Write timeout\n";
96 delete socket;
97 return 0;
98 }
99
100 return socket;
101}
102
103// Init Run
104////////////////////////////////////////////////////////////////////////////
105t_irc bncGetThread::initRun() {
106
107 // Send the Request
108 // ----------------
109 QString msg;
110
111 _socket = bncGetThread::request(_mountPoint, _timeOut, msg);
112
113 emit(newMessage(msg.toAscii()));
114
115 if (!_socket) {
116 return failure;
117 }
118
119 // Read Caster Response
120 // --------------------
121 _socket->waitForReadyRead(_timeOut);
122 if (_socket->canReadLine()) {
123 QString line = _socket->readLine();
124 if (line.indexOf("Unauthorized") != -1) {
125 emit(newMessage(("Caster Response:\n" + line).toAscii()));
126 exit(1);
127 return failure;
128 }
129 if (line.indexOf("ICY 200 OK") != 0) {
130 emit(newMessage(("Wrong Caster Response:\n" + line).toAscii()));
131 exit(1);
132 return failure;
133 }
134 }
135 else {
136 emit(newMessage("Response Timeout"));
137 return failure;
138 }
139
140 // Instantiate the filter
141 // ----------------------
142 if (!_decoder) {
143 if (_format.indexOf("RTCM_2") != -1) {
144 emit(newMessage("Get Data: " + _staID + " in RTCM 2.x format"));
145 _decoder = new RTCM('A',true);
146 }
147 else if (_format.indexOf("RTCM_3") != -1) {
148 emit(newMessage("Get Data: " + _staID + " in RTCM 3.0 format"));
149 _decoder = new rtcm3();
150 }
151 else if (_format.indexOf("RTIGS") != -1) {
152 emit(newMessage("Get Data: " + _staID + " in RTIGS format"));
153 _decoder = new rtigs();
154 }
155 else {
156 emit(newMessage(_staID + " Unknown data format " + _format));
157 exit(1);
158 }
159 }
160 return success;
161}
162
163// Run
164////////////////////////////////////////////////////////////////////////////
165void bncGetThread::run() {
166
167 if ( initRun() != success ) {
168 emit(newMessage("initRun failed, reconnecting"));
169 tryReconnect();
170 }
171
172 // Read Incoming Data
173 // ------------------
174 while (true) {
175
176 if (_socket->state() != QAbstractSocket::ConnectedState) {
177 emit(newMessage("Socket not connected, reconnecting"));
178 tryReconnect();
179 }
180
181 _socket->waitForReadyRead(_timeOut);
182 qint64 nBytes = _socket->bytesAvailable();
183 if (nBytes > 0) {
184 char* data = new char[nBytes];
185 _socket->read(data, nBytes);
186 _decoder->Decode(data, nBytes);
187 delete data;
188 for (list<Observation*>::iterator it = _decoder->m_lObsList.begin();
189 it != _decoder->m_lObsList.end(); it++) {
190 emit newObs(_staID, *it);
191 }
192 _decoder->m_lObsList.clear();
193 }
194 else {
195 emit(newMessage("Data Timeout, reconnecting"));
196 tryReconnect();
197 }
198 }
199}
200
201// Exit
202////////////////////////////////////////////////////////////////////////////
203void bncGetThread::exit(int exitCode) {
204 if (exitCode!= 0) {
205 emit error(_staID);
206 }
207 QThread::exit(exitCode);
208 terminate();
209}
210
211// Try Re-Connect
212////////////////////////////////////////////////////////////////////////////
213void bncGetThread::tryReconnect() {
214 while (1) {
215 delete _socket; _socket = 0;
216 sleep(_nextSleep);
217 if ( initRun() == success ) {
218 break;
219 }
220 else {
221 _nextSleep *= 2;
222 }
223 }
224 _nextSleep = 1;
225}
Note: See TracBrowser for help on using the repository browser.