1 |
|
---|
2 | /* -------------------------------------------------------------------------
|
---|
3 | * Bernese 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 "RTCM/RTCM.h"
|
---|
21 |
|
---|
22 | // Constructor
|
---|
23 | ////////////////////////////////////////////////////////////////////////////
|
---|
24 | bncCaster::bncCaster(const QString& outFileName, int port) {
|
---|
25 |
|
---|
26 | if ( !outFileName.isEmpty() ) {
|
---|
27 | _outFile = new QFile(outFileName);
|
---|
28 | _outFile->open(QIODevice::WriteOnly);
|
---|
29 | _out = new QTextStream(_outFile);
|
---|
30 | _out->setRealNumberNotation(QTextStream::FixedNotation);
|
---|
31 | _out->setRealNumberPrecision(5);
|
---|
32 | }
|
---|
33 | else {
|
---|
34 | _outFile = 0;
|
---|
35 | _out = 0;
|
---|
36 | }
|
---|
37 |
|
---|
38 | _port = port;
|
---|
39 |
|
---|
40 | if (_port != 0) {
|
---|
41 | _server = new QTcpServer;
|
---|
42 | _server->listen(QHostAddress::Any, _port);
|
---|
43 | connect(_server, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));
|
---|
44 | _sockets = new QList<QTcpSocket*>;
|
---|
45 | }
|
---|
46 | else {
|
---|
47 | _server = 0;
|
---|
48 | _sockets = 0;
|
---|
49 | }
|
---|
50 |
|
---|
51 | _epochs = new QMultiMap<long, Observation*>;
|
---|
52 |
|
---|
53 | _lastDumpSec = 0;
|
---|
54 | }
|
---|
55 |
|
---|
56 | // Destructor
|
---|
57 | ////////////////////////////////////////////////////////////////////////////
|
---|
58 | bncCaster::~bncCaster() {
|
---|
59 | delete _out;
|
---|
60 | delete _outFile;
|
---|
61 | }
|
---|
62 |
|
---|
63 | // Run
|
---|
64 | ////////////////////////////////////////////////////////////////////////////
|
---|
65 | void bncCaster::run() {
|
---|
66 | exec();
|
---|
67 | }
|
---|
68 |
|
---|
69 | // New Observations
|
---|
70 | ////////////////////////////////////////////////////////////////////////////
|
---|
71 | void bncCaster::slotNewObs(const QByteArray& mountPoint, Observation* obs) {
|
---|
72 |
|
---|
73 | long newTime = obs->GPSWeek * 7*24*3600 + obs->GPSWeeks;
|
---|
74 |
|
---|
75 | // First time, set the _lastDumpSec immediately
|
---|
76 | // --------------------------------------------
|
---|
77 | if (_lastDumpSec == 0) {
|
---|
78 | _lastDumpSec = newTime - 1;
|
---|
79 | }
|
---|
80 |
|
---|
81 | // An old observation - throw it away
|
---|
82 | // ----------------------------------
|
---|
83 | if (newTime <= _lastDumpSec) {
|
---|
84 | delete obs;
|
---|
85 | return;
|
---|
86 | }
|
---|
87 |
|
---|
88 | // Rename the station and save the observation
|
---|
89 | // -------------------------------------------
|
---|
90 | strncpy(obs->StatID, mountPoint.constData(),sizeof(obs->StatID));
|
---|
91 | _epochs->insert(newTime, obs);
|
---|
92 |
|
---|
93 | // Dump older epochs
|
---|
94 | // -----------------
|
---|
95 | const long waitTime = 5;
|
---|
96 | dumpEpochs(_lastDumpSec + 1, newTime - waitTime);
|
---|
97 | _lastDumpSec = newTime - waitTime;
|
---|
98 | }
|
---|
99 |
|
---|
100 | // New Connection
|
---|
101 | ////////////////////////////////////////////////////////////////////////////
|
---|
102 | void bncCaster::slotNewConnection() {
|
---|
103 | _sockets->push_back( _server->nextPendingConnection() );
|
---|
104 | }
|
---|
105 |
|
---|
106 | // Add New Thread
|
---|
107 | ////////////////////////////////////////////////////////////////////////////
|
---|
108 | void bncCaster::addGetThread(bncGetThread* getThread) {
|
---|
109 | connect(getThread, SIGNAL(newObs(const QByteArray&, Observation*)),
|
---|
110 | this, SLOT(slotNewObs(const QByteArray&, Observation*)));
|
---|
111 |
|
---|
112 | connect(getThread, SIGNAL(error(const QByteArray&)),
|
---|
113 | this, SLOT(slotGetThreadError(const QByteArray&)));
|
---|
114 |
|
---|
115 | _mountPoints.push_back(getThread->mountPoint());
|
---|
116 | }
|
---|
117 |
|
---|
118 | // Error in get thread
|
---|
119 | ////////////////////////////////////////////////////////////////////////////
|
---|
120 | void bncCaster::slotGetThreadError(const QByteArray& mountPoint) {
|
---|
121 | _mountPoints.removeAll(mountPoint);
|
---|
122 | emit( newMessage(
|
---|
123 | QString("Mountpoint size %1").arg(_mountPoints.size()).toAscii()) );
|
---|
124 | if (_mountPoints.size() == 0) {
|
---|
125 | emit(newMessage("bncCaster:: last get thread terminated"));
|
---|
126 | emit getThreadErrors();
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | // Dump Complete Epochs
|
---|
131 | ////////////////////////////////////////////////////////////////////////////
|
---|
132 | void bncCaster::dumpEpochs(long minTime, long maxTime) {
|
---|
133 |
|
---|
134 | const char begEpoch = 'A';
|
---|
135 | const char begObs = 'B';
|
---|
136 | const char endEpoch = 'C';
|
---|
137 |
|
---|
138 | for (long sec = minTime; sec <= maxTime; sec++) {
|
---|
139 |
|
---|
140 | bool first = true;
|
---|
141 | QList<Observation*> allObs = _epochs->values(sec);
|
---|
142 | QListIterator<Observation*> it(allObs);
|
---|
143 | while (it.hasNext()) {
|
---|
144 | Observation* obs = it.next();
|
---|
145 |
|
---|
146 | // Output into the file
|
---|
147 | // --------------------
|
---|
148 | if (_out) {
|
---|
149 | if (first) {
|
---|
150 | *_out << begEpoch << endl;;
|
---|
151 | }
|
---|
152 | *_out << obs->StatID << " "
|
---|
153 | << (int) obs->SVPRN << " "
|
---|
154 | << (int) obs->GPSWeek << " "
|
---|
155 | << obs->GPSWeeks << " "
|
---|
156 | << obs->sec << " "
|
---|
157 | << obs->pCodeIndicator << " "
|
---|
158 | << obs->cumuLossOfCont << " "
|
---|
159 | << obs->C1 << " "
|
---|
160 | << obs->P2 << " "
|
---|
161 | << obs->L1 << " "
|
---|
162 | << obs->L2 << endl;
|
---|
163 | if (!it.hasNext()) {
|
---|
164 | *_out << endEpoch << endl;
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | // Output into the socket
|
---|
169 | // ----------------------
|
---|
170 | if (_sockets) {
|
---|
171 | int numBytes = sizeof(*obs);
|
---|
172 | QListIterator<QTcpSocket*> is(*_sockets);
|
---|
173 | while (is.hasNext()) {
|
---|
174 | QTcpSocket* sock = is.next();
|
---|
175 | if (first) {
|
---|
176 | sock->write(&begEpoch, 1);
|
---|
177 | }
|
---|
178 | sock->write(&begObs, 1);
|
---|
179 | sock->write((char*) obs, numBytes);
|
---|
180 | if (!it.hasNext()) {
|
---|
181 | sock->write(&endEpoch, 1);
|
---|
182 | }
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | // Prepare RINEX Output
|
---|
187 | // --------------------
|
---|
188 | if (1) {
|
---|
189 | if (_rinexWriters.find(obs->StatID) == _rinexWriters.end()) {
|
---|
190 | _rinexWriters.insert(obs->StatID, new bncRinex(obs->StatID));
|
---|
191 | }
|
---|
192 | bncRinex* rnx = _rinexWriters.find(obs->StatID).value();
|
---|
193 | rnx->deepCopy(obs);
|
---|
194 | }
|
---|
195 |
|
---|
196 | delete obs;
|
---|
197 | _epochs->remove(sec);
|
---|
198 | first = false;
|
---|
199 | }
|
---|
200 |
|
---|
201 | // Write RINEX Files
|
---|
202 | // -----------------
|
---|
203 | QMapIterator<QString, bncRinex*> ir(_rinexWriters);
|
---|
204 | while (ir.hasNext()) {
|
---|
205 | bncRinex* rnx = ir.next().value();
|
---|
206 | rnx->dumpEpoch();
|
---|
207 | }
|
---|
208 | }
|
---|
209 | }
|
---|