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

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

* empty log message *

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