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

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