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

Last change on this file since 392 was 392, checked in by mervart, 19 years ago

* empty log message *

File size: 10.0 KB
RevLine 
[280]1// Part of BNC, a utility for retrieving decoding and
2// converting GNSS data streams from NTRIP broadcasters,
3// written by Leos Mervart.
4//
5// Copyright (C) 2006
6// German Federal Agency for Cartography and Geodesy (BKG)
7// http://www.bkg.bund.de
8// Czech Technical University Prague, Department of Advanced Geodesy
9// http://www.fsv.cvut.cz
10//
11// Email: euref-ip@bkg.bund.de
12//
13// This program is free software; you can redistribute it and/or
14// modify it under the terms of the GNU General Public License
15// as published by the Free Software Foundation, version 2.
16//
17// This program is distributed in the hope that it will be useful,
18// but WITHOUT ANY WARRANTY; without even the implied warranty of
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20// GNU General Public License for more details.
21//
22// You should have received a copy of the GNU General Public License
23// along with this program; if not, write to the Free Software
24// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
[35]25
26/* -------------------------------------------------------------------------
[93]27 * BKG NTRIP Client
[35]28 * -------------------------------------------------------------------------
29 *
30 * Class: bncCaster
31 *
32 * Purpose: buffers and disseminates the data
33 *
34 * Author: L. Mervart
35 *
36 * Created: 24-Dec-2005
37 *
38 * Changes:
39 *
40 * -----------------------------------------------------------------------*/
41
[222]42#include <math.h>
43
[35]44#include "bnccaster.h"
45#include "bncgetthread.h"
[134]46#include "bncutils.h"
[207]47#include "RTCM/GPSDecoder.h"
[35]48
49// Constructor
50////////////////////////////////////////////////////////////////////////////
51bncCaster::bncCaster(const QString& outFileName, int port) {
52
[275]53 QSettings settings;
54
[35]55 if ( !outFileName.isEmpty() ) {
[134]56 QString lName = outFileName;
57 expandEnvVar(lName);
58 _outFile = new QFile(lName);
[275]59 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
60 _outFile->open(QIODevice::WriteOnly | QIODevice::Append);
61 }
62 else {
63 _outFile->open(QIODevice::WriteOnly);
64 }
[35]65 _out = new QTextStream(_outFile);
66 _out->setRealNumberNotation(QTextStream::FixedNotation);
67 }
68 else {
69 _outFile = 0;
70 _out = 0;
71 }
72
73 _port = port;
74
75 if (_port != 0) {
76 _server = new QTcpServer;
77 _server->listen(QHostAddress::Any, _port);
78 connect(_server, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));
79 _sockets = new QList<QTcpSocket*>;
80 }
81 else {
82 _server = 0;
83 _sockets = 0;
84 }
85
86 _epochs = new QMultiMap<long, Observation*>;
87
88 _lastDumpSec = 0;
[133]89
90 _samplingRate = settings.value("rnxSampl").toInt();
[136]91 _waitTime = settings.value("waitTime").toInt();
[139]92 if (_waitTime < 1) {
93 _waitTime = 1;
[136]94 }
[382]95
[392]96 if ( settings.value("rnxPath").toString().isEmpty() ) {
97 _rinexWriters = 0;
98 }
99 else {
100 _rinexWriters = new QMap<QString, bncRinex*>;
101 }
102
[382]103 // Start dump epoch loop
104 // ---------------------
105 _newTime = 0;
106 dumpEpochSlot();
[35]107}
108
109// Destructor
110////////////////////////////////////////////////////////////////////////////
111bncCaster::~bncCaster() {
[186]112 QListIterator<bncGetThread*> it(_threads);
113 while(it.hasNext()){
114 bncGetThread* thread = it.next();
115 thread->terminate();
[188]116 thread->wait();
[230]117 delete thread;
[186]118 }
[35]119 delete _out;
120 delete _outFile;
[187]121 delete _server;
122 delete _sockets;
123 delete _epochs;
[392]124 delete _rinexWriters;
[35]125}
126
[369]127// Reconnecting
128////////////////////////////////////////////////////////////////////////////
129void bncCaster::reconnecting(const QByteArray& staID) {
[387]130 QMutexLocker locker(&_mutex);
131
[392]132 if (_rinexWriters && _rinexWriters->find(staID) != _rinexWriters->end()) {
133 bncRinex* rnx = _rinexWriters->find(staID).value();
[369]134 rnx->setReconnectFlag(true);
135 }
136}
137
[35]138// New Observations
139////////////////////////////////////////////////////////////////////////////
[350]140void bncCaster::newObs(const QByteArray& staID, const QUrl& mountPoint,
141 bool firstObs, Observation* obs,
[366]142 const QByteArray& format,
143 const QByteArray& latitude,
144 const QByteArray& longitude,
145 const QByteArray& nmea) {
[35]146
[243]147 QMutexLocker locker(&_mutex);
148
[222]149 long iSec = long(floor(obs->GPSWeeks+0.5));
[382]150 _newTime = obs->GPSWeek * 7*24*3600 + iSec;
[35]151
[160]152 // Rename the Station
153 // ------------------
154 strncpy(obs->StatID, staID.constData(),sizeof(obs->StatID));
[333]155 obs->StatID[sizeof(obs->StatID)-1] = '\0';
[160]156
[350]157 // Prepare RINEX Output
158 // --------------------
[392]159 if (_rinexWriters) {
160 if (_rinexWriters->find(obs->StatID) == _rinexWriters->end()) {
161 _rinexWriters->insert(obs->StatID, new bncRinex(obs->StatID, mountPoint,
162 format, latitude, longitude, nmea));
163 }
164 bncRinex* rnx = _rinexWriters->find(obs->StatID).value();
165 if (_samplingRate == 0 || iSec % _samplingRate == 0) {
166 rnx->deepCopy(obs);
167 }
168 rnx->dumpEpoch(_newTime);
[350]169 }
170
[35]171 // First time, set the _lastDumpSec immediately
172 // --------------------------------------------
173 if (_lastDumpSec == 0) {
[382]174 _lastDumpSec = _newTime - 1;
[35]175 }
176
177 // An old observation - throw it away
178 // ----------------------------------
[382]179 if (_newTime <= _lastDumpSec) {
[257]180 if (firstObs) {
181 QSettings settings;
182 if ( !settings.value("outFile").toString().isEmpty() ||
183 !settings.value("outPort").toString().isEmpty() ) {
[258]184 emit( newMessage(QString("Station %1: old epoch %2 thrown away")
185 .arg(staID.data()).arg(iSec).toAscii()) );
[257]186 }
[181]187 }
[35]188 delete obs;
[350]189 return;
[35]190 }
191
[160]192 // Save the observation
193 // --------------------
[382]194 _epochs->insert(_newTime, obs);
195}
[35]196
[382]197// Dump Loop Event
198////////////////////////////////////////////////////////////////////////////
199void bncCaster::dumpEpochSlot() {
[385]200 QMutexLocker locker(&_mutex);
[382]201 if (_newTime != 0 && _epochs->size() > 0) {
202 dumpEpochs(_lastDumpSec + 1, _newTime - _waitTime);
[252]203
[382]204 if (_lastDumpSec < _newTime - _waitTime) {
205 _lastDumpSec = _newTime - _waitTime;
206 }
[252]207 }
[382]208 QTimer::singleShot(100, this, SLOT(dumpEpochSlot()));
[35]209}
210
211// New Connection
212////////////////////////////////////////////////////////////////////////////
213void bncCaster::slotNewConnection() {
214 _sockets->push_back( _server->nextPendingConnection() );
215}
216
217// Add New Thread
218////////////////////////////////////////////////////////////////////////////
219void bncCaster::addGetThread(bncGetThread* getThread) {
220 connect(getThread, SIGNAL(error(const QByteArray&)),
221 this, SLOT(slotGetThreadError(const QByteArray&)));
222
[88]223 _staIDs.push_back(getThread->staID());
[186]224 _threads.push_back(getThread);
[35]225}
226
227// Error in get thread
228////////////////////////////////////////////////////////////////////////////
[88]229void bncCaster::slotGetThreadError(const QByteArray& staID) {
[243]230 QMutexLocker locker(&_mutex);
[88]231 _staIDs.removeAll(staID);
[82]232 emit( newMessage(
[88]233 QString("Mountpoint size %1").arg(_staIDs.size()).toAscii()) );
234 if (_staIDs.size() == 0) {
[82]235 emit(newMessage("bncCaster:: last get thread terminated"));
[35]236 emit getThreadErrors();
237 }
238}
239
240// Dump Complete Epochs
241////////////////////////////////////////////////////////////////////////////
242void bncCaster::dumpEpochs(long minTime, long maxTime) {
243
244 const char begEpoch = 'A';
245 const char begObs = 'B';
246 const char endEpoch = 'C';
247
248 for (long sec = minTime; sec <= maxTime; sec++) {
[140]249
[35]250 bool first = true;
251 QList<Observation*> allObs = _epochs->values(sec);
252 QListIterator<Observation*> it(allObs);
253 while (it.hasNext()) {
254 Observation* obs = it.next();
255
[140]256 if (_samplingRate == 0 || sec % _samplingRate == 0) {
257
258 // Output into the file
259 // --------------------
260 if (_out) {
[35]261 if (first) {
[341]262 _out->setFieldWidth(1); *_out << begEpoch << endl;;
[35]263 }
[343]264 _out->setFieldWidth(0); *_out << obs->StatID;
[342]265 _out->setFieldWidth(1); *_out << " " << obs->satSys;
[344]266 _out->setPadChar('0');
[342]267 _out->setFieldWidth(2); *_out << obs->satNum;
[344]268 _out->setPadChar(' ');
[342]269 _out->setFieldWidth(1); *_out << " ";
270 _out->setFieldWidth(4); *_out << obs->GPSWeek;
271 _out->setFieldWidth(1); *_out << " ";
272 _out->setFieldWidth(14); _out->setRealNumberPrecision(7); *_out << obs->GPSWeeks;
273 _out->setFieldWidth(1); *_out << " ";
274 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->C1;
275 _out->setFieldWidth(1); *_out << " ";
[366]276 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->C2;
277 _out->setFieldWidth(1); *_out << " ";
[342]278 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->P1;
279 _out->setFieldWidth(1); *_out << " ";
280 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->P2;
281 _out->setFieldWidth(1); *_out << " ";
282 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->L1;
283 _out->setFieldWidth(1); *_out << " ";
284 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->L2;
[367]285 _out->setFieldWidth(1); *_out << " ";
286 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->S1;
287 _out->setFieldWidth(1); *_out << " ";
288 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->S2;
[342]289 _out->setFieldWidth(1);
290 *_out << " " << obs->SNR1 << " " << obs->SNR2 << endl;
[35]291 if (!it.hasNext()) {
[341]292 _out->setFieldWidth(1); *_out << endEpoch << endl;
[35]293 }
[347]294 _out->flush();
[35]295 }
[140]296
297 // Output into the socket
298 // ----------------------
299 if (_sockets) {
300 int numBytes = sizeof(*obs);
301 QListIterator<QTcpSocket*> is(*_sockets);
302 while (is.hasNext()) {
303 QTcpSocket* sock = is.next();
304 if (first) {
305 sock->write(&begEpoch, 1);
306 }
[383]307 sock->write(&begObs, 1);
308 sock->write((char*) obs, numBytes);
309 if (!it.hasNext()) {
310 sock->write(&endEpoch, 1);
311 }
[140]312 }
313 }
[73]314 }
315
[35]316 delete obs;
317 _epochs->remove(sec);
318 first = false;
319 }
320 }
321}
Note: See TracBrowser for help on using the repository browser.