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