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

Last change on this file since 458 was 458, checked in by mervart, 17 years ago

* empty log message *

File size: 9.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
96 // Start dump epoch loop
97 // ---------------------
[393]98 _newObsRunning = false;
[382]99 _newTime = 0;
100 dumpEpochSlot();
[35]101}
102
103// Destructor
104////////////////////////////////////////////////////////////////////////////
105bncCaster::~bncCaster() {
[186]106 QListIterator<bncGetThread*> it(_threads);
107 while(it.hasNext()){
108 bncGetThread* thread = it.next();
109 thread->terminate();
[188]110 thread->wait();
[230]111 delete thread;
[186]112 }
[35]113 delete _out;
114 delete _outFile;
[187]115 delete _server;
116 delete _sockets;
117 delete _epochs;
[35]118}
119
120// New Observations
121////////////////////////////////////////////////////////////////////////////
[408]122void bncCaster::newObs(const QByteArray& staID, bool firstObs,
123 Observation* obs) {
[35]124
[243]125 QMutexLocker locker(&_mutex);
[393]126 _newObsRunning = true;
[243]127
[222]128 long iSec = long(floor(obs->GPSWeeks+0.5));
[382]129 _newTime = obs->GPSWeek * 7*24*3600 + iSec;
[35]130
[160]131 // Rename the Station
132 // ------------------
133 strncpy(obs->StatID, staID.constData(),sizeof(obs->StatID));
[333]134 obs->StatID[sizeof(obs->StatID)-1] = '\0';
[160]135
[35]136 // First time, set the _lastDumpSec immediately
137 // --------------------------------------------
138 if (_lastDumpSec == 0) {
[382]139 _lastDumpSec = _newTime - 1;
[35]140 }
141
142 // An old observation - throw it away
143 // ----------------------------------
[382]144 if (_newTime <= _lastDumpSec) {
[257]145 if (firstObs) {
146 QSettings settings;
147 if ( !settings.value("outFile").toString().isEmpty() ||
148 !settings.value("outPort").toString().isEmpty() ) {
[258]149 emit( newMessage(QString("Station %1: old epoch %2 thrown away")
150 .arg(staID.data()).arg(iSec).toAscii()) );
[257]151 }
[181]152 }
[35]153 delete obs;
[350]154 return;
[35]155 }
156
[160]157 // Save the observation
158 // --------------------
[382]159 _epochs->insert(_newTime, obs);
[393]160
161 _newObsRunning = false;
[382]162}
[35]163
[382]164// Dump Loop Event
165////////////////////////////////////////////////////////////////////////////
166void bncCaster::dumpEpochSlot() {
[393]167 if (!_newObsRunning) {
168 if (_newTime != 0 && _epochs->size() > 0) {
169 dumpEpochs(_lastDumpSec + 1, _newTime - _waitTime);
170
171 if (_lastDumpSec < _newTime - _waitTime) {
172 _lastDumpSec = _newTime - _waitTime;
173 }
[382]174 }
[252]175 }
[382]176 QTimer::singleShot(100, this, SLOT(dumpEpochSlot()));
[35]177}
178
179// New Connection
180////////////////////////////////////////////////////////////////////////////
181void bncCaster::slotNewConnection() {
182 _sockets->push_back( _server->nextPendingConnection() );
183}
184
185// Add New Thread
186////////////////////////////////////////////////////////////////////////////
187void bncCaster::addGetThread(bncGetThread* getThread) {
[458]188 connect(getThread, SIGNAL(error(const QByteArray)),
189 this, SLOT(slotGetThreadError(const QByteArray)));
[35]190
[88]191 _staIDs.push_back(getThread->staID());
[186]192 _threads.push_back(getThread);
[35]193}
194
195// Error in get thread
196////////////////////////////////////////////////////////////////////////////
[458]197void bncCaster::slotGetThreadError(const QByteArray staID) {
[243]198 QMutexLocker locker(&_mutex);
[88]199 _staIDs.removeAll(staID);
[82]200 emit( newMessage(
[88]201 QString("Mountpoint size %1").arg(_staIDs.size()).toAscii()) );
202 if (_staIDs.size() == 0) {
[82]203 emit(newMessage("bncCaster:: last get thread terminated"));
[35]204 emit getThreadErrors();
205 }
206}
207
208// Dump Complete Epochs
209////////////////////////////////////////////////////////////////////////////
210void bncCaster::dumpEpochs(long minTime, long maxTime) {
211
212 const char begEpoch = 'A';
213 const char begObs = 'B';
214 const char endEpoch = 'C';
215
216 for (long sec = minTime; sec <= maxTime; sec++) {
[140]217
[35]218 bool first = true;
219 QList<Observation*> allObs = _epochs->values(sec);
220 QListIterator<Observation*> it(allObs);
221 while (it.hasNext()) {
222 Observation* obs = it.next();
223
[140]224 if (_samplingRate == 0 || sec % _samplingRate == 0) {
225
226 // Output into the file
227 // --------------------
228 if (_out) {
[35]229 if (first) {
[341]230 _out->setFieldWidth(1); *_out << begEpoch << endl;;
[35]231 }
[343]232 _out->setFieldWidth(0); *_out << obs->StatID;
[342]233 _out->setFieldWidth(1); *_out << " " << obs->satSys;
[344]234 _out->setPadChar('0');
[342]235 _out->setFieldWidth(2); *_out << obs->satNum;
[344]236 _out->setPadChar(' ');
[342]237 _out->setFieldWidth(1); *_out << " ";
238 _out->setFieldWidth(4); *_out << obs->GPSWeek;
239 _out->setFieldWidth(1); *_out << " ";
240 _out->setFieldWidth(14); _out->setRealNumberPrecision(7); *_out << obs->GPSWeeks;
241 _out->setFieldWidth(1); *_out << " ";
242 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->C1;
243 _out->setFieldWidth(1); *_out << " ";
[366]244 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->C2;
245 _out->setFieldWidth(1); *_out << " ";
[342]246 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->P1;
247 _out->setFieldWidth(1); *_out << " ";
248 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->P2;
249 _out->setFieldWidth(1); *_out << " ";
250 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->L1;
251 _out->setFieldWidth(1); *_out << " ";
252 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->L2;
[367]253 _out->setFieldWidth(1); *_out << " ";
254 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->S1;
255 _out->setFieldWidth(1); *_out << " ";
256 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->S2;
[342]257 _out->setFieldWidth(1);
258 *_out << " " << obs->SNR1 << " " << obs->SNR2 << endl;
[35]259 if (!it.hasNext()) {
[341]260 _out->setFieldWidth(1); *_out << endEpoch << endl;
[35]261 }
[347]262 _out->flush();
[35]263 }
[140]264
265 // Output into the socket
266 // ----------------------
267 if (_sockets) {
268 int numBytes = sizeof(*obs);
269 QListIterator<QTcpSocket*> is(*_sockets);
270 while (is.hasNext()) {
271 QTcpSocket* sock = is.next();
[397]272 if (sock->state() == QAbstractSocket::ConnectedState) {
273 if (first) {
274 sock->write(&begEpoch, 1);
275 }
276 sock->write(&begObs, 1);
277 sock->write((char*) obs, numBytes);
278 if (!it.hasNext()) {
279 sock->write(&endEpoch, 1);
280 }
[140]281 }
282 }
283 }
[73]284 }
285
[35]286 delete obs;
287 _epochs->remove(sec);
288 first = false;
289 }
290 }
291}
Note: See TracBrowser for help on using the repository browser.