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

Last change on this file since 10792 was 10753, checked in by stuerze, 3 months ago

NtripSever functionallity added

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