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

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

* empty log message *

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