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

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

* empty log message *

File size: 5.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
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 &&
177 _clkSocket->state() == QAbstractSocket::ConnectedState &&
178 _clkSocket->canReadLine()) {
179 readEpoch();
180 }
181 else {
182 msleep(100);
183 }
184 }
185}
186
187//
188////////////////////////////////////////////////////////////////////////////
189void t_bns::readEpoch() {
190
191 QByteArray line = _clkSocket->readLine();
192
193 cout << line.data();
194
195 if (line.indexOf('*') == -1) {
196 return;
197 }
198
199 QTextStream in(line);
200
201 QString hlp;
202 int mjd, numSat;
203 double sec;
204
205 in >> hlp >> mjd >> sec >> numSat;
206
207 for (int ii = 1; ii <= numSat; ii++) {
208 line = _clkSocket->readLine();
209
210 cout << line.data();
211
212 QTextStream in(line);
213
214 QString prn;
215 ColumnVector xx(4);
216
217 in >> prn >> xx(1) >> xx(2) >> xx(3) >> xx(4);
218
219 processSatellite(mjd, sec, prn, xx);
220 }
221}
222
223//
224////////////////////////////////////////////////////////////////////////////
225void t_bns::processSatellite(int mjd, double sec, const QString& prn,
226 const ColumnVector& xx) {
227
228}
Note: See TracBrowser for help on using the repository browser.