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

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

* empty log message *

File size: 5.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
19#include "bns.h"
20
21using namespace std;
22
23// Constructor
24////////////////////////////////////////////////////////////////////////////
25t_bns::t_bns(QObject* parent) : QThread(parent) {
26
27 this->setTerminationEnabled(true);
28
29 _bnseph = new t_bnseph(parent);
30
31 connect(_bnseph, SIGNAL(newEph(gpsEph*)), this, SLOT(slotNewEph(gpsEph*)));
32
33 connect(_bnseph, SIGNAL(newMessage(QByteArray)),
34 this, SLOT(slotMessage(const QByteArray)));
35
36 connect(_bnseph, SIGNAL(error(QByteArray)),
37 this, SLOT(slotError(const QByteArray)));
38
39 _clkServer = 0;
40 _clkSocket = 0;
41
42 _outSocket = 0;
43}
44
45// Destructor
46////////////////////////////////////////////////////////////////////////////
47t_bns::~t_bns() {
48 deleteBnsEph();
49 delete _clkServer;
50 delete _outSocket;
51 QMapIterator<QString, t_ephPair*> it(_ephList);
52 while (it.hasNext()) {
53 it.next();
54 delete it.value();
55 }
56}
57
58// Delete bns thread
59////////////////////////////////////////////////////////////////////////////
60void t_bns::deleteBnsEph() {
61 if (_bnseph) {
62 _bnseph->terminate();
63 _bnseph->wait(100);
64 delete _bnseph;
65 _bnseph = 0;
66 }
67}
68
69// Write a Program Message
70////////////////////////////////////////////////////////////////////////////
71void t_bns::slotMessage(const QByteArray msg) {
72 cout << msg.data() << endl;
73 emit(newMessage(msg));
74}
75
76// Write a Program Message
77////////////////////////////////////////////////////////////////////////////
78void t_bns::slotError(const QByteArray msg) {
79 cerr << msg.data() << endl;
80 deleteBnsEph();
81 emit(error(msg));
82}
83
84// New Connection
85////////////////////////////////////////////////////////////////////////////
86void t_bns::slotNewConnection() {
87 _clkSocket = _clkServer->nextPendingConnection();
88}
89
90// Start the Communication with NTRIP Caster
91////////////////////////////////////////////////////////////////////////////
92void t_bns::openCaster() {
93
94 QSettings settings;
95 QString host = settings.value("outHost").toString();
96 int port = settings.value("outPort").toInt();
97
98 _outSocket = new QTcpSocket();
99 _outSocket->connectToHost(host, port);
100
101 QString mountpoint = settings.value("mountpoint").toString();
102 QString password = settings.value("password").toString();
103
104 QByteArray msg = "SOURCE " + password.toAscii() + " /" +
105 mountpoint.toAscii() + "\r\n" +
106 "Source-Agent: NTRIP BNS/1.0\r\n\r\n";
107
108 _outSocket->write(msg);
109
110 QByteArray ans = _outSocket->readLine();
111
112 if (ans.indexOf("OK") == -1) {
113 delete _outSocket;
114 _outSocket = 0;
115 }
116}
117
118//
119////////////////////////////////////////////////////////////////////////////
120void t_bns::slotNewEph(gpsEph* ep) {
121
122 QMutexLocker locker(&_mutex);
123
124 t_ephPair* pair;
125 if ( !_ephList.contains(ep->prn) ) {
126 pair = new t_ephPair();
127 _ephList.insert(ep->prn, pair);
128 }
129 else {
130 pair = _ephList[ep->prn];
131 }
132
133 if (pair->eph == 0) {
134 pair->eph = ep;
135 }
136 else {
137 if (ep->GPSweek > pair->eph->GPSweek ||
138 (ep->GPSweek == pair->eph->GPSweek && ep->TOC > pair->eph->TOC)) {
139 delete pair->oldEph;
140 pair->oldEph = pair->eph;
141 pair->eph = ep;
142 }
143 else {
144 delete ep;
145 }
146 }
147}
148
149// Start
150////////////////////////////////////////////////////////////////////////////
151void t_bns::run() {
152
153 slotMessage("============ Start BNS ============");
154
155 // Start Thread that retrieves broadcast Ephemeris
156 // -----------------------------------------------
157 _bnseph->start();
158
159 // Open the connection to NTRIP Caster
160 // -----------------------------------
161 openCaster();
162
163 // Start listening for rtnet results
164 // ---------------------------------
165 QSettings settings;
166 _clkServer = new QTcpServer;
167 _clkServer->listen(QHostAddress::Any, settings.value("clkPort").toInt());
168 connect(_clkServer, SIGNAL(newConnection()),this, SLOT(slotNewConnection()));
169
170 // Endless loop
171 // ------------
172 while (true) {
173 if (_clkSocket) {
174 if (_clkSocket->state() != QAbstractSocket::ConnectedState) {
175 delete _clkSocket;
176 _clkSocket = 0;
177 continue;
178 }
179 if (!_clkSocket->canReadLine()) {
180 _clkSocket->waitForReadyRead();
181 }
182 else {
183 readEpoch();
184 }
185 }
186 else {
187 msleep(100);
188 }
189 }
190}
191
192//
193////////////////////////////////////////////////////////////////////////////
194void t_bns::readEpoch() {
195
196 QByteArray line = _clkSocket->readLine();
197 if (line.indexOf('*') == -1) {
198 return;
199 }
200
201 QTextStream in(line);
202
203 QString hlp;
204 int mjd, numSat;
205 double sec;
206
207 in >> hlp >> mjd >> sec >> numSat;
208
209 for (int ii = 1; ii <= numSat; ii++) {
210 if (!_clkSocket->canReadLine()) {
211 _clkSocket->waitForReadyRead();
212 }
213 line = _clkSocket->readLine();
214 QTextStream in(line);
215
216 QString prn;
217 ColumnVector xx(4);
218
219 in >> prn >> xx(1) >> xx(2) >> xx(3) >> xx(4);
220
221 processSatellite(mjd, sec, prn, xx);
222 }
223}
224
225//
226////////////////////////////////////////////////////////////////////////////
227void t_bns::processSatellite(int mjd, double sec, const QString& prn,
228 const ColumnVector& xx) {
229
230}
Note: See TracBrowser for help on using the repository browser.