source: ntrip/trunk/BNC/src/upload/bncuploadcaster.cpp@ 9484

Last change on this file since 9484 was 8904, checked in by stuerze, 5 years ago

minor changes

File size: 4.8 KB
Line 
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 "bnccore.h"
21#include "bnctableitem.h"
22
23using namespace std;
24
25// Constructor
26////////////////////////////////////////////////////////////////////////////
27bncUploadCaster::bncUploadCaster(const QString& mountpoint,
28 const QString& outHost, int outPort,
29 const QString& ntripVersion,
30 const QString& userName, const QString& password,
31 int iRow,
32 int rate) {
33 _mountpoint = mountpoint;
34 _outHost = outHost;
35 _outPort = outPort;
36 _ntripVersion = ntripVersion;
37 _userName = userName;
38 _password = password;
39 _outSocket = 0;
40 _sOpenTrial = 0;
41 _iRow = iRow;
42 _rate = rate;
43 if (_rate < 0) {
44 _rate = 0;
45 }
46 else if (_rate > 60) {
47 _rate = 60;
48 }
49 _isToBeDeleted = false;
50
51 connect(this, SIGNAL(newMessage(QByteArray,bool)),
52 BNC_CORE, SLOT(slotMessage(const QByteArray,bool)));
53
54 if (BNC_CORE->_uploadTableItems.find(_iRow) != BNC_CORE->_uploadTableItems.end()){
55 connect(this, SIGNAL(newBytes(QByteArray,double)),
56 BNC_CORE->_uploadTableItems.value(iRow),
57 SLOT(slotNewBytes(const QByteArray,double)));
58 }
59 if (BNC_CORE->_uploadEphTableItems.find(_iRow) != BNC_CORE->_uploadEphTableItems.end()){
60 connect(this, SIGNAL(newBytes(QByteArray,double)),
61 BNC_CORE->_uploadEphTableItems.value(iRow),
62 SLOT(slotNewBytes(const QByteArray,double)));
63 }
64}
65
66// Safe Desctructor
67////////////////////////////////////////////////////////////////////////////
68void bncUploadCaster::deleteSafely() {
69 _isToBeDeleted = true;
70 if (!isRunning()) {
71 delete this;
72 }
73}
74
75// Destructor
76////////////////////////////////////////////////////////////////////////////
77bncUploadCaster::~bncUploadCaster() {
78 if (isRunning()) {
79 wait();
80 }
81 if (_outSocket) {
82 delete _outSocket;
83 }
84}
85
86// Endless Loop
87////////////////////////////////////////////////////////////////////////////
88void bncUploadCaster::run() {
89 while (true) {
90 if (_isToBeDeleted) {
91 QThread::quit();
92 deleteLater();
93 return;
94 }
95 open();
96 if (_outSocket && _outSocket->state() == QAbstractSocket::ConnectedState) {
97 QMutexLocker locker(&_mutex);
98 if (_outBuffer.size() > 0) {
99 _outSocket->write(_outBuffer);
100 _outSocket->flush();
101 emit newBytes(_mountpoint.toLatin1(), _outBuffer.size());
102 }
103 }
104 if (_rate == 0) {
105 {
106 QMutexLocker locker(&_mutex);
107 _outBuffer.clear();
108 }
109 msleep(100); //sleep 0.1 sec
110 }
111 else {
112 sleep(_rate);
113 }
114 }
115}
116
117// Start the Communication with NTRIP Caster
118////////////////////////////////////////////////////////////////////////////
119void bncUploadCaster::open() {
120
121 if (_mountpoint.isEmpty()) {
122 return;
123 }
124
125 if (_outSocket != 0 &&
126 _outSocket->state() == QAbstractSocket::ConnectedState) {
127 return;
128 }
129
130 delete _outSocket; _outSocket = 0;
131
132 double minDt = pow(2.0,_sOpenTrial);
133 if (++_sOpenTrial > 4) {
134 _sOpenTrial = 4;
135 }
136 if (_outSocketOpenTime.isValid() &&
137 _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
138 return;
139 }
140 else {
141 _outSocketOpenTime = QDateTime::currentDateTime();
142 }
143
144 _outSocket = new QTcpSocket();
145 _outSocket->setProxy(QNetworkProxy::NoProxy); // Ntrip1: to prevent the usage of system entries
146 _outSocket->connectToHost(_outHost, _outPort);
147
148 const int timeOut = 5000; // 5 seconds
149 if (!_outSocket->waitForConnected(timeOut)) {
150 emit(newMessage("Broadcaster: Connect timeout for " + _mountpoint.toLatin1()
151 + "(" + _outHost.toLatin1() + "), " + _outSocket->errorString().toLatin1(), true));
152 delete _outSocket;
153 _outSocket = 0;
154 return;
155 }
156
157 QByteArray msg = "SOURCE " + _password.toLatin1() + " /" +
158 _mountpoint.toLatin1() + "\r\n" +
159 "Source-Agent: NTRIP BNC/" BNCVERSION "\r\n\r\n";
160
161 _outSocket->write(msg);
162 _outSocket->waitForBytesWritten();
163
164 _outSocket->waitForReadyRead();
165 QByteArray ans = _outSocket->readLine();
166
167 if (ans.indexOf("OK") == -1) {
168 delete _outSocket;
169 _outSocket = 0;
170 emit(newMessage("Broadcaster: Connection broken for " + _mountpoint.toLatin1(), true));
171 }
172 else {
173 emit(newMessage("Broadcaster: Connection opened for " + _mountpoint.toLatin1(), true));
174 _sOpenTrial = 0;
175 }
176}
177
Note: See TracBrowser for help on using the repository browser.