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

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

* empty log message *

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