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

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