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

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

* empty log message *

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