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

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

* empty log message *

File size: 6.0 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 cout << "A: " << ep->prn.toAscii().data() << " "
146 << ep->GPSweek << " " << ep->TOC << endl;
147 }
148 else {
149 if (ep->GPSweek > pair->eph->GPSweek ||
150 (ep->GPSweek == pair->eph->GPSweek && ep->TOC > pair->eph->TOC)) {
151 delete pair->oldEph;
152 pair->oldEph = pair->eph;
153 pair->eph = ep;
154 cout << "B: " << ep->prn.toAscii().data() << " "
155 << ep->GPSweek << " " << ep->TOC << endl;
156 }
157 else {
158 delete ep;
159 }
160 }
161}
162
163// Start
164////////////////////////////////////////////////////////////////////////////
165void t_bns::run() {
166
167 slotMessage("============ Start BNS ============");
168
169 // Start Thread that retrieves broadcast Ephemeris
170 // -----------------------------------------------
171 _bnseph->start();
172
173 // Open the connection to NTRIP Caster
174 // -----------------------------------
175 openCaster();
176
177 // Endless loop
178 // ------------
179 while (true) {
180 cout << "_clkSocket = " << _clkSocket << endl;
181 if (_clkSocket) {
182 if (_clkSocket->state() != QAbstractSocket::ConnectedState) {
183 delete _clkSocket;
184 _clkSocket = 0;
185 continue;
186 }
187 if (!_clkSocket->canReadLine()) {
188 _clkSocket->waitForReadyRead();
189 }
190 else {
191 readEpoch();
192 }
193 }
194 else {
195 msleep(100);
196 }
197 }
198}
199
200//
201////////////////////////////////////////////////////////////////////////////
202void t_bns::readEpoch() {
203
204 QByteArray line = _clkSocket->readLine();
205
206 cout << line.data();
207
208 if (line.indexOf('*') == -1) {
209 return;
210 }
211
212 QTextStream in(line);
213
214 QString hlp;
215 int mjd, numSat;
216 double sec;
217
218 in >> hlp >> mjd >> sec >> numSat;
219
220 for (int ii = 1; ii <= numSat; ii++) {
221 if (!_clkSocket->canReadLine()) {
222 _clkSocket->waitForReadyRead();
223 }
224 line = _clkSocket->readLine();
225 cout << line.data();
226 QTextStream in(line);
227
228 QString prn;
229 ColumnVector xx(4);
230
231 in >> prn >> xx(1) >> xx(2) >> xx(3) >> xx(4);
232
233 processSatellite(mjd, sec, prn, xx);
234 }
235}
236
237//
238////////////////////////////////////////////////////////////////////////////
239void t_bns::processSatellite(int mjd, double sec, const QString& prn,
240 const ColumnVector& xx) {
241
242}
Note: See TracBrowser for help on using the repository browser.