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

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

* empty log message *

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