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

Last change on this file since 2046 was 2035, checked in by mervart, 15 years ago

* empty log message *

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