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