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

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

minor changes

File size: 4.8 KB
RevLine 
[3172]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 *
[7661]13 * Changes:
[3172]14 *
15 * -----------------------------------------------------------------------*/
16
17#include <math.h>
[7661]18#include "bncuploadcaster.h"
[3172]19#include "bncversion.h"
[5070]20#include "bnccore.h"
[3235]21#include "bnctableitem.h"
[3172]22
23using namespace std;
24
25// Constructor
26////////////////////////////////////////////////////////////////////////////
27bncUploadCaster::bncUploadCaster(const QString& mountpoint,
28 const QString& outHost, int outPort,
[8275]29 const QString& ntripVersion,
30 const QString& userName, const QString& password,
31 int iRow,
[3273]32 int rate) {
[3224]33 _mountpoint = mountpoint;
34 _outHost = outHost;
35 _outPort = outPort;
[8275]36 _ntripVersion = ntripVersion;
37 _userName = userName;
[3224]38 _password = password;
39 _outSocket = 0;
40 _sOpenTrial = 0;
[3233]41 _iRow = iRow;
[3273]42 _rate = rate;
[4809]43 if (_rate < 0) {
44 _rate = 0;
[3273]45 }
46 else if (_rate > 60) {
47 _rate = 60;
48 }
[3207]49 _isToBeDeleted = false;
[3235]50
[7661]51 connect(this, SIGNAL(newMessage(QByteArray,bool)),
[5068]52 BNC_CORE, SLOT(slotMessage(const QByteArray,bool)));
[3235]53
[5068]54 if (BNC_CORE->_uploadTableItems.find(_iRow) != BNC_CORE->_uploadTableItems.end()){
[7661]55 connect(this, SIGNAL(newBytes(QByteArray,double)),
56 BNC_CORE->_uploadTableItems.value(iRow),
[3236]57 SLOT(slotNewBytes(const QByteArray,double)));
[3235]58 }
[8733]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 }
[3172]64}
65
[3207]66// Safe Desctructor
67////////////////////////////////////////////////////////////////////////////
68void bncUploadCaster::deleteSafely() {
69 _isToBeDeleted = true;
[3208]70 if (!isRunning()) {
71 delete this;
72 }
[3207]73}
74
[3172]75// Destructor
76////////////////////////////////////////////////////////////////////////////
77bncUploadCaster::~bncUploadCaster() {
[3208]78 if (isRunning()) {
79 wait();
80 }
[7661]81 if (_outSocket) {
82 delete _outSocket;
83 }
[3172]84}
85
[3226]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);
[4808]98 if (_outBuffer.size() > 0) {
99 _outSocket->write(_outBuffer);
100 _outSocket->flush();
[8204]101 emit newBytes(_mountpoint.toLatin1(), _outBuffer.size());
[4808]102 }
[3226]103 }
[4809]104 if (_rate == 0) {
[4985]105 {
106 QMutexLocker locker(&_mutex);
107 _outBuffer.clear();
108 }
[8708]109 msleep(100); //sleep 0.1 sec
[4809]110 }
111 else {
112 sleep(_rate);
113 }
[3226]114 }
115}
116
[3172]117// Start the Communication with NTRIP Caster
118////////////////////////////////////////////////////////////////////////////
119void bncUploadCaster::open() {
120
121 if (_mountpoint.isEmpty()) {
122 return;
123 }
124
[7661]125 if (_outSocket != 0 &&
[3172]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();
[8904]145 _outSocket->setProxy(QNetworkProxy::NoProxy); // Ntrip1: to prevent the usage of system entries
[3172]146 _outSocket->connectToHost(_outHost, _outPort);
147
148 const int timeOut = 5000; // 5 seconds
149 if (!_outSocket->waitForConnected(timeOut)) {
[8770]150 emit(newMessage("Broadcaster: Connect timeout for " + _mountpoint.toLatin1()
151 + "(" + _outHost.toLatin1() + "), " + _outSocket->errorString().toLatin1(), true));
[3172]152 delete _outSocket;
153 _outSocket = 0;
154 return;
155 }
156
[8204]157 QByteArray msg = "SOURCE " + _password.toLatin1() + " /" +
158 _mountpoint.toLatin1() + "\r\n" +
[3172]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;
[8204]170 emit(newMessage("Broadcaster: Connection broken for " + _mountpoint.toLatin1(), true));
[3172]171 }
172 else {
[8204]173 emit(newMessage("Broadcaster: Connection opened for " + _mountpoint.toLatin1(), true));
[3172]174 _sOpenTrial = 0;
175 }
176}
177
Note: See TracBrowser for help on using the repository browser.