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

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

* empty log message *

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