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

Last change on this file since 1022 was 1022, checked in by mervart, 16 years ago

* empty log message *

File size: 23.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 _makePause = false;
118 if ( Qt::CheckState(settings.value("makePause").toInt()) == Qt::Checked) {_makePause = true; }
119 _adviseScript = settings.value("adviseScript").toString();
120 expandEnvVar(_adviseScript);
121
122 // Latency interval/average
123 // ------------------------
124 _perfIntr = 86400;
125 if ( settings.value("perfIntr").toString().isEmpty() ) { _perfIntr = 0; }
126 if ( settings.value("perfIntr").toString().indexOf("1 min") != -1 ) { _perfIntr = 60; }
127 if ( settings.value("perfIntr").toString().indexOf("5 min") != -1 ) { _perfIntr = 300; }
128 if ( settings.value("perfIntr").toString().indexOf("15 min") != -1 ) { _perfIntr = 900; }
129 if ( settings.value("perfIntr").toString().indexOf("1 hour") != -1 ) { _perfIntr = 3600; }
130 if ( settings.value("perfIntr").toString().indexOf("6 hours") != -1 ) { _perfIntr = 21600; }
131 if ( settings.value("perfIntr").toString().indexOf("1 day") != -1 ) { _perfIntr = 86400; }
132
133 // RINEX writer
134 // ------------
135 _samplingRate = settings.value("rnxSampl").toInt();
136 if ( settings.value("rnxPath").toString().isEmpty() ) {
137 _rnx = 0;
138 }
139 else {
140 _rnx = new bncRinex(_staID, mountPoint, format, latitude, longitude, nmea);
141 }
142
143 msleep(100); //sleep 0.1 sec
144}
145
146// Destructor
147////////////////////////////////////////////////////////////////////////////
148bncGetThread::~bncGetThread() {
149 if (_socket) {
150 _socket->close();
151#if QT_VERSION == 0x040203
152 delete _socket;
153#else
154 _socket->deleteLater();
155#endif
156 }
157 delete _decoder;
158 delete _rnx;
159}
160
161#define AGENTVERSION "1.6"
162// Connect to Caster, send the Request (static)
163////////////////////////////////////////////////////////////////////////////
164QTcpSocket* bncGetThread::request(const QUrl& mountPoint,
165 QByteArray& latitude, QByteArray& longitude,
166 QByteArray& nmea, int timeOut,
167 QString& msg) {
168
169 // Connect the Socket
170 // ------------------
171 QSettings settings;
172 QString proxyHost = settings.value("proxyHost").toString();
173 int proxyPort = settings.value("proxyPort").toInt();
174
175 QTcpSocket* socket = new QTcpSocket();
176 if ( proxyHost.isEmpty() ) {
177 socket->connectToHost(mountPoint.host(), mountPoint.port());
178 }
179 else {
180 socket->connectToHost(proxyHost, proxyPort);
181 }
182 if (!socket->waitForConnected(timeOut)) {
183 msg += "Connect timeout\n";
184 delete socket;
185 return 0;
186 }
187
188 // Send Request
189 // ------------
190 QString uName = QUrl::fromPercentEncoding(mountPoint.userName().toAscii());
191 QString passW = QUrl::fromPercentEncoding(mountPoint.password().toAscii());
192 QByteArray userAndPwd;
193
194 if(!uName.isEmpty() || !passW.isEmpty())
195 {
196 userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
197 passW.toAscii()).toBase64() + "\r\n";
198 }
199
200 QUrl hlp;
201 hlp.setScheme("http");
202 hlp.setHost(mountPoint.host());
203 hlp.setPort(mountPoint.port());
204 hlp.setPath(mountPoint.path());
205
206 QByteArray reqStr;
207 if ( proxyHost.isEmpty() ) {
208 if (hlp.path().indexOf("/") != 0) {
209 hlp.setPath("/");
210 }
211 reqStr = "GET " + hlp.path().toAscii() + " HTTP/1.0\r\n";
212 } else {
213 reqStr = "GET " + hlp.toEncoded() + " HTTP/1.0\r\n";
214 }
215 reqStr += "User-Agent: NTRIP BNC/" AGENTVERSION "\r\n"
216 "Host: " + hlp.host().toAscii() + "\r\n"
217 + userAndPwd + "\r\n";
218
219 // NMEA string to handle VRS stream
220 // --------------------------------
221 double lat, lon;
222
223 lat = strtod(latitude,NULL);
224 lon = strtod(longitude,NULL);
225
226 if ((nmea == "yes") && (hlp.path().length() > 2) && (hlp.path().indexOf(".skl") < 0)) {
227 const char* flagN="N";
228 const char* flagE="E";
229 if (lon >180.) {lon=(lon-360.)*(-1.); flagE="W";}
230 if ((lon < 0.) && (lon >= -180.)) {lon=lon*(-1.); flagE="W";}
231 if (lon < -180.) {lon=(lon+360.); flagE="E";}
232 if (lat < 0.) {lat=lat*(-1.); flagN="S";}
233 QTime ttime(QDateTime::currentDateTime().toUTC().time());
234 int lat_deg = (int)lat;
235 double lat_min=(lat-lat_deg)*60.;
236 int lon_deg = (int)lon;
237 double lon_min=(lon-lon_deg)*60.;
238 int hh = 0 , mm = 0;
239 double ss = 0.0;
240 hh=ttime.hour();
241 mm=ttime.minute();
242 ss=(double)ttime.second()+0.001*ttime.msec();
243 QString gga;
244 gga += "GPGGA,";
245 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'));
246 gga += QString("%1%2,").arg((int)lat_deg,2, 10, QLatin1Char('0')).arg(lat_min, 7, 'f', 4, QLatin1Char('0'));
247 gga += flagN;
248 gga += QString(",%1%2,").arg((int)lon_deg,3, 10, QLatin1Char('0')).arg(lon_min, 7, 'f', 4, QLatin1Char('0'));
249 gga += flagE + QString(",1,05,1.00,+00100,M,10.000,M,,");
250 int xori;
251 char XOR = 0;
252 char *Buff =gga.toAscii().data();
253 int iLen = strlen(Buff);
254 for (xori = 0; xori < iLen; xori++) {
255 XOR ^= (char)Buff[xori];
256 }
257 gga += QString("*%1").arg(XOR, 2, 16, QLatin1Char('0'));
258 reqStr += "$";
259 reqStr += gga;
260 reqStr += "\r\n";
261 }
262
263 msg += reqStr;
264
265 socket->write(reqStr, reqStr.length());
266
267 if (!socket->waitForBytesWritten(timeOut)) {
268 msg += "Write timeout\n";
269 delete socket;
270 return 0;
271 }
272
273 return socket;
274}
275
276// Init Run
277////////////////////////////////////////////////////////////////////////////
278t_irc bncGetThread::initRun() {
279
280 // Initialize Socket
281 // -----------------
282 QString msg;
283 _socket = this->request(_mountPoint, _latitude, _longitude,
284 _nmea, _timeOut, msg);
285 if (!_socket) {
286 return failure;
287 }
288
289 // Read Caster Response
290 // --------------------
291 _socket->waitForReadyRead(_timeOut);
292 if (_socket->canReadLine()) {
293 QString line = _socket->readLine();
294
295 // Skip messages from proxy server
296 // -------------------------------
297 if (line.indexOf("ICY 200 OK") == -1 &&
298 line.indexOf("200 OK") != -1 ) {
299 bool proxyRespond = true;
300 while (true) {
301 if (_socket->canReadLine()) {
302 line = _socket->readLine();
303 if (!proxyRespond) {
304 break;
305 }
306 if (line.trimmed().isEmpty()) {
307 proxyRespond = false;
308 }
309 }
310 else {
311 _socket->waitForReadyRead(_timeOut);
312 if (_socket->bytesAvailable() <= 0) {
313 break;
314 }
315 }
316 }
317 }
318
319 if (line.indexOf("Unauthorized") != -1) {
320 QStringList table;
321 bncTableDlg::getFullTable(_mountPoint.host(), _mountPoint.port(), table);
322 QString net;
323 QStringListIterator it(table);
324 while (it.hasNext()) {
325 QString line = it.next();
326 if (line.indexOf("STR") == 0) {
327 QStringList tags = line.split(";");
328 if (tags.at(1) == _staID_orig) {
329 net = tags.at(7);
330 break;
331 }
332 }
333 }
334
335 QString reg;
336 it.toFront();
337 while (it.hasNext()) {
338 QString line = it.next();
339 if (line.indexOf("NET") == 0) {
340 QStringList tags = line.split(";");
341 if (tags.at(1) == net) {
342 reg = tags.at(7);
343 break;
344 }
345 }
346 }
347 emit(newMessage((_staID + ": Caster Response: " + line +
348 " Adjust User-ID and Password Register, see"
349 "\n " + reg).toAscii()));
350 return fatal;
351 }
352 if (line.indexOf("ICY 200 OK") != 0) {
353 emit(newMessage((_staID + ": Wrong Caster Response:\n" + line).toAscii()));
354 return failure;
355 }
356 }
357 else {
358 emit(newMessage(_staID + ": Response Timeout"));
359 return failure;
360 }
361
362 // Instantiate the filter
363 // ----------------------
364 if (!_decoder) {
365 if (_format.indexOf("RTCM_2") != -1) {
366 emit(newMessage("Get Data: " + _staID + " in RTCM 2.x format"));
367 _decoder = new RTCM2Decoder();
368 }
369 else if (_format.indexOf("RTCM_3") != -1) {
370 emit(newMessage("Get Data: " + _staID + " in RTCM 3.x format"));
371 _decoder = new RTCM3Decoder(_staID);
372 connect((RTCM3Decoder*) _decoder, SIGNAL(newMessage(QByteArray)),
373 this, SIGNAL(newMessage(QByteArray)));
374 }
375 else if (_format.indexOf("RTIGS") != -1) {
376 emit(newMessage("Get Data: " + _staID + " in RTIGS format"));
377 _decoder = new RTIGSDecoder();
378 }
379 else if (_format.indexOf("ZERO") != -1) {
380 emit(newMessage("Get Data: " + _staID + " in original format"));
381 _decoder = new bncZeroDecoder(_staID);
382 }
383 else {
384 emit(newMessage(_staID + ": Unknown data format " + _format));
385 return fatal;
386 }
387 }
388 return success;
389}
390
391// Run
392////////////////////////////////////////////////////////////////////////////
393void bncGetThread::run() {
394
395 const double maxDt = 600.0; // Check observation epoch
396 bool wrongEpoch = false;
397 bool decode = true;
398 int numSucc = 0;
399 int secSucc = 0;
400 int secFail = 0;
401 int initPause = 30; // Initial pause for corrupted streams
402 int currPause = 0;
403 bool begCorrupt = false;
404 bool endCorrupt = false;
405 bool followSec = false;
406 int oldSecGPS= 0;
407 int newSecGPS = 0;
408 int numGaps = 0;
409 int diffSecGPS = 0;
410 int numLat = 0;
411 double sumLat = 0.;
412 double meanDiff = 0.;
413 double minLat = maxDt;
414 double maxLat = -maxDt;
415 double curLat = 0.;
416 double leapsec = 14.; // Leap second for latency estimation
417
418 _decodeTime = QDateTime::currentDateTime();
419 _decodeSucc = QDateTime::currentDateTime();
420 t_irc irc = initRun();
421
422 if (irc == fatal) {
423 QThread::exit(1);
424 return;
425 }
426 else if (irc != success) {
427 emit(newMessage(_staID + ": initRun failed, reconnecting"));
428 tryReconnect();
429 }
430
431 if (initPause < _inspSegm) {
432 initPause = _inspSegm;
433 }
434 if(!_makePause) {initPause = 0;}
435 currPause = initPause;
436
437 // Read Incoming Data
438 // ------------------
439 while (true) {
440 try {
441 if (_socket->state() != QAbstractSocket::ConnectedState) {
442 emit(newMessage(_staID + ": Socket not connected, reconnecting"));
443 tryReconnect();
444 }
445
446 QListIterator<p_obs> it(_decoder->_obsList);
447 while (it.hasNext()) {
448 delete it.next();
449 }
450 _decoder->_obsList.clear();
451
452 _socket->waitForReadyRead(_timeOut);
453 qint64 nBytes = _socket->bytesAvailable();
454 if (nBytes > 0) {
455 emit newBytes(_staID, nBytes);
456
457 char* data = new char[nBytes];
458 _socket->read(data, nBytes);
459
460 if (_inspSegm<1) {
461 _decoder->Decode(data, nBytes);
462 }
463 else {
464
465 // Decode data
466 // -----------
467 if (!_decodePause.isValid() ||
468 _decodePause.secsTo(QDateTime::currentDateTime()) >= currPause ) {
469
470 if (decode) {
471 if ( _decoder->Decode(data, nBytes) == success ) {
472 numSucc += 1;
473 }
474 if ( _decodeTime.secsTo(QDateTime::currentDateTime()) > _inspSegm ) {
475 decode = false;
476 }
477 }
478
479 // Check - once per inspect segment
480 // --------------------------------
481 if (!decode) {
482 _decodeTime = QDateTime::currentDateTime();
483 if (numSucc>0) {
484 secSucc += _inspSegm;
485 _decodeSucc = QDateTime::currentDateTime();
486 if (secSucc > _adviseReco * 60) {
487 secSucc = _adviseReco * 60 + 1;
488 }
489 numSucc = 0;
490 currPause = initPause;
491 _decodePause.setDate(QDate());
492 _decodePause.setTime(QTime());
493 }
494 else {
495 secFail += _inspSegm;
496 secSucc = 0;
497 if (secFail > _adviseFail * 60) {
498 secFail = _adviseFail * 60 + 1;
499 }
500 if (!_decodePause.isValid() || !_makePause) {
501 _decodePause = QDateTime::currentDateTime();
502 }
503 else {
504 _decodePause.setDate(QDate());
505 _decodePause.setTime(QTime());
506 secFail = secFail + currPause - _inspSegm;
507 currPause = currPause * 2;
508 if (currPause > 960) {
509 currPause = 960;
510 }
511 }
512 }
513
514 // End corrupt threshold
515 // ---------------------
516 if ( begCorrupt && !endCorrupt && secSucc > _adviseReco * 60 ) {
517 _endDateCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
518 _endTimeCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
519 emit(newMessage((_staID + ": Recovery threshold exceeded, corruption ended " + _endDateCor + " " + _endTimeCor).toAscii()));
520 callScript(("End_Corrupted " + _endDateCor + " " + _endTimeCor + " Begin was " + _begDateCor + " " + _begTimeCor).toAscii());
521 endCorrupt = true;
522 begCorrupt = false;
523 secFail = 0;
524 }
525 else {
526
527 // Begin corrupt threshold
528 // -----------------------
529 if ( !begCorrupt && secFail > _adviseFail * 60 ) {
530 _begDateCor = _decodeSucc.toUTC().date().toString("yy-MM-dd");
531 _begTimeCor = _decodeSucc.toUTC().time().toString("hh:mm:ss");
532 emit(newMessage((_staID + ": Failure threshold exceeded, corrupted since " + _begDateCor + " " + _begTimeCor).toAscii()));
533 callScript(("Begin_Corrupted " + _begDateCor + " " + _begTimeCor).toAscii());
534 begCorrupt = true;
535 endCorrupt = false;
536 secSucc = 0;
537 numSucc = 0;
538 }
539 }
540 decode = true;
541 }
542 }
543 }
544
545 // End outage threshold
546 // --------------------
547 if ( _decodeStart.isValid() && _decodeStart.secsTo(QDateTime::currentDateTime()) > _adviseReco * 60 ) {
548 _decodeStart.setDate(QDate());
549 _decodeStart.setTime(QTime());
550 if (_inspSegm>0) {
551 _endDateOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
552 _endTimeOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
553 emit(newMessage((_staID + ": Recovery threshold exceeded, outage ended " + _endDateOut + " " + _endTimeOut).toAscii()));
554 callScript(("End_Outage " + _endDateOut + " " + _endTimeOut + " Begin was " + _begDateOut + " " + _begTimeOut).toAscii());
555 }
556 }
557
558 delete [] data;
559
560 QListIterator<p_obs> it(_decoder->_obsList);
561 while (it.hasNext()) {
562 p_obs obs = it.next();
563
564 // Check observation epoch
565 // -----------------------
566 int week;
567 double sec;
568 currentGPSWeeks(week, sec);
569 const double secPerWeek = 7.0 * 24.0 * 3600.0;
570
571 if (week < obs->_o.GPSWeek) {
572 week += 1;
573 sec -= secPerWeek;
574 }
575 if (week > obs->_o.GPSWeek) {
576 week -= 1;
577 sec += secPerWeek;
578 }
579 double dt = fabs(sec - obs->_o.GPSWeeks);
580 if (week != obs->_o.GPSWeek || dt > maxDt) {
581 if (!wrongEpoch) {
582 emit( newMessage(_staID + ": Wrong observation epoch(s)") );
583 wrongEpoch = true;
584 }
585 delete obs;
586 continue;
587 }
588 else {
589 wrongEpoch = false;
590
591 // Latency and completeness
592 // ------------------------
593 if (_perfIntr>0) {
594 newSecGPS = static_cast<int>(obs->_o.GPSWeeks);
595 if (newSecGPS != oldSecGPS) {
596 if (newSecGPS % _perfIntr < oldSecGPS % _perfIntr) {
597 if (numLat>0) {
598 if (meanDiff>0.) {
599 emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, %5 epochs, %6 gaps")
600 .arg(_staID.data())
601 .arg(int(sumLat/numLat*100)/100.)
602 .arg(int(minLat*100)/100.)
603 .arg(int(maxLat*100)/100.)
604 .arg(numLat)
605 .arg(numGaps)
606 .toAscii()) );
607 } else {
608 emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, %5 epochs")
609 .arg(_staID.data())
610 .arg(int(sumLat/numLat*100)/100.)
611 .arg(int(minLat*100)/100.)
612 .arg(int(maxLat*100)/100.)
613 .arg(numLat)
614 .toAscii()) );
615 }
616 }
617 meanDiff = diffSecGPS/numLat;
618 diffSecGPS = 0;
619 numGaps = 0;
620 sumLat = 0.;
621 numLat = 0;
622 minLat = maxDt;
623 maxLat = -maxDt;
624 }
625 if (followSec) {
626 diffSecGPS += newSecGPS - oldSecGPS;
627 if (meanDiff>0.) {
628 if (newSecGPS - oldSecGPS > 1.5 * meanDiff) {
629 numGaps += 1;
630 }
631 }
632 }
633 curLat = sec - obs->_o.GPSWeeks + leapsec;
634 sumLat += curLat;
635 if (curLat < minLat) minLat = curLat;
636 if (curLat >= maxLat) maxLat = curLat;
637 numLat += 1;
638 oldSecGPS = newSecGPS;
639 followSec = true;
640 }
641 }
642 }
643
644 // RINEX Output
645 // ------------
646 if (_rnx) {
647 long iSec = long(floor(obs->_o.GPSWeeks+0.5));
648 long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
649 if (_samplingRate == 0 || iSec % _samplingRate == 0) {
650 _rnx->deepCopy(obs);
651 }
652 _rnx->dumpEpoch(newTime);
653 }
654
655 bool firstObs = (obs == _decoder->_obsList.first());
656 obs->_status = t_obs::posted;
657 emit newObs(_staID, firstObs, obs);
658 }
659 _decoder->_obsList.clear();
660 }
661 else {
662 emit(newMessage(_staID + ": Data Timeout, reconnecting"));
663 tryReconnect();
664 }
665 }
666 catch (const char* msg) {
667 emit(newMessage(_staID + msg));
668 tryReconnect();
669 }
670 }
671}
672
673// Exit
674////////////////////////////////////////////////////////////////////////////
675void bncGetThread::exit(int exitCode) {
676 if (exitCode!= 0) {
677 emit error(_staID);
678 }
679 QThread::exit(exitCode);
680 terminate();
681}
682
683// Try Re-Connect
684////////////////////////////////////////////////////////////////////////////
685void bncGetThread::tryReconnect() {
686 if (_rnx) {
687 _rnx->setReconnectFlag(true);
688 }
689 if ( !_decodeStart.isValid()) {
690 _decodeStop = QDateTime::currentDateTime();
691 }
692 while (1) {
693 delete _socket; _socket = 0;
694 sleep(_nextSleep);
695 if ( initRun() == success ) {
696 if ( !_decodeStop.isValid()) {
697 _decodeStart = QDateTime::currentDateTime();
698 }
699 break;
700 }
701 else {
702
703 // Begin outage threshold
704 // ----------------------
705 if ( _decodeStop.isValid() && _decodeStop.secsTo(QDateTime::currentDateTime()) > _adviseFail * 60 ) {
706 _decodeStop.setDate(QDate());
707 _decodeStop.setTime(QTime());
708 if (_inspSegm>0) {
709 _begDateOut = _decodeTime.toUTC().date().toString("yy-MM-dd");
710 _begTimeOut = _decodeTime.toUTC().time().toString("hh:mm:ss");
711 emit(newMessage((_staID + ": Failure threshold exceeded, outage since " + _begDateOut + " " + _begTimeOut).toAscii()));
712 callScript(("Begin_Outage " + _begDateOut + " " + _begTimeOut).toAscii());
713 }
714 }
715 _nextSleep *= 2;
716 if (_nextSleep > 256) {
717 _nextSleep = 256;
718 }
719 _nextSleep += rand() % 6;
720 }
721 }
722 _nextSleep = 1;
723}
724
725// Call advisory notice script
726////////////////////////////////////////////////////////////////////////////
727void bncGetThread::callScript(const char* _comment) {
728 QMutexLocker locker(&_mutex);
729 if (!_adviseScript.isEmpty()) {
730 msleep(1);
731#ifdef WIN32
732 QProcess::startDetached(_adviseScript, QStringList() << _staID << _comment) ;
733#else
734 QProcess::startDetached("nohup", QStringList() << _adviseScript << _staID << _comment) ;
735#endif
736 }
737}
Note: See TracBrowser for help on using the repository browser.