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

Last change on this file since 256 was 256, checked in by mervart, 18 years ago

* empty log message *

File size: 6.4 KB
Line 
1
2/* -------------------------------------------------------------------------
3 * BKG NTRIP Client
4 * -------------------------------------------------------------------------
5 *
6 * Class: bncCaster
7 *
8 * Purpose: buffers and disseminates the data
9 *
10 * Author: L. Mervart
11 *
12 * Created: 24-Dec-2005
13 *
14 * Changes:
15 *
16 * -----------------------------------------------------------------------*/
17
18#include <math.h>
19
20#include "bnccaster.h"
21#include "bncgetthread.h"
22#include "bncutils.h"
23#include "RTCM/GPSDecoder.h"
24
25// Constructor
26////////////////////////////////////////////////////////////////////////////
27bncCaster::bncCaster(const QString& outFileName, int port) {
28
29 if ( !outFileName.isEmpty() ) {
30 QString lName = outFileName;
31 expandEnvVar(lName);
32 _outFile = new QFile(lName);
33 _outFile->open(QIODevice::WriteOnly);
34 _out = new QTextStream(_outFile);
35 _out->setRealNumberNotation(QTextStream::FixedNotation);
36 _out->setRealNumberPrecision(5);
37 }
38 else {
39 _outFile = 0;
40 _out = 0;
41 }
42
43 _port = port;
44
45 if (_port != 0) {
46 _server = new QTcpServer;
47 _server->listen(QHostAddress::Any, _port);
48 connect(_server, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));
49 _sockets = new QList<QTcpSocket*>;
50 }
51 else {
52 _server = 0;
53 _sockets = 0;
54 }
55
56 _epochs = new QMultiMap<long, Observation*>;
57
58 _lastDumpSec = 0;
59
60 QSettings settings;
61 _samplingRate = settings.value("rnxSampl").toInt();
62 _waitTime = settings.value("waitTime").toInt();
63 if (_waitTime < 1) {
64 _waitTime = 1;
65 }
66}
67
68// Destructor
69////////////////////////////////////////////////////////////////////////////
70bncCaster::~bncCaster() {
71 QListIterator<bncGetThread*> it(_threads);
72 while(it.hasNext()){
73 bncGetThread* thread = it.next();
74 thread->terminate();
75 thread->wait();
76 delete thread;
77 }
78 delete _out;
79 delete _outFile;
80 delete _server;
81 delete _sockets;
82 delete _epochs;
83}
84
85// New Observations
86////////////////////////////////////////////////////////////////////////////
87void bncCaster::newObs(const QByteArray& staID, const QUrl& mountPoint,
88 Observation* obs) {
89
90 QMutexLocker locker(&_mutex);
91
92 long iSec = long(floor(obs->GPSWeeks+0.5));
93 long newTime = obs->GPSWeek * 7*24*3600 + iSec;
94
95 // Rename the Station
96 // ------------------
97 strncpy(obs->StatID, staID.constData(),sizeof(obs->StatID));
98
99 // Prepare RINEX Output
100 // --------------------
101 if (_rinexWriters.find(obs->StatID) == _rinexWriters.end()) {
102 _rinexWriters.insert(obs->StatID, new bncRinex(obs->StatID, mountPoint));
103 }
104 bncRinex* rnx = _rinexWriters.find(obs->StatID).value();
105 if (_samplingRate == 0 || iSec % _samplingRate == 0) {
106 rnx->deepCopy(obs);
107 }
108 rnx->dumpEpoch(newTime);
109
110 // First time, set the _lastDumpSec immediately
111 // --------------------------------------------
112 if (_lastDumpSec == 0) {
113 _lastDumpSec = newTime - 1;
114 }
115
116 // An old observation - throw it away
117 // ----------------------------------
118 if (newTime <= _lastDumpSec) {
119 QSettings settings;
120 if ( !settings.value("outFile").toString().isEmpty() ||
121 !settings.value("outPort").toString().isEmpty() ) {
122 emit( newMessage(QString("Station %1: old epoch %2 thrown away"
123 "(newTime = %3 lastDump = %4)")
124 .arg(staID.data()).arg(iSec).arg(newTime).arg(_lastDumpSec).toAscii()) );
125 }
126 delete obs;
127 return;
128 }
129
130 // Save the observation
131 // --------------------
132 _epochs->insert(newTime, obs);
133
134 // Dump older epochs
135 // -----------------
136 dumpEpochs(_lastDumpSec + 1, newTime - _waitTime);
137
138 if (_lastDumpSec < newTime - _waitTime) {
139 _lastDumpSec = newTime - _waitTime;
140 }
141}
142
143// New Connection
144////////////////////////////////////////////////////////////////////////////
145void bncCaster::slotNewConnection() {
146 _sockets->push_back( _server->nextPendingConnection() );
147}
148
149// Add New Thread
150////////////////////////////////////////////////////////////////////////////
151void bncCaster::addGetThread(bncGetThread* getThread) {
152 connect(getThread, SIGNAL(error(const QByteArray&)),
153 this, SLOT(slotGetThreadError(const QByteArray&)));
154
155 _staIDs.push_back(getThread->staID());
156 _threads.push_back(getThread);
157}
158
159// Error in get thread
160////////////////////////////////////////////////////////////////////////////
161void bncCaster::slotGetThreadError(const QByteArray& staID) {
162 QMutexLocker locker(&_mutex);
163 _staIDs.removeAll(staID);
164 emit( newMessage(
165 QString("Mountpoint size %1").arg(_staIDs.size()).toAscii()) );
166 if (_staIDs.size() == 0) {
167 emit(newMessage("bncCaster:: last get thread terminated"));
168 emit getThreadErrors();
169 }
170}
171
172// Dump Complete Epochs
173////////////////////////////////////////////////////////////////////////////
174void bncCaster::dumpEpochs(long minTime, long maxTime) {
175
176 const char begEpoch = 'A';
177 const char begObs = 'B';
178 const char endEpoch = 'C';
179
180 for (long sec = minTime; sec <= maxTime; sec++) {
181
182 bool first = true;
183 QList<Observation*> allObs = _epochs->values(sec);
184 QListIterator<Observation*> it(allObs);
185 while (it.hasNext()) {
186 Observation* obs = it.next();
187
188 if (_samplingRate == 0 || sec % _samplingRate == 0) {
189
190 // Output into the file
191 // --------------------
192 if (_out) {
193 if (first) {
194 *_out << begEpoch << endl;;
195 }
196 *_out << obs->StatID << " "
197 << obs->SVPRN << " "
198 << obs->GPSWeek << " "
199 << obs->GPSWeeks << " "
200 << obs->C1 << " "
201 << obs->P1 << " "
202 << obs->P2 << " "
203 << obs->L1 << " "
204 << obs->L2 << endl;
205 if (!it.hasNext()) {
206 *_out << endEpoch << endl;
207 }
208 }
209
210 // Output into the socket
211 // ----------------------
212 if (_sockets) {
213 int numBytes = sizeof(*obs);
214 QListIterator<QTcpSocket*> is(*_sockets);
215 while (is.hasNext()) {
216 QTcpSocket* sock = is.next();
217 if (first) {
218 sock->write(&begEpoch, 1);
219 }
220 sock->write(&begObs, 1);
221 sock->write((char*) obs, numBytes);
222 if (!it.hasNext()) {
223 sock->write(&endEpoch, 1);
224 }
225 }
226 }
227 }
228
229 delete obs;
230 _epochs->remove(sec);
231 first = false;
232 }
233 }
234}
Note: See TracBrowser for help on using the repository browser.