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

Last change on this file since 8916 was 8916, checked in by stuerze, 4 years ago

minor changes

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