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

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

* empty log message *

File size: 9.8 KB
Line 
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.
25
26/* -------------------------------------------------------------------------
27 * BKG NTRIP Client
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
42#include <math.h>
43
44#include "bnccaster.h"
45#include "bncgetthread.h"
46#include "bncutils.h"
47#include "RTCM/GPSDecoder.h"
48
49// Constructor
50////////////////////////////////////////////////////////////////////////////
51bncCaster::bncCaster(const QString& outFileName, int port) {
52
53 QSettings settings;
54
55 if ( !outFileName.isEmpty() ) {
56 QString lName = outFileName;
57 expandEnvVar(lName);
58 _outFile = new QFile(lName);
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 }
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;
89
90 _samplingRate = settings.value("rnxSampl").toInt();
91 _waitTime = settings.value("waitTime").toInt();
92 if (_waitTime < 1) {
93 _waitTime = 1;
94 }
95
96 // Start dump epoch loop
97 // ---------------------
98 _newTime = 0;
99 dumpEpochSlot();
100}
101
102// Destructor
103////////////////////////////////////////////////////////////////////////////
104bncCaster::~bncCaster() {
105 QListIterator<bncGetThread*> it(_threads);
106 while(it.hasNext()){
107 bncGetThread* thread = it.next();
108 thread->terminate();
109 thread->wait();
110 delete thread;
111 }
112 delete _out;
113 delete _outFile;
114 delete _server;
115 delete _sockets;
116 delete _epochs;
117}
118
119// Reconnecting
120////////////////////////////////////////////////////////////////////////////
121void bncCaster::reconnecting(const QByteArray& staID) {
122 if (_rinexWriters.find(staID) != _rinexWriters.end()) {
123 bncRinex* rnx = _rinexWriters.find(staID).value();
124 rnx->setReconnectFlag(true);
125 }
126}
127
128// New Observations
129////////////////////////////////////////////////////////////////////////////
130void bncCaster::newObs(const QByteArray& staID, const QUrl& mountPoint,
131 bool firstObs, Observation* obs,
132 const QByteArray& format,
133 const QByteArray& latitude,
134 const QByteArray& longitude,
135 const QByteArray& nmea) {
136
137 QMutexLocker locker(&_mutex);
138
139 long iSec = long(floor(obs->GPSWeeks+0.5));
140 _newTime = obs->GPSWeek * 7*24*3600 + iSec;
141
142 // Rename the Station
143 // ------------------
144 strncpy(obs->StatID, staID.constData(),sizeof(obs->StatID));
145 obs->StatID[sizeof(obs->StatID)-1] = '\0';
146
147 // Prepare RINEX Output
148 // --------------------
149 if (_rinexWriters.find(obs->StatID) == _rinexWriters.end()) {
150 _rinexWriters.insert(obs->StatID, new bncRinex(obs->StatID,
151 mountPoint, format, latitude, longitude, nmea));
152 }
153 bncRinex* rnx = _rinexWriters.find(obs->StatID).value();
154 if (_samplingRate == 0 || iSec % _samplingRate == 0) {
155 rnx->deepCopy(obs);
156 }
157 rnx->dumpEpoch(_newTime);
158
159 // First time, set the _lastDumpSec immediately
160 // --------------------------------------------
161 if (_lastDumpSec == 0) {
162 _lastDumpSec = _newTime - 1;
163 }
164
165 // An old observation - throw it away
166 // ----------------------------------
167 if (_newTime <= _lastDumpSec) {
168 if (firstObs) {
169 QSettings settings;
170 if ( !settings.value("outFile").toString().isEmpty() ||
171 !settings.value("outPort").toString().isEmpty() ) {
172 emit( newMessage(QString("Station %1: old epoch %2 thrown away")
173 .arg(staID.data()).arg(iSec).toAscii()) );
174 }
175 }
176 delete obs;
177 return;
178 }
179
180 // Save the observation
181 // --------------------
182 _epochs->insert(_newTime, obs);
183}
184
185// Dump Loop Event
186////////////////////////////////////////////////////////////////////////////
187void bncCaster::dumpEpochSlot() {
188 QMutexLocker locker(&_mutex);
189 if (_newTime != 0 && _epochs->size() > 0) {
190 dumpEpochs(_lastDumpSec + 1, _newTime - _waitTime);
191
192 if (_lastDumpSec < _newTime - _waitTime) {
193 _lastDumpSec = _newTime - _waitTime;
194 }
195 }
196 QTimer::singleShot(100, this, SLOT(dumpEpochSlot()));
197}
198
199// New Connection
200////////////////////////////////////////////////////////////////////////////
201void bncCaster::slotNewConnection() {
202 _sockets->push_back( _server->nextPendingConnection() );
203}
204
205// Add New Thread
206////////////////////////////////////////////////////////////////////////////
207void bncCaster::addGetThread(bncGetThread* getThread) {
208 connect(getThread, SIGNAL(error(const QByteArray&)),
209 this, SLOT(slotGetThreadError(const QByteArray&)));
210
211 _staIDs.push_back(getThread->staID());
212 _threads.push_back(getThread);
213}
214
215// Error in get thread
216////////////////////////////////////////////////////////////////////////////
217void bncCaster::slotGetThreadError(const QByteArray& staID) {
218 QMutexLocker locker(&_mutex);
219 _staIDs.removeAll(staID);
220 emit( newMessage(
221 QString("Mountpoint size %1").arg(_staIDs.size()).toAscii()) );
222 if (_staIDs.size() == 0) {
223 emit(newMessage("bncCaster:: last get thread terminated"));
224 emit getThreadErrors();
225 }
226}
227
228// Dump Complete Epochs
229////////////////////////////////////////////////////////////////////////////
230void bncCaster::dumpEpochs(long minTime, long maxTime) {
231
232 const char begEpoch = 'A';
233 const char begObs = 'B';
234 const char endEpoch = 'C';
235
236 for (long sec = minTime; sec <= maxTime; sec++) {
237
238 bool first = true;
239 QList<Observation*> allObs = _epochs->values(sec);
240 QListIterator<Observation*> it(allObs);
241 while (it.hasNext()) {
242 Observation* obs = it.next();
243
244 if (_samplingRate == 0 || sec % _samplingRate == 0) {
245
246 // Output into the file
247 // --------------------
248 if (_out) {
249 if (first) {
250 _out->setFieldWidth(1); *_out << begEpoch << endl;;
251 }
252 _out->setFieldWidth(0); *_out << obs->StatID;
253 _out->setFieldWidth(1); *_out << " " << obs->satSys;
254 _out->setPadChar('0');
255 _out->setFieldWidth(2); *_out << obs->satNum;
256 _out->setPadChar(' ');
257 _out->setFieldWidth(1); *_out << " ";
258 _out->setFieldWidth(4); *_out << obs->GPSWeek;
259 _out->setFieldWidth(1); *_out << " ";
260 _out->setFieldWidth(14); _out->setRealNumberPrecision(7); *_out << obs->GPSWeeks;
261 _out->setFieldWidth(1); *_out << " ";
262 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->C1;
263 _out->setFieldWidth(1); *_out << " ";
264 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->C2;
265 _out->setFieldWidth(1); *_out << " ";
266 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->P1;
267 _out->setFieldWidth(1); *_out << " ";
268 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->P2;
269 _out->setFieldWidth(1); *_out << " ";
270 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->L1;
271 _out->setFieldWidth(1); *_out << " ";
272 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->L2;
273 _out->setFieldWidth(1); *_out << " ";
274 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->S1;
275 _out->setFieldWidth(1); *_out << " ";
276 _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->S2;
277 _out->setFieldWidth(1);
278 *_out << " " << obs->SNR1 << " " << obs->SNR2 << endl;
279 if (!it.hasNext()) {
280 _out->setFieldWidth(1); *_out << endEpoch << endl;
281 }
282 _out->flush();
283 }
284
285 // Output into the socket
286 // ----------------------
287 if (_sockets) {
288 int numBytes = sizeof(*obs);
289 QListIterator<QTcpSocket*> is(*_sockets);
290 while (is.hasNext()) {
291 QTcpSocket* sock = is.next();
292 if (first) {
293 sock->write(&begEpoch, 1);
294 }
295 sock->write(&begObs, 1);
296 sock->write((char*) obs, numBytes);
297 if (!it.hasNext()) {
298 sock->write(&endEpoch, 1);
299 }
300 }
301 }
302 }
303
304 delete obs;
305 _epochs->remove(sec);
306 first = false;
307 }
308 }
309}
Note: See TracBrowser for help on using the repository browser.