source: ntrip/trunk/BNC/bnccaster.cpp@ 2316

Last change on this file since 2316 was 2316, checked in by mervart, 14 years ago

* empty log message *

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