source: ntrip/trunk/BNC/upload/bncuploadcaster.cpp@ 3236

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