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

Last change on this file since 357 was 357, checked in by weber, 17 years ago

* empty log message *

File size: 11.7 KB
Line 
1// Part of BNC, a utility for retrieving decoding and
2// converting GNSS data streams from NTRIP broadcasters,
3// written by Leos Mervart.
4//
5// Copyright (C) 2006
6// German Federal Agency for Cartography and Geodesy (BKG)
7// http://www.bkg.bund.de
8// Czech Technical University Prague, Department of Advanced Geodesy
9// http://www.fsv.cvut.cz
10//
11// Email: euref-ip@bkg.bund.de
12//
13// This program is free software; you can redistribute it and/or
14// modify it under the terms of the GNU General Public License
15// as published by the Free Software Foundation, version 2.
16//
17// This program is distributed in the hope that it will be useful,
18// but WITHOUT ANY WARRANTY; without even the implied warranty of
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20// GNU General Public License for more details.
21//
22// You should have received a copy of the GNU General Public License
23// along with this program; if not, write to the Free Software
24// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26/* -------------------------------------------------------------------------
27 * BKG NTRIP Client
28 * -------------------------------------------------------------------------
29 *
30 * Class: bncGetThread
31 *
32 * Purpose: Thread that retrieves data from NTRIP caster
33 *
34 * Author: L. Mervart
35 *
36 * Created: 24-Dec-2005
37 *
38 * Changes:
39 *
40 * -----------------------------------------------------------------------*/
41
42#include <stdlib.h>
43
44#include <QFile>
45#include <QTextStream>
46#include <QtNetwork>
47#include <QTime>
48
49#include "bncgetthread.h"
50#include "bnctabledlg.h"
51#include "bncapp.h"
52#include "bncutils.h"
53
54#include "RTCM/RTCM2Decoder.h"
55#include "RTCM3/RTCM3Decoder.h"
56#include "RTIGS/RTIGSDecoder.h"
57
58using namespace std;
59
60// Constructor
61////////////////////////////////////////////////////////////////////////////
62bncGetThread::bncGetThread(const QUrl& mountPoint,
63 const QByteArray& format, int iMount) {
64 _decoder = 0;
65 _mountPoint = mountPoint;
66 _staID = mountPoint.path().mid(1).toAscii();
67 _staID_orig = _staID;
68 _format = format;
69 _socket = 0;
70 _timeOut = 20*1000; // 20 seconds
71 _nextSleep = 1; // 1 second
72 _iMount = iMount; // index in mountpoints array
73
74 // Check name conflict
75 // -------------------
76 QSettings settings;
77 QListIterator<QString> it(settings.value("mountPoints").toStringList());
78 int num = 0;
79 int ind = -1;
80 while (it.hasNext()) {
81 ++ind;
82 QStringList hlp = it.next().split(" ");
83 if (hlp.size() <= 1) continue;
84 QUrl url(hlp[0]);
85 if (_mountPoint.path() == url.path()) {
86 if (_iMount > ind) {
87 ++num;
88 }
89 }
90 }
91
92 if (num > 0) {
93 _staID = _staID.left(_staID.length()-1) + QString("%1").arg(num).toAscii();
94 }
95 msleep(100); //sleep 0.1 sec
96}
97
98// Destructor
99////////////////////////////////////////////////////////////////////////////
100bncGetThread::~bncGetThread() {
101 delete _socket;
102 delete _decoder;
103}
104
105// Connect to Caster, send the Request (static)
106////////////////////////////////////////////////////////////////////////////
107QTcpSocket* bncGetThread::request(const QUrl& mountPoint, int timeOut,
108 QString& msg) {
109
110 // Connect the Socket
111 // ------------------
112 QSettings settings;
113 QString proxyHost = settings.value("proxyHost").toString();
114 int proxyPort = settings.value("proxyPort").toInt();
115
116 QTcpSocket* socket = new QTcpSocket();
117 if ( proxyHost.isEmpty() ) {
118 socket->connectToHost(mountPoint.host(), mountPoint.port());
119 }
120 else {
121 socket->connectToHost(proxyHost, proxyPort);
122 }
123 if (!socket->waitForConnected(timeOut)) {
124 msg += "Connect timeout\n";
125 delete socket;
126 return 0;
127 }
128
129 // Send Request
130 // ------------
131 QByteArray userAndPwd = mountPoint.userName().toAscii() + ":" +
132 mountPoint.password().toAscii();
133
134 QUrl hlp;
135 hlp.setScheme("http");
136 hlp.setHost(mountPoint.host());
137 hlp.setPort(mountPoint.port());
138 hlp.setPath(mountPoint.path());
139
140 QByteArray reqStr;
141 if ( proxyHost.isEmpty() ) {
142 if (hlp.path().indexOf("/") != 0) hlp.setPath("/");
143 reqStr = "GET " + hlp.path().toAscii() +
144 " HTTP/1.0\r\n"
145 "User-Agent: NTRIP BNC 1.1b\r\n"
146 "Authorization: Basic " +
147 userAndPwd.toBase64() + "\r\n\r\n";
148 } else {
149 reqStr = "GET " + hlp.toEncoded() +
150 " HTTP/1.0\r\n"
151 "User-Agent: NTRIP BNC 1.1b\r\n"
152 "Authorization: Basic " +
153 userAndPwd.toBase64() + "\r\n\r\n";
154 }
155
156//////////////////////////////////////////////////////////////////
157// Additional NMEA String in request to handle VRS data streams //
158// will be ignored from standard casters //
159//////////////////////////////////////////////////////////////////
160
161 double lat, lon;
162 lat = settings.value("approxLat", 0).toDouble();
163 lon = settings.value("approxLon", 0).toDouble();
164 printf("%s\n",reqStr.data());
165 if ((lat != 0.0) && (lon != 0.0) && (hlp.path().length() > 2) && (hlp.path().indexOf(".skl") < 0)) {
166 const char* flagN="N";
167 const char* flagE="E";
168 if (lon >180.) {lon=(lon-360.)*(-1.); flagE="W";}
169 if ((lon < 0.) && (lon >= -180.)) {lon=lon*(-1.); flagE="W";}
170 if (lon < -180.) {lon=(lon+360.); flagE="E";}
171 if (lat < 0.) {lat=lat*(-1.); flagN="S";}
172 QTime ttime(QTime::currentTime());
173 int lat_deg = (int)lat;
174 double lat_min=(lat-lat_deg)*60.;
175 int lon_deg = (int)lon;
176 double lon_min=(lon-lon_deg)*60.;
177 int hh = 0 , mm = 0;
178 double ss = 0.0;
179 hh=ttime.hour();
180 mm=ttime.minute();
181 ss=(double)ttime.second()+0.001*ttime.msec();
182 QString gga;
183 gga += "GPGGA,";
184 gga += QString("%1%2%3,").arg((int)hh, 2, 10, QLatin1Char('0')).arg((int)mm, 2, 10, QLatin1Char('0')).arg((int)ss, 2, 10, QLatin1Char('0'));
185 gga += QString("%1%2,").arg((int)lat_deg,2, 10, QLatin1Char('0')).arg(lat_min, 7, 'f', 4, QLatin1Char('0'));
186 gga += flagN;
187 gga += QString(",%1%2,").arg((int)lon_deg,3, 10, QLatin1Char('0')).arg(lon_min, 7, 'f', 4, QLatin1Char('0'));
188 gga += flagE + QString(",1,05,1.00,+00100,M,10.000,M,,");
189 int xori;
190 char XOR = 0;
191 char *Buff =gga.toAscii().data();
192 int iLen = strlen(Buff);
193 for (xori = 0; xori < iLen; xori++) {
194 XOR ^= (char)Buff[xori];
195 }
196 gga += QString("*%1").arg(XOR, 2, 16, QLatin1Char('0'));
197 reqStr += "$";
198 reqStr += gga;
199 reqStr += "\r\n";
200 }
201////////////////////////////////////////////////////////////////
202
203 msg += reqStr;
204
205 socket->write(reqStr, reqStr.length());
206
207 if (!socket->waitForBytesWritten(timeOut)) {
208 msg += "Write timeout\n";
209 delete socket;
210 return 0;
211 }
212
213 return socket;
214}
215
216// Init Run
217////////////////////////////////////////////////////////////////////////////
218t_irc bncGetThread::initRun() {
219
220 // Send the Request
221 // ----------------
222 QString msg;
223
224 _socket = bncGetThread::request(_mountPoint, _timeOut, msg);
225
226 //// emit(newMessage(msg.toAscii()));
227
228 if (!_socket) {
229 return failure;
230 }
231
232 // Read Caster Response
233 // --------------------
234 _socket->waitForReadyRead(_timeOut);
235 if (_socket->canReadLine()) {
236 QString line = _socket->readLine();
237 if (line.indexOf("Unauthorized") != -1) {
238 QStringList table;
239 bncTableDlg::getFullTable(_mountPoint.host(), _mountPoint.port(), table);
240 QString net;
241 QStringListIterator it(table);
242 while (it.hasNext()) {
243 QString line = it.next();
244 if (line.indexOf("STR") == 0) {
245 QStringList tags = line.split(";");
246 if (tags.at(1) == _staID_orig) {
247 net = tags.at(7);
248 break;
249 }
250 }
251 }
252
253 QString reg;
254 it.toFront();
255 while (it.hasNext()) {
256 QString line = it.next();
257 if (line.indexOf("NET") == 0) {
258 QStringList tags = line.split(";");
259 if (tags.at(1) == net) {
260 reg = tags.at(7);
261 break;
262 }
263 }
264 }
265 emit(newMessage((_staID + ": Caster Response: " + line +
266 " Adjust User-ID and Password Register, see"
267 "\n " + reg).toAscii()));
268 return fatal;
269 }
270 if (line.indexOf("ICY 200 OK") != 0) {
271 emit(newMessage((_staID + ": Wrong Caster Response:\n" + line).toAscii()));
272 return failure;
273 }
274 }
275 else {
276 emit(newMessage(_staID + ": Response Timeout"));
277 return failure;
278 }
279
280 // Instantiate the filter
281 // ----------------------
282 if (!_decoder) {
283 if (_format.indexOf("RTCM_2") != -1) {
284 emit(newMessage("Get Data: " + _staID + " in RTCM 2.x format"));
285 _decoder = new RTCM2Decoder();
286 }
287 else if (_format.indexOf("RTCM_3") != -1) {
288 emit(newMessage("Get Data: " + _staID + " in RTCM 3.0 format"));
289 _decoder = new RTCM3Decoder();
290 }
291 else if (_format.indexOf("RTIGS") != -1) {
292 emit(newMessage("Get Data: " + _staID + " in RTIGS format"));
293 _decoder = new RTIGSDecoder();
294 }
295 else {
296 emit(newMessage(_staID + ": Unknown data format " + _format));
297 return fatal;
298 }
299 }
300 return success;
301}
302
303// Run
304////////////////////////////////////////////////////////////////////////////
305void bncGetThread::run() {
306
307 t_irc irc = initRun();
308
309 if (irc == fatal) {
310 QThread::exit(1);
311 return;
312 }
313 else if (irc != success) {
314 emit(newMessage(_staID + ": initRun failed, reconnecting"));
315 tryReconnect();
316 }
317
318 // Read Incoming Data
319 // ------------------
320 while (true) {
321 try {
322 if (_socket->state() != QAbstractSocket::ConnectedState) {
323 emit(newMessage(_staID + ": Socket not connected, reconnecting"));
324 tryReconnect();
325 }
326
327
328 _socket->waitForReadyRead(_timeOut);
329 qint64 nBytes = _socket->bytesAvailable();
330 if (nBytes > 0) {
331 char* data = new char[nBytes];
332 _socket->read(data, nBytes);
333 _decoder->Decode(data, nBytes);
334 delete [] data;
335 for (list<Observation*>::iterator it = _decoder->_obsList.begin();
336 it != _decoder->_obsList.end(); it++) {
337
338 // Check observation epoch
339 // -----------------------
340 int week;
341 double sec;
342 currentGPSWeeks(week, sec);
343
344 const double secPerWeek = 7.0 * 24.0 * 3600.0;
345 const double maxDt = 600.0;
346
347 if (week < (*it)->GPSWeek) {
348 week += 1;
349 sec -= secPerWeek;
350 }
351 if (week > (*it)->GPSWeek) {
352 week -= 1;
353 sec += secPerWeek;
354 }
355 double dt = fabs(sec - (*it)->GPSWeeks);
356 if (week != (*it)->GPSWeek || dt > maxDt) {
357 emit( newMessage("Wrong observation epoch") );
358 delete (*it);
359 continue;
360 }
361
362 emit newObs(_staID, *it);
363 bool firstObs = (it == _decoder->_obsList.begin());
364 _global_caster->newObs(_staID, _mountPoint, firstObs, *it, _format);
365 }
366 _decoder->_obsList.clear();
367 }
368 else {
369 emit(newMessage(_staID + ": Data Timeout, reconnecting"));
370 tryReconnect();
371 }
372 }
373 catch (const char* msg) {
374 emit(newMessage(_staID + msg));
375 tryReconnect();
376 }
377 }
378}
379
380// Exit
381////////////////////////////////////////////////////////////////////////////
382void bncGetThread::exit(int exitCode) {
383 if (exitCode!= 0) {
384 emit error(_staID);
385 }
386 QThread::exit(exitCode);
387 terminate();
388}
389
390// Try Re-Connect
391////////////////////////////////////////////////////////////////////////////
392void bncGetThread::tryReconnect() {
393 while (1) {
394 delete _socket; _socket = 0;
395 sleep(_nextSleep);
396 if ( initRun() == success ) {
397 break;
398 }
399 else {
400 _nextSleep *= 2;
401 if (_nextSleep > 128) {
402 _nextSleep = 128;
403 }
404 _nextSleep += rand() % 6;
405 }
406 }
407 _nextSleep = 1;
408}
Note: See TracBrowser for help on using the repository browser.