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

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

* empty log message *

File size: 20.1 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 bool wrongEpoch = false;
380 bool decode = true;
381 int numSucc = 0;
382 int secSucc = 0;
383 int secFail = 0;
384 int initPause = 30;
385 int currPause = 0;
386 bool begCorrupt = false;
387 bool endCorrupt = false;
388
389 _decodeTime = QDateTime::currentDateTime();
390 _decodeSucc = QDateTime::currentDateTime();
391 t_irc irc = initRun();
392
393 if (irc == fatal) {
394 QThread::exit(1);
395 return;
396 }
397 else if (irc != success) {
398 emit(newMessage(_staID + ": initRun failed, reconnecting"));
399 tryReconnect();
400 }
401
402 if (initPause < _inspSegm) {
403 initPause = _inspSegm;
404 }
405 currPause = initPause;
406
407 // Read Incoming Data
408 // ------------------
409 while (true) {
410 try {
411 if (_socket->state() != QAbstractSocket::ConnectedState) {
412 emit(newMessage(_staID + ": Socket not connected, reconnecting"));
413 tryReconnect();
414 }
415
416 QListIterator<p_obs> it(_decoder->_obsList);
417 while (it.hasNext()) {
418 delete it.next();
419 }
420 _decoder->_obsList.clear();
421
422 _socket->waitForReadyRead(_timeOut);
423 qint64 nBytes = _socket->bytesAvailable();
424 if (nBytes > 0) {
425 emit newBytes(_staID, nBytes);
426
427 char* data = new char[nBytes];
428 _socket->read(data, nBytes);
429
430 if (_inspSegm<1) {
431 _decoder->Decode(data, nBytes);
432 }
433 else {
434
435 // Decode data
436 // -----------
437 if (!_decodePause.isValid() ||
438 _decodePause.secsTo(QDateTime::currentDateTime()) >= currPause ) {
439
440 if (decode) {
441 if ( _decoder->Decode(data, nBytes) == success ) {
442 numSucc += 1;
443 }
444 if ( _decodeTime.secsTo(QDateTime::currentDateTime()) > _inspSegm ) {
445 decode = false;
446 }
447 }
448
449 // Check - once per inspect segment
450 // --------------------------------
451 if (!decode) {
452 _decodeTime = QDateTime::currentDateTime();
453 if (numSucc>0) {
454 secSucc += _inspSegm;
455 _decodeSucc = QDateTime::currentDateTime();
456 if (secSucc > _adviseReco * 60) {
457 secSucc = _adviseReco * 60 + 1;
458 }
459 numSucc = 0;
460 currPause = initPause;
461 _decodePause.setDate(QDate());
462 _decodePause.setTime(QTime());
463 }
464 else {
465 secFail += _inspSegm;
466 secSucc = 0;
467 if (secFail > _adviseFail * 60) {
468 secFail = _adviseFail * 60 + 1;
469 }
470 if (!_decodePause.isValid()) {
471 _decodePause = QDateTime::currentDateTime();
472 }
473 else {
474 _decodePause.setDate(QDate());
475 _decodePause.setTime(QTime());
476 secFail = secFail + currPause - _inspSegm;
477 currPause = currPause * 2;
478 if (currPause > 960) {
479 currPause = 960;
480 }
481 }
482 }
483
484 // End corrupt threshold
485 // ---------------------
486 if ( begCorrupt && !endCorrupt && secSucc > _adviseReco * 60 ) {
487 _endDateCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
488 _endTimeCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
489 emit(newMessage((_staID + ": Recovery threshold exceeded, corruption ended " + _endDateCor + " " + _endTimeCor).toAscii()));
490 callScript(("End_Corrupted " + _endDateCor + " " + _endTimeCor + " Begin was " + _begDateCor + " " + _begTimeCor).toAscii());
491 endCorrupt = true;
492 begCorrupt = false;
493 secFail = 0;
494 }
495 else {
496
497 // Begin corrupt threshold
498 // -----------------------
499 if ( !begCorrupt && secFail > _adviseFail * 60 ) {
500 _begDateCor = _decodeSucc.toUTC().date().toString("yy-MM-dd");
501 _begTimeCor = _decodeSucc.toUTC().time().toString("hh:mm:ss");
502 emit(newMessage((_staID + ": Failure threshold exceeded, corrupted since " + _begDateCor + " " + _begTimeCor).toAscii()));
503 callScript(("Begin_Corrupted " + _begDateCor + " " + _begTimeCor).toAscii());
504 begCorrupt = true;
505 endCorrupt = false;
506 secSucc = 0;
507 numSucc = 0;
508 }
509 }
510 decode = true;
511 }
512 }
513 }
514
515 // End outage threshold
516 // --------------------
517 if ( _decodeStart.isValid() && _decodeStart.secsTo(QDateTime::currentDateTime()) > _adviseReco * 60 ) {
518 _decodeStart.setDate(QDate());
519 _decodeStart.setTime(QTime());
520 if (_inspSegm>0) {
521 _endDateOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
522 _endTimeOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
523 emit(newMessage((_staID + ": Recovery threshold exceeded, outage ended " + _endDateOut + " " + _endTimeOut).toAscii()));
524 callScript(("End_Outage " + _endDateOut + " " + _endTimeOut + " Begin was " + _begDateOut + " " + _begTimeOut).toAscii());
525 }
526 }
527
528 delete [] data;
529
530 QListIterator<p_obs> it(_decoder->_obsList);
531 while (it.hasNext()) {
532 p_obs obs = it.next();
533
534 // Check observation epoch
535 // -----------------------
536 int week;
537 double sec;
538 currentGPSWeeks(week, sec);
539
540 const double secPerWeek = 7.0 * 24.0 * 3600.0;
541 const double maxDt = 600.0;
542
543 if (week < obs->_o.GPSWeek) {
544 week += 1;
545 sec -= secPerWeek;
546 }
547 if (week > obs->_o.GPSWeek) {
548 week -= 1;
549 sec += secPerWeek;
550 }
551 double dt = fabs(sec - obs->_o.GPSWeeks);
552 if (week != obs->_o.GPSWeek || dt > maxDt) {
553 if (!wrongEpoch) {
554 emit( newMessage(_staID + ": Wrong observation epoch") );
555 wrongEpoch = true;
556 }
557 delete obs;
558 continue;
559 }
560 else {
561 wrongEpoch = false;
562 }
563
564 // RINEX Output
565 // ------------
566 if (_rnx) {
567 long iSec = long(floor(obs->_o.GPSWeeks+0.5));
568 long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
569 if (_samplingRate == 0 || iSec % _samplingRate == 0) {
570 _rnx->deepCopy(obs);
571 }
572 _rnx->dumpEpoch(newTime);
573 }
574
575 bool firstObs = (obs == _decoder->_obsList.first());
576 obs->_status = t_obs::posted;
577 emit newObs(_staID, firstObs, obs);
578 }
579 _decoder->_obsList.clear();
580 }
581 else {
582 emit(newMessage(_staID + ": Data Timeout, reconnecting"));
583 tryReconnect();
584 }
585 }
586 catch (const char* msg) {
587 emit(newMessage(_staID + msg));
588 tryReconnect();
589 }
590 }
591}
592
593// Exit
594////////////////////////////////////////////////////////////////////////////
595void bncGetThread::exit(int exitCode) {
596 if (exitCode!= 0) {
597 emit error(_staID);
598 }
599 QThread::exit(exitCode);
600 terminate();
601}
602
603// Try Re-Connect
604////////////////////////////////////////////////////////////////////////////
605void bncGetThread::tryReconnect() {
606 if (_rnx) {
607 _rnx->setReconnectFlag(true);
608 }
609 if ( !_decodeStart.isValid()) {
610 _decodeStop = QDateTime::currentDateTime();
611 }
612 while (1) {
613 delete _socket; _socket = 0;
614 sleep(_nextSleep);
615 if ( initRun() == success ) {
616 if ( !_decodeStop.isValid()) {
617 _decodeStart = QDateTime::currentDateTime();
618 }
619 break;
620 }
621 else {
622
623 // Begin outage threshold
624 // ----------------------
625 if ( _decodeStop.isValid() && _decodeStop.secsTo(QDateTime::currentDateTime()) > _adviseFail * 60 ) {
626 _decodeStop.setDate(QDate());
627 _decodeStop.setTime(QTime());
628 if (_inspSegm>0) {
629 _begDateOut = _decodeTime.toUTC().date().toString("yy-MM-dd");
630 _begTimeOut = _decodeTime.toUTC().time().toString("hh:mm:ss");
631 emit(newMessage((_staID + ": Failure threshold exceeded, outage since " + _begDateOut + " " + _begTimeOut).toAscii()));
632 callScript(("Begin_Outage " + _begDateOut + " " + _begTimeOut).toAscii());
633 }
634 }
635 _nextSleep *= 2;
636 if (_nextSleep > 256) {
637 _nextSleep = 256;
638 }
639 _nextSleep += rand() % 6;
640 }
641 }
642 _nextSleep = 1;
643}
644
645// Call advisory notice script
646////////////////////////////////////////////////////////////////////////////
647void bncGetThread::callScript(const char* _comment) {
648 QMutexLocker locker(&_mutex);
649 if (!_adviseScript.isEmpty()) {
650 msleep(1);
651#ifdef WIN32
652 QProcess::startDetached(_adviseScript, QStringList() << _staID << _comment) ;
653#else
654 QProcess::startDetached("nohup", QStringList() << _adviseScript << _staID << _comment) ;
655#endif
656 }
657}
Note: See TracBrowser for help on using the repository browser.