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

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

minor changes

File size: 4.7 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->connectToHost(_outHost, _outPort);
146
147 const int timeOut = 5000; // 5 seconds
148 if (!_outSocket->waitForConnected(timeOut)) {
149 delete _outSocket;
150 _outSocket = 0;
151 emit(newMessage("Broadcaster: Connect timeout for " + _mountpoint.toLatin1(), true));
152 return;
153 }
154
155 QByteArray msg = "SOURCE " + _password.toLatin1() + " /" +
156 _mountpoint.toLatin1() + "\r\n" +
157 "Source-Agent: NTRIP BNC/" BNCVERSION "\r\n\r\n";
158
159 _outSocket->write(msg);
160 _outSocket->waitForBytesWritten();
161
162 _outSocket->waitForReadyRead();
163 QByteArray ans = _outSocket->readLine();
164
165 if (ans.indexOf("OK") == -1) {
166 delete _outSocket;
167 _outSocket = 0;
168 emit(newMessage("Broadcaster: Connection broken for " + _mountpoint.toLatin1(), true));
169 }
170 else {
171 emit(newMessage("Broadcaster: Connection opened for " + _mountpoint.toLatin1(), true));
172 _sOpenTrial = 0;
173 }
174}
175
Note: See TracBrowser for help on using the repository browser.