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

Last change on this file since 819 was 819, 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
154 QString mountpoint = settings.value("mountpoint").toString();
155 QString password = settings.value("password").toString();
156
157 QByteArray msg = "SOURCE " + password.toAscii() + " /" +
158 mountpoint.toAscii() + "\r\n" +
159 "Source-Agent: NTRIP BNS/1.0\r\n\r\n";
160
161 _outSocket->write(msg);
162
163 QByteArray ans = _outSocket->readLine();
164
165 cout << "Ans: >" << ans.data() << "<" << endl;
166
167 if (ans.indexOf("OK") == -1) {
168 delete _outSocket;
169 _outSocket = 0;
170 }
171}
172
173//
174////////////////////////////////////////////////////////////////////////////
175void t_bns::slotNewEph(gpsEph* ep) {
176
177 QMutexLocker locker(&_mutex);
178
179 t_ephPair* pair;
180 if ( !_ephList.contains(ep->prn) ) {
181 pair = new t_ephPair();
182 _ephList.insert(ep->prn, pair);
183 }
184 else {
185 pair = _ephList[ep->prn];
186 }
187
188 if (pair->eph == 0) {
189 pair->eph = ep;
190 }
191 else {
192 if (ep->GPSweek > pair->eph->GPSweek ||
193 (ep->GPSweek == pair->eph->GPSweek && ep->TOC > pair->eph->TOC)) {
194 delete pair->oldEph;
195 pair->oldEph = pair->eph;
196 pair->eph = ep;
197 }
198 else {
199 delete ep;
200 }
201 }
202}
203
204// Start
205////////////////////////////////////////////////////////////////////////////
206void t_bns::run() {
207
208 slotMessage("============ Start BNS ============");
209
210 // Start Thread that retrieves broadcast Ephemeris
211 // -----------------------------------------------
212 _bnseph->start();
213
214 // Endless loop
215 // ------------
216 while (true) {
217 if (_clkSocket && _clkSocket->state() == QAbstractSocket::ConnectedState) {
218 if ( _clkSocket->canReadLine()) {
219 if (_outSocket == 0) {
220 openCaster();
221 }
222 readEpoch();
223 }
224 else {
225 _clkSocket->waitForReadyRead(10);
226 }
227 }
228 else {
229 msleep(10);
230 }
231 }
232}
233
234//
235////////////////////////////////////////////////////////////////////////////
236void t_bns::readEpoch() {
237
238 QByteArray line = _clkSocket->readLine();
239
240 if (line.indexOf('*') == -1) {
241 return;
242 }
243
244 QTextStream in(line);
245
246 QString hlp;
247 int GPSweek, numSat;
248 double GPSweeks;
249
250 in >> hlp >> GPSweek >> GPSweeks >> numSat;
251
252 for (int ii = 1; ii <= numSat; ii++) {
253 line = _clkSocket->readLine();
254
255 QTextStream in(line);
256
257 QString prn;
258 ColumnVector xx(4);
259
260 in >> prn >> xx(1) >> xx(2) >> xx(3) >> xx(4);
261 xx(4) *= 1e-6;
262
263 processSatellite(GPSweek, GPSweeks, prn, xx);
264 }
265}
266
267//
268////////////////////////////////////////////////////////////////////////////
269void t_bns::processSatellite(int GPSweek, double GPSweeks, const QString& prn,
270 const ColumnVector& xx) {
271
272 // No broadcast ephemeris available
273 // --------------------------------
274 if ( !_ephList.contains(prn) ) {
275 return;
276 }
277
278 t_ephPair* pair = _ephList[prn];
279 gpsEph* ep = pair->eph;
280
281 ColumnVector xB(4);
282 ColumnVector vv(3);
283
284 satellitePosition(GPSweek, GPSweeks, ep, xB(1), xB(2), xB(3), xB(4),
285 vv(1), vv(2), vv(3));
286
287 ColumnVector dx = xx.Rows(1,3) - xB.Rows(1,3);
288 double dClk = (xx(4) - xB(4)) * 299792458.0;
289 ColumnVector rsw(3);
290
291 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
292
293 QString line;
294 line.sprintf("%d %.1f %s %3d %3d %8.3f %8.3f %8.3f %8.3f\n",
295 GPSweek, GPSweeks, ep->prn.toAscii().data(),
296 int(ep->IODC), int(ep->IODE), dClk, rsw(1), rsw(2), rsw(3));
297
298 if (_outStream) {
299 *_outStream << line;
300 _outStream->flush();
301 }
302 if (_outSocket) {
303 _outSocket->write(line.toAscii());
304 }
305}
Note: See TracBrowser for help on using the repository browser.