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

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