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

Last change on this file since 8119 was 8119, checked in by stuerze, 7 years ago

minor changes to switch of the re-read functionlality as it is requitred in command line mode

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