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

Last change on this file since 831 was 831, checked in by mervart, 17 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);
[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;
[831]169 slotMessage("bns::openCaster socket deleted");
[770]170 }
[831]171 else {
172 slotMessage("bns::openCaster socket OK");
173 }
[770]174}
175
[784]176//
177////////////////////////////////////////////////////////////////////////////
178void t_bns::slotNewEph(gpsEph* ep) {
179
180 QMutexLocker locker(&_mutex);
181
182 t_ephPair* pair;
183 if ( !_ephList.contains(ep->prn) ) {
184 pair = new t_ephPair();
185 _ephList.insert(ep->prn, pair);
186 }
187 else {
188 pair = _ephList[ep->prn];
189 }
190
191 if (pair->eph == 0) {
192 pair->eph = ep;
193 }
194 else {
195 if (ep->GPSweek > pair->eph->GPSweek ||
196 (ep->GPSweek == pair->eph->GPSweek && ep->TOC > pair->eph->TOC)) {
197 delete pair->oldEph;
198 pair->oldEph = pair->eph;
199 pair->eph = ep;
200 }
201 else {
202 delete ep;
203 }
204 }
205}
206
[756]207// Start
208////////////////////////////////////////////////////////////////////////////
[757]209void t_bns::run() {
[769]210
[758]211 slotMessage("============ Start BNS ============");
[770]212
[828]213 // Start Thread that retrieves broadcast Ephemeris
214 // -----------------------------------------------
[758]215 _bnseph->start();
[769]216
[770]217 // Endless loop
218 // ------------
[769]219 while (true) {
[796]220 if (_clkSocket && _clkSocket->state() == QAbstractSocket::ConnectedState) {
221 if ( _clkSocket->canReadLine()) {
[830]222 if (_outSocket == 0) {
223 openCaster();
224 }
[796]225 readEpoch();
226 }
[809]227 else {
228 _clkSocket->waitForReadyRead(10);
229 }
[769]230 }
231 else {
[794]232 msleep(10);
[769]233 }
234 }
[756]235}
236
[778]237//
238////////////////////////////////////////////////////////////////////////////
[784]239void t_bns::readEpoch() {
[778]240
[784]241 QByteArray line = _clkSocket->readLine();
[786]242
[784]243 if (line.indexOf('*') == -1) {
244 return;
[778]245 }
246
[784]247 QTextStream in(line);
248
249 QString hlp;
[798]250 int GPSweek, numSat;
251 double GPSweeks;
[784]252
[798]253 in >> hlp >> GPSweek >> GPSweeks >> numSat;
[784]254
255 for (int ii = 1; ii <= numSat; ii++) {
[792]256 line = _clkSocket->readLine();
[791]257
[784]258 QTextStream in(line);
259
260 QString prn;
261 ColumnVector xx(4);
262
[795]263 in >> prn >> xx(1) >> xx(2) >> xx(3) >> xx(4);
[797]264 xx(4) *= 1e-6;
[784]265
[798]266 processSatellite(GPSweek, GPSweeks, prn, xx);
[780]267 }
[778]268}
[784]269
270//
271////////////////////////////////////////////////////////////////////////////
[798]272void t_bns::processSatellite(int GPSweek, double GPSweeks, const QString& prn,
[784]273 const ColumnVector& xx) {
274
[795]275 // No broadcast ephemeris available
276 // --------------------------------
277 if ( !_ephList.contains(prn) ) {
278 return;
279 }
280
281 t_ephPair* pair = _ephList[prn];
282 gpsEph* ep = pair->eph;
283
[799]284 ColumnVector xB(4);
[802]285 ColumnVector vv(3);
[799]286
[803]287 satellitePosition(GPSweek, GPSweeks, ep, xB(1), xB(2), xB(3), xB(4),
[802]288 vv(1), vv(2), vv(3));
[799]289
[804]290 ColumnVector dx = xx.Rows(1,3) - xB.Rows(1,3);
291 double dClk = (xx(4) - xB(4)) * 299792458.0;
292 ColumnVector rsw(3);
[800]293
[806]294 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
[804]295
[811]296 QString line;
[817]297 line.sprintf("%d %.1f %s %3d %3d %8.3f %8.3f %8.3f %8.3f\n",
[811]298 GPSweek, GPSweeks, ep->prn.toAscii().data(),
299 int(ep->IODC), int(ep->IODE), dClk, rsw(1), rsw(2), rsw(3));
300
[816]301 if (_outStream) {
302 *_outStream << line;
[818]303 _outStream->flush();
[811]304 }
305 if (_outSocket) {
306 _outSocket->write(line.toAscii());
[831]307 _outSocket->flush();
[811]308 }
[784]309}
Note: See TracBrowser for help on using the repository browser.