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

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

* empty log message *

File size: 9.6 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
[637]41#include <iostream>
[222]42#include <math.h>
[636]43#include <unistd.h>
[222]44
[35]45#include "bnccaster.h"
46#include "bncgetthread.h"
[134]47#include "bncutils.h"
[207]48#include "RTCM/GPSDecoder.h"
[35]49
[635]50
[35]51// Constructor
52////////////////////////////////////////////////////////////////////////////
53bncCaster::bncCaster(const QString& outFileName, int port) {
54
[275]55 QSettings settings;
56
[35]57 if ( !outFileName.isEmpty() ) {
[134]58 QString lName = outFileName;
59 expandEnvVar(lName);
60 _outFile = new QFile(lName);
[275]61 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
62 _outFile->open(QIODevice::WriteOnly | QIODevice::Append);
63 }
64 else {
65 _outFile->open(QIODevice::WriteOnly);
66 }
[35]67 _out = new QTextStream(_outFile);
68 _out->setRealNumberNotation(QTextStream::FixedNotation);
69 }
70 else {
71 _outFile = 0;
72 _out = 0;
73 }
74
75 _port = port;
76
77 if (_port != 0) {
78 _server = new QTcpServer;
79 _server->listen(QHostAddress::Any, _port);
80 connect(_server, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));
81 _sockets = new QList<QTcpSocket*>;
82 }
83 else {
84 _server = 0;
85 _sockets = 0;
86 }
87
[622]88 _epochs = new QMultiMap<long, p_obs>;
[35]89
90 _lastDumpSec = 0;
[133]91
[740]92 _samplingRate = settings.value("binSampl").toInt();
[136]93 _waitTime = settings.value("waitTime").toInt();
[139]94 if (_waitTime < 1) {
95 _waitTime = 1;
[136]96 }
[35]97}
98
99// Destructor
100////////////////////////////////////////////////////////////////////////////
101bncCaster::~bncCaster() {
[186]102 QListIterator<bncGetThread*> it(_threads);
103 while(it.hasNext()){
104 bncGetThread* thread = it.next();
105 thread->terminate();
[605]106 thread->wait();
[230]107 delete thread;
[186]108 }
[35]109 delete _out;
110 delete _outFile;
[187]111 delete _server;
[631]112 delete _sockets;
[603]113 if (_epochs) {
[622]114 QListIterator<p_obs> it(_epochs->values());
[603]115 while (it.hasNext()) {
116 delete it.next();
117 }
118 delete _epochs;
119 }
[35]120}
121
122// New Observations
123////////////////////////////////////////////////////////////////////////////
[622]124void bncCaster::newObs(const QByteArray staID, bool firstObs, p_obs obs) {
[35]125
[243]126 QMutexLocker locker(&_mutex);
127
[624]128 obs->_status = t_obs::received;
129
[622]130 long iSec = long(floor(obs->_o.GPSWeeks+0.5));
131 long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
[35]132
[160]133 // Rename the Station
134 // ------------------
[622]135 strncpy(obs->_o.StatID, staID.constData(),sizeof(obs->_o.StatID));
136 obs->_o.StatID[sizeof(obs->_o.StatID)-1] = '\0';
[160]137
[35]138 // First time, set the _lastDumpSec immediately
139 // --------------------------------------------
140 if (_lastDumpSec == 0) {
[462]141 _lastDumpSec = newTime - 1;
[35]142 }
143
144 // An old observation - throw it away
145 // ----------------------------------
[462]146 if (newTime <= _lastDumpSec) {
[257]147 if (firstObs) {
148 QSettings settings;
149 if ( !settings.value("outFile").toString().isEmpty() ||
150 !settings.value("outPort").toString().isEmpty() ) {
[258]151 emit( newMessage(QString("Station %1: old epoch %2 thrown away")
152 .arg(staID.data()).arg(iSec).toAscii()) );
[257]153 }
[181]154 }
[35]155 delete obs;
[350]156 return;
[35]157 }
158
[160]159 // Save the observation
160 // --------------------
[462]161 _epochs->insert(newTime, obs);
[393]162
[462]163 // Dump Epochs
164 // -----------
165 if (newTime - _waitTime > _lastDumpSec) {
166 dumpEpochs(_lastDumpSec + 1, newTime - _waitTime);
167 _lastDumpSec = newTime - _waitTime;
[252]168 }
[35]169}
170
171// New Connection
172////////////////////////////////////////////////////////////////////////////
173void bncCaster::slotNewConnection() {
174 _sockets->push_back( _server->nextPendingConnection() );
[630]175 emit( newMessage(QString("New Connection # %1")
176 .arg(_sockets->size()).toAscii()) );
[35]177}
178
179// Add New Thread
180////////////////////////////////////////////////////////////////////////////
181void bncCaster::addGetThread(bncGetThread* getThread) {
[624]182
183 qRegisterMetaType<p_obs>("p_obs");
184
[628]185 connect(getThread, SIGNAL(newObs(QByteArray, bool, p_obs)),
186 this, SLOT(newObs(QByteArray, bool, p_obs)));
[463]187
[628]188 connect(getThread, SIGNAL(error(QByteArray)),
189 this, SLOT(slotGetThreadError(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////////////////////////////////////////////////////////////////////////////
[628]197void bncCaster::slotGetThreadError(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;
[622]219 QList<p_obs> allObs = _epochs->values(sec);
220 QListIterator<p_obs> it(allObs);
[35]221 while (it.hasNext()) {
[622]222 p_obs obs = it.next();
[35]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 }
[622]232 _out->setFieldWidth(0); *_out << obs->_o.StatID;
233 _out->setFieldWidth(1); *_out << " " << obs->_o.satSys;
[344]234 _out->setPadChar('0');
[622]235 _out->setFieldWidth(2); *_out << obs->_o.satNum;
[344]236 _out->setPadChar(' ');
[342]237 _out->setFieldWidth(1); *_out << " ";
[622]238 _out->setFieldWidth(4); *_out << obs->_o.GPSWeek;
[342]239 _out->setFieldWidth(1); *_out << " ";
[622]240 _out->setFieldWidth(14); _out->setRealNumberPrecision(7); *_out << obs->_o.GPSWeeks;
[342]241 _out->setFieldWidth(1); *_out << " ";
[622]242 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->_o.C1;
[342]243 _out->setFieldWidth(1); *_out << " ";
[622]244 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->_o.C2;
[366]245 _out->setFieldWidth(1); *_out << " ";
[622]246 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->_o.P1;
[342]247 _out->setFieldWidth(1); *_out << " ";
[622]248 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->_o.P2;
[342]249 _out->setFieldWidth(1); *_out << " ";
[622]250 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->_o.L1;
[342]251 _out->setFieldWidth(1); *_out << " ";
[622]252 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->_o.L2;
[367]253 _out->setFieldWidth(1); *_out << " ";
[622]254 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->_o.S1;
[367]255 _out->setFieldWidth(1); *_out << " ";
[622]256 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->_o.S2;
[342]257 _out->setFieldWidth(1);
[622]258 *_out << " " << obs->_o.SNR1 << " " << obs->_o.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) {
[622]268 int numBytes = sizeof(obs->_o);
[637]269 QMutableListIterator<QTcpSocket*> is(*_sockets);
[140]270 while (is.hasNext()) {
271 QTcpSocket* sock = is.next();
[397]272 if (sock->state() == QAbstractSocket::ConnectedState) {
[637]273 bool ok = true;
[635]274 int fd = sock->socketDescriptor();
[397]275 if (first) {
[637]276 if (::write(fd, &begEpoch, 1) != 1) {
277 ok = false;
278 }
[397]279 }
[637]280 if (::write(fd, &begObs, 1) != 1) {
281 ok = false;
282 }
283 if (::write(fd, &obs->_o, numBytes) != numBytes) {
284 ok = false;
285 }
[397]286 if (!it.hasNext()) {
[637]287 if (::write(fd, &endEpoch, 1) != 1) {
288 ok = false;
289 }
[397]290 }
[637]291 if (!ok) {
292 delete sock;
293 is.remove();
294 }
[140]295 }
[637]296 else if (sock->state() != QAbstractSocket::ConnectingState) {
297 delete sock;
298 is.remove();
299 }
[140]300 }
301 }
[73]302 }
303
[35]304 delete obs;
305 _epochs->remove(sec);
306 first = false;
307 }
308 }
309}
Note: See TracBrowser for help on using the repository browser.