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

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

* empty log message *

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