[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 | *
|
---|
| 13 | * Changes:
|
---|
| 14 | *
|
---|
| 15 | * -----------------------------------------------------------------------*/
|
---|
| 16 |
|
---|
| 17 | #include <math.h>
|
---|
| 18 | #include "bncuploadcaster.h"
|
---|
| 19 | #include "bncversion.h"
|
---|
| 20 | #include "bncapp.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,
|
---|
[3273] | 29 | const QString& password, int iRow,
|
---|
| 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;
|
---|
| 39 | if (_rate < 5) {
|
---|
| 40 | _rate = 5;
|
---|
| 41 | }
|
---|
| 42 | else if (_rate > 60) {
|
---|
| 43 | _rate = 60;
|
---|
| 44 | }
|
---|
[3207] | 45 | _isToBeDeleted = false;
|
---|
[3235] | 46 |
|
---|
| 47 | bncApp* app = (bncApp*) qApp;
|
---|
| 48 | connect(this, SIGNAL(newMessage(QByteArray,bool)),
|
---|
| 49 | app, SLOT(slotMessage(const QByteArray,bool)));
|
---|
| 50 |
|
---|
| 51 | if (app->_uploadTableItems.find(_iRow) != app->_uploadTableItems.end()){
|
---|
| 52 | connect(this, SIGNAL(newBytes(QByteArray,double)),
|
---|
| 53 | app->_uploadTableItems.value(iRow),
|
---|
[3236] | 54 | SLOT(slotNewBytes(const QByteArray,double)));
|
---|
[3235] | 55 | }
|
---|
[3172] | 56 | }
|
---|
| 57 |
|
---|
[3207] | 58 | // Safe Desctructor
|
---|
| 59 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 60 | void bncUploadCaster::deleteSafely() {
|
---|
| 61 | _isToBeDeleted = true;
|
---|
[3208] | 62 | if (!isRunning()) {
|
---|
| 63 | delete this;
|
---|
| 64 | }
|
---|
[3207] | 65 | }
|
---|
| 66 |
|
---|
[3172] | 67 | // Destructor
|
---|
| 68 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 69 | bncUploadCaster::~bncUploadCaster() {
|
---|
[3208] | 70 | if (isRunning()) {
|
---|
| 71 | wait();
|
---|
| 72 | }
|
---|
[3172] | 73 | }
|
---|
| 74 |
|
---|
[3226] | 75 | // Endless Loop
|
---|
| 76 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 77 | void bncUploadCaster::run() {
|
---|
| 78 | while (true) {
|
---|
| 79 | if (_isToBeDeleted) {
|
---|
| 80 | QThread::quit();
|
---|
| 81 | deleteLater();
|
---|
| 82 | return;
|
---|
| 83 | }
|
---|
| 84 | open();
|
---|
| 85 | if (_outSocket && _outSocket->state() == QAbstractSocket::ConnectedState) {
|
---|
| 86 | QMutexLocker locker(&_mutex);
|
---|
| 87 | _outSocket->write(_outBuffer);
|
---|
| 88 | _outSocket->flush();
|
---|
[3234] | 89 | emit newBytes(_mountpoint.toAscii(), _outBuffer.size());
|
---|
[3226] | 90 | }
|
---|
[3273] | 91 | sleep(_rate);
|
---|
[3226] | 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[3172] | 95 | // Start the Communication with NTRIP Caster
|
---|
| 96 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 97 | void bncUploadCaster::open() {
|
---|
| 98 |
|
---|
| 99 | if (_mountpoint.isEmpty()) {
|
---|
| 100 | return;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | if (_outSocket != 0 &&
|
---|
| 104 | _outSocket->state() == QAbstractSocket::ConnectedState) {
|
---|
| 105 | return;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | delete _outSocket; _outSocket = 0;
|
---|
| 109 |
|
---|
| 110 | double minDt = pow(2.0,_sOpenTrial);
|
---|
| 111 | if (++_sOpenTrial > 4) {
|
---|
| 112 | _sOpenTrial = 4;
|
---|
| 113 | }
|
---|
| 114 | if (_outSocketOpenTime.isValid() &&
|
---|
| 115 | _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
|
---|
| 116 | return;
|
---|
| 117 | }
|
---|
| 118 | else {
|
---|
| 119 | _outSocketOpenTime = QDateTime::currentDateTime();
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | _outSocket = new QTcpSocket();
|
---|
| 123 | _outSocket->connectToHost(_outHost, _outPort);
|
---|
| 124 |
|
---|
| 125 | const int timeOut = 5000; // 5 seconds
|
---|
| 126 | if (!_outSocket->waitForConnected(timeOut)) {
|
---|
| 127 | delete _outSocket;
|
---|
| 128 | _outSocket = 0;
|
---|
[3189] | 129 | emit(newMessage("Broadcaster: Connect timeout", true));
|
---|
[3172] | 130 | return;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | QByteArray msg = "SOURCE " + _password.toAscii() + " /" +
|
---|
| 134 | _mountpoint.toAscii() + "\r\n" +
|
---|
| 135 | "Source-Agent: NTRIP BNC/" BNCVERSION "\r\n\r\n";
|
---|
| 136 |
|
---|
| 137 | _outSocket->write(msg);
|
---|
| 138 | _outSocket->waitForBytesWritten();
|
---|
| 139 |
|
---|
| 140 | _outSocket->waitForReadyRead();
|
---|
| 141 | QByteArray ans = _outSocket->readLine();
|
---|
| 142 |
|
---|
| 143 | if (ans.indexOf("OK") == -1) {
|
---|
| 144 | delete _outSocket;
|
---|
| 145 | _outSocket = 0;
|
---|
[3189] | 146 | emit(newMessage("Broadcaster: Connection broken", true));
|
---|
[3172] | 147 | }
|
---|
| 148 | else {
|
---|
[3189] | 149 | emit(newMessage("Broadcaster: Connection opened", true));
|
---|
[3172] | 150 | _sOpenTrial = 0;
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|