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 | _bnseph = new t_bnseph(parent);
|
---|
30 |
|
---|
31 | connect(_bnseph, SIGNAL(newEph(gpsEph*)), this, SLOT(slotNewEph(gpsEph*)));
|
---|
32 |
|
---|
33 | connect(_bnseph, SIGNAL(newMessage(QByteArray)),
|
---|
34 | this, SLOT(slotMessage(const QByteArray)));
|
---|
35 |
|
---|
36 | connect(_bnseph, SIGNAL(error(QByteArray)),
|
---|
37 | this, SLOT(slotError(const QByteArray)));
|
---|
38 |
|
---|
39 | _clkServer = 0;
|
---|
40 | _clkSocket = 0;
|
---|
41 |
|
---|
42 | _outSocket = 0;
|
---|
43 | }
|
---|
44 |
|
---|
45 | // Destructor
|
---|
46 | ////////////////////////////////////////////////////////////////////////////
|
---|
47 | t_bns::~t_bns() {
|
---|
48 | deleteBnsEph();
|
---|
49 | delete _clkServer;
|
---|
50 | delete _outSocket;
|
---|
51 | }
|
---|
52 |
|
---|
53 | // Delete bns thread
|
---|
54 | ////////////////////////////////////////////////////////////////////////////
|
---|
55 | void t_bns::deleteBnsEph() {
|
---|
56 | if (_bnseph) {
|
---|
57 | _bnseph->terminate();
|
---|
58 | _bnseph->wait(100);
|
---|
59 | delete _bnseph;
|
---|
60 | _bnseph = 0;
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | // Write a Program Message
|
---|
65 | ////////////////////////////////////////////////////////////////////////////
|
---|
66 | void t_bns::slotMessage(const QByteArray msg) {
|
---|
67 | cout << msg.data() << endl;
|
---|
68 | emit(newMessage(msg));
|
---|
69 | }
|
---|
70 |
|
---|
71 | // Write a Program Message
|
---|
72 | ////////////////////////////////////////////////////////////////////////////
|
---|
73 | void t_bns::slotError(const QByteArray msg) {
|
---|
74 | cerr << msg.data() << endl;
|
---|
75 | deleteBnsEph();
|
---|
76 | emit(error(msg));
|
---|
77 | }
|
---|
78 |
|
---|
79 | // New Connection
|
---|
80 | ////////////////////////////////////////////////////////////////////////////
|
---|
81 | void t_bns::slotNewConnection() {
|
---|
82 | _clkSocket = _clkServer->nextPendingConnection();
|
---|
83 | }
|
---|
84 |
|
---|
85 | // Start the Communication with NTRIP Caster
|
---|
86 | ////////////////////////////////////////////////////////////////////////////
|
---|
87 | void t_bns::openCaster() {
|
---|
88 |
|
---|
89 | QSettings settings;
|
---|
90 | QString host = settings.value("outHost").toString();
|
---|
91 | int port = settings.value("outPort").toInt();
|
---|
92 |
|
---|
93 | _outSocket = new QTcpSocket();
|
---|
94 | _outSocket->connectToHost(host, port);
|
---|
95 |
|
---|
96 | QString mountpoint = settings.value("mountpoint").toString();
|
---|
97 | QString password = settings.value("password").toString();
|
---|
98 |
|
---|
99 | QByteArray msg = "SOURCE " + password.toAscii() + " /" +
|
---|
100 | mountpoint.toAscii() + "\r\n" +
|
---|
101 | "Source-Agent: NTRIP BNS/1.0\r\n\r\n";
|
---|
102 |
|
---|
103 | _outSocket->write(msg);
|
---|
104 |
|
---|
105 | QByteArray ans = _outSocket->readLine();
|
---|
106 |
|
---|
107 | if (ans.indexOf("OK") == -1) {
|
---|
108 | delete _outSocket;
|
---|
109 | _outSocket = 0;
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | // Start
|
---|
114 | ////////////////////////////////////////////////////////////////////////////
|
---|
115 | void t_bns::run() {
|
---|
116 |
|
---|
117 | slotMessage("============ Start BNS ============");
|
---|
118 |
|
---|
119 | // Start Thread that retrieves broadcast Ephemeris
|
---|
120 | // -----------------------------------------------
|
---|
121 | _bnseph->start();
|
---|
122 |
|
---|
123 | // Open the connection to NTRIP Caster
|
---|
124 | // -----------------------------------
|
---|
125 | openCaster();
|
---|
126 |
|
---|
127 | // Start listening for rtnet results
|
---|
128 | // ---------------------------------
|
---|
129 | QSettings settings;
|
---|
130 | _clkServer = new QTcpServer;
|
---|
131 | _clkServer->listen(QHostAddress::Any, settings.value("clkPort").toInt());
|
---|
132 | connect(_clkServer, SIGNAL(newConnection()),this, SLOT(slotNewConnection()));
|
---|
133 |
|
---|
134 | // Endless loop
|
---|
135 | // ------------
|
---|
136 | while (true) {
|
---|
137 | if (_clkSocket) {
|
---|
138 |
|
---|
139 | }
|
---|
140 | else {
|
---|
141 | msleep(100);
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | //
|
---|
147 | ////////////////////////////////////////////////////////////////////////////
|
---|
148 | void t_bns::slotNewEph(gpsEph* ep) {
|
---|
149 |
|
---|
150 | QMutexLocker locker(&_mutex);
|
---|
151 |
|
---|
152 | t_ephPair* pair;
|
---|
153 | if ( !_ephList.contains(ep->prn) ) {
|
---|
154 | pair = new t_ephPair();
|
---|
155 | _ephList.insert(ep->prn, pair);
|
---|
156 | }
|
---|
157 | else {
|
---|
158 | pair = _ephList[ep->prn];
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | }
|
---|