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

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

* empty log message *

File size: 5.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
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 return failure;
132 }
133 }
134 else {
135 emit(newMessage("Response Timeout"));
136 return failure;
137 }
138
139 // Instantiate the filter
140 // ----------------------
141 if (!_decoder) {
142 if (_format.indexOf("RTCM_2") != -1) {
143 emit(newMessage("Get Data: " + _staID + " in RTCM 2.x format"));
144 _decoder = new RTCM('A',true);
145 }
146 else if (_format.indexOf("RTCM_3") != -1) {
147 emit(newMessage("Get Data: " + _staID + " in RTCM 3.0 format"));
148 _decoder = new rtcm3();
149 }
150 else if (_format.indexOf("RTIGS") != -1) {
151 emit(newMessage("Get Data: " + _staID + " in RTIGS format"));
152 _decoder = new rtigs();
153 }
154 else {
155 emit(newMessage(_staID + " Unknown data format " + _format));
156 exit(1);
157 }
158 }
159 return success;
160}
161
162// Run
163////////////////////////////////////////////////////////////////////////////
164void bncGetThread::run() {
165
166 if ( initRun() != success ) {
167 emit(newMessage("initRun failed, reconnecting"));
168 tryReconnect();
169 }
170
171 // Read Incoming Data
172 // ------------------
173 while (true) {
174
175 if (_socket->state() != QAbstractSocket::ConnectedState) {
176 emit(newMessage("Socket not connected, reconnecting"));
177 tryReconnect();
178 }
179
180 _socket->waitForReadyRead(_timeOut);
181 qint64 nBytes = _socket->bytesAvailable();
182 if (nBytes > 0) {
183 char* data = new char[nBytes];
184 _socket->read(data, nBytes);
185 _decoder->Decode(data, nBytes);
186 delete data;
187 for (list<Observation*>::iterator it = _decoder->m_lObsList.begin();
188 it != _decoder->m_lObsList.end(); it++) {
189 emit newObs(_staID, *it);
190 }
191 _decoder->m_lObsList.clear();
192 }
193 else {
194 emit(newMessage("Data Timeout, reconnecting"));
195 tryReconnect();
196 }
197 }
198}
199
200// Exit
201////////////////////////////////////////////////////////////////////////////
202void bncGetThread::exit(int exitCode) {
203 if (exitCode!= 0) {
204 emit error(_staID);
205 }
206 QThread::exit(exitCode);
207 terminate();
208}
209
210// Try Re-Connect
211////////////////////////////////////////////////////////////////////////////
212void bncGetThread::tryReconnect() {
213 while (1) {
214 delete _socket; _socket = 0;
215 sleep(_nextSleep);
216 if ( initRun() == success ) {
217 break;
218 }
219 else {
220 _nextSleep *= 2;
221 if (_nextSleep > 60) {
222 _nextSleep = 60;
223 }
224 }
225 }
226 _nextSleep = 1;
227}
Note: See TracBrowser for help on using the repository browser.