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

Last change on this file since 695 was 695, checked in by weber, 16 years ago

* empty log message *

File size: 18.8 KB
Line 
1// Part of BNC, a utility for retrieving decoding and
2// converting GNSS data streams from NTRIP broadcasters.
3//
4// Copyright (C) 2007
5// German Federal Agency for Cartography and Geodesy (BKG)
6// http://www.bkg.bund.de
7// Czech Technical University Prague, Department of Geodesy
8// http://www.fsv.cvut.cz
9//
10// Email: euref-ip@bkg.bund.de
11//
12// This program is free software; you can redistribute it and/or
13// modify it under the terms of the GNU General Public License
14// as published by the Free Software Foundation, version 2.
15//
16// This program is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU General Public License for more details.
20//
21// You should have received a copy of the GNU General Public License
22// along with this program; if not, write to the Free Software
23// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25/* -------------------------------------------------------------------------
26 * BKG NTRIP Client
27 * -------------------------------------------------------------------------
28 *
29 * Class: bncGetThread
30 *
31 * Purpose: Thread that retrieves data from NTRIP caster
32 *
33 * Author: L. Mervart
34 *
35 * Created: 24-Dec-2005
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <stdlib.h>
42
43#include <QFile>
44#include <QTextStream>
45#include <QtNetwork>
46#include <QTime>
47
48#include "bncgetthread.h"
49#include "bnctabledlg.h"
50#include "bncapp.h"
51#include "bncutils.h"
52#include "bncrinex.h"
53#include "bnczerodecoder.h"
54
55#include "RTCM/RTCM2Decoder.h"
56#include "RTCM3/RTCM3Decoder.h"
57#include "RTIGS/RTIGSDecoder.h"
58
59using namespace std;
60
61// Constructor
62////////////////////////////////////////////////////////////////////////////
63bncGetThread::bncGetThread(const QUrl& mountPoint,
64 const QByteArray& format,
65 const QByteArray& latitude,
66 const QByteArray& longitude,
67 const QByteArray& nmea, int iMount) {
68
69 setTerminationEnabled(true);
70
71 _decoder = 0;
72 _mountPoint = mountPoint;
73 _staID = mountPoint.path().mid(1).toAscii();
74 _staID_orig = _staID;
75 _format = format;
76 _latitude = latitude;
77 _longitude = longitude;
78 _nmea = nmea;
79 _socket = 0;
80 _timeOut = 20*1000; // 20 seconds
81 _nextSleep = 1; // 1 second
82 _iMount = iMount; // index in mountpoints array
83
84 // Check name conflict
85 // -------------------
86 QSettings settings;
87 QListIterator<QString> it(settings.value("mountPoints").toStringList());
88 int num = 0;
89 int ind = -1;
90 while (it.hasNext()) {
91 ++ind;
92 QStringList hlp = it.next().split(" ");
93 if (hlp.size() <= 1) continue;
94 QUrl url(hlp[0]);
95 if (_mountPoint.path() == url.path()) {
96 if (_iMount > ind) {
97 ++num;
98 }
99 }
100 }
101
102 if (num > 0) {
103 _staID = _staID.left(_staID.length()-1) + QString("%1").arg(num).toAscii();
104 }
105
106 // Notice threshold
107 // ----------------
108 _inspSegm = 50;
109 if ( settings.value("obsRate").toString().isEmpty() ) { _inspSegm = 0; }
110 if ( settings.value("obsRate").toString().indexOf("5 Hz") != -1 ) { _inspSegm = 2; }
111 if ( settings.value("obsRate").toString().indexOf("1 Hz") != -1 ) { _inspSegm = 10; }
112 if ( settings.value("obsRate").toString().indexOf("0.5 Hz") != -1 ) { _inspSegm = 20; }
113 if ( settings.value("obsRate").toString().indexOf("0.2 Hz") != -1 ) { _inspSegm = 40; }
114 if ( settings.value("obsRate").toString().indexOf("0.1 Hz") != -1 ) { _inspSegm = 50; }
115 _adviseFail = settings.value("adviseFail").toInt();
116 _adviseReco = settings.value("adviseReco").toInt();
117 _adviseScript = settings.value("adviseScript").toString();
118 expandEnvVar(_adviseScript);
119
120 // RINEX writer
121 // ------------
122 _samplingRate = settings.value("rnxSampl").toInt();
123 if ( settings.value("rnxPath").toString().isEmpty() ) {
124 _rnx = 0;
125 }
126 else {
127 _rnx = new bncRinex(_staID, mountPoint, format, latitude, longitude, nmea);
128 }
129
130 msleep(100); //sleep 0.1 sec
131}
132
133// Destructor
134////////////////////////////////////////////////////////////////////////////
135bncGetThread::~bncGetThread() {
136 if (_socket) {
137 _socket->close();
138#if QT_VERSION == 0x040203
139 delete _socket;
140#else
141 _socket->deleteLater();
142#endif
143 }
144 delete _decoder;
145 delete _rnx;
146}
147
148#define AGENTVERSION "1.5"
149// Connect to Caster, send the Request (static)
150////////////////////////////////////////////////////////////////////////////
151QTcpSocket* bncGetThread::request(const QUrl& mountPoint,
152 QByteArray& latitude, QByteArray& longitude,
153 QByteArray& nmea, int timeOut,
154 QString& msg) {
155
156 // Connect the Socket
157 // ------------------
158 QSettings settings;
159 QString proxyHost = settings.value("proxyHost").toString();
160 int proxyPort = settings.value("proxyPort").toInt();
161
162 QTcpSocket* socket = new QTcpSocket();
163 if ( proxyHost.isEmpty() ) {
164 socket->connectToHost(mountPoint.host(), mountPoint.port());
165 }
166 else {
167 socket->connectToHost(proxyHost, proxyPort);
168 }
169 if (!socket->waitForConnected(timeOut)) {
170 msg += "Connect timeout\n";
171 delete socket;
172 return 0;
173 }
174
175 // Send Request
176 // ------------
177 QString uName = QUrl::fromPercentEncoding(mountPoint.userName().toAscii());
178 QString passW = QUrl::fromPercentEncoding(mountPoint.password().toAscii());
179 QByteArray userAndPwd;
180
181 if(!uName.isEmpty() || !passW.isEmpty())
182 {
183 userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
184 passW.toAscii()).toBase64() + "\r\n";
185 }
186
187 QUrl hlp;
188 hlp.setScheme("http");
189 hlp.setHost(mountPoint.host());
190 hlp.setPort(mountPoint.port());
191 hlp.setPath(mountPoint.path());
192
193 QByteArray reqStr;
194 if ( proxyHost.isEmpty() ) {
195 if (hlp.path().indexOf("/") != 0) hlp.setPath("/");
196 reqStr = "GET " + hlp.path().toAscii() + " HTTP/1.0\r\n";
197 } else {
198 reqStr = "GET " + hlp.toEncoded() + " HTTP/1.0\r\n";
199 }
200 reqStr += "User-Agent: NTRIP BNC/" AGENTVERSION "\r\n"
201 "Host: " + hlp.host().toAscii() + "\r\n"
202 + userAndPwd + "\r\n";
203
204// NMEA string to handle VRS stream
205// --------------------------------
206
207 double lat, lon;
208
209 lat = strtod(latitude,NULL);
210 lon = strtod(longitude,NULL);
211
212 if ((nmea == "yes") && (hlp.path().length() > 2) && (hlp.path().indexOf(".skl") < 0)) {
213 const char* flagN="N";
214 const char* flagE="E";
215 if (lon >180.) {lon=(lon-360.)*(-1.); flagE="W";}
216 if ((lon < 0.) && (lon >= -180.)) {lon=lon*(-1.); flagE="W";}
217 if (lon < -180.) {lon=(lon+360.); flagE="E";}
218 if (lat < 0.) {lat=lat*(-1.); flagN="S";}
219 QTime ttime(QDateTime::currentDateTime().toUTC().time());
220 int lat_deg = (int)lat;
221 double lat_min=(lat-lat_deg)*60.;
222 int lon_deg = (int)lon;
223 double lon_min=(lon-lon_deg)*60.;
224 int hh = 0 , mm = 0;
225 double ss = 0.0;
226 hh=ttime.hour();
227 mm=ttime.minute();
228 ss=(double)ttime.second()+0.001*ttime.msec();
229 QString gga;
230 gga += "GPGGA,";
231 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'));
232 gga += QString("%1%2,").arg((int)lat_deg,2, 10, QLatin1Char('0')).arg(lat_min, 7, 'f', 4, QLatin1Char('0'));
233 gga += flagN;
234 gga += QString(",%1%2,").arg((int)lon_deg,3, 10, QLatin1Char('0')).arg(lon_min, 7, 'f', 4, QLatin1Char('0'));
235 gga += flagE + QString(",1,05,1.00,+00100,M,10.000,M,,");
236 int xori;
237 char XOR = 0;
238 char *Buff =gga.toAscii().data();
239 int iLen = strlen(Buff);
240 for (xori = 0; xori < iLen; xori++) {
241 XOR ^= (char)Buff[xori];
242 }
243 gga += QString("*%1").arg(XOR, 2, 16, QLatin1Char('0'));
244 reqStr += "$";
245 reqStr += gga;
246 reqStr += "\r\n";
247 }
248
249 msg += reqStr;
250
251 socket->write(reqStr, reqStr.length());
252
253 if (!socket->waitForBytesWritten(timeOut)) {
254 msg += "Write timeout\n";
255 delete socket;
256 return 0;
257 }
258
259 return socket;
260}
261
262// Init Run
263////////////////////////////////////////////////////////////////////////////
264t_irc bncGetThread::initRun() {
265
266 // Initialize Socket
267 // -----------------
268 QString msg;
269 _socket = this->request(_mountPoint, _latitude, _longitude,
270 _nmea, _timeOut, msg);
271 if (!_socket) {
272 return failure;
273 }
274
275 // Read Caster Response
276 // --------------------
277 _socket->waitForReadyRead(_timeOut);
278 if (_socket->canReadLine()) {
279 QString line = _socket->readLine();
280
281 // Skip messages from proxy server
282 // -------------------------------
283 if (line.indexOf("ICY 200 OK") == -1 &&
284 line.indexOf("200 OK") != -1 ) {
285 bool proxyRespond = true;
286 while (true) {
287 if (_socket->canReadLine()) {
288 line = _socket->readLine();
289 if (!proxyRespond) {
290 break;
291 }
292 if (line.trimmed().isEmpty()) {
293 proxyRespond = false;
294 }
295 }
296 else {
297 _socket->waitForReadyRead(_timeOut);
298 if (_socket->bytesAvailable() <= 0) {
299 break;
300 }
301 }
302 }
303 }
304
305 if (line.indexOf("Unauthorized") != -1) {
306 QStringList table;
307 bncTableDlg::getFullTable(_mountPoint.host(), _mountPoint.port(), table);
308 QString net;
309 QStringListIterator it(table);
310 while (it.hasNext()) {
311 QString line = it.next();
312 if (line.indexOf("STR") == 0) {
313 QStringList tags = line.split(";");
314 if (tags.at(1) == _staID_orig) {
315 net = tags.at(7);
316 break;
317 }
318 }
319 }
320
321 QString reg;
322 it.toFront();
323 while (it.hasNext()) {
324 QString line = it.next();
325 if (line.indexOf("NET") == 0) {
326 QStringList tags = line.split(";");
327 if (tags.at(1) == net) {
328 reg = tags.at(7);
329 break;
330 }
331 }
332 }
333 emit(newMessage((_staID + ": Caster Response: " + line +
334 " Adjust User-ID and Password Register, see"
335 "\n " + reg).toAscii()));
336 return fatal;
337 }
338 if (line.indexOf("ICY 200 OK") != 0) {
339 emit(newMessage((_staID + ": Wrong Caster Response:\n" + line).toAscii()));
340 return failure;
341 }
342 }
343 else {
344 emit(newMessage(_staID + ": Response Timeout"));
345 return failure;
346 }
347
348 // Instantiate the filter
349 // ----------------------
350 if (!_decoder) {
351 if (_format.indexOf("RTCM_2") != -1) {
352 emit(newMessage("Get Data: " + _staID + " in RTCM 2.x format"));
353 _decoder = new RTCM2Decoder();
354 }
355 else if (_format.indexOf("RTCM_3") != -1) {
356 emit(newMessage("Get Data: " + _staID + " in RTCM 3.x format"));
357 _decoder = new RTCM3Decoder();
358 }
359 else if (_format.indexOf("RTIGS") != -1) {
360 emit(newMessage("Get Data: " + _staID + " in RTIGS format"));
361 _decoder = new RTIGSDecoder();
362 }
363 else if (_format.indexOf("SP3") != -1 || _format.indexOf("ZERO") != -1) {
364 emit(newMessage("Get Data: " + _staID + " in original format"));
365 _decoder = new bncZeroDecoder(_staID);
366 }
367 else {
368 emit(newMessage(_staID + ": Unknown data format " + _format));
369 return fatal;
370 }
371 }
372 return success;
373}
374
375// Run
376////////////////////////////////////////////////////////////////////////////
377void bncGetThread::run() {
378
379 t_irc irc = initRun();
380
381 if (irc == fatal) {
382 QThread::exit(1);
383 return;
384 }
385 else if (irc != success) {
386 emit(newMessage(_staID + ": initRun failed, reconnecting"));
387 tryReconnect();
388 }
389
390 bool wrongEpoch = false;
391 bool decode = true;
392 int numSucc = 0;
393 int secSucc = 0;
394 int secFail = 0;
395 int initPause = 30;
396 int currPause = 0;
397 bool begCorrupt = false;
398 bool endCorrupt = false;
399 _decodeTime = QDateTime::currentDateTime();
400
401 if (initPause < _inspSegm) {
402 initPause = _inspSegm;
403 }
404 currPause = initPause;
405
406 // Read Incoming Data
407 // ------------------
408 while (true) {
409 try {
410 if (_socket->state() != QAbstractSocket::ConnectedState) {
411 emit(newMessage(_staID + ": Socket not connected, reconnecting"));
412 tryReconnect();
413 }
414
415 QListIterator<p_obs> it(_decoder->_obsList);
416 while (it.hasNext()) {
417 delete it.next();
418 }
419 _decoder->_obsList.clear();
420
421 _socket->waitForReadyRead(_timeOut);
422 qint64 nBytes = _socket->bytesAvailable();
423 if (nBytes > 0) {
424 emit newBytes(_staID, nBytes);
425
426 char* data = new char[nBytes];
427 _socket->read(data, nBytes);
428
429 if (_inspSegm<1) {
430 _decoder->Decode(data, nBytes);
431 }
432 else {
433
434 // Decode data
435 // -----------
436 if (!_decodePause.isValid() ||
437 _decodePause.secsTo(QDateTime::currentDateTime()) >= currPause ) {
438
439 if (decode) {
440 if ( _decoder->Decode(data, nBytes) == success ) {
441 numSucc += 1;
442 }
443 if ( _decodeTime.secsTo(QDateTime::currentDateTime()) > _inspSegm ) {
444 decode = false;
445 }
446 }
447
448 // Check - once per inspect segment
449 // --------------------------------
450 if (!decode) {
451 _decodeTime = QDateTime::currentDateTime();
452 if (numSucc>0) {
453 secSucc += _inspSegm;
454 if (secSucc > _adviseReco * 60) {
455 secSucc = _adviseReco * 60 + 1;
456 }
457 numSucc = 0;
458 currPause = initPause;
459 _decodePause.setDate(QDate());
460 _decodePause.setTime(QTime());
461 }
462 else {
463 secFail += _inspSegm;
464 secSucc = 0;
465 if (secFail > _adviseFail * 60) {
466 secFail = _adviseFail * 60 + 1;
467 }
468 if (!_decodePause.isValid()) {
469 _decodePause = QDateTime::currentDateTime();
470 }
471 else {
472 _decodePause.setDate(QDate());
473 _decodePause.setTime(QTime());
474 secFail = secFail + currPause - _inspSegm;
475 currPause = currPause * 2;
476 if (currPause > 960) {
477 currPause = 960;
478 }
479 }
480 }
481
482 // End corrupt threshold
483 // ---------------------
484 if ( begCorrupt && !endCorrupt && secSucc > _adviseReco * 60 ) {
485 emit(newMessage(_staID + ": Corrupted recovery threshold exceeded"));
486 callScript("End_Corrupted");
487 endCorrupt = true;
488 begCorrupt = false;
489 secFail = 0;
490 }
491 else {
492
493 // Begin corrupt threshold
494 // -----------------------
495 if ( !begCorrupt && secFail > _adviseFail * 60 ) {
496 emit(newMessage(_staID + ": Corrupted failure threshold exceeded"));
497 callScript("Begin_Corrupted");
498 begCorrupt = true;
499 endCorrupt = false;
500 secSucc = 0;
501 numSucc = 0;
502 }
503 }
504 decode = true;
505 }
506 }
507 }
508
509 // End outage threshold
510 // --------------------
511 if ( _decodeStart.isValid() && _decodeStart.secsTo(QDateTime::currentDateTime()) > _adviseReco * 60 ) {
512 _decodeStart.setDate(QDate());
513 _decodeStart.setTime(QTime());
514 if (_inspSegm>0) {
515 emit(newMessage(_staID + ": Outage recovery threshold exceeded"));
516 callScript("End_Outage");
517 }
518 }
519
520 delete [] data;
521
522 QListIterator<p_obs> it(_decoder->_obsList);
523 while (it.hasNext()) {
524 p_obs obs = it.next();
525
526 // Check observation epoch
527 // -----------------------
528 int week;
529 double sec;
530 currentGPSWeeks(week, sec);
531
532 const double secPerWeek = 7.0 * 24.0 * 3600.0;
533 const double maxDt = 600.0;
534
535 if (week < obs->_o.GPSWeek) {
536 week += 1;
537 sec -= secPerWeek;
538 }
539 if (week > obs->_o.GPSWeek) {
540 week -= 1;
541 sec += secPerWeek;
542 }
543 double dt = fabs(sec - obs->_o.GPSWeeks);
544 if (week != obs->_o.GPSWeek || dt > maxDt) {
545 if (!wrongEpoch) {
546 emit( newMessage(_staID + ": Wrong observation epoch") );
547 wrongEpoch = true;
548 }
549 delete obs;
550 continue;
551 }
552 else {
553 wrongEpoch = false;
554 }
555
556 // RINEX Output
557 // ------------
558 if (_rnx) {
559 long iSec = long(floor(obs->_o.GPSWeeks+0.5));
560 long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
561 if (_samplingRate == 0 || iSec % _samplingRate == 0) {
562 _rnx->deepCopy(obs);
563 }
564 _rnx->dumpEpoch(newTime);
565 }
566
567 bool firstObs = (obs == _decoder->_obsList.first());
568 obs->_status = t_obs::posted;
569 emit newObs(_staID, firstObs, obs);
570 }
571 _decoder->_obsList.clear();
572 }
573 else {
574 emit(newMessage(_staID + ": Data Timeout, reconnecting"));
575 tryReconnect();
576 }
577 }
578 catch (const char* msg) {
579 emit(newMessage(_staID + msg));
580 tryReconnect();
581 }
582 }
583}
584
585// Exit
586////////////////////////////////////////////////////////////////////////////
587void bncGetThread::exit(int exitCode) {
588 if (exitCode!= 0) {
589 emit error(_staID);
590 }
591 QThread::exit(exitCode);
592 terminate();
593}
594
595// Try Re-Connect
596////////////////////////////////////////////////////////////////////////////
597void bncGetThread::tryReconnect() {
598 if (_rnx) {
599 _rnx->setReconnectFlag(true);
600 }
601 if ( !_decodeStart.isValid()) {
602 _decodeStop = QDateTime::currentDateTime();
603 }
604 while (1) {
605 delete _socket; _socket = 0;
606 sleep(_nextSleep);
607 if ( initRun() == success ) {
608 if ( !_decodeStop.isValid()) {
609 _decodeStart = QDateTime::currentDateTime();
610 }
611 break;
612 }
613 else {
614
615 // Begin outage threshold
616 // ----------------------
617 if ( _decodeStop.isValid() && _decodeStop.secsTo(QDateTime::currentDateTime()) > _adviseFail * 60 ) {
618 _decodeStop.setDate(QDate());
619 _decodeStop.setTime(QTime());
620 if (_inspSegm>0) {
621 emit(newMessage(_staID + ": Outage failure threshold exceeded"));
622 callScript("Begin_Outage");
623 }
624 }
625 _nextSleep *= 2;
626 if (_nextSleep > 256) {
627 _nextSleep = 256;
628 }
629 _nextSleep += rand() % 6;
630 }
631 }
632 _nextSleep = 1;
633}
634
635// Call advisory notice script
636////////////////////////////////////////////////////////////////////////////
637void bncGetThread::callScript(const char* _comment) {
638 QMutexLocker locker(&_mutex);
639 if (!_adviseScript.isEmpty()) {
640 msleep(1);
641#ifdef WIN32
642 QProcess::startDetached(_adviseScript, QStringList() << _staID << _comment) ;
643#else
644 QProcess::startDetached("nohup", QStringList() << _adviseScript << _staID << _comment) ;
645#endif
646 }
647}
Note: See TracBrowser for help on using the repository browser.