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

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

* empty log message *

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