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

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

minor changes

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