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