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

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

* empty log message *

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