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

Last change on this file since 5681 was 5070, checked in by mervart, 12 years ago
File size: 4.1 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 *
13 * Changes:
14 *
15 * -----------------------------------------------------------------------*/
16
17#include <math.h>
18#include "bncuploadcaster.h"
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,
[3273]29 const QString& password, int iRow,
30 int rate) {
[3224]31 _mountpoint = mountpoint;
32 _outHost = outHost;
33 _outPort = outPort;
34 _password = password;
35 _outSocket = 0;
36 _sOpenTrial = 0;
[3233]37 _iRow = iRow;
[3273]38 _rate = rate;
[4809]39 if (_rate < 0) {
40 _rate = 0;
[3273]41 }
42 else if (_rate > 60) {
43 _rate = 60;
44 }
[3207]45 _isToBeDeleted = false;
[3235]46
47 connect(this, SIGNAL(newMessage(QByteArray,bool)),
[5068]48 BNC_CORE, SLOT(slotMessage(const QByteArray,bool)));
[3235]49
[5068]50 if (BNC_CORE->_uploadTableItems.find(_iRow) != BNC_CORE->_uploadTableItems.end()){
[3235]51 connect(this, SIGNAL(newBytes(QByteArray,double)),
[5068]52 BNC_CORE->_uploadTableItems.value(iRow),
[3236]53 SLOT(slotNewBytes(const QByteArray,double)));
[3235]54 }
[3172]55}
56
[3207]57// Safe Desctructor
58////////////////////////////////////////////////////////////////////////////
59void bncUploadCaster::deleteSafely() {
60 _isToBeDeleted = true;
[3208]61 if (!isRunning()) {
62 delete this;
63 }
[3207]64}
65
[3172]66// Destructor
67////////////////////////////////////////////////////////////////////////////
68bncUploadCaster::~bncUploadCaster() {
[3208]69 if (isRunning()) {
70 wait();
71 }
[3172]72}
73
[3226]74// Endless Loop
75////////////////////////////////////////////////////////////////////////////
76void bncUploadCaster::run() {
77 while (true) {
78 if (_isToBeDeleted) {
79 QThread::quit();
80 deleteLater();
81 return;
82 }
83 open();
84 if (_outSocket && _outSocket->state() == QAbstractSocket::ConnectedState) {
85 QMutexLocker locker(&_mutex);
[4808]86 if (_outBuffer.size() > 0) {
87 _outSocket->write(_outBuffer);
88 _outSocket->flush();
89 emit newBytes(_mountpoint.toAscii(), _outBuffer.size());
90 }
[3226]91 }
[4809]92 if (_rate == 0) {
[4985]93 {
94 QMutexLocker locker(&_mutex);
95 _outBuffer.clear();
96 }
[4810]97 msleep(100); //sleep 0.1 sec
[4809]98 }
99 else {
100 sleep(_rate);
101 }
[3226]102 }
103}
104
[3172]105// Start the Communication with NTRIP Caster
106////////////////////////////////////////////////////////////////////////////
107void bncUploadCaster::open() {
108
109 if (_mountpoint.isEmpty()) {
110 return;
111 }
112
113 if (_outSocket != 0 &&
114 _outSocket->state() == QAbstractSocket::ConnectedState) {
115 return;
116 }
117
118 delete _outSocket; _outSocket = 0;
119
120 double minDt = pow(2.0,_sOpenTrial);
121 if (++_sOpenTrial > 4) {
122 _sOpenTrial = 4;
123 }
124 if (_outSocketOpenTime.isValid() &&
125 _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
126 return;
127 }
128 else {
129 _outSocketOpenTime = QDateTime::currentDateTime();
130 }
131
132 _outSocket = new QTcpSocket();
133 _outSocket->connectToHost(_outHost, _outPort);
134
135 const int timeOut = 5000; // 5 seconds
136 if (!_outSocket->waitForConnected(timeOut)) {
137 delete _outSocket;
138 _outSocket = 0;
[3189]139 emit(newMessage("Broadcaster: Connect timeout", true));
[3172]140 return;
141 }
142
143 QByteArray msg = "SOURCE " + _password.toAscii() + " /" +
144 _mountpoint.toAscii() + "\r\n" +
145 "Source-Agent: NTRIP BNC/" BNCVERSION "\r\n\r\n";
146
147 _outSocket->write(msg);
148 _outSocket->waitForBytesWritten();
149
150 _outSocket->waitForReadyRead();
151 QByteArray ans = _outSocket->readLine();
152
153 if (ans.indexOf("OK") == -1) {
154 delete _outSocket;
155 _outSocket = 0;
[3189]156 emit(newMessage("Broadcaster: Connection broken", true));
[3172]157 }
158 else {
[3189]159 emit(newMessage("Broadcaster: Connection opened", true));
[3172]160 _sOpenTrial = 0;
161 }
162}
163
Note: See TracBrowser for help on using the repository browser.