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

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

* empty log message *

File size: 7.5 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 <iostream>
18#include <newmatio.h>
19
20#include "bns.h"
21#include "bnsutils.h"
22
23using namespace std;
24
25// Constructor
26////////////////////////////////////////////////////////////////////////////
27t_bns::t_bns(QObject* parent) : QThread(parent) {
28
29 this->setTerminationEnabled(true);
30
31 // Thread that handles broadcast ephemeris
32 // ---------------------------------------
33 _bnseph = new t_bnseph(parent);
34
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)));
40
41 // Server listening for rtnet results
42 // ----------------------------------
43 QSettings settings;
44 _clkSocket = 0;
45 _clkServer = new QTcpServer;
46 _clkServer->listen(QHostAddress::Any, settings.value("clkPort").toInt());
47 connect(_clkServer, SIGNAL(newConnection()),this, SLOT(slotNewConnection()));
48
49 // Socket and file for outputting the results
50 // -------------------------------------------
51 _outSocket = 0;
52
53 QString outFileName = settings.value("outFile").toString();
54 if (outFileName.isEmpty()) {
55 _outFile = 0;
56 }
57 else {
58 _outFile = new QFile(outFileName);
59 if (_outFile->open(QIODevice::WriteOnly | QIODevice::Unbuffered)) {
60 _outStream = new QTextStream(_outFile);
61 }
62 }
63
64 // Log File
65 // --------
66 QString logFileName = settings.value("logFile").toString();
67 if (logFileName.isEmpty()) {
68 _logFile = 0;
69 }
70 else {
71 _logFile = new QFile(logFileName);
72 if (_logFile->open(QIODevice::WriteOnly | QIODevice::Unbuffered)) {
73 _logStream = new QTextStream(_logFile);
74 }
75 }
76}
77
78// Destructor
79////////////////////////////////////////////////////////////////////////////
80t_bns::~t_bns() {
81 deleteBnsEph();
82 delete _clkServer;
83 /// delete _clkSocket;
84 delete _outSocket;
85 delete _outStream;
86 delete _logStream;
87 delete _outFile;
88 delete _logFile;
89 QMapIterator<QString, t_ephPair*> it(_ephList);
90 while (it.hasNext()) {
91 it.next();
92 delete it.value();
93 }
94}
95
96// Delete bns thread
97////////////////////////////////////////////////////////////////////////////
98void t_bns::deleteBnsEph() {
99 if (_bnseph) {
100 _bnseph->terminate();
101 _bnseph->wait(100);
102 delete _bnseph;
103 _bnseph = 0;
104 }
105}
106
107// Write a Program Message
108////////////////////////////////////////////////////////////////////////////
109void t_bns::slotMessage(const QByteArray msg) {
110 if (_logStream) {
111 *_logStream << msg << endl;
112 _logStream->flush();
113 }
114 emit(newMessage(msg));
115}
116
117// Write a Program Message
118////////////////////////////////////////////////////////////////////////////
119void t_bns::slotError(const QByteArray msg) {
120 if (_logStream) {
121 *_logStream << msg << endl;
122 _logStream->flush();
123 }
124 deleteBnsEph();
125 emit(error(msg));
126}
127
128// New Connection
129////////////////////////////////////////////////////////////////////////////
130void t_bns::slotNewConnection() {
131 slotMessage("t_bns::slotNewConnection");
132 delete _clkSocket;
133 _clkSocket = _clkServer->nextPendingConnection();
134}
135
136// Start the Communication with NTRIP Caster
137////////////////////////////////////////////////////////////////////////////
138void t_bns::openCaster() {
139
140 QSettings settings;
141
142 _outSocket = new QTcpSocket();
143 _outSocket->connectToHost(settings.value("outHost").toString(),
144 settings.value("outPort").toInt());
145
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
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);
161 _outSocket->waitForBytesWritten();
162
163 _outSocket->waitForReadyRead();
164 QByteArray ans = _outSocket->readLine();
165
166 if (ans.indexOf("OK") == -1) {
167 delete _outSocket;
168 _outSocket = 0;
169 }
170}
171
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
203// Start
204////////////////////////////////////////////////////////////////////////////
205void t_bns::run() {
206
207 slotMessage("============ Start BNS ============");
208
209 // Start Thread that retrieves broadcast Ephemeris
210 // -----------------------------------------------
211 _bnseph->start();
212
213 // Endless loop
214 // ------------
215 while (true) {
216 if (_clkSocket && _clkSocket->state() == QAbstractSocket::ConnectedState) {
217 if ( _clkSocket->canReadLine()) {
218 if (_outSocket == 0) {
219 openCaster();
220 }
221 readEpoch();
222 }
223 else {
224 _clkSocket->waitForReadyRead(10);
225 }
226 }
227 else {
228 msleep(10);
229 }
230 }
231}
232
233//
234////////////////////////////////////////////////////////////////////////////
235void t_bns::readEpoch() {
236
237 QByteArray line = _clkSocket->readLine();
238
239 if (line.indexOf('*') == -1) {
240 return;
241 }
242
243 QTextStream in(line);
244
245 QString hlp;
246 int GPSweek, numSat;
247 double GPSweeks;
248
249 in >> hlp >> GPSweek >> GPSweeks >> numSat;
250
251 for (int ii = 1; ii <= numSat; ii++) {
252 line = _clkSocket->readLine();
253
254 QTextStream in(line);
255
256 QString prn;
257 ColumnVector xx(4);
258
259 in >> prn >> xx(1) >> xx(2) >> xx(3) >> xx(4);
260 xx(4) *= 1e-6;
261
262 processSatellite(GPSweek, GPSweeks, prn, xx);
263 }
264}
265
266//
267////////////////////////////////////////////////////////////////////////////
268void t_bns::processSatellite(int GPSweek, double GPSweeks, const QString& prn,
269 const ColumnVector& xx) {
270
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
280 ColumnVector xB(4);
281 ColumnVector vv(3);
282
283 satellitePosition(GPSweek, GPSweeks, ep, xB(1), xB(2), xB(3), xB(4),
284 vv(1), vv(2), vv(3));
285
286 ColumnVector dx = xx.Rows(1,3) - xB.Rows(1,3);
287 double dClk = (xx(4) - xB(4)) * 299792458.0;
288 ColumnVector rsw(3);
289
290 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
291
292 QString line;
293 line.sprintf("%d %.1f %s %3d %3d %8.3f %8.3f %8.3f %8.3f\n",
294 GPSweek, GPSweeks, ep->prn.toAscii().data(),
295 int(ep->IODC), int(ep->IODE), dClk, rsw(1), rsw(2), rsw(3));
296
297 if (_outStream) {
298 *_outStream << line;
299 _outStream->flush();
300 }
301 if (_outSocket) {
302 _outSocket->write(line.toAscii());
303 }
304}
Note: See TracBrowser for help on using the repository browser.