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

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

* empty log message *

File size: 6.2 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, Observation* obs) {
88
89 QMutexLocker locker(&_mutex);
90
91 long iSec = long(floor(obs->GPSWeeks+0.5));
92 long newTime = obs->GPSWeek * 7*24*3600 + iSec;
93
94 // Rename the Station
95 // ------------------
96 strncpy(obs->StatID, staID.constData(),sizeof(obs->StatID));
97
98 // Prepare RINEX Output
99 // --------------------
100 if (_rinexWriters.find(obs->StatID) == _rinexWriters.end()) {
101 _rinexWriters.insert(obs->StatID, new bncRinex(obs->StatID));
102 }
103 bncRinex* rnx = _rinexWriters.find(obs->StatID).value();
104 if (_samplingRate == 0 || iSec % _samplingRate == 0) {
105 rnx->deepCopy(obs);
106 }
107 rnx->dumpEpoch(newTime);
108
109 // First time, set the _lastDumpSec immediately
110 // --------------------------------------------
111 if (_lastDumpSec == 0) {
112 _lastDumpSec = newTime - 1;
113 }
114
115 // An old observation - throw it away
116 // ----------------------------------
117 if (newTime <= _lastDumpSec) {
118 QSettings settings;
119 if ( !settings.value("outFile").toString().isEmpty() ||
120 !settings.value("outPort").toString().isEmpty() ) {
121 emit( newMessage(QString("Station %1: old epoch %2 thrown away")
122 .arg(staID.data()).arg(iSec).toAscii()) );
123 }
124 delete obs;
125 return;
126 }
127
128 // Save the observation
129 // --------------------
130 _epochs->insert(newTime, obs);
131
132 // Dump older epochs
133 // -----------------
134 dumpEpochs(_lastDumpSec + 1, newTime - _waitTime);
135 _lastDumpSec = newTime - _waitTime;
136}
137
138// New Connection
139////////////////////////////////////////////////////////////////////////////
140void bncCaster::slotNewConnection() {
141 _sockets->push_back( _server->nextPendingConnection() );
142}
143
144// Add New Thread
145////////////////////////////////////////////////////////////////////////////
146void bncCaster::addGetThread(bncGetThread* getThread) {
147 connect(getThread, SIGNAL(error(const QByteArray&)),
148 this, SLOT(slotGetThreadError(const QByteArray&)));
149
150 _staIDs.push_back(getThread->staID());
151 _threads.push_back(getThread);
152}
153
154// Error in get thread
155////////////////////////////////////////////////////////////////////////////
156void bncCaster::slotGetThreadError(const QByteArray& staID) {
157 QMutexLocker locker(&_mutex);
158 _staIDs.removeAll(staID);
159 emit( newMessage(
160 QString("Mountpoint size %1").arg(_staIDs.size()).toAscii()) );
161 if (_staIDs.size() == 0) {
162 emit(newMessage("bncCaster:: last get thread terminated"));
163 emit getThreadErrors();
164 }
165}
166
167// Dump Complete Epochs
168////////////////////////////////////////////////////////////////////////////
169void bncCaster::dumpEpochs(long minTime, long maxTime) {
170
171 const char begEpoch = 'A';
172 const char begObs = 'B';
173 const char endEpoch = 'C';
174
175 for (long sec = minTime; sec <= maxTime; sec++) {
176
177 bool first = true;
178 QList<Observation*> allObs = _epochs->values(sec);
179 QListIterator<Observation*> it(allObs);
180 while (it.hasNext()) {
181 Observation* obs = it.next();
182
183 if (_samplingRate == 0 || sec % _samplingRate == 0) {
184
185 // Output into the file
186 // --------------------
187 if (_out) {
188 if (first) {
189 *_out << begEpoch << endl;;
190 }
191 *_out << obs->StatID << " "
192 << obs->SVPRN << " "
193 << obs->GPSWeek << " "
194 << obs->GPSWeeks << " "
195 << obs->C1 << " "
196 << obs->P1 << " "
197 << obs->P2 << " "
198 << obs->L1 << " "
199 << obs->L2 << endl;
200 if (!it.hasNext()) {
201 *_out << endEpoch << endl;
202 }
203 }
204
205 // Output into the socket
206 // ----------------------
207 if (_sockets) {
208 int numBytes = sizeof(*obs);
209 QListIterator<QTcpSocket*> is(*_sockets);
210 while (is.hasNext()) {
211 QTcpSocket* sock = is.next();
212 if (first) {
213 sock->write(&begEpoch, 1);
214 }
215 sock->write(&begObs, 1);
216 sock->write((char*) obs, numBytes);
217 if (!it.hasNext()) {
218 sock->write(&endEpoch, 1);
219 }
220 }
221 }
222 }
223
224 delete obs;
225 _epochs->remove(sec);
226 first = false;
227 }
228 }
229}
Note: See TracBrowser for help on using the repository browser.