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 |
|
---|
21 | using namespace std;
|
---|
22 |
|
---|
23 | // Constructor
|
---|
24 | ////////////////////////////////////////////////////////////////////////////
|
---|
25 | t_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 | ////////////////////////////////////////////////////////////////////////////
|
---|
54 | t_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 | ////////////////////////////////////////////////////////////////////////////
|
---|
68 | void 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 | ////////////////////////////////////////////////////////////////////////////
|
---|
79 | void t_bns::slotMessage(const QByteArray msg) {
|
---|
80 | cout << msg.data() << endl;
|
---|
81 | emit(newMessage(msg));
|
---|
82 | }
|
---|
83 |
|
---|
84 | // Write a Program Message
|
---|
85 | ////////////////////////////////////////////////////////////////////////////
|
---|
86 | void t_bns::slotError(const QByteArray msg) {
|
---|
87 | cerr << msg.data() << endl;
|
---|
88 | deleteBnsEph();
|
---|
89 | emit(error(msg));
|
---|
90 | }
|
---|
91 |
|
---|
92 | // New Connection
|
---|
93 | ////////////////////////////////////////////////////////////////////////////
|
---|
94 | void 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 | ////////////////////////////////////////////////////////////////////////////
|
---|
102 | void 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 | ////////////////////////////////////////////////////////////////////////////
|
---|
130 | void 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 | ////////////////////////////////////////////////////////////////////////////
|
---|
161 | void 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 | if (_clkSocket->state() != QAbstractSocket::ConnectedState) {
|
---|
178 | delete _clkSocket;
|
---|
179 | _clkSocket = 0;
|
---|
180 | continue;
|
---|
181 | }
|
---|
182 | if (!_clkSocket->canReadLine()) {
|
---|
183 | _clkSocket->waitForReadyRead(10);
|
---|
184 | }
|
---|
185 | else {
|
---|
186 | readEpoch();
|
---|
187 | }
|
---|
188 | }
|
---|
189 | else {
|
---|
190 | msleep(100);
|
---|
191 | }
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | //
|
---|
196 | ////////////////////////////////////////////////////////////////////////////
|
---|
197 | void t_bns::readEpoch() {
|
---|
198 |
|
---|
199 | if (_clkSocket->state() != QAbstractSocket::ConnectedState) {
|
---|
200 | return;
|
---|
201 | }
|
---|
202 |
|
---|
203 | QByteArray line = _clkSocket->readLine();
|
---|
204 |
|
---|
205 | cout << line.data();
|
---|
206 |
|
---|
207 | if (line.indexOf('*') == -1) {
|
---|
208 | return;
|
---|
209 | }
|
---|
210 |
|
---|
211 | QTextStream in(line);
|
---|
212 |
|
---|
213 | QString hlp;
|
---|
214 | int mjd, numSat;
|
---|
215 | double sec;
|
---|
216 |
|
---|
217 | in >> hlp >> mjd >> sec >> numSat;
|
---|
218 |
|
---|
219 | for (int ii = 1; ii <= numSat; ii++) {
|
---|
220 | if (_clkSocket->state() != QAbstractSocket::ConnectedState) {
|
---|
221 | return;
|
---|
222 | }
|
---|
223 | else {
|
---|
224 | if (!_clkSocket->canReadLine()) {
|
---|
225 | _clkSocket->waitForReadyRead(10);
|
---|
226 | }
|
---|
227 | line = _clkSocket->readLine();
|
---|
228 | }
|
---|
229 |
|
---|
230 | cout << line.data();
|
---|
231 |
|
---|
232 | QTextStream in(line);
|
---|
233 |
|
---|
234 | QString prn;
|
---|
235 | ColumnVector xx(4);
|
---|
236 |
|
---|
237 | in >> prn >> xx(1) >> xx(2) >> xx(3) >> xx(4);
|
---|
238 |
|
---|
239 | processSatellite(mjd, sec, prn, xx);
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | //
|
---|
244 | ////////////////////////////////////////////////////////////////////////////
|
---|
245 | void t_bns::processSatellite(int mjd, double sec, const QString& prn,
|
---|
246 | const ColumnVector& xx) {
|
---|
247 |
|
---|
248 | }
|
---|