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

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