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

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