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

Last change on this file since 636 was 636, checked in by mervart, 16 years ago

* empty log message *

File size: 9.0 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 "bncgetthread.h"
46#include "bncutils.h"
47#include "RTCM/GPSDecoder.h"
48
49
50// Constructor
51////////////////////////////////////////////////////////////////////////////
52bncCaster::bncCaster(const QString& outFileName, int port) {
53
54 QSettings settings;
55
56 if ( !outFileName.isEmpty() ) {
57 QString lName = outFileName;
58 expandEnvVar(lName);
59 _outFile = new QFile(lName);
60 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
61 _outFile->open(QIODevice::WriteOnly | QIODevice::Append);
62 }
63 else {
64 _outFile->open(QIODevice::WriteOnly);
65 }
66 _out = new QTextStream(_outFile);
67 _out->setRealNumberNotation(QTextStream::FixedNotation);
68 }
69 else {
70 _outFile = 0;
71 _out = 0;
72 }
73
74 _port = port;
75
76 if (_port != 0) {
77 _server = new QTcpServer;
78 _server->listen(QHostAddress::Any, _port);
79 connect(_server, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));
80 _sockets = new QList<QTcpSocket*>;
81 }
82 else {
83 _server = 0;
84 _sockets = 0;
85 }
86
87 _epochs = new QMultiMap<long, p_obs>;
88
89 _lastDumpSec = 0;
90
91 _samplingRate = settings.value("rnxSampl").toInt();
92 _waitTime = settings.value("waitTime").toInt();
93 if (_waitTime < 1) {
94 _waitTime = 1;
95 }
96}
97
98// Destructor
99////////////////////////////////////////////////////////////////////////////
100bncCaster::~bncCaster() {
101 QListIterator<bncGetThread*> it(_threads);
102 while(it.hasNext()){
103 bncGetThread* thread = it.next();
104 thread->terminate();
105 thread->wait();
106 delete thread;
107 }
108 delete _out;
109 delete _outFile;
110 delete _server;
111 delete _sockets;
112 if (_epochs) {
113 QListIterator<p_obs> it(_epochs->values());
114 while (it.hasNext()) {
115 delete it.next();
116 }
117 delete _epochs;
118 }
119}
120
121// New Observations
122////////////////////////////////////////////////////////////////////////////
123void bncCaster::newObs(const QByteArray staID, bool firstObs, p_obs obs) {
124
125 QMutexLocker locker(&_mutex);
126
127 obs->_status = t_obs::received;
128
129 long iSec = long(floor(obs->_o.GPSWeeks+0.5));
130 long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
131
132 // Rename the Station
133 // ------------------
134 strncpy(obs->_o.StatID, staID.constData(),sizeof(obs->_o.StatID));
135 obs->_o.StatID[sizeof(obs->_o.StatID)-1] = '\0';
136
137 // First time, set the _lastDumpSec immediately
138 // --------------------------------------------
139 if (_lastDumpSec == 0) {
140 _lastDumpSec = newTime - 1;
141 }
142
143 // An old observation - throw it away
144 // ----------------------------------
145 if (newTime <= _lastDumpSec) {
146 if (firstObs) {
147 QSettings settings;
148 if ( !settings.value("outFile").toString().isEmpty() ||
149 !settings.value("outPort").toString().isEmpty() ) {
150 emit( newMessage(QString("Station %1: old epoch %2 thrown away")
151 .arg(staID.data()).arg(iSec).toAscii()) );
152 }
153 }
154 delete obs;
155 return;
156 }
157
158 // Save the observation
159 // --------------------
160 _epochs->insert(newTime, obs);
161
162 // Dump Epochs
163 // -----------
164 if (newTime - _waitTime > _lastDumpSec) {
165 dumpEpochs(_lastDumpSec + 1, newTime - _waitTime);
166 _lastDumpSec = newTime - _waitTime;
167 }
168}
169
170// New Connection
171////////////////////////////////////////////////////////////////////////////
172void bncCaster::slotNewConnection() {
173 _sockets->push_back( _server->nextPendingConnection() );
174 emit( newMessage(QString("New Connection # %1")
175 .arg(_sockets->size()).toAscii()) );
176}
177
178// Add New Thread
179////////////////////////////////////////////////////////////////////////////
180void bncCaster::addGetThread(bncGetThread* getThread) {
181
182 qRegisterMetaType<p_obs>("p_obs");
183
184 connect(getThread, SIGNAL(newObs(QByteArray, bool, p_obs)),
185 this, SLOT(newObs(QByteArray, bool, p_obs)));
186
187 connect(getThread, SIGNAL(error(QByteArray)),
188 this, SLOT(slotGetThreadError(QByteArray)));
189
190 _staIDs.push_back(getThread->staID());
191 _threads.push_back(getThread);
192}
193
194// Error in get thread
195////////////////////////////////////////////////////////////////////////////
196void bncCaster::slotGetThreadError(QByteArray staID) {
197 QMutexLocker locker(&_mutex);
198 _staIDs.removeAll(staID);
199 emit( newMessage(
200 QString("Mountpoint size %1").arg(_staIDs.size()).toAscii()) );
201 if (_staIDs.size() == 0) {
202 emit(newMessage("bncCaster:: last get thread terminated"));
203 emit getThreadErrors();
204 }
205}
206
207// Dump Complete Epochs
208////////////////////////////////////////////////////////////////////////////
209void bncCaster::dumpEpochs(long minTime, long maxTime) {
210
211 const char begEpoch = 'A';
212 const char begObs = 'B';
213 const char endEpoch = 'C';
214
215 for (long sec = minTime; sec <= maxTime; sec++) {
216
217 bool first = true;
218 QList<p_obs> allObs = _epochs->values(sec);
219 QListIterator<p_obs> it(allObs);
220 while (it.hasNext()) {
221 p_obs obs = it.next();
222
223 if (_samplingRate == 0 || sec % _samplingRate == 0) {
224
225 // Output into the file
226 // --------------------
227 if (_out) {
228 if (first) {
229 _out->setFieldWidth(1); *_out << begEpoch << endl;;
230 }
231 _out->setFieldWidth(0); *_out << obs->_o.StatID;
232 _out->setFieldWidth(1); *_out << " " << obs->_o.satSys;
233 _out->setPadChar('0');
234 _out->setFieldWidth(2); *_out << obs->_o.satNum;
235 _out->setPadChar(' ');
236 _out->setFieldWidth(1); *_out << " ";
237 _out->setFieldWidth(4); *_out << obs->_o.GPSWeek;
238 _out->setFieldWidth(1); *_out << " ";
239 _out->setFieldWidth(14); _out->setRealNumberPrecision(7); *_out << obs->_o.GPSWeeks;
240 _out->setFieldWidth(1); *_out << " ";
241 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->_o.C1;
242 _out->setFieldWidth(1); *_out << " ";
243 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->_o.C2;
244 _out->setFieldWidth(1); *_out << " ";
245 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->_o.P1;
246 _out->setFieldWidth(1); *_out << " ";
247 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->_o.P2;
248 _out->setFieldWidth(1); *_out << " ";
249 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->_o.L1;
250 _out->setFieldWidth(1); *_out << " ";
251 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->_o.L2;
252 _out->setFieldWidth(1); *_out << " ";
253 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->_o.S1;
254 _out->setFieldWidth(1); *_out << " ";
255 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->_o.S2;
256 _out->setFieldWidth(1);
257 *_out << " " << obs->_o.SNR1 << " " << obs->_o.SNR2 << endl;
258 if (!it.hasNext()) {
259 _out->setFieldWidth(1); *_out << endEpoch << endl;
260 }
261 _out->flush();
262 }
263
264 // Output into the socket
265 // ----------------------
266 if (_sockets) {
267 int numBytes = sizeof(obs->_o);
268 QListIterator<QTcpSocket*> is(*_sockets);
269 while (is.hasNext()) {
270 QTcpSocket* sock = is.next();
271 if (sock->state() == QAbstractSocket::ConnectedState) {
272 int fd = sock->socketDescriptor();
273 if (first) {
274 ::write(fd, &begEpoch, 1);
275 }
276 ::write(fd, &begObs, 1);
277 ::write(fd, &obs->_o, numBytes);
278 if (!it.hasNext()) {
279 ::write(fd, &endEpoch, 1);
280 }
281 }
282 }
283 }
284 }
285
286 delete obs;
287 _epochs->remove(sec);
288 first = false;
289 }
290 }
291}
Note: See TracBrowser for help on using the repository browser.