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

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

* empty log message *

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