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

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

* empty log message *

File size: 7.7 KB
RevLine 
[756]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 <iostream>
[800]18#include <newmatio.h>
[756]19
20#include "bns.h"
[799]21#include "bnsutils.h"
[756]22
23using namespace std;
24
25// Constructor
26////////////////////////////////////////////////////////////////////////////
[757]27t_bns::t_bns(QObject* parent) : QThread(parent) {
[760]28
[764]29 this->setTerminationEnabled(true);
30
[787]31 // Thread that handles broadcast ephemeris
32 // ---------------------------------------
[758]33 _bnseph = new t_bnseph(parent);
[760]34
[787]35 // Server listening for rtnet results
36 // ----------------------------------
[786]37 QSettings settings;
[769]38 _clkSocket = 0;
[786]39 _clkServer = new QTcpServer;
[787]40 _clkServer->listen(QHostAddress::Any, settings.value("clkPort").toInt());
[770]41
[816]42 // Socket and file for outputting the results
43 // -------------------------------------------
[770]44 _outSocket = 0;
[816]45
46 QString outFileName = settings.value("outFile").toString();
47 if (outFileName.isEmpty()) {
48 _outFile = 0;
[811]49 }
[816]50 else {
51 _outFile = new QFile(outFileName);
[817]52 if (_outFile->open(QIODevice::WriteOnly | QIODevice::Unbuffered)) {
[816]53 _outStream = new QTextStream(_outFile);
54 }
55 }
[812]56
57 // Log File
58 // --------
[816]59 QString logFileName = settings.value("logFile").toString();
60 if (logFileName.isEmpty()) {
61 _logFile = 0;
[812]62 }
[816]63 else {
64 _logFile = new QFile(logFileName);
[817]65 if (_logFile->open(QIODevice::WriteOnly | QIODevice::Unbuffered)) {
[816]66 _logStream = new QTextStream(_logFile);
67 }
68 }
[756]69}
70
71// Destructor
72////////////////////////////////////////////////////////////////////////////
[757]73t_bns::~t_bns() {
[763]74 deleteBnsEph();
[769]75 delete _clkServer;
[789]76 /// delete _clkSocket;
[770]77 delete _outSocket;
[816]78 delete _outStream;
79 delete _logStream;
[812]80 delete _outFile;
81 delete _logFile;
[779]82 QMapIterator<QString, t_ephPair*> it(_ephList);
83 while (it.hasNext()) {
84 it.next();
85 delete it.value();
86 }
[756]87}
88
[763]89// Delete bns thread
90////////////////////////////////////////////////////////////////////////////
91void t_bns::deleteBnsEph() {
92 if (_bnseph) {
93 _bnseph->terminate();
[764]94 _bnseph->wait(100);
[763]95 delete _bnseph;
96 _bnseph = 0;
97 }
98}
99
[756]100// Write a Program Message
101////////////////////////////////////////////////////////////////////////////
[758]102void t_bns::slotMessage(const QByteArray msg) {
[816]103 if (_logStream) {
104 *_logStream << msg << endl;
[818]105 _logStream->flush();
[812]106 }
[757]107 emit(newMessage(msg));
[756]108}
109
[760]110// Write a Program Message
111////////////////////////////////////////////////////////////////////////////
112void t_bns::slotError(const QByteArray msg) {
[816]113 if (_logStream) {
114 *_logStream << msg << endl;
[818]115 _logStream->flush();
[812]116 }
[763]117 deleteBnsEph();
[760]118 emit(error(msg));
119}
120
[769]121// New Connection
122////////////////////////////////////////////////////////////////////////////
123void t_bns::slotNewConnection() {
[786]124 slotMessage("t_bns::slotNewConnection");
[787]125 delete _clkSocket;
[769]126 _clkSocket = _clkServer->nextPendingConnection();
127}
128
[770]129// Start the Communication with NTRIP Caster
130////////////////////////////////////////////////////////////////////////////
131void t_bns::openCaster() {
132
133 QSettings settings;
134
135 _outSocket = new QTcpSocket();
[811]136 _outSocket->connectToHost(settings.value("outHost").toString(),
137 settings.value("outPort").toInt());
[770]138
[819]139 const int timeOut = 100; // 0.1 seconds
140 if (!_outSocket->waitForConnected(timeOut)) {
141 delete _outSocket;
142 _outSocket = 0;
143 emit(error("bns::openCaster Connect Timeout"));
144 }
145
[770]146 QString mountpoint = settings.value("mountpoint").toString();
147 QString password = settings.value("password").toString();
148
149 QByteArray msg = "SOURCE " + password.toAscii() + " /" +
150 mountpoint.toAscii() + "\r\n" +
151 "Source-Agent: NTRIP BNS/1.0\r\n\r\n";
152
153 _outSocket->write(msg);
[820]154 _outSocket->waitForBytesWritten();
[770]155
[820]156 _outSocket->waitForReadyRead();
[770]157 QByteArray ans = _outSocket->readLine();
158
159 if (ans.indexOf("OK") == -1) {
160 delete _outSocket;
161 _outSocket = 0;
[822]162 emit(slotMessage("bns::openCaster socket deleted"));
[770]163 }
[821]164 else {
[822]165 emit(slotMessage("bns::openCaster socket OK"));
[821]166 }
[770]167}
168
[784]169//
170////////////////////////////////////////////////////////////////////////////
171void t_bns::slotNewEph(gpsEph* ep) {
172
173 QMutexLocker locker(&_mutex);
174
175 t_ephPair* pair;
176 if ( !_ephList.contains(ep->prn) ) {
177 pair = new t_ephPair();
178 _ephList.insert(ep->prn, pair);
179 }
180 else {
181 pair = _ephList[ep->prn];
182 }
183
184 if (pair->eph == 0) {
185 pair->eph = ep;
186 }
187 else {
188 if (ep->GPSweek > pair->eph->GPSweek ||
189 (ep->GPSweek == pair->eph->GPSweek && ep->TOC > pair->eph->TOC)) {
190 delete pair->oldEph;
191 pair->oldEph = pair->eph;
192 pair->eph = ep;
193 }
194 else {
195 delete ep;
196 }
197 }
198}
199
[756]200// Start
201////////////////////////////////////////////////////////////////////////////
[757]202void t_bns::run() {
[769]203
[758]204 slotMessage("============ Start BNS ============");
[770]205
206 // Start Thread that retrieves broadcast Ephemeris
207 // -----------------------------------------------
[758]208 _bnseph->start();
[769]209
[824]210 // Connect Slots-Signals
211 // ---------------------
212 connect(_bnseph, SIGNAL(newEph(gpsEph*)),
213 this, SLOT(slotNewEph(gpsEph*)));
214
215 connect(_bnseph, SIGNAL(newMessage(QByteArray)),
216 this, SLOT(slotMessage(const QByteArray)));
217
218 connect(_bnseph, SIGNAL(error(QByteArray)),
219 this, SLOT(slotError(const QByteArray)));
220
221 connect(_clkServer, SIGNAL(newConnection()),this,
222 SLOT(slotNewConnection()));
223
[770]224 // Endless loop
225 // ------------
[769]226 while (true) {
[796]227 if (_clkSocket && _clkSocket->state() == QAbstractSocket::ConnectedState) {
228 if ( _clkSocket->canReadLine()) {
[811]229 if (_outSocket == 0) {
230 openCaster();
231 }
[796]232 readEpoch();
233 }
[809]234 else {
235 _clkSocket->waitForReadyRead(10);
236 }
[769]237 }
238 else {
[794]239 msleep(10);
[769]240 }
241 }
[756]242}
243
[778]244//
245////////////////////////////////////////////////////////////////////////////
[784]246void t_bns::readEpoch() {
[778]247
[784]248 QByteArray line = _clkSocket->readLine();
[786]249
[784]250 if (line.indexOf('*') == -1) {
251 return;
[778]252 }
253
[784]254 QTextStream in(line);
255
256 QString hlp;
[798]257 int GPSweek, numSat;
258 double GPSweeks;
[784]259
[798]260 in >> hlp >> GPSweek >> GPSweeks >> numSat;
[784]261
262 for (int ii = 1; ii <= numSat; ii++) {
[792]263 line = _clkSocket->readLine();
[791]264
[784]265 QTextStream in(line);
266
267 QString prn;
268 ColumnVector xx(4);
269
[795]270 in >> prn >> xx(1) >> xx(2) >> xx(3) >> xx(4);
[797]271 xx(4) *= 1e-6;
[784]272
[798]273 processSatellite(GPSweek, GPSweeks, prn, xx);
[780]274 }
[778]275}
[784]276
277//
278////////////////////////////////////////////////////////////////////////////
[798]279void t_bns::processSatellite(int GPSweek, double GPSweeks, const QString& prn,
[784]280 const ColumnVector& xx) {
281
[795]282 // No broadcast ephemeris available
283 // --------------------------------
284 if ( !_ephList.contains(prn) ) {
285 return;
286 }
287
288 t_ephPair* pair = _ephList[prn];
289 gpsEph* ep = pair->eph;
290
[799]291 ColumnVector xB(4);
[802]292 ColumnVector vv(3);
[799]293
[803]294 satellitePosition(GPSweek, GPSweeks, ep, xB(1), xB(2), xB(3), xB(4),
[802]295 vv(1), vv(2), vv(3));
[799]296
[804]297 ColumnVector dx = xx.Rows(1,3) - xB.Rows(1,3);
298 double dClk = (xx(4) - xB(4)) * 299792458.0;
299 ColumnVector rsw(3);
[800]300
[806]301 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
[804]302
[811]303 QString line;
[817]304 line.sprintf("%d %.1f %s %3d %3d %8.3f %8.3f %8.3f %8.3f\n",
[811]305 GPSweek, GPSweeks, ep->prn.toAscii().data(),
306 int(ep->IODC), int(ep->IODE), dClk, rsw(1), rsw(2), rsw(3));
307
[816]308 if (_outStream) {
309 *_outStream << line;
[818]310 _outStream->flush();
[811]311 }
312 if (_outSocket) {
313 _outSocket->write(line.toAscii());
314 }
[784]315}
Note: See TracBrowser for help on using the repository browser.