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

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

* empty log message *

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