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

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