[3172] | 1 | /* -------------------------------------------------------------------------
|
---|
| 2 | * BKG NTRIP Server
|
---|
| 3 | * -------------------------------------------------------------------------
|
---|
| 4 | *
|
---|
| 5 | * Class: bncUploadCaster
|
---|
| 6 | *
|
---|
| 7 | * Purpose: Connection to NTRIP Caster
|
---|
| 8 | *
|
---|
| 9 | * Author: L. Mervart
|
---|
| 10 | *
|
---|
| 11 | * Created: 29-Mar-2011
|
---|
| 12 | *
|
---|
[7661] | 13 | * Changes:
|
---|
[3172] | 14 | *
|
---|
| 15 | * -----------------------------------------------------------------------*/
|
---|
| 16 |
|
---|
| 17 | #include <math.h>
|
---|
[7661] | 18 | #include "bncuploadcaster.h"
|
---|
[3172] | 19 | #include "bncversion.h"
|
---|
[5070] | 20 | #include "bnccore.h"
|
---|
[3235] | 21 | #include "bnctableitem.h"
|
---|
[3172] | 22 |
|
---|
| 23 | using namespace std;
|
---|
| 24 |
|
---|
| 25 | // Constructor
|
---|
| 26 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 27 | bncUploadCaster::bncUploadCaster(const QString& mountpoint,
|
---|
| 28 | const QString& outHost, int outPort,
|
---|
[7661] | 29 | const QString& password, int iRow,
|
---|
[3273] | 30 | int rate) {
|
---|
[3224] | 31 | _mountpoint = mountpoint;
|
---|
| 32 | _outHost = outHost;
|
---|
| 33 | _outPort = outPort;
|
---|
| 34 | _password = password;
|
---|
| 35 | _outSocket = 0;
|
---|
| 36 | _sOpenTrial = 0;
|
---|
[3233] | 37 | _iRow = iRow;
|
---|
[3273] | 38 | _rate = rate;
|
---|
[4809] | 39 | if (_rate < 0) {
|
---|
| 40 | _rate = 0;
|
---|
[3273] | 41 | }
|
---|
| 42 | else if (_rate > 60) {
|
---|
| 43 | _rate = 60;
|
---|
| 44 | }
|
---|
[3207] | 45 | _isToBeDeleted = false;
|
---|
[3235] | 46 |
|
---|
[7661] | 47 | connect(this, SIGNAL(newMessage(QByteArray,bool)),
|
---|
[5068] | 48 | BNC_CORE, SLOT(slotMessage(const QByteArray,bool)));
|
---|
[3235] | 49 |
|
---|
[5068] | 50 | if (BNC_CORE->_uploadTableItems.find(_iRow) != BNC_CORE->_uploadTableItems.end()){
|
---|
[7661] | 51 | connect(this, SIGNAL(newBytes(QByteArray,double)),
|
---|
| 52 | BNC_CORE->_uploadTableItems.value(iRow),
|
---|
[3236] | 53 | SLOT(slotNewBytes(const QByteArray,double)));
|
---|
[3235] | 54 | }
|
---|
[3172] | 55 | }
|
---|
| 56 |
|
---|
[3207] | 57 | // Safe Desctructor
|
---|
| 58 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 59 | void bncUploadCaster::deleteSafely() {
|
---|
| 60 | _isToBeDeleted = true;
|
---|
[3208] | 61 | if (!isRunning()) {
|
---|
| 62 | delete this;
|
---|
| 63 | }
|
---|
[3207] | 64 | }
|
---|
| 65 |
|
---|
[3172] | 66 | // Destructor
|
---|
| 67 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 68 | bncUploadCaster::~bncUploadCaster() {
|
---|
[3208] | 69 | if (isRunning()) {
|
---|
| 70 | wait();
|
---|
| 71 | }
|
---|
[7661] | 72 | if (_outSocket) {
|
---|
| 73 | delete _outSocket;
|
---|
| 74 | }
|
---|
[3172] | 75 | }
|
---|
| 76 |
|
---|
[3226] | 77 | // Endless Loop
|
---|
| 78 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 79 | void bncUploadCaster::run() {
|
---|
| 80 | while (true) {
|
---|
| 81 | if (_isToBeDeleted) {
|
---|
| 82 | QThread::quit();
|
---|
| 83 | deleteLater();
|
---|
| 84 | return;
|
---|
| 85 | }
|
---|
| 86 | open();
|
---|
| 87 | if (_outSocket && _outSocket->state() == QAbstractSocket::ConnectedState) {
|
---|
| 88 | QMutexLocker locker(&_mutex);
|
---|
[4808] | 89 | if (_outBuffer.size() > 0) {
|
---|
| 90 | _outSocket->write(_outBuffer);
|
---|
| 91 | _outSocket->flush();
|
---|
| 92 | emit newBytes(_mountpoint.toAscii(), _outBuffer.size());
|
---|
| 93 | }
|
---|
[3226] | 94 | }
|
---|
[4809] | 95 | if (_rate == 0) {
|
---|
[4985] | 96 | {
|
---|
| 97 | QMutexLocker locker(&_mutex);
|
---|
| 98 | _outBuffer.clear();
|
---|
| 99 | }
|
---|
[4810] | 100 | msleep(100); //sleep 0.1 sec
|
---|
[4809] | 101 | }
|
---|
| 102 | else {
|
---|
| 103 | sleep(_rate);
|
---|
| 104 | }
|
---|
[3226] | 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[3172] | 108 | // Start the Communication with NTRIP Caster
|
---|
| 109 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 110 | void bncUploadCaster::open() {
|
---|
| 111 |
|
---|
| 112 | if (_mountpoint.isEmpty()) {
|
---|
| 113 | return;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[7661] | 116 | if (_outSocket != 0 &&
|
---|
[3172] | 117 | _outSocket->state() == QAbstractSocket::ConnectedState) {
|
---|
| 118 | return;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | delete _outSocket; _outSocket = 0;
|
---|
| 122 |
|
---|
| 123 | double minDt = pow(2.0,_sOpenTrial);
|
---|
| 124 | if (++_sOpenTrial > 4) {
|
---|
| 125 | _sOpenTrial = 4;
|
---|
| 126 | }
|
---|
| 127 | if (_outSocketOpenTime.isValid() &&
|
---|
| 128 | _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
|
---|
| 129 | return;
|
---|
| 130 | }
|
---|
| 131 | else {
|
---|
| 132 | _outSocketOpenTime = QDateTime::currentDateTime();
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | _outSocket = new QTcpSocket();
|
---|
| 136 | _outSocket->connectToHost(_outHost, _outPort);
|
---|
| 137 |
|
---|
| 138 | const int timeOut = 5000; // 5 seconds
|
---|
| 139 | if (!_outSocket->waitForConnected(timeOut)) {
|
---|
| 140 | delete _outSocket;
|
---|
| 141 | _outSocket = 0;
|
---|
[8350] | 142 | emit(newMessage("Broadcaster: Connect timeout for " + _mountpoint.toAscii()
|
---|
| 143 | + " (" + _outHost.toAscii() + ")", true));
|
---|
[3172] | 144 | return;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[7661] | 147 | QByteArray msg = "SOURCE " + _password.toAscii() + " /" +
|
---|
[3172] | 148 | _mountpoint.toAscii() + "\r\n" +
|
---|
| 149 | "Source-Agent: NTRIP BNC/" BNCVERSION "\r\n\r\n";
|
---|
| 150 |
|
---|
| 151 | _outSocket->write(msg);
|
---|
| 152 | _outSocket->waitForBytesWritten();
|
---|
| 153 |
|
---|
| 154 | _outSocket->waitForReadyRead();
|
---|
| 155 | QByteArray ans = _outSocket->readLine();
|
---|
| 156 |
|
---|
| 157 | if (ans.indexOf("OK") == -1) {
|
---|
| 158 | delete _outSocket;
|
---|
| 159 | _outSocket = 0;
|
---|
[8350] | 160 | emit(newMessage("Broadcaster: Connection broken for " + _mountpoint.toAscii()
|
---|
| 161 | + " (" + _outHost.toAscii() + ")", true));
|
---|
[3172] | 162 | }
|
---|
| 163 | else {
|
---|
[8350] | 164 | emit(newMessage("Broadcaster: Connection opened for " + _mountpoint.toAscii()
|
---|
| 165 | + " (" + _outHost.toAscii() + ")", true));
|
---|
[3172] | 166 | _sOpenTrial = 0;
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|