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