[2925] | 1 | /* -------------------------------------------------------------------------
|
---|
| 2 | * BKG NTRIP Server
|
---|
| 3 | * -------------------------------------------------------------------------
|
---|
| 4 | *
|
---|
| 5 | * Class: cmbCaster
|
---|
| 6 | *
|
---|
| 7 | * Purpose: Connection to NTRIP Caster
|
---|
| 8 | *
|
---|
| 9 | * Author: L. Mervart
|
---|
| 10 | *
|
---|
| 11 | * Created: 29-Jan-2011
|
---|
| 12 | *
|
---|
| 13 | * Changes:
|
---|
| 14 | *
|
---|
| 15 | * -----------------------------------------------------------------------*/
|
---|
| 16 |
|
---|
| 17 | #include <math.h>
|
---|
| 18 | #include "cmbcaster.h"
|
---|
| 19 | #include "bncsettings.h"
|
---|
| 20 | #include "bncversion.h"
|
---|
[2990] | 21 | #include "bncapp.h"
|
---|
[2925] | 22 |
|
---|
| 23 | using namespace std;
|
---|
| 24 |
|
---|
| 25 | // Constructor
|
---|
| 26 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 27 | cmbCaster::cmbCaster() {
|
---|
| 28 | bncSettings settings;
|
---|
| 29 | _mountpoint = settings.value("cmbMountpoint").toString();
|
---|
| 30 | _outSocket = 0;
|
---|
| 31 | _sOpenTrial = 0;
|
---|
[2990] | 32 | connect(this, SIGNAL(newMessage(QByteArray,bool)),
|
---|
| 33 | ((bncApp*)qApp), SLOT(slotMessage(const QByteArray,bool)));
|
---|
[2925] | 34 | }
|
---|
| 35 |
|
---|
| 36 | // Destructor
|
---|
| 37 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 38 | cmbCaster::~cmbCaster() {
|
---|
| 39 | delete _outSocket;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | // Start the Communication with NTRIP Caster
|
---|
| 43 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 44 | void cmbCaster::open() {
|
---|
| 45 |
|
---|
| 46 | if (_mountpoint.isEmpty()) {
|
---|
| 47 | return;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | if (_outSocket != 0 &&
|
---|
| 51 | _outSocket->state() == QAbstractSocket::ConnectedState) {
|
---|
| 52 | return;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | delete _outSocket; _outSocket = 0;
|
---|
| 56 |
|
---|
| 57 | double minDt = pow(2.0,_sOpenTrial);
|
---|
| 58 | if (++_sOpenTrial > 4) {
|
---|
| 59 | _sOpenTrial = 4;
|
---|
| 60 | }
|
---|
| 61 | if (_outSocketOpenTime.isValid() &&
|
---|
| 62 | _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
|
---|
| 63 | return;
|
---|
| 64 | }
|
---|
| 65 | else {
|
---|
| 66 | _outSocketOpenTime = QDateTime::currentDateTime();
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | bncSettings settings;
|
---|
| 70 | _outSocket = new QTcpSocket();
|
---|
| 71 | QString password;
|
---|
| 72 | _outSocket->connectToHost(settings.value("cmbOutHost").toString(),
|
---|
| 73 | settings.value("cmbOutPort").toInt());
|
---|
| 74 | password = settings.value("cmbPassword").toString();
|
---|
| 75 |
|
---|
| 76 | const int timeOut = 5000; // 5 seconds
|
---|
| 77 | if (!_outSocket->waitForConnected(timeOut)) {
|
---|
| 78 | delete _outSocket;
|
---|
| 79 | _outSocket = 0;
|
---|
[2990] | 80 | emit(newMessage("Broadcaster: Connect timeout", true));
|
---|
[2925] | 81 | return;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | QByteArray msg = "SOURCE " + password.toAscii() + " /" +
|
---|
| 85 | _mountpoint.toAscii() + "\r\n" +
|
---|
| 86 | "Source-Agent: NTRIP BNC/" BNCVERSION "\r\n\r\n";
|
---|
| 87 |
|
---|
| 88 | _outSocket->write(msg);
|
---|
| 89 | _outSocket->waitForBytesWritten();
|
---|
| 90 |
|
---|
| 91 | _outSocket->waitForReadyRead();
|
---|
| 92 | QByteArray ans = _outSocket->readLine();
|
---|
| 93 |
|
---|
| 94 | if (ans.indexOf("OK") == -1) {
|
---|
| 95 | delete _outSocket;
|
---|
| 96 | _outSocket = 0;
|
---|
[2990] | 97 | emit(newMessage("Broadcaster: Connection broken", true));
|
---|
[2925] | 98 | }
|
---|
| 99 | else {
|
---|
[2990] | 100 | emit(newMessage("Broadcaster: Connection opened", true));
|
---|
[2925] | 101 | _sOpenTrial = 0;
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | // Write buffer
|
---|
| 106 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 107 | void cmbCaster::write(char* buffer, unsigned len) {
|
---|
| 108 | if (_outSocket) {
|
---|
| 109 | _outSocket->write(buffer, len);
|
---|
| 110 | _outSocket->flush();
|
---|
| 111 | }
|
---|
| 112 | }
|
---|