source: ntrip/trunk/BNC/src/bnccaster.cpp@ 10514

Last change on this file since 10514 was 10503, checked in by stuerze, 5 months ago

add an docoder-string ZERO2FILE

File size: 18.1 KB
RevLine 
[280]1// Part of BNC, a utility for retrieving decoding and
[464]2// converting GNSS data streams from NTRIP broadcasters.
[280]3//
[464]4// Copyright (C) 2007
[280]5// German Federal Agency for Cartography and Geodesy (BKG)
6// http://www.bkg.bund.de
[464]7// Czech Technical University Prague, Department of Geodesy
[280]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.
[35]24
25/* -------------------------------------------------------------------------
[93]26 * BKG NTRIP Client
[35]27 * -------------------------------------------------------------------------
28 *
29 * Class: bncCaster
30 *
31 * Purpose: buffers and disseminates the data
32 *
33 * Author: L. Mervart
34 *
35 * Created: 24-Dec-2005
36 *
[7681]37 * Changes:
[35]38 *
39 * -----------------------------------------------------------------------*/
40
[222]41#include <math.h>
[9952]42#ifndef WIN32
[636]43#include <unistd.h>
[9954]44#else
[9958]45#include <windows.h>
[9952]46#endif
[2831]47#include <iostream>
48#include <iomanip>
49#include <sstream>
[222]50
[35]51#include "bnccaster.h"
[2697]52#include "bncrinex.h"
[5070]53#include "bnccore.h"
[35]54#include "bncgetthread.h"
[134]55#include "bncutils.h"
[1535]56#include "bncsettings.h"
[35]57
[2831]58using namespace std;
59
[35]60// Constructor
61////////////////////////////////////////////////////////////////////////////
[5528]62bncCaster::bncCaster() {
[35]63
[1535]64 bncSettings settings;
[275]65
[7681]66 connect(this, SIGNAL(newMessage(QByteArray,bool)),
[5068]67 BNC_CORE, SLOT(slotMessage(const QByteArray,bool)));
[1228]68
[5528]69 _outFile = 0;
70 _out = 0;
71 reopenOutFile();
[35]72
[6447]73 int port = settings.value("outPort").toInt();
[35]74
[6447]75 if (port != 0) {
[35]76 _server = new QTcpServer;
[8921]77 _server->setProxy(QNetworkProxy::NoProxy);
[8917]78 if ( !_server->listen(QHostAddress::Any, port) ) {
[8918]79 QString message = "bncCaster: Cannot listen on sync port "
[8921]80 + QByteArray::number(port) + ": "
81 + _server->errorString();
[8917]82 emit newMessage(message.toLatin1(), true);
[1228]83 }
[35]84 connect(_server, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));
85 _sockets = new QList<QTcpSocket*>;
86 }
87 else {
88 _server = 0;
89 _sockets = 0;
90 }
91
[1222]92 int uPort = settings.value("outUPort").toInt();
93 if (uPort != 0) {
94 _uServer = new QTcpServer;
[8921]95 _uServer->setProxy(QNetworkProxy::NoProxy);
[1228]96 if ( !_uServer->listen(QHostAddress::Any, uPort) ) {
[8918]97 QString message = "bncCaster: Cannot listen on usync port "
[8921]98 + QByteArray::number(uPort) + ": "
99 + _uServer->errorString();
[8917]100 emit newMessage(message.toLatin1(), true);
[1228]101 }
[1222]102 connect(_uServer, SIGNAL(newConnection()), this, SLOT(slotNewUConnection()));
103 _uSockets = new QList<QTcpSocket*>;
104 }
105 else {
106 _uServer = 0;
107 _uSockets = 0;
108 }
109
[10238]110 _outLockTime = settings.value("outLockTime",false).toBool();
[8397]111 _samplingRateMult10 = int(settings.value("outSampl").toString().split("sec").first().toDouble() * 10.0);
[10238]112 _outWait = settings.value("outWait").toDouble();
[9107]113 if (_outWait <= 0.0) {
114 _outWait = 0.01;
[6449]115 }
[1705]116 _confInterval = -1;
[5647]117
[5649]118 // Miscellaneous output port
[5647]119 // -------------------------
120 _miscMount = settings.value("miscMount").toString();
121 _miscPort = settings.value("miscPort").toInt();
122 if (!_miscMount.isEmpty() && _miscPort != 0) {
123 _miscServer = new QTcpServer;
[8921]124 _miscServer->setProxy(QNetworkProxy::NoProxy);
[5647]125 if ( !_miscServer->listen(QHostAddress::Any, _miscPort) ) {
[8918]126 QString message = "bncCaster: Cannot listen on miscellaneous output port "
[8921]127 + QByteArray::number(_miscPort) + ": "
128 + _miscServer->errorString();
[8918]129 emit newMessage(message.toLatin1(), true);
[5647]130 }
131 connect(_miscServer, SIGNAL(newConnection()), this, SLOT(slotNewMiscConnection()));
132 _miscSockets = new QList<QTcpSocket*>;
133 }
134 else {
135 _miscServer = 0;
136 _miscSockets = 0;
137 }
[35]138}
139
140// Destructor
141////////////////////////////////////////////////////////////////////////////
142bncCaster::~bncCaster() {
[2647]143
144 QMutexLocker locker(&_mutex);
145
[186]146 QListIterator<bncGetThread*> it(_threads);
[10355]147
[186]148 while(it.hasNext()){
149 bncGetThread* thread = it.next();
[7854]150 disconnect(thread, 0, 0, 0);
151 _staIDs.removeAll(thread->staID());
152 _threads.removeAll(thread);
[1525]153 thread->terminate();
[186]154 }
[10355]155
[35]156 delete _out;
157 delete _outFile;
[187]158 delete _server;
[631]159 delete _sockets;
[1222]160 delete _uServer;
161 delete _uSockets;
[5647]162 delete _miscServer;
163 delete _miscSockets;
[35]164}
165
166// New Observations
167////////////////////////////////////////////////////////////////////////////
[6137]168void bncCaster::slotNewObs(const QByteArray staID, QList<t_satObs> obsList) {
[35]169
[243]170 QMutexLocker locker(&_mutex);
171
[5528]172 reopenOutFile();
173
[5527]174 unsigned index = 0;
[6137]175 QMutableListIterator<t_satObs> it(obsList);
[5524]176 while (it.hasNext()) {
[5527]177 ++index;
[6137]178 t_satObs& obs = it.next();
[2833]179
[5524]180 // Rename the Station
181 // ------------------
[6137]182 obs._staID = staID.data();
[7681]183
[9016]184 // Update/Set Slip Counters
185 // ------------------------
[9088]186 setSlipCounters(obs);
[9020]187
[5524]188 // Output into the socket
189 // ----------------------
190 if (_uSockets) {
[7681]191
[5524]192 ostringstream oStr;
193 oStr.setf(ios::showpoint | ios::fixed);
[7681]194 oStr << obs._staID << " "
[6137]195 << setw(4) << obs._time.gpsw() << " "
196 << setw(14) << setprecision(7) << obs._time.gpssec() << " "
[8621]197 << bncRinex::asciiSatLine(obs,_outLockTime) << endl;
[7681]198
[5524]199 string hlpStr = oStr.str();
[7681]200
[5524]201 QMutableListIterator<QTcpSocket*> is(*_uSockets);
202 while (is.hasNext()) {
203 QTcpSocket* sock = is.next();
204 if (sock->state() == QAbstractSocket::ConnectedState) {
205 int numBytes = hlpStr.length();
206 if (myWrite(sock, hlpStr.c_str(), numBytes) != numBytes) {
207 delete sock;
208 is.remove();
209 }
210 }
211 else if (sock->state() != QAbstractSocket::ConnectingState) {
[1222]212 delete sock;
213 is.remove();
214 }
215 }
216 }
[7681]217
[6449]218 // First time: set the _lastDumpTime
219 // ---------------------------------
220 if (!_lastDumpTime.valid()) {
221 _lastDumpTime = obs._time - 1.0;
[5524]222 }
[7681]223
[5524]224 // An old observation - throw it away
225 // ----------------------------------
[6449]226 if (obs._time <= _lastDumpTime) {
[5527]227 if (index == 1) {
228 bncSettings settings;
[7681]229 if ( !settings.value("outFile").toString().isEmpty() ||
230 !settings.value("outPort").toString().isEmpty() ) {
[8670]231 emit( newMessage(QString("%1: Old or erroneous observation epoch %2 thrown away from %3")
232 .arg(staID.data())
233 .arg(string(obs._time).c_str())
234 .arg(obs._prn.toString().c_str())
[8678]235 .toLatin1(), true) );
[5527]236 }
[257]237 }
[5527]238 continue;
[181]239 }
[7681]240
[5524]241 // Save the observation
242 // --------------------
[7681]243 _epochs[obs._time].append(obs);
[35]244
[5527]245 // Dump Epochs
246 // -----------
[9107]247 if ((obs._time - _outWait) > _lastDumpTime) {
248 dumpEpochs(obs._time - _outWait);
249 _lastDumpTime = obs._time - _outWait;
[5527]250 }
[252]251 }
[35]252}
253
254// New Connection
255////////////////////////////////////////////////////////////////////////////
256void bncCaster::slotNewConnection() {
257 _sockets->push_back( _server->nextPendingConnection() );
[1450]258 emit( newMessage(QString("New client connection on sync port: # %1")
[8127]259 .arg(_sockets->size()).toLatin1(), true) );
[35]260}
261
[1222]262void bncCaster::slotNewUConnection() {
263 _uSockets->push_back( _uServer->nextPendingConnection() );
[1450]264 emit( newMessage(QString("New client connection on usync port: # %1")
[8127]265 .arg(_uSockets->size()).toLatin1(), true) );
[1222]266}
267
[35]268// Add New Thread
269////////////////////////////////////////////////////////////////////////////
[2528]270void bncCaster::addGetThread(bncGetThread* getThread, bool noNewThread) {
[624]271
[6137]272 qRegisterMetaType<t_satObs>("t_satObs");
273 qRegisterMetaType< QList<t_satObs> >("QList<t_satObs>");
[624]274
[6137]275 connect(getThread, SIGNAL(newObs(QByteArray, QList<t_satObs>)),
276 this, SLOT(slotNewObs(QByteArray, QList<t_satObs>)));
[463]277
[6137]278 connect(getThread, SIGNAL(newObs(QByteArray, QList<t_satObs>)),
279 this, SIGNAL(newObs(QByteArray, QList<t_satObs>)));
[5722]280
[5648]281 connect(getThread, SIGNAL(newRawData(QByteArray, QByteArray)),
282 this, SLOT(slotNewRawData(QByteArray, QByteArray)));
283
[7681]284 connect(getThread, SIGNAL(getThreadFinished(QByteArray)),
[1556]285 this, SLOT(slotGetThreadFinished(QByteArray)));
[35]286
[88]287 _staIDs.push_back(getThread->staID());
[186]288 _threads.push_back(getThread);
[1170]289
[2528]290 if (noNewThread) {
291 getThread->run();
292 }
293 else {
294 getThread->start();
295 }
[35]296}
297
[1556]298// Get Thread destroyed
[35]299////////////////////////////////////////////////////////////////////////////
[1556]300void bncCaster::slotGetThreadFinished(QByteArray staID) {
[10355]301 //QMutexLocker locker(&_mutex);
[1560]302
303 QListIterator<bncGetThread*> it(_threads);
304 while (it.hasNext()) {
305 bncGetThread* thread = it.next();
306 if (thread->staID() == staID) {
307 _threads.removeOne(thread);
308 }
309 }
[88]310 _staIDs.removeAll(staID);
[82]311 emit( newMessage(
[8127]312 QString("Decoding %1 stream(s)").arg(_staIDs.size()).toLatin1(), true) );
[88]313 if (_staIDs.size() == 0) {
[1556]314 emit getThreadsFinished();
[35]315 }
[10355]316
[35]317}
318
319// Dump Complete Epochs
320////////////////////////////////////////////////////////////////////////////
[6449]321void bncCaster::dumpEpochs(const bncTime& maxTime) {
[35]322
[6449]323 QMutableMapIterator<bncTime, QList<t_satObs> > itEpo(_epochs);
324 while (itEpo.hasNext()) {
325 itEpo.next();
326 const bncTime& epoTime = itEpo.key();
327 if (epoTime <= maxTime) {
328 const QList<t_satObs>& allObs = itEpo.value();
[8372]329 int sec = int(nint(epoTime.gpssec()*10));
[8397]330 if ( (_out || _sockets) && (sec % (_samplingRateMult10) == 0) ) {
[6449]331 QListIterator<t_satObs> it(allObs);
332 bool firstObs = true;
333 while (it.hasNext()) {
334 const t_satObs& obs = it.next();
[7681]335
[6449]336 ostringstream oStr;
337 oStr.setf(ios::showpoint | ios::fixed);
[7681]338 if (firstObs) {
[6449]339 firstObs = false;
[7681]340 oStr << "> " << obs._time.gpsw() << ' '
[7180]341 << setprecision(7) << obs._time.gpssec() << endl;
[6449]342 }
[8617]343 oStr << obs._staID << ' '
[8621]344 << bncRinex::asciiSatLine(obs,_outLockTime) << endl;
[7681]345 if (!it.hasNext()) {
[6449]346 oStr << endl;
347 }
348 string hlpStr = oStr.str();
[7681]349
[6449]350 // Output into the File
351 // --------------------
352 if (_out) {
353 *_out << hlpStr.c_str();
354 _out->flush();
355 }
[7681]356
[6449]357 // Output into the socket
358 // ----------------------
359 if (_sockets) {
360 QMutableListIterator<QTcpSocket*> is(*_sockets);
361 while (is.hasNext()) {
362 QTcpSocket* sock = is.next();
363 if (sock->state() == QAbstractSocket::ConnectedState) {
[7681]364 int numBytes = hlpStr.length();
[6449]365 if (myWrite(sock, hlpStr.c_str(), numBytes) != numBytes) {
366 delete sock;
367 is.remove();
368 }
369 }
370 else if (sock->state() != QAbstractSocket::ConnectingState) {
[637]371 delete sock;
372 is.remove();
373 }
[140]374 }
375 }
376 }
[73]377 }
[9188]378 //_epochs.remove(epoTime);
379 itEpo.remove();
[35]380 }
381 }
382}
[1170]383
[4250]384// Reread configuration (private slot)
[1170]385////////////////////////////////////////////////////////////////////////////
[1179]386void bncCaster::slotReadMountPoints() {
[1170]387
[4252]388 bncSettings settings;
389 settings.reRead();
390
[4250]391 readMountPoints();
392}
393
394// Read Mountpoints
395////////////////////////////////////////////////////////////////////////////
396void bncCaster::readMountPoints() {
397
[1535]398 bncSettings settings;
[1170]399
400 // Reread several options
401 // ----------------------
[8397]402 _samplingRateMult10 = int(settings.value("outSampl").toString().split("sec").first().toDouble() * 10.0);
[10238]403
404 _outWait = settings.value("outWait").toDouble();
405 if (_outWait <= 0.0) {
406 _outWait = 0.01;
[1170]407 }
408
409 // Add new mountpoints
410 // -------------------
411 int iMount = -1;
412 QListIterator<QString> it(settings.value("mountPoints").toStringList());
413 while (it.hasNext()) {
414 ++iMount;
415 QStringList hlp = it.next().split(" ");
[7182]416 if (hlp.size() < 7) continue;
[1170]417 QUrl url(hlp[0]);
418
419 // Does it already exist?
420 // ----------------------
421 bool existFlg = false;
422 QListIterator<bncGetThread*> iTh(_threads);
423 while (iTh.hasNext()) {
424 bncGetThread* thread = iTh.next();
425 if (thread->mountPoint() == url) {
426 existFlg = true;
427 break;
428 }
429 }
430
431 // New bncGetThread
432 // ----------------
433 if (!existFlg) {
[8127]434 QByteArray format = hlp[1].toLatin1();
435 QByteArray latitude = hlp[3].toLatin1();
436 QByteArray longitude = hlp[4].toLatin1();
437 QByteArray nmea = hlp[5].toLatin1();
438 QByteArray ntripVersion = hlp[6].toLatin1();
[7681]439
440 bncGetThread* getThread = new bncGetThread(url, format, latitude,
[3003]441 longitude, nmea, ntripVersion);
[1170]442 addGetThread(getThread);
[7180]443
[1170]444 }
445 }
446
447 // Remove mountpoints
448 // ------------------
449 QListIterator<bncGetThread*> iTh(_threads);
450 while (iTh.hasNext()) {
451 bncGetThread* thread = iTh.next();
452
453 bool existFlg = false;
454 QListIterator<QString> it(settings.value("mountPoints").toStringList());
455 while (it.hasNext()) {
456 QStringList hlp = it.next().split(" ");
457 if (hlp.size() <= 1) continue;
458 QUrl url(hlp[0]);
459
460 if (thread->mountPoint() == url) {
461 existFlg = true;
462 break;
463 }
464 }
465
466 if (!existFlg) {
467 disconnect(thread, 0, 0, 0);
468 _staIDs.removeAll(thread->staID());
469 _threads.removeAll(thread);
470 thread->terminate();
471 }
472 }
473
[1179]474 emit mountPointsRead(_threads);
[9854]475 emit(newMessage(QString("Configuration read: " + BNC_CORE->confFileName() + ", %1 stream(s)").arg(_threads.count()).toLatin1(), true));
[1176]476
[1170]477 // (Re-) Start the configuration timer
478 // -----------------------------------
[8119]479 if (settings.value("onTheFlyInterval").toString() == "no") {
480 return;
481 }
482
[1170]483 int ms = 0;
[1705]484 if (_confInterval != -1) {
[1170]485 ms = 1000 * _confInterval;
486 }
487 else {
488 QTime currTime = currentDateAndTimeGPS().time();
489 QTime nextShotTime;
490 if (settings.value("onTheFlyInterval").toString() == "1 min") {
[9124]491 _confInterval = 60;
492 nextShotTime = QTime(currTime.hour(), currTime.minute()+1, 0);
[1170]493 }
[4536]494 else if (settings.value("onTheFlyInterval").toString() == "5 min") {
495 _confInterval = 300;
496 nextShotTime = QTime(currTime.hour(), currTime.minute()+5, 0);
497 }
[1170]498 else if (settings.value("onTheFlyInterval").toString() == "1 hour") {
499 _confInterval = 3600;
500 nextShotTime = QTime(currTime.hour()+1, 0, 0);
501 }
502 else {
503 _confInterval = 86400;
504 nextShotTime = QTime(23, 59, 59, 999);
505 }
506
507 ms = currTime.msecsTo(nextShotTime);
[1176]508 if (ms < 30000) {
509 ms = 30000;
510 }
[1170]511 }
512
[1705]513 QTimer::singleShot(ms, this, SLOT(slotReadMountPoints()));
[1170]514}
[1182]515
[7681]516//
[1182]517////////////////////////////////////////////////////////////////////////////
518int bncCaster::myWrite(QTcpSocket* sock, const char* buf, int bufLen) {
[1229]519 sock->write(buf, bufLen);
520 for (int ii = 1; ii <= 10; ii++) {
521 if (sock->waitForBytesWritten(10)) { // wait 10 ms
522 return bufLen;
[1182]523 }
524 }
[1229]525 return -1;
[1182]526}
[2182]527
[7681]528//
[2182]529////////////////////////////////////////////////////////////////////////////
[5528]530void bncCaster::reopenOutFile() {
531
532 bncSettings settings;
533
534 QString outFileName = settings.value("outFile").toString();
535 if ( !outFileName.isEmpty() ) {
536 expandEnvVar(outFileName);
537 if (!_outFile || _outFile->fileName() != outFileName) {
538 delete _out;
539 delete _outFile;
[7681]540 _outFile = new QFile(outFileName);
[5528]541 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
542 _outFile->open(QIODevice::WriteOnly | QIODevice::Append);
543 }
544 else {
545 _outFile->open(QIODevice::WriteOnly);
546 }
547 _out = new QTextStream(_outFile);
548 _out->setRealNumberNotation(QTextStream::FixedNotation);
549 }
550 }
551 else {
552 delete _out; _out = 0;
553 delete _outFile; _outFile = 0;
554 }
555}
556
[5649]557// Output into the Miscellaneous socket
[5648]558////////////////////////////////////////////////////////////////////////////
559void bncCaster::slotNewRawData(QByteArray staID, QByteArray data) {
560 if (_miscSockets && (_miscMount == "ALL" || _miscMount == staID)) {
561 QMutableListIterator<QTcpSocket*> is(*_miscSockets);
562 while (is.hasNext()) {
563 QTcpSocket* sock = is.next();
564 if (sock->state() == QAbstractSocket::ConnectedState) {
565 sock->write(data);
[7681]566 }
[5648]567 else if (sock->state() != QAbstractSocket::ConnectingState) {
568 delete sock;
569 is.remove();
[7681]570 }
571 }
572 }
[5648]573}
[5647]574
[5649]575// New Connection
[5647]576////////////////////////////////////////////////////////////////////////////
577void bncCaster::slotNewMiscConnection() {
578 _miscSockets->push_back( _miscServer->nextPendingConnection() );
579 emit( newMessage(QString("New client connection on Miscellaneous Output Port: # %1")
[8127]580 .arg(_miscSockets->size()).toLatin1(), true) );
[5647]581}
[9016]582
[9020]583// Set/Update Slip Counters
[9016]584////////////////////////////////////////////////////////////////////////////
585void bncCaster::setSlipCounters(t_satObs& obs) {
586
587 double minLockTime = -1.0;
588 for (unsigned ii = 0; ii < obs._obs.size(); ii++) {
589 const t_frqObs* frqObs = obs._obs[ii];
590 if (frqObs->_lockTimeValid) {
591 if (minLockTime == -1.0 || minLockTime < frqObs->_lockTime) {
592 minLockTime = frqObs->_lockTime;
593 }
594 }
595 }
596
597 if (minLockTime == -1.0) {
598 return;
599 }
600
[9019]601 QMap<t_prn, double>& ltMap = _lockTimeMap[obs._staID];
602 QMap<t_prn, int>& jcMap = _jumpCounterMap[obs._staID];
603 QMap<t_prn, double>::const_iterator it = ltMap.find(obs._prn);
[9096]604 if (it == ltMap.end()) { // init satellite
[9019]605 ltMap[obs._prn] = minLockTime;
606 jcMap[obs._prn] = 0;
[9016]607 }
608 else {
[9096]609 if (minLockTime < ltMap[obs._prn]) {
[9019]610 jcMap[obs._prn] += 1;
611 if (jcMap[obs._prn] > 9999) {
612 jcMap[obs._prn] = 0;
[9016]613 }
614 }
[9096]615 ltMap[obs._prn] = minLockTime;
[9016]616 }
[9019]617 for (unsigned ii = 0; ii < obs._obs.size(); ii++) {
618 t_frqObs* frqObs = obs._obs[ii];
[9020]619 frqObs->_slipCounter = jcMap[obs._prn];
[9019]620 }
[9016]621}
Note: See TracBrowser for help on using the repository browser.