source: ntrip/trunk/BNS/bns.cpp@ 979

Last change on this file since 979 was 979, checked in by mervart, 16 years ago

* empty log message *

File size: 11.9 KB
Line 
1/* -------------------------------------------------------------------------
2 * BKG NTRIP Server
3 * -------------------------------------------------------------------------
4 *
5 * Class: bns
6 *
7 * Purpose: This class implements the main application behaviour
8 *
9 * Author: L. Mervart
10 *
11 * Created: 29-Mar-2008
12 *
13 * Changes:
14 *
15 * -----------------------------------------------------------------------*/
16
17#include <math.h>
18#include <iostream>
19#include <newmatio.h>
20
21#include "bns.h"
22#include "bnsutils.h"
23#include "bnsrinex.h"
24#include "bnssp3.h"
25
26using namespace std;
27
28// Constructor
29////////////////////////////////////////////////////////////////////////////
30t_bns::t_bns(QObject* parent) : QThread(parent) {
31
32 this->setTerminationEnabled(true);
33
34 connect(this, SIGNAL(moveSocket(QThread*)),
35 this, SLOT(slotMoveSocket(QThread*)));
36
37 QSettings settings;
38
39 // Set Proxy (application-wide)
40 // ----------------------------
41 QString proxyHost = settings.value("proxyHost").toString();
42 int proxyPort = settings.value("proxyPort").toInt();
43 if (!proxyHost.isEmpty()) {
44 QNetworkProxy proxy;
45 proxy.setType(QNetworkProxy::Socks5Proxy);
46 proxy.setHostName(proxyHost);
47 proxy.setPort(proxyPort);
48 QNetworkProxy::setApplicationProxy(proxy);
49 }
50
51 // Thread that handles broadcast ephemeris
52 // ---------------------------------------
53 _bnseph = new t_bnseph(parent);
54
55 connect(_bnseph, SIGNAL(newEph(t_eph*)), this, SLOT(slotNewEph(t_eph*)));
56 connect(_bnseph, SIGNAL(newMessage(QByteArray)),
57 this, SLOT(slotMessage(const QByteArray)));
58 connect(_bnseph, SIGNAL(error(QByteArray)),
59 this, SLOT(slotError(const QByteArray)));
60
61 // Server listening for rtnet results
62 // ----------------------------------
63 _clkSocket = 0;
64 _clkServer = new QTcpServer;
65 _clkServer->listen(QHostAddress::Any, settings.value("clkPort").toInt());
66 connect(_clkServer, SIGNAL(newConnection()),this, SLOT(slotNewConnection()));
67
68 // Socket and file for outputting the results
69 // -------------------------------------------
70 _outSocket = 0;
71 _outSocketOpenTrial = 0;
72
73 QString outFileName = settings.value("outFile").toString();
74 if (outFileName.isEmpty()) {
75 _outFile = 0;
76 _outStream = 0;
77 }
78 else {
79 _outFile = new QFile(outFileName);
80 if (_outFile->open(QIODevice::WriteOnly | QIODevice::Unbuffered)) {
81 _outStream = new QTextStream(_outFile);
82 }
83 }
84
85 // Log File
86 // --------
87 QString logFileName = settings.value("logFile").toString();
88 if (logFileName.isEmpty()) {
89 _logFile = 0;
90 _logStream = 0;
91 }
92 else {
93 _logFile = new QFile(logFileName);
94 if (_logFile->open(QIODevice::WriteOnly | QIODevice::Unbuffered)) {
95 _logStream = new QTextStream(_logFile);
96 }
97 else {
98 _logStream = 0;
99 }
100 }
101
102 // RINEX writer
103 // ------------
104 if ( settings.value("rnxPath").toString().isEmpty() ) {
105 _rnx = 0;
106 }
107 else {
108 QString prep = "BNS";
109 QString ext = ".clk";
110 QString path = settings.value("rnxPath").toString();
111 QString intr = settings.value("rnxIntr").toString();
112 int sampl = settings.value("rnxSampl").toInt();
113 _rnx = new bnsRinex(prep, ext, path, intr, sampl);
114 }
115
116 // SP3 writer
117 // ----------
118 if ( settings.value("sp3Path").toString().isEmpty() ) {
119 _sp3 = 0;
120 }
121 else {
122 QString prep = "BNS";
123 QString ext = ".sp3";
124 QString path = settings.value("sp3Path").toString();
125 QString intr = settings.value("sp3Intr").toString();
126 int sampl = settings.value("sp3Sampl").toInt();
127 _sp3 = new bnsSP3(prep, ext, path, intr, sampl);
128 }
129}
130
131// Destructor
132////////////////////////////////////////////////////////////////////////////
133t_bns::~t_bns() {
134 deleteBnsEph();
135 delete _clkServer;
136 delete _clkSocket;
137 delete _outSocket;
138 delete _outStream;
139 delete _logStream;
140 delete _outFile;
141 delete _logFile;
142 QMapIterator<QString, t_ephPair*> it(_ephList);
143 while (it.hasNext()) {
144 it.next();
145 delete it.value();
146 }
147 delete _rnx;
148 delete _sp3;
149}
150
151// Delete bns thread
152////////////////////////////////////////////////////////////////////////////
153void t_bns::deleteBnsEph() {
154 if (_bnseph) {
155 _bnseph->terminate();
156 _bnseph->wait(100);
157 delete _bnseph;
158 _bnseph = 0;
159 }
160}
161
162// Write a Program Message
163////////////////////////////////////////////////////////////////////////////
164void t_bns::slotMessage(const QByteArray msg) {
165 if (_logStream) {
166 *_logStream << msg << endl;
167 _logStream->flush();
168 }
169 emit(newMessage(msg));
170}
171
172// Write a Program Message
173////////////////////////////////////////////////////////////////////////////
174void t_bns::slotError(const QByteArray msg) {
175 if (_logStream) {
176 *_logStream << msg << endl;
177 _logStream->flush();
178 }
179 deleteBnsEph();
180 emit(error(msg));
181}
182
183// New Connection
184////////////////////////////////////////////////////////////////////////////
185void t_bns::slotNewConnection() {
186 slotMessage("t_bns::slotNewConnection");
187 delete _clkSocket;
188 _clkSocket = _clkServer->nextPendingConnection();
189}
190
191// Start the Communication with NTRIP Caster
192////////////////////////////////////////////////////////////////////////////
193void t_bns::openCaster() {
194
195 delete _outSocket; _outSocket = 0;
196
197 double minDt = exp2(_outSocketOpenTrial);
198 if (++_outSocketOpenTrial > 8) {
199 _outSocketOpenTrial = 8;
200 }
201 if (_outSocketOpenTime.isValid() &&
202 _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
203 return;
204 }
205 else {
206 _outSocketOpenTime = QDateTime::currentDateTime();
207 }
208
209 QSettings settings;
210 _outSocket = new QTcpSocket();
211 _outSocket->connectToHost(settings.value("outHost").toString(),
212 settings.value("outPort").toInt());
213
214 const int timeOut = 100; // 0.1 seconds
215 if (!_outSocket->waitForConnected(timeOut)) {
216 delete _outSocket;
217 _outSocket = 0;
218 emit(error("bns::openCaster Connect Timeout"));
219 return;
220 }
221
222 QString mountpoint = settings.value("mountpoint").toString();
223 QString password = settings.value("password").toString();
224
225 QByteArray msg = "SOURCE " + password.toAscii() + " /" +
226 mountpoint.toAscii() + "\r\n" +
227 "Source-Agent: NTRIP BNS/1.0\r\n\r\n";
228
229 _outSocket->write(msg);
230 _outSocket->waitForBytesWritten();
231
232 _outSocket->waitForReadyRead();
233 QByteArray ans = _outSocket->readLine();
234
235 if (ans.indexOf("OK") == -1) {
236 delete _outSocket;
237 _outSocket = 0;
238 slotMessage("bns::openCaster socket deleted");
239 }
240 else {
241 slotMessage("bns::openCaster socket OK");
242 _outSocketOpenTrial = 0;
243 }
244}
245
246//
247////////////////////////////////////////////////////////////////////////////
248void t_bns::slotNewEph(t_eph* ep) {
249
250 QMutexLocker locker(&_mutex);
251
252 t_ephPair* pair;
253 if ( !_ephList.contains(ep->prn()) ) {
254 pair = new t_ephPair();
255 _ephList.insert(ep->prn(), pair);
256 }
257 else {
258 pair = _ephList[ep->prn()];
259 }
260
261 if (pair->eph == 0) {
262 pair->eph = ep;
263 }
264 else {
265 if (ep->isNewerThan(pair->eph)) {
266 delete pair->oldEph;
267 pair->oldEph = pair->eph;
268 pair->eph = ep;
269 }
270 else {
271 delete ep;
272 }
273 }
274}
275
276// Start
277////////////////////////////////////////////////////////////////////////////
278void t_bns::run() {
279
280 slotMessage("============ Start BNS ============");
281
282 // Start Thread that retrieves broadcast Ephemeris
283 // -----------------------------------------------
284 _bnseph->start();
285
286 // Endless loop
287 // ------------
288 while (true) {
289
290 if (_clkSocket && _clkSocket->thread() != currentThread()) {
291 emit(moveSocket(currentThread()));
292 }
293
294 if (_clkSocket && _clkSocket->state() == QAbstractSocket::ConnectedState) {
295 if ( _clkSocket->canReadLine()) {
296 if (_outSocket == 0 ||
297 _outSocket->state() != QAbstractSocket::ConnectedState) {
298 openCaster();
299 }
300 readEpoch();
301 }
302 else {
303 _clkSocket->waitForReadyRead(10);
304 }
305 }
306 else {
307 msleep(10);
308 }
309 }
310}
311
312//
313////////////////////////////////////////////////////////////////////////////
314void t_bns::readEpoch() {
315
316 QByteArray line = _clkSocket->readLine();
317
318 if (line.indexOf('*') == -1) {
319 return;
320 }
321
322 QTextStream in(line);
323
324 QString hlp;
325 int GPSweek, numSat;
326 double GPSweeks;
327
328 in >> hlp >> GPSweek >> GPSweeks >> numSat;
329
330 if (numSat > 0) {
331
332 QStringList prns;
333
334 /// for (int oldEph = 0; oldEph <= 1; oldEph++) {
335 for (int oldEph = 0; oldEph <= 0; oldEph++) {
336
337 struct ClockOrbit co;
338 memset(&co, 0, sizeof(co));
339 co.GPSEpochTime = (int)GPSweeks;
340 co.GLONASSEpochTime = (int)fmod(GPSweeks, 86400.0);
341 co.ClockDataSupplied = 1;
342 co.OrbitDataSupplied = 1;
343 co.SatRefPoint = POINT_CENTER;
344 co.SatRefDatum = DATUM_ITRF;
345
346 for (int ii = 1; ii <= numSat; ii++) {
347
348 QString prn;
349 ColumnVector xx(5);
350 t_eph* ep = 0;
351
352 if (oldEph == 0) {
353 line = _clkSocket->readLine();
354 QTextStream in(line);
355 in >> prn;
356 prns << prn;
357 if ( _ephList.contains(prn) ) {
358 in >> xx(1) >> xx(2) >> xx(3) >> xx(4) >> xx(5); xx(4) *= 1e-6;
359
360 //// beg test (zero clock correction for Gerhard Wuebbena)
361 //// xx(4) -= xx(5) / 299792458.0;
362 //// end test
363
364 t_ephPair* pair = _ephList[prn];
365 pair->xx = xx;
366 ep = pair->eph;
367 }
368 }
369 else {
370 prn = prns[ii-1];
371 if ( _ephList.contains(prn) ) {
372 t_ephPair* pair = _ephList[prn];
373 prn = pair->eph->prn();
374 xx = pair->xx;
375 ep = pair->oldEph;
376 }
377 }
378
379 if (ep != 0) {
380 struct ClockOrbit::SatData* sd = 0;
381 if (prn[0] == 'G') {
382 sd = co.Sat + co.NumberOfGPSSat;
383 ++co.NumberOfGPSSat;
384 }
385 else if (prn[0] == 'R') {
386 sd = co.Sat + CLOCKORBIT_NUMGPS + co.NumberOfGLONASSSat;
387 ++co.NumberOfGLONASSSat;
388 }
389 if (sd) {
390 processSatellite(oldEph, ep, GPSweek, GPSweeks, prn, xx, sd);
391 }
392 }
393 }
394
395 if ( _outSocket &&
396 (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) ) {
397 char obuffer[CLOCKORBIT_BUFFERSIZE];
398 int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
399 if (len > 0) {
400 _outSocket->write(obuffer, len);
401 _outSocket->flush();
402 }
403 }
404 }
405 }
406}
407
408//
409////////////////////////////////////////////////////////////////////////////
410void t_bns::processSatellite(int oldEph, t_eph* ep, int GPSweek, double GPSweeks,
411 const QString& prn, const ColumnVector& xx,
412 struct ClockOrbit::SatData* sd) {
413
414 ColumnVector xB(4);
415 ColumnVector vv(3);
416
417 ep->position(GPSweek, GPSweeks, xB, vv);
418
419 ColumnVector dx = xx.Rows(1,3) - xB.Rows(1,3);
420
421 double dClk = (xx(4) - xB(4)) * 299792458.0;
422 ColumnVector rsw(3);
423
424 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
425
426 if (sd) {
427 sd->ID = prn.mid(1).toInt();
428 sd->IOD = ep->IOD();
429 sd->Clock.DeltaA0 = dClk;
430 sd->Orbit.DeltaRadial = rsw(1);
431 sd->Orbit.DeltaAlongTrack = rsw(2);
432 sd->Orbit.DeltaCrossTrack = rsw(3);
433 }
434
435 if (_outStream) {
436 QString line;
437 char oldCh = (oldEph ? '!' : ' ');
438 line.sprintf("%c %d %.1f %s %3d %10.3f %8.3f %8.3f %8.3f\n",
439 oldCh, GPSweek, GPSweeks, ep->prn().toAscii().data(),
440 ep->IOD(), dClk, rsw(1), rsw(2), rsw(3));
441 *_outStream << line;
442 _outStream->flush();
443 }
444 if (!oldEph) {
445 if (_rnx) {
446 _rnx->write(GPSweek, GPSweeks, prn, xx);
447 }
448 if (_sp3) {
449 _sp3->write(GPSweek, GPSweeks, prn, xx);
450 }
451 }
452}
453
454//
455////////////////////////////////////////////////////////////////////////////
456void t_bns::slotMoveSocket(QThread* tt) {
457 _clkSocket->setParent(0);
458 _clkSocket->moveToThread(tt);
459 slotMessage("bns::slotMoveSocket");
460}
Note: See TracBrowser for help on using the repository browser.