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

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

* empty log message *

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