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

Last change on this file since 4990 was 4990, checked in by mervart, 11 years ago
File size: 15.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"
[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
[4793]137 if (!settings.value("rtroverMode").toString().isEmpty()) {
[4763]138 _bncRtrover = new t_bncRtrover();
139 _bncRtrover->start();
[4793]140 }
141 else {
142 _bncRtrover = 0;
143 }
[4763]144#endif
[35]145}
146
147// Destructor
148////////////////////////////////////////////////////////////////////////////
149bncCaster::~bncCaster() {
[2647]150
151 QMutexLocker locker(&_mutex);
152
[186]153 QListIterator<bncGetThread*> it(_threads);
154 while(it.hasNext()){
155 bncGetThread* thread = it.next();
[1525]156 thread->terminate();
[186]157 }
[35]158 delete _out;
159 delete _outFile;
[187]160 delete _server;
[631]161 delete _sockets;
[1222]162 delete _uServer;
163 delete _uSockets;
[2186]164 delete _nmeaServer;
165 delete _nmeaSockets;
[2711]166 delete _epochs;
[4765]167#ifdef RTROVER_INTERFACE
[4793]168 if (_bncRtrover) {
169 _bncRtrover->quit();
170 _bncRtrover->deleteLater();
171 }
[4765]172#endif
[35]173}
174
175// New Observations
176////////////////////////////////////////////////////////////////////////////
[2711]177void bncCaster::newObs(const QByteArray staID, bool firstObs, t_obs obs) {
[35]178
[243]179 QMutexLocker locker(&_mutex);
180
[2711]181 long iSec = long(floor(obs.GPSWeeks+0.5));
182 long newTime = obs.GPSWeek * 7*24*3600 + iSec;
[624]183
[2833]184 // Rename the Station
185 // ------------------
186 strncpy(obs.StatID, staID.constData(),sizeof(obs.StatID));
187 obs.StatID[sizeof(obs.StatID)-1] = '\0';
188
[1222]189 // Output into the socket
190 // ----------------------
191 if (_uSockets) {
[2831]192
193 ostringstream oStr;
194 oStr.setf(ios::showpoint | ios::fixed);
195 oStr << obs.StatID << " "
196 << obs.GPSWeek << " "
197 << setprecision(7) << obs.GPSWeeks << " "
[3324]198 << bncRinex::asciiSatLine(obs) << endl;
[2831]199
200 string hlpStr = oStr.str();
201
[1222]202 QMutableListIterator<QTcpSocket*> is(*_uSockets);
203 while (is.hasNext()) {
204 QTcpSocket* sock = is.next();
205 if (sock->state() == QAbstractSocket::ConnectedState) {
[2831]206 int numBytes = hlpStr.length();
207 if (myWrite(sock, hlpStr.c_str(), numBytes) != numBytes) {
[1222]208 delete sock;
209 is.remove();
210 }
211 }
212 else if (sock->state() != QAbstractSocket::ConnectingState) {
213 delete sock;
214 is.remove();
215 }
216 }
217 }
218
[35]219 // First time, set the _lastDumpSec immediately
220 // --------------------------------------------
[2316]221 if (_lastDumpSec == 0) {
222 _lastDumpSec = newTime - 1;
[35]223 }
224
225 // An old observation - throw it away
226 // ----------------------------------
[2316]227 if (newTime <= _lastDumpSec) {
[257]228 if (firstObs) {
[1535]229 bncSettings settings;
[257]230 if ( !settings.value("outFile").toString().isEmpty() ||
231 !settings.value("outPort").toString().isEmpty() ) {
[1810]232
[2316]233 QTime enomtime = QTime(0,0,0).addSecs(iSec);
[1810]234
235 emit( newMessage(QString("%1: Old epoch %2 (%3) thrown away")
[2316]236 .arg(staID.data()).arg(iSec)
[1810]237 .arg(enomtime.toString("HH:mm:ss"))
238 .toAscii(), true) );
[257]239 }
[181]240 }
[350]241 return;
[35]242 }
243
[160]244 // Save the observation
245 // --------------------
[2316]246 _epochs->insert(newTime, obs);
[393]247
[462]248 // Dump Epochs
249 // -----------
[2316]250 if (newTime - _waitTime > _lastDumpSec) {
251 dumpEpochs(_lastDumpSec + 1, newTime - _waitTime);
252 _lastDumpSec = newTime - _waitTime;
[252]253 }
[35]254}
255
256// New Connection
257////////////////////////////////////////////////////////////////////////////
258void bncCaster::slotNewConnection() {
259 _sockets->push_back( _server->nextPendingConnection() );
[1450]260 emit( newMessage(QString("New client connection on sync port: # %1")
[1299]261 .arg(_sockets->size()).toAscii(), true) );
[35]262}
263
[1222]264void bncCaster::slotNewUConnection() {
265 _uSockets->push_back( _uServer->nextPendingConnection() );
[1450]266 emit( newMessage(QString("New client connection on usync port: # %1")
[1299]267 .arg(_uSockets->size()).toAscii(), true) );
[1222]268}
269
[2183]270void bncCaster::slotNewNMEAConnection() {
271 _nmeaSockets->push_back( _nmeaServer->nextPendingConnection() );
272 emit( newMessage(QString("New PPP client on port: # %1")
273 .arg(_nmeaSockets->size()).toAscii(), true) );
274}
275
[35]276// Add New Thread
277////////////////////////////////////////////////////////////////////////////
[2528]278void bncCaster::addGetThread(bncGetThread* getThread, bool noNewThread) {
[624]279
[2711]280 qRegisterMetaType<t_obs>("t_obs");
[2585]281 qRegisterMetaType<gpsephemeris>("gpsephemeris");
282 qRegisterMetaType<glonassephemeris>("glonassephemeris");
[3522]283 qRegisterMetaType<galileoephemeris>("galileoephemeris");
[624]284
[2711]285 connect(getThread, SIGNAL(newObs(QByteArray, bool, t_obs)),
286 this, SLOT(newObs(QByteArray, bool, t_obs)));
[463]287
[4763]288#ifdef RTROVER_INTERFACE
[4793]289 if (_bncRtrover) {
290 connect(getThread, SIGNAL(newObs(QByteArray, bool, t_obs)),
291 _bncRtrover, SLOT(slotNewObs(QByteArray, bool, t_obs)));
292 }
[4763]293#endif
294
[1556]295 connect(getThread, SIGNAL(getThreadFinished(QByteArray)),
296 this, SLOT(slotGetThreadFinished(QByteArray)));
[35]297
[2182]298 connect(getThread, SIGNAL(newNMEAstr(QByteArray)),
299 this, SLOT(slotNewNMEAstr(QByteArray)));
300
[88]301 _staIDs.push_back(getThread->staID());
[186]302 _threads.push_back(getThread);
[1170]303
[2528]304 if (noNewThread) {
305 getThread->run();
306 }
307 else {
308 getThread->start();
309 }
[35]310}
311
[1556]312// Get Thread destroyed
[35]313////////////////////////////////////////////////////////////////////////////
[1556]314void bncCaster::slotGetThreadFinished(QByteArray staID) {
[243]315 QMutexLocker locker(&_mutex);
[1560]316
317 QListIterator<bncGetThread*> it(_threads);
318 while (it.hasNext()) {
319 bncGetThread* thread = it.next();
320 if (thread->staID() == staID) {
321 _threads.removeOne(thread);
322 }
323 }
324
[88]325 _staIDs.removeAll(staID);
[82]326 emit( newMessage(
[1986]327 QString("Decoding %1 stream(s)").arg(_staIDs.size()).toAscii(), true) );
[88]328 if (_staIDs.size() == 0) {
[1450]329 emit(newMessage("bncCaster: Last get thread terminated", true));
[1556]330 emit getThreadsFinished();
[35]331 }
332}
333
334// Dump Complete Epochs
335////////////////////////////////////////////////////////////////////////////
[2316]336void bncCaster::dumpEpochs(long minTime, long maxTime) {
[35]337
[2316]338 for (long sec = minTime; sec <= maxTime; sec++) {
[140]339
[4990]340 if ( (_out || _sockets) &&
341 (_samplingRate == 0 || sec % _samplingRate == 0) ) {
[1999]342
[4990]343 QList<t_obs> allObs = _epochs->values(sec);
344
345 QListIterator<t_obs> it(allObs);
346 bool firstObs = true;
347 while (it.hasNext()) {
348 const t_obs& obs = it.next();
[4987]349
[4990]350 ostringstream oStr;
351 oStr.setf(ios::showpoint | ios::fixed);
352 if (firstObs) {
353 firstObs = false;
354 oStr << "> " << obs.GPSWeek << ' '
355 << setprecision(7) << obs.GPSWeeks << endl;;
356 }
357 oStr << obs.StatID << ' ' << bncRinex::asciiSatLine(obs) << endl;
358 if (!it.hasNext()) {
359 oStr << endl;
360 }
361 string hlpStr = oStr.str();
[35]362
[4990]363 // Output into the File
364 // --------------------
365 if (_out) {
366 *_out << hlpStr.c_str();
367 _out->flush();
368 }
[1810]369
[4990]370 // Output into the socket
371 // ----------------------
372 if (_sockets) {
373 QMutableListIterator<QTcpSocket*> is(*_sockets);
374 while (is.hasNext()) {
375 QTcpSocket* sock = is.next();
376 if (sock->state() == QAbstractSocket::ConnectedState) {
377 int numBytes = hlpStr.length();
378 if (myWrite(sock, hlpStr.c_str(), numBytes) != numBytes) {
[637]379 delete sock;
380 is.remove();
381 }
[140]382 }
[4990]383 else if (sock->state() != QAbstractSocket::ConnectingState) {
384 delete sock;
385 is.remove();
386 }
[140]387 }
388 }
[73]389 }
[35]390 }
[4990]391 _epochs->remove(sec);
[35]392 }
393}
[1170]394
[4250]395// Reread configuration (private slot)
[1170]396////////////////////////////////////////////////////////////////////////////
[1179]397void bncCaster::slotReadMountPoints() {
[1170]398
[4252]399 bncSettings settings;
400 settings.reRead();
401
[4250]402 readMountPoints();
403}
404
405// Read Mountpoints
406////////////////////////////////////////////////////////////////////////////
407void bncCaster::readMountPoints() {
408
[1535]409 bncSettings settings;
[1170]410
411 // Reread several options
412 // ----------------------
[2316]413 _samplingRate = settings.value("binSampl").toInt();
414 _waitTime = settings.value("waitTime").toInt();
415 if (_waitTime < 1) {
416 _waitTime = 1;
[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(" ");
426 if (hlp.size() <= 1) continue;
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) {
444 QByteArray format = hlp[1].toAscii();
445 QByteArray latitude = hlp[2].toAscii();
446 QByteArray longitude = hlp[3].toAscii();
447 QByteArray nmea = hlp[4].toAscii();
[1353]448 QByteArray ntripVersion = hlp[5].toAscii();
[1170]449
450 bncGetThread* getThread = new bncGetThread(url, format, latitude,
[3003]451 longitude, nmea, ntripVersion);
[1170]452 addGetThread(getThread);
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);
[1529]484 emit( newMessage(QString("Configuration read: "
[1542]485 + ((bncApp*) qApp)->confFileName()
[1529]486 + ", %1 stream(s)")
[1299]487 .arg(_threads.count()).toAscii(), true) );
[1176]488
[1170]489 // (Re-) Start the configuration timer
490 // -----------------------------------
491 int ms = 0;
492
[1705]493 if (_confInterval != -1) {
[1170]494 ms = 1000 * _confInterval;
495 }
496 else {
497 QTime currTime = currentDateAndTimeGPS().time();
498 QTime nextShotTime;
499
500 if (settings.value("onTheFlyInterval").toString() == "1 min") {
501 _confInterval = 60;
502 nextShotTime = QTime(currTime.hour(), currTime.minute()+1, 0);
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
526//
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
538//
539////////////////////////////////////////////////////////////////////////////
540void bncCaster::slotNewNMEAstr(QByteArray str) {
[2184]541 if (_nmeaSockets) {
542 QMutableListIterator<QTcpSocket*> is(*_nmeaSockets);
543 while (is.hasNext()) {
544 QTcpSocket* sock = is.next();
545 if (sock->state() == QAbstractSocket::ConnectedState) {
546 sock->write(str);
547 }
548 else if (sock->state() != QAbstractSocket::ConnectingState) {
549 delete sock;
550 is.remove();
551 }
552 }
553 }
[2182]554}
Note: See TracBrowser for help on using the repository browser.