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