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

Last change on this file since 8730 was 8708, checked in by stuerze, 6 years ago

minor changes

File size: 4.4 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}
60
61// Safe Desctructor
62////////////////////////////////////////////////////////////////////////////
63void bncUploadCaster::deleteSafely() {
64 _isToBeDeleted = true;
65 if (!isRunning()) {
66 delete this;
67 }
68}
69
70// Destructor
71////////////////////////////////////////////////////////////////////////////
72bncUploadCaster::~bncUploadCaster() {
73 if (isRunning()) {
74 wait();
75 }
76 if (_outSocket) {
77 delete _outSocket;
78 }
79}
80
81// Endless Loop
82////////////////////////////////////////////////////////////////////////////
83void bncUploadCaster::run() {
84 while (true) {
85 if (_isToBeDeleted) {
86 QThread::quit();
87 deleteLater();
88 return;
89 }
90 open();
91 if (_outSocket && _outSocket->state() == QAbstractSocket::ConnectedState) {
92 QMutexLocker locker(&_mutex);
93 if (_outBuffer.size() > 0) {
94 _outSocket->write(_outBuffer);
95 _outSocket->flush();
96 emit newBytes(_mountpoint.toLatin1(), _outBuffer.size());
97 }
98 }
99 if (_rate == 0) {
100 {
101 QMutexLocker locker(&_mutex);
102 _outBuffer.clear();
103 }
104 msleep(100); //sleep 0.1 sec
105 }
106 else {
107 sleep(_rate);
108 }
109 }
110}
111
112// Start the Communication with NTRIP Caster
113////////////////////////////////////////////////////////////////////////////
114void bncUploadCaster::open() {
115
116 if (_mountpoint.isEmpty()) {
117 return;
118 }
119
120 if (_outSocket != 0 &&
121 _outSocket->state() == QAbstractSocket::ConnectedState) {
122 return;
123 }
124
125 delete _outSocket; _outSocket = 0;
126
127 double minDt = pow(2.0,_sOpenTrial);
128 if (++_sOpenTrial > 4) {
129 _sOpenTrial = 4;
130 }
131 if (_outSocketOpenTime.isValid() &&
132 _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
133 return;
134 }
135 else {
136 _outSocketOpenTime = QDateTime::currentDateTime();
137 }
138
139 _outSocket = new QTcpSocket();
140 _outSocket->connectToHost(_outHost, _outPort);
141
142 const int timeOut = 5000; // 5 seconds
143 if (!_outSocket->waitForConnected(timeOut)) {
144 delete _outSocket;
145 _outSocket = 0;
146 emit(newMessage("Broadcaster: Connect timeout for " + _mountpoint.toLatin1(), true));
147 return;
148 }
149
150 QByteArray msg = "SOURCE " + _password.toLatin1() + " /" +
151 _mountpoint.toLatin1() + "\r\n" +
152 "Source-Agent: NTRIP BNC/" BNCVERSION "\r\n\r\n";
153
154 _outSocket->write(msg);
155 _outSocket->waitForBytesWritten();
156
157 _outSocket->waitForReadyRead();
158 QByteArray ans = _outSocket->readLine();
159
160 if (ans.indexOf("OK") == -1) {
161 delete _outSocket;
162 _outSocket = 0;
163 emit(newMessage("Broadcaster: Connection broken for " + _mountpoint.toLatin1(), true));
164 }
165 else {
166 emit(newMessage("Broadcaster: Connection opened for " + _mountpoint.toLatin1(), true));
167 _sOpenTrial = 0;
168 }
169}
170
Note: See TracBrowser for help on using the repository browser.