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