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

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

* empty log message *

File size: 5.8 KB
RevLine 
[756]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////////////////////////////////////////////////////////////////////////////
[757]25t_bns::t_bns(QObject* parent) : QThread(parent) {
[760]26
[764]27 this->setTerminationEnabled(true);
28
[787]29 // Thread that handles broadcast ephemeris
30 // ---------------------------------------
[758]31 _bnseph = new t_bnseph(parent);
[760]32
[778]33 connect(_bnseph, SIGNAL(newEph(gpsEph*)), this, SLOT(slotNewEph(gpsEph*)));
[758]34 connect(_bnseph, SIGNAL(newMessage(QByteArray)),
35 this, SLOT(slotMessage(const QByteArray)));
[760]36 connect(_bnseph, SIGNAL(error(QByteArray)),
37 this, SLOT(slotError(const QByteArray)));
[769]38
[787]39 // Server listening for rtnet results
40 // ----------------------------------
[786]41 QSettings settings;
[769]42 _clkSocket = 0;
[786]43 _clkServer = new QTcpServer;
[787]44 _clkServer->listen(QHostAddress::Any, settings.value("clkPort").toInt());
[786]45 connect(_clkServer, SIGNAL(newConnection()),this, SLOT(slotNewConnection()));
[770]46
[787]47 // Socket for outputting the results
48 // ---------------------------------
[770]49 _outSocket = 0;
[756]50}
51
52// Destructor
53////////////////////////////////////////////////////////////////////////////
[757]54t_bns::~t_bns() {
[763]55 deleteBnsEph();
[769]56 delete _clkServer;
[789]57 /// delete _clkSocket;
[770]58 delete _outSocket;
[779]59 QMapIterator<QString, t_ephPair*> it(_ephList);
60 while (it.hasNext()) {
61 it.next();
62 delete it.value();
63 }
[756]64}
65
[763]66// Delete bns thread
67////////////////////////////////////////////////////////////////////////////
68void t_bns::deleteBnsEph() {
69 if (_bnseph) {
70 _bnseph->terminate();
[764]71 _bnseph->wait(100);
[763]72 delete _bnseph;
73 _bnseph = 0;
74 }
75}
76
[756]77// Write a Program Message
78////////////////////////////////////////////////////////////////////////////
[758]79void t_bns::slotMessage(const QByteArray msg) {
[756]80 cout << msg.data() << endl;
[757]81 emit(newMessage(msg));
[756]82}
83
[760]84// Write a Program Message
85////////////////////////////////////////////////////////////////////////////
86void t_bns::slotError(const QByteArray msg) {
87 cerr << msg.data() << endl;
[763]88 deleteBnsEph();
[760]89 emit(error(msg));
90}
91
[769]92// New Connection
93////////////////////////////////////////////////////////////////////////////
94void t_bns::slotNewConnection() {
[786]95 slotMessage("t_bns::slotNewConnection");
[787]96 delete _clkSocket;
[769]97 _clkSocket = _clkServer->nextPendingConnection();
98}
99
[770]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
[784]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
[756]159// Start
160////////////////////////////////////////////////////////////////////////////
[757]161void t_bns::run() {
[769]162
[758]163 slotMessage("============ Start BNS ============");
[770]164
165 // Start Thread that retrieves broadcast Ephemeris
166 // -----------------------------------------------
[758]167 _bnseph->start();
[769]168
[770]169 // Open the connection to NTRIP Caster
170 // -----------------------------------
171 openCaster();
172
173 // Endless loop
174 // ------------
[769]175 while (true) {
[793]176 if (_clkSocket &&
177 _clkSocket->state() == QAbstractSocket::ConnectedState &&
178 _clkSocket->canReadLine()) {
179 readEpoch();
[769]180 }
181 else {
[794]182 msleep(10);
[769]183 }
184 }
[756]185}
186
[778]187//
188////////////////////////////////////////////////////////////////////////////
[784]189void t_bns::readEpoch() {
[778]190
[784]191 QByteArray line = _clkSocket->readLine();
[786]192
193 cout << line.data();
194
[784]195 if (line.indexOf('*') == -1) {
196 return;
[778]197 }
198
[784]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++) {
[792]208 line = _clkSocket->readLine();
[791]209
210 cout << line.data();
211
[784]212 QTextStream in(line);
213
214 QString prn;
215 ColumnVector xx(4);
216
[795]217 in >> prn >> xx(1) >> xx(2) >> xx(3) >> xx(4);
218 xx(4) *= 1e6;
[784]219
220 processSatellite(mjd, sec, prn, xx);
[780]221 }
[778]222}
[784]223
224//
225////////////////////////////////////////////////////////////////////////////
226void t_bns::processSatellite(int mjd, double sec, const QString& prn,
227 const ColumnVector& xx) {
228
[795]229 // No broadcast ephemeris available
230 // --------------------------------
231 if ( !_ephList.contains(prn) ) {
232 return;
233 }
234
235 t_ephPair* pair = _ephList[prn];
236 gpsEph* ep = pair->eph;
237
238 cout << "PRN, CLK " << prn.toAscii().data() << " "
239 << ep->clock_bias << " " << xx(4) << endl;
[784]240}
Note: See TracBrowser for help on using the repository browser.