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

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

* empty log message *

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