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

Last change on this file since 10239 was 10238, checked in by stuerze, 14 months ago

minor changes

File size: 18.2 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);
147 while(it.hasNext()){
148 bncGetThread* thread = it.next();
[7854]149 disconnect(thread, 0, 0, 0);
150 _staIDs.removeAll(thread->staID());
151 _threads.removeAll(thread);
[1525]152 thread->terminate();
[186]153 }
[35]154 delete _out;
155 delete _outFile;
[187]156 delete _server;
[631]157 delete _sockets;
[1222]158 delete _uServer;
159 delete _uSockets;
[5647]160 delete _miscServer;
161 delete _miscSockets;
[35]162}
163
164// New Observations
165////////////////////////////////////////////////////////////////////////////
[6137]166void bncCaster::slotNewObs(const QByteArray staID, QList<t_satObs> obsList) {
[35]167
[243]168 QMutexLocker locker(&_mutex);
169
[5528]170 reopenOutFile();
171
[5527]172 unsigned index = 0;
[6137]173 QMutableListIterator<t_satObs> it(obsList);
[5524]174 while (it.hasNext()) {
[5527]175 ++index;
[6137]176 t_satObs& obs = it.next();
[2833]177
[5524]178 // Rename the Station
179 // ------------------
[6137]180 obs._staID = staID.data();
[7681]181
[9016]182 // Update/Set Slip Counters
183 // ------------------------
[9088]184 setSlipCounters(obs);
[9020]185
[5524]186 // Output into the socket
187 // ----------------------
188 if (_uSockets) {
[7681]189
[5524]190 ostringstream oStr;
191 oStr.setf(ios::showpoint | ios::fixed);
[7681]192 oStr << obs._staID << " "
[6137]193 << setw(4) << obs._time.gpsw() << " "
194 << setw(14) << setprecision(7) << obs._time.gpssec() << " "
[8621]195 << bncRinex::asciiSatLine(obs,_outLockTime) << endl;
[7681]196
[5524]197 string hlpStr = oStr.str();
[7681]198
[5524]199 QMutableListIterator<QTcpSocket*> is(*_uSockets);
200 while (is.hasNext()) {
201 QTcpSocket* sock = is.next();
202 if (sock->state() == QAbstractSocket::ConnectedState) {
203 int numBytes = hlpStr.length();
204 if (myWrite(sock, hlpStr.c_str(), numBytes) != numBytes) {
205 delete sock;
206 is.remove();
207 }
208 }
209 else if (sock->state() != QAbstractSocket::ConnectingState) {
[1222]210 delete sock;
211 is.remove();
212 }
213 }
214 }
[7681]215
[6449]216 // First time: set the _lastDumpTime
217 // ---------------------------------
218 if (!_lastDumpTime.valid()) {
219 _lastDumpTime = obs._time - 1.0;
[5524]220 }
[7681]221
[5524]222 // An old observation - throw it away
223 // ----------------------------------
[6449]224 if (obs._time <= _lastDumpTime) {
[5527]225 if (index == 1) {
226 bncSettings settings;
[7681]227 if ( !settings.value("outFile").toString().isEmpty() ||
228 !settings.value("outPort").toString().isEmpty() ) {
[8670]229 emit( newMessage(QString("%1: Old or erroneous observation epoch %2 thrown away from %3")
230 .arg(staID.data())
231 .arg(string(obs._time).c_str())
232 .arg(obs._prn.toString().c_str())
[8678]233 .toLatin1(), true) );
[5527]234 }
[257]235 }
[5527]236 continue;
[181]237 }
[7681]238
[5524]239 // Save the observation
240 // --------------------
[7681]241 _epochs[obs._time].append(obs);
[35]242
[5527]243 // Dump Epochs
244 // -----------
[9107]245 if ((obs._time - _outWait) > _lastDumpTime) {
246 dumpEpochs(obs._time - _outWait);
247 _lastDumpTime = obs._time - _outWait;
[5527]248 }
[252]249 }
[35]250}
251
252// New Connection
253////////////////////////////////////////////////////////////////////////////
254void bncCaster::slotNewConnection() {
255 _sockets->push_back( _server->nextPendingConnection() );
[1450]256 emit( newMessage(QString("New client connection on sync port: # %1")
[8127]257 .arg(_sockets->size()).toLatin1(), true) );
[35]258}
259
[1222]260void bncCaster::slotNewUConnection() {
261 _uSockets->push_back( _uServer->nextPendingConnection() );
[1450]262 emit( newMessage(QString("New client connection on usync port: # %1")
[8127]263 .arg(_uSockets->size()).toLatin1(), true) );
[1222]264}
265
[35]266// Add New Thread
267////////////////////////////////////////////////////////////////////////////
[2528]268void bncCaster::addGetThread(bncGetThread* getThread, bool noNewThread) {
[624]269
[6137]270 qRegisterMetaType<t_satObs>("t_satObs");
271 qRegisterMetaType< QList<t_satObs> >("QList<t_satObs>");
[624]272
[6137]273 connect(getThread, SIGNAL(newObs(QByteArray, QList<t_satObs>)),
274 this, SLOT(slotNewObs(QByteArray, QList<t_satObs>)));
[463]275
[6137]276 connect(getThread, SIGNAL(newObs(QByteArray, QList<t_satObs>)),
277 this, SIGNAL(newObs(QByteArray, QList<t_satObs>)));
[5722]278
[5648]279 connect(getThread, SIGNAL(newRawData(QByteArray, QByteArray)),
280 this, SLOT(slotNewRawData(QByteArray, QByteArray)));
281
[7681]282 connect(getThread, SIGNAL(getThreadFinished(QByteArray)),
[1556]283 this, SLOT(slotGetThreadFinished(QByteArray)));
[35]284
[88]285 _staIDs.push_back(getThread->staID());
[186]286 _threads.push_back(getThread);
[1170]287
[2528]288 if (noNewThread) {
289 getThread->run();
290 }
291 else {
292 getThread->start();
293 }
[35]294}
295
[1556]296// Get Thread destroyed
[35]297////////////////////////////////////////////////////////////////////////////
[1556]298void bncCaster::slotGetThreadFinished(QByteArray staID) {
[243]299 QMutexLocker locker(&_mutex);
[1560]300
301 QListIterator<bncGetThread*> it(_threads);
302 while (it.hasNext()) {
303 bncGetThread* thread = it.next();
304 if (thread->staID() == staID) {
305 _threads.removeOne(thread);
306 }
307 }
308
[88]309 _staIDs.removeAll(staID);
[82]310 emit( newMessage(
[8127]311 QString("Decoding %1 stream(s)").arg(_staIDs.size()).toLatin1(), true) );
[88]312 if (_staIDs.size() == 0) {
[1450]313 emit(newMessage("bncCaster: Last get thread terminated", true));
[1556]314 emit getThreadsFinished();
[35]315 }
316}
317
318// Dump Complete Epochs
319////////////////////////////////////////////////////////////////////////////
[6449]320void bncCaster::dumpEpochs(const bncTime& maxTime) {
[35]321
[6449]322 QMutableMapIterator<bncTime, QList<t_satObs> > itEpo(_epochs);
323 while (itEpo.hasNext()) {
324 itEpo.next();
325 const bncTime& epoTime = itEpo.key();
326 if (epoTime <= maxTime) {
327 const QList<t_satObs>& allObs = itEpo.value();
[8372]328 int sec = int(nint(epoTime.gpssec()*10));
[8397]329 if ( (_out || _sockets) && (sec % (_samplingRateMult10) == 0) ) {
[6449]330 QListIterator<t_satObs> it(allObs);
331 bool firstObs = true;
332 while (it.hasNext()) {
333 const t_satObs& obs = it.next();
[7681]334
[6449]335 ostringstream oStr;
336 oStr.setf(ios::showpoint | ios::fixed);
[7681]337 if (firstObs) {
[6449]338 firstObs = false;
[7681]339 oStr << "> " << obs._time.gpsw() << ' '
[7180]340 << setprecision(7) << obs._time.gpssec() << endl;
[6449]341 }
[8617]342 oStr << obs._staID << ' '
[8621]343 << bncRinex::asciiSatLine(obs,_outLockTime) << endl;
[7681]344 if (!it.hasNext()) {
[6449]345 oStr << endl;
346 }
347 string hlpStr = oStr.str();
[7681]348
[6449]349 // Output into the File
350 // --------------------
351 if (_out) {
352 *_out << hlpStr.c_str();
353 _out->flush();
354 }
[7681]355
[6449]356 // Output into the socket
357 // ----------------------
358 if (_sockets) {
359 QMutableListIterator<QTcpSocket*> is(*_sockets);
360 while (is.hasNext()) {
361 QTcpSocket* sock = is.next();
362 if (sock->state() == QAbstractSocket::ConnectedState) {
[7681]363 int numBytes = hlpStr.length();
[6449]364 if (myWrite(sock, hlpStr.c_str(), numBytes) != numBytes) {
365 delete sock;
366 is.remove();
367 }
368 }
369 else if (sock->state() != QAbstractSocket::ConnectingState) {
[637]370 delete sock;
371 is.remove();
372 }
[140]373 }
374 }
375 }
[73]376 }
[9188]377 //_epochs.remove(epoTime);
378 itEpo.remove();
[35]379 }
380 }
381}
[1170]382
[4250]383// Reread configuration (private slot)
[1170]384////////////////////////////////////////////////////////////////////////////
[1179]385void bncCaster::slotReadMountPoints() {
[1170]386
[4252]387 bncSettings settings;
388 settings.reRead();
389
[4250]390 readMountPoints();
391}
392
393// Read Mountpoints
394////////////////////////////////////////////////////////////////////////////
395void bncCaster::readMountPoints() {
396
[1535]397 bncSettings settings;
[1170]398
399 // Reread several options
400 // ----------------------
[8397]401 _samplingRateMult10 = int(settings.value("outSampl").toString().split("sec").first().toDouble() * 10.0);
[10238]402
403 _outWait = settings.value("outWait").toDouble();
404 if (_outWait <= 0.0) {
405 _outWait = 0.01;
[1170]406 }
407
408 // Add new mountpoints
409 // -------------------
410 int iMount = -1;
411 QListIterator<QString> it(settings.value("mountPoints").toStringList());
412 while (it.hasNext()) {
413 ++iMount;
414 QStringList hlp = it.next().split(" ");
[7182]415 if (hlp.size() < 7) continue;
[1170]416 QUrl url(hlp[0]);
417
418 // Does it already exist?
419 // ----------------------
420 bool existFlg = false;
421 QListIterator<bncGetThread*> iTh(_threads);
422 while (iTh.hasNext()) {
423 bncGetThread* thread = iTh.next();
424 if (thread->mountPoint() == url) {
425 existFlg = true;
426 break;
427 }
428 }
429
430 // New bncGetThread
431 // ----------------
432 if (!existFlg) {
[8127]433 QByteArray format = hlp[1].toLatin1();
434 QByteArray latitude = hlp[3].toLatin1();
435 QByteArray longitude = hlp[4].toLatin1();
436 QByteArray nmea = hlp[5].toLatin1();
437 QByteArray ntripVersion = hlp[6].toLatin1();
[7681]438
439 bncGetThread* getThread = new bncGetThread(url, format, latitude,
[3003]440 longitude, nmea, ntripVersion);
[1170]441 addGetThread(getThread);
[7180]442
[1170]443 }
444 }
445
446 // Remove mountpoints
447 // ------------------
448 QListIterator<bncGetThread*> iTh(_threads);
449 while (iTh.hasNext()) {
450 bncGetThread* thread = iTh.next();
451
452 bool existFlg = false;
453 QListIterator<QString> it(settings.value("mountPoints").toStringList());
454 while (it.hasNext()) {
455 QStringList hlp = it.next().split(" ");
456 if (hlp.size() <= 1) continue;
457 QUrl url(hlp[0]);
458
459 if (thread->mountPoint() == url) {
460 existFlg = true;
461 break;
462 }
463 }
464
465 if (!existFlg) {
466 disconnect(thread, 0, 0, 0);
467 _staIDs.removeAll(thread->staID());
468 _threads.removeAll(thread);
469 thread->terminate();
470 }
471 }
472
[1179]473 emit mountPointsRead(_threads);
[9854]474 emit(newMessage(QString("Configuration read: " + BNC_CORE->confFileName() + ", %1 stream(s)").arg(_threads.count()).toLatin1(), true));
[1176]475
[1170]476 // (Re-) Start the configuration timer
477 // -----------------------------------
[8119]478 if (settings.value("onTheFlyInterval").toString() == "no") {
479 return;
480 }
481
[1170]482 int ms = 0;
[1705]483 if (_confInterval != -1) {
[1170]484 ms = 1000 * _confInterval;
485 }
486 else {
487 QTime currTime = currentDateAndTimeGPS().time();
488 QTime nextShotTime;
489 if (settings.value("onTheFlyInterval").toString() == "1 min") {
[9124]490 _confInterval = 60;
491 nextShotTime = QTime(currTime.hour(), currTime.minute()+1, 0);
[1170]492 }
[4536]493 else if (settings.value("onTheFlyInterval").toString() == "5 min") {
494 _confInterval = 300;
495 nextShotTime = QTime(currTime.hour(), currTime.minute()+5, 0);
496 }
[1170]497 else if (settings.value("onTheFlyInterval").toString() == "1 hour") {
498 _confInterval = 3600;
499 nextShotTime = QTime(currTime.hour()+1, 0, 0);
500 }
501 else {
502 _confInterval = 86400;
503 nextShotTime = QTime(23, 59, 59, 999);
504 }
505
506 ms = currTime.msecsTo(nextShotTime);
[1176]507 if (ms < 30000) {
508 ms = 30000;
509 }
[1170]510 }
511
[1705]512 QTimer::singleShot(ms, this, SLOT(slotReadMountPoints()));
[1170]513}
[1182]514
[7681]515//
[1182]516////////////////////////////////////////////////////////////////////////////
517int bncCaster::myWrite(QTcpSocket* sock, const char* buf, int bufLen) {
[1229]518 sock->write(buf, bufLen);
519 for (int ii = 1; ii <= 10; ii++) {
520 if (sock->waitForBytesWritten(10)) { // wait 10 ms
521 return bufLen;
[1182]522 }
523 }
[1229]524 return -1;
[1182]525}
[2182]526
[7681]527//
[2182]528////////////////////////////////////////////////////////////////////////////
[5528]529void bncCaster::reopenOutFile() {
530
531 bncSettings settings;
532
533 QString outFileName = settings.value("outFile").toString();
534 if ( !outFileName.isEmpty() ) {
535 expandEnvVar(outFileName);
536 if (!_outFile || _outFile->fileName() != outFileName) {
537 delete _out;
538 delete _outFile;
[7681]539 _outFile = new QFile(outFileName);
[5528]540 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
541 _outFile->open(QIODevice::WriteOnly | QIODevice::Append);
542 }
543 else {
544 _outFile->open(QIODevice::WriteOnly);
545 }
546 _out = new QTextStream(_outFile);
547 _out->setRealNumberNotation(QTextStream::FixedNotation);
548 }
549 }
550 else {
551 delete _out; _out = 0;
552 delete _outFile; _outFile = 0;
553 }
554}
555
[5649]556// Output into the Miscellaneous socket
[5648]557////////////////////////////////////////////////////////////////////////////
558void bncCaster::slotNewRawData(QByteArray staID, QByteArray data) {
559 if (_miscSockets && (_miscMount == "ALL" || _miscMount == staID)) {
560 QMutableListIterator<QTcpSocket*> is(*_miscSockets);
561 while (is.hasNext()) {
562 QTcpSocket* sock = is.next();
563 if (sock->state() == QAbstractSocket::ConnectedState) {
564 sock->write(data);
[7681]565 }
[5648]566 else if (sock->state() != QAbstractSocket::ConnectingState) {
567 delete sock;
568 is.remove();
[7681]569 }
570 }
571 }
[5648]572}
[5647]573
[5649]574// New Connection
[5647]575////////////////////////////////////////////////////////////////////////////
576void bncCaster::slotNewMiscConnection() {
577 _miscSockets->push_back( _miscServer->nextPendingConnection() );
578 emit( newMessage(QString("New client connection on Miscellaneous Output Port: # %1")
[8127]579 .arg(_miscSockets->size()).toLatin1(), true) );
[5647]580}
[9016]581
[9020]582// Set/Update Slip Counters
[9016]583////////////////////////////////////////////////////////////////////////////
584void bncCaster::setSlipCounters(t_satObs& obs) {
585
586 double minLockTime = -1.0;
587 for (unsigned ii = 0; ii < obs._obs.size(); ii++) {
588 const t_frqObs* frqObs = obs._obs[ii];
589 if (frqObs->_lockTimeValid) {
590 if (minLockTime == -1.0 || minLockTime < frqObs->_lockTime) {
591 minLockTime = frqObs->_lockTime;
592 }
593 }
594 }
595
596 if (minLockTime == -1.0) {
597 return;
598 }
599
[9019]600 QMap<t_prn, double>& ltMap = _lockTimeMap[obs._staID];
601 QMap<t_prn, int>& jcMap = _jumpCounterMap[obs._staID];
602 QMap<t_prn, double>::const_iterator it = ltMap.find(obs._prn);
[9096]603 if (it == ltMap.end()) { // init satellite
[9019]604 ltMap[obs._prn] = minLockTime;
605 jcMap[obs._prn] = 0;
[9016]606 }
607 else {
[9096]608 if (minLockTime < ltMap[obs._prn]) {
[9019]609 jcMap[obs._prn] += 1;
610 if (jcMap[obs._prn] > 9999) {
611 jcMap[obs._prn] = 0;
[9016]612 }
613 }
[9096]614 ltMap[obs._prn] = minLockTime;
[9016]615 }
[9019]616 for (unsigned ii = 0; ii < obs._obs.size(); ii++) {
617 t_frqObs* frqObs = obs._obs[ii];
[9020]618 frqObs->_slipCounter = jcMap[obs._prn];
[9019]619 }
[9016]620}
Note: See TracBrowser for help on using the repository browser.