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

Last change on this file since 5993 was 5993, checked in by mervart, 10 years ago
File size: 17.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 *
37 * Changes:
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"
[5738]53#include "GPSDecoder.h"
[35]54
[2831]55using namespace std;
56
[35]57// Constructor
58////////////////////////////////////////////////////////////////////////////
[5528]59bncCaster::bncCaster() {
[35]60
[1535]61 bncSettings settings;
[275]62
[1299]63 connect(this, SIGNAL(newMessage(QByteArray,bool)),
[5068]64 BNC_CORE, SLOT(slotMessage(const QByteArray,bool)));
[1228]65
[5528]66 _outFile = 0;
67 _out = 0;
68 reopenOutFile();
[35]69
[5528]70 _port = settings.value("outPort").toInt();
[35]71
72 if (_port != 0) {
73 _server = new QTcpServer;
[1228]74 if ( !_server->listen(QHostAddress::Any, _port) ) {
[1450]75 emit newMessage("bncCaster: Cannot listen on sync port", true);
[1228]76 }
[35]77 connect(_server, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));
78 _sockets = new QList<QTcpSocket*>;
79 }
80 else {
81 _server = 0;
82 _sockets = 0;
83 }
84
[1222]85 int uPort = settings.value("outUPort").toInt();
86 if (uPort != 0) {
87 _uServer = new QTcpServer;
[1228]88 if ( !_uServer->listen(QHostAddress::Any, uPort) ) {
[1450]89 emit newMessage("bncCaster: Cannot listen on usync port", true);
[1228]90 }
[1222]91 connect(_uServer, SIGNAL(newConnection()), this, SLOT(slotNewUConnection()));
92 _uSockets = new QList<QTcpSocket*>;
93 }
94 else {
95 _uServer = 0;
96 _uSockets = 0;
97 }
98
[5992]99 int nmeaPort = settings.value("PPP/nmeaPort").toInt();
[5993]100 cout << "nmeaPort = " << nmeaPort << endl;
[2183]101 if (nmeaPort != 0) {
102 _nmeaServer = new QTcpServer;
103 if ( !_nmeaServer->listen(QHostAddress::Any, nmeaPort) ) {
104 emit newMessage("bncCaster: Cannot listen on port", true);
105 }
106 connect(_nmeaServer, SIGNAL(newConnection()), this, SLOT(slotNewNMEAConnection()));
107 _nmeaSockets = new QList<QTcpSocket*>;
108 }
109 else {
110 _nmeaServer = 0;
111 _nmeaSockets = 0;
112 }
113
[2711]114 _epochs = new QMultiMap<long, t_obs>;
[35]115
[3543]116 _samplingRate = settings.value("binSampl").toInt();
117 _waitTime = settings.value("waitTime").toInt();
[3034]118 _lastDumpSec = 0;
[1705]119 _confInterval = -1;
[5647]120
[5649]121 // Miscellaneous output port
[5647]122 // -------------------------
123 _miscMount = settings.value("miscMount").toString();
124 _miscPort = settings.value("miscPort").toInt();
125 if (!_miscMount.isEmpty() && _miscPort != 0) {
126 _miscServer = new QTcpServer;
127 if ( !_miscServer->listen(QHostAddress::Any, _miscPort) ) {
128 emit newMessage("bncCaster: Cannot listen on Miscellaneous Output Port", true);
129 }
130 connect(_miscServer, SIGNAL(newConnection()), this, SLOT(slotNewMiscConnection()));
131 _miscSockets = new QList<QTcpSocket*>;
132 }
133 else {
134 _miscServer = 0;
135 _miscSockets = 0;
136 }
[35]137}
138
139// Destructor
140////////////////////////////////////////////////////////////////////////////
141bncCaster::~bncCaster() {
[2647]142
143 QMutexLocker locker(&_mutex);
144
[186]145 QListIterator<bncGetThread*> it(_threads);
146 while(it.hasNext()){
147 bncGetThread* thread = it.next();
[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;
[2186]156 delete _nmeaServer;
157 delete _nmeaSockets;
[2711]158 delete _epochs;
[5647]159 delete _miscServer;
160 delete _miscSockets;
[35]161}
162
163// New Observations
164////////////////////////////////////////////////////////////////////////////
[5524]165void bncCaster::slotNewObs(const QByteArray staID, QList<t_obs> obsList) {
[35]166
[243]167 QMutexLocker locker(&_mutex);
168
[5528]169 reopenOutFile();
170
[5527]171 unsigned index = 0;
[5524]172 QMutableListIterator<t_obs> it(obsList);
173 while (it.hasNext()) {
[5527]174 ++index;
[5524]175 t_obs& obs = it.next();
[2833]176
[5524]177 long iSec = long(floor(obs.GPSWeeks+0.5));
[5527]178 long newTime = obs.GPSWeek * 7*24*3600 + iSec;
[5524]179
180 // Rename the Station
181 // ------------------
182 strncpy(obs.StatID, staID.constData(),sizeof(obs.StatID));
183 obs.StatID[sizeof(obs.StatID)-1] = '\0';
184
185 // Output into the socket
186 // ----------------------
187 if (_uSockets) {
188
189 ostringstream oStr;
190 oStr.setf(ios::showpoint | ios::fixed);
[5526]191 oStr << obs.StatID << " "
192 << setw(4) << obs.GPSWeek << " "
193 << setw(14) << setprecision(7) << obs.GPSWeeks << " "
[5524]194 << bncRinex::asciiSatLine(obs) << endl;
195
196 string hlpStr = oStr.str();
197
198 QMutableListIterator<QTcpSocket*> is(*_uSockets);
199 while (is.hasNext()) {
200 QTcpSocket* sock = is.next();
201 if (sock->state() == QAbstractSocket::ConnectedState) {
202 int numBytes = hlpStr.length();
203 if (myWrite(sock, hlpStr.c_str(), numBytes) != numBytes) {
204 delete sock;
205 is.remove();
206 }
207 }
208 else if (sock->state() != QAbstractSocket::ConnectingState) {
[1222]209 delete sock;
210 is.remove();
211 }
212 }
213 }
[5524]214
215 // First time, set the _lastDumpSec immediately
216 // --------------------------------------------
217 if (_lastDumpSec == 0) {
218 _lastDumpSec = newTime - 1;
219 }
220
221 // An old observation - throw it away
222 // ----------------------------------
223 if (newTime <= _lastDumpSec) {
[5527]224 if (index == 1) {
225 bncSettings settings;
226 if ( !settings.value("outFile").toString().isEmpty() ||
227 !settings.value("outPort").toString().isEmpty() ) {
[5524]228
[5527]229 QTime enomtime = QTime(0,0,0).addSecs(iSec);
[5524]230
[5527]231 emit( newMessage(QString("%1: Old epoch %2 (%3) thrown away")
232 .arg(staID.data()).arg(iSec)
233 .arg(enomtime.toString("HH:mm:ss"))
234 .toAscii(), true) );
235 }
[257]236 }
[5527]237 continue;
[181]238 }
[5524]239
240 // Save the observation
241 // --------------------
242 _epochs->insert(newTime, obs);
[35]243
[5527]244 // Dump Epochs
245 // -----------
246 if (newTime - _waitTime > _lastDumpSec) {
247 dumpEpochs(_lastDumpSec + 1, newTime - _waitTime);
248 _lastDumpSec = newTime - _waitTime;
249 }
[252]250 }
[35]251}
252
253// New Connection
254////////////////////////////////////////////////////////////////////////////
255void bncCaster::slotNewConnection() {
256 _sockets->push_back( _server->nextPendingConnection() );
[1450]257 emit( newMessage(QString("New client connection on sync port: # %1")
[1299]258 .arg(_sockets->size()).toAscii(), true) );
[35]259}
260
[1222]261void bncCaster::slotNewUConnection() {
262 _uSockets->push_back( _uServer->nextPendingConnection() );
[1450]263 emit( newMessage(QString("New client connection on usync port: # %1")
[1299]264 .arg(_uSockets->size()).toAscii(), true) );
[1222]265}
266
[2183]267void bncCaster::slotNewNMEAConnection() {
268 _nmeaSockets->push_back( _nmeaServer->nextPendingConnection() );
269 emit( newMessage(QString("New PPP client on port: # %1")
270 .arg(_nmeaSockets->size()).toAscii(), true) );
271}
272
[35]273// Add New Thread
274////////////////////////////////////////////////////////////////////////////
[2528]275void bncCaster::addGetThread(bncGetThread* getThread, bool noNewThread) {
[624]276
[2711]277 qRegisterMetaType<t_obs>("t_obs");
[5524]278 qRegisterMetaType< QList<t_obs> >("QList<t_obs>");
[2585]279 qRegisterMetaType<gpsephemeris>("gpsephemeris");
280 qRegisterMetaType<glonassephemeris>("glonassephemeris");
[3522]281 qRegisterMetaType<galileoephemeris>("galileoephemeris");
[624]282
[5524]283 connect(getThread, SIGNAL(newObs(QByteArray, QList<t_obs>)),
284 this, SLOT(slotNewObs(QByteArray, QList<t_obs>)));
[463]285
[5722]286 connect(getThread, SIGNAL(newObs(QByteArray, QList<t_obs>)),
[5724]287 this, SIGNAL(newObs(QByteArray, QList<t_obs>)));
[5722]288
[5648]289 connect(getThread, SIGNAL(newRawData(QByteArray, QByteArray)),
290 this, SLOT(slotNewRawData(QByteArray, QByteArray)));
291
[1556]292 connect(getThread, SIGNAL(getThreadFinished(QByteArray)),
293 this, SLOT(slotGetThreadFinished(QByteArray)));
[35]294
[5991]295 connect(BNC_CORE, SIGNAL(newNMEAstr(QByteArray)), this, SLOT(slotNewNMEAstr(QByteArray)));
[2182]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) {
[243]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 }
320
[88]321 _staIDs.removeAll(staID);
[82]322 emit( newMessage(
[1986]323 QString("Decoding %1 stream(s)").arg(_staIDs.size()).toAscii(), true) );
[88]324 if (_staIDs.size() == 0) {
[1450]325 emit(newMessage("bncCaster: Last get thread terminated", true));
[1556]326 emit getThreadsFinished();
[35]327 }
328}
329
330// Dump Complete Epochs
331////////////////////////////////////////////////////////////////////////////
[2316]332void bncCaster::dumpEpochs(long minTime, long maxTime) {
[35]333
[2316]334 for (long sec = minTime; sec <= maxTime; sec++) {
[140]335
[4990]336 if ( (_out || _sockets) &&
337 (_samplingRate == 0 || sec % _samplingRate == 0) ) {
[1999]338
[4990]339 QList<t_obs> allObs = _epochs->values(sec);
340
341 QListIterator<t_obs> it(allObs);
342 bool firstObs = true;
343 while (it.hasNext()) {
344 const t_obs& obs = it.next();
[4987]345
[4990]346 ostringstream oStr;
347 oStr.setf(ios::showpoint | ios::fixed);
348 if (firstObs) {
349 firstObs = false;
350 oStr << "> " << obs.GPSWeek << ' '
351 << setprecision(7) << obs.GPSWeeks << endl;;
352 }
353 oStr << obs.StatID << ' ' << bncRinex::asciiSatLine(obs) << endl;
354 if (!it.hasNext()) {
355 oStr << endl;
356 }
357 string hlpStr = oStr.str();
[35]358
[4990]359 // Output into the File
360 // --------------------
361 if (_out) {
362 *_out << hlpStr.c_str();
363 _out->flush();
364 }
[1810]365
[4990]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) {
373 int numBytes = hlpStr.length();
374 if (myWrite(sock, hlpStr.c_str(), numBytes) != numBytes) {
[637]375 delete sock;
376 is.remove();
377 }
[140]378 }
[4990]379 else if (sock->state() != QAbstractSocket::ConnectingState) {
380 delete sock;
381 is.remove();
382 }
[140]383 }
384 }
[73]385 }
[35]386 }
[4990]387 _epochs->remove(sec);
[35]388 }
389}
[1170]390
[4250]391// Reread configuration (private slot)
[1170]392////////////////////////////////////////////////////////////////////////////
[1179]393void bncCaster::slotReadMountPoints() {
[1170]394
[4252]395 bncSettings settings;
396 settings.reRead();
397
[4250]398 readMountPoints();
399}
400
401// Read Mountpoints
402////////////////////////////////////////////////////////////////////////////
403void bncCaster::readMountPoints() {
404
[1535]405 bncSettings settings;
[1170]406
407 // Reread several options
408 // ----------------------
[2316]409 _samplingRate = settings.value("binSampl").toInt();
410 _waitTime = settings.value("waitTime").toInt();
411 if (_waitTime < 1) {
412 _waitTime = 1;
[1170]413 }
414
415 // Add new mountpoints
416 // -------------------
417 int iMount = -1;
418 QListIterator<QString> it(settings.value("mountPoints").toStringList());
419 while (it.hasNext()) {
420 ++iMount;
421 QStringList hlp = it.next().split(" ");
422 if (hlp.size() <= 1) continue;
423 QUrl url(hlp[0]);
424
425 // Does it already exist?
426 // ----------------------
427 bool existFlg = false;
428 QListIterator<bncGetThread*> iTh(_threads);
429 while (iTh.hasNext()) {
430 bncGetThread* thread = iTh.next();
431 if (thread->mountPoint() == url) {
432 existFlg = true;
433 break;
434 }
435 }
436
437 // New bncGetThread
438 // ----------------
439 if (!existFlg) {
440 QByteArray format = hlp[1].toAscii();
441 QByteArray latitude = hlp[2].toAscii();
442 QByteArray longitude = hlp[3].toAscii();
443 QByteArray nmea = hlp[4].toAscii();
[1353]444 QByteArray ntripVersion = hlp[5].toAscii();
[1170]445
446 bncGetThread* getThread = new bncGetThread(url, format, latitude,
[3003]447 longitude, nmea, ntripVersion);
[1170]448 addGetThread(getThread);
449 }
450 }
451
452 // Remove mountpoints
453 // ------------------
454 QListIterator<bncGetThread*> iTh(_threads);
455 while (iTh.hasNext()) {
456 bncGetThread* thread = iTh.next();
457
458 bool existFlg = false;
459 QListIterator<QString> it(settings.value("mountPoints").toStringList());
460 while (it.hasNext()) {
461 QStringList hlp = it.next().split(" ");
462 if (hlp.size() <= 1) continue;
463 QUrl url(hlp[0]);
464
465 if (thread->mountPoint() == url) {
466 existFlg = true;
467 break;
468 }
469 }
470
471 if (!existFlg) {
472 disconnect(thread, 0, 0, 0);
473 _staIDs.removeAll(thread->staID());
474 _threads.removeAll(thread);
475 thread->terminate();
476 }
477 }
478
[1179]479 emit mountPointsRead(_threads);
[1529]480 emit( newMessage(QString("Configuration read: "
[5068]481 + BNC_CORE->confFileName()
[1529]482 + ", %1 stream(s)")
[1299]483 .arg(_threads.count()).toAscii(), true) );
[1176]484
[1170]485 // (Re-) Start the configuration timer
486 // -----------------------------------
487 int ms = 0;
488
[1705]489 if (_confInterval != -1) {
[1170]490 ms = 1000 * _confInterval;
491 }
492 else {
493 QTime currTime = currentDateAndTimeGPS().time();
494 QTime nextShotTime;
495
496 if (settings.value("onTheFlyInterval").toString() == "1 min") {
497 _confInterval = 60;
498 nextShotTime = QTime(currTime.hour(), currTime.minute()+1, 0);
499 }
[4536]500 else if (settings.value("onTheFlyInterval").toString() == "5 min") {
501 _confInterval = 300;
502 nextShotTime = QTime(currTime.hour(), currTime.minute()+5, 0);
503 }
[1170]504 else if (settings.value("onTheFlyInterval").toString() == "1 hour") {
505 _confInterval = 3600;
506 nextShotTime = QTime(currTime.hour()+1, 0, 0);
507 }
508 else {
509 _confInterval = 86400;
510 nextShotTime = QTime(23, 59, 59, 999);
511 }
512
513 ms = currTime.msecsTo(nextShotTime);
[1176]514 if (ms < 30000) {
515 ms = 30000;
516 }
[1170]517 }
518
[1705]519 QTimer::singleShot(ms, this, SLOT(slotReadMountPoints()));
[1170]520}
[1182]521
522//
523////////////////////////////////////////////////////////////////////////////
524int bncCaster::myWrite(QTcpSocket* sock, const char* buf, int bufLen) {
[1229]525 sock->write(buf, bufLen);
526 for (int ii = 1; ii <= 10; ii++) {
527 if (sock->waitForBytesWritten(10)) { // wait 10 ms
528 return bufLen;
[1182]529 }
530 }
[1229]531 return -1;
[1182]532}
[2182]533
534//
535////////////////////////////////////////////////////////////////////////////
536void bncCaster::slotNewNMEAstr(QByteArray str) {
[2184]537 if (_nmeaSockets) {
538 QMutableListIterator<QTcpSocket*> is(*_nmeaSockets);
539 while (is.hasNext()) {
540 QTcpSocket* sock = is.next();
541 if (sock->state() == QAbstractSocket::ConnectedState) {
542 sock->write(str);
543 }
544 else if (sock->state() != QAbstractSocket::ConnectingState) {
545 delete sock;
546 is.remove();
547 }
548 }
549 }
[2182]550}
[5528]551
552//
553////////////////////////////////////////////////////////////////////////////
554void bncCaster::reopenOutFile() {
555
556 bncSettings settings;
557
558 QString outFileName = settings.value("outFile").toString();
559 if ( !outFileName.isEmpty() ) {
560 expandEnvVar(outFileName);
561 if (!_outFile || _outFile->fileName() != outFileName) {
562 delete _out;
563 delete _outFile;
564 _outFile = new QFile(outFileName);
565 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
566 _outFile->open(QIODevice::WriteOnly | QIODevice::Append);
567 }
568 else {
569 _outFile->open(QIODevice::WriteOnly);
570 }
571 _out = new QTextStream(_outFile);
572 _out->setRealNumberNotation(QTextStream::FixedNotation);
573 }
574 }
575 else {
576 delete _out; _out = 0;
577 delete _outFile; _outFile = 0;
578 }
579}
580
[5649]581// Output into the Miscellaneous socket
[5648]582////////////////////////////////////////////////////////////////////////////
583void bncCaster::slotNewRawData(QByteArray staID, QByteArray data) {
584 if (_miscSockets && (_miscMount == "ALL" || _miscMount == staID)) {
585 QMutableListIterator<QTcpSocket*> is(*_miscSockets);
586 while (is.hasNext()) {
587 QTcpSocket* sock = is.next();
588 if (sock->state() == QAbstractSocket::ConnectedState) {
589 sock->write(data);
590 }
591 else if (sock->state() != QAbstractSocket::ConnectingState) {
592 delete sock;
593 is.remove();
594 }
595 }
596 }
597}
[5647]598
[5649]599// New Connection
[5647]600////////////////////////////////////////////////////////////////////////////
601void bncCaster::slotNewMiscConnection() {
602 _miscSockets->push_back( _miscServer->nextPendingConnection() );
603 emit( newMessage(QString("New client connection on Miscellaneous Output Port: # %1")
604 .arg(_miscSockets->size()).toAscii(), true) );
605}
Note: See TracBrowser for help on using the repository browser.