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

Last change on this file since 10911 was 10911, checked in by stuerze, 4 months ago

minor changes to solve an issue under windows operation system

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