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

Last change on this file since 9725 was 9725, checked in by stuerze, 2 years ago

minor changes

File size: 10.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 "bnccore.h"
21#include "bnctableitem.h"
22#include "bncsettings.h"
23#include "bncsslconfig.h"
24
25using namespace std;
26
27// Constructor
28////////////////////////////////////////////////////////////////////////////
29bncUploadCaster::bncUploadCaster(const QString &mountpoint,
30 const QString &outHost, int outPort, const QString &ntripVersion,
31 const QString &userName, const QString &password, int iRow, int rate) {
32 bncSettings settings;
33
34 _mountpoint = mountpoint;
35 _casterOutHost = outHost;
36 _casterOutPort = outPort;
37 _ntripVersion = ntripVersion;
38 _userName = userName;
39 _password = password;
40 _outSocket = 0;
41 _sOpenTrial = 0;
42 _iRow = iRow;
43 _rate = rate;
44
45 if (_rate < 0) {
46 _rate = 0;
47 } else if (_rate > 60) {
48 _rate = 60;
49 }
50 _isToBeDeleted = false;
51
52 connect(this, SIGNAL(newMessage(QByteArray,bool)),
53 BNC_CORE, SLOT(slotMessage(const QByteArray,bool)));
54
55 if (BNC_CORE->_uploadTableItems.find(_iRow)
56 != BNC_CORE->_uploadTableItems.end()) {
57 connect(this, SIGNAL(newBytes(QByteArray,double)),
58 BNC_CORE->_uploadTableItems.value(iRow),
59 SLOT(slotNewBytes(const QByteArray,double)));
60 }
61 if (BNC_CORE->_uploadEphTableItems.find(_iRow)
62 != BNC_CORE->_uploadEphTableItems.end()) {
63 connect(this, SIGNAL(newBytes(QByteArray,double)),
64 BNC_CORE->_uploadEphTableItems.value(iRow),
65 SLOT(slotNewBytes(const QByteArray,double)));
66 }
67
68 _sslIgnoreErrors = (Qt::CheckState(settings.value("sslIgnoreErrors").toInt())
69 == Qt::Checked);
70
71 _proxyOutHost = settings.value("proxyHost").toString();
72 _proxyOutPort = settings.value("proxyPort").toInt();
73 (_proxyOutHost.isEmpty()) ? _proxy = false : _proxy = true;
74
75 _secure = false;
76 if (_ntripVersion == "2s") {
77 if (!QSslSocket::supportsSsl()) {
78 emit(newMessage(
79 "For SSL support please install OpenSSL run-time libraries: Ntrip Version 2 is tried",
80 true));
81 _ntripVersion == "2";
82 } else {
83 _secure = true;
84 _casterOutPort = 443;
85 }
86 }
87
88 if (!_secure && _proxy) {
89 _postExtension = QString("http://%1:%2").arg(_casterOutHost).arg(_casterOutPort);
90 } else {
91 _postExtension = "";
92 }
93}
94
95// Safe Desctructor
96////////////////////////////////////////////////////////////////////////////
97void bncUploadCaster::deleteSafely() {
98 _isToBeDeleted = true;
99 if (!isRunning()) {
100 delete this;
101 }
102}
103
104// Destructor
105////////////////////////////////////////////////////////////////////////////
106bncUploadCaster::~bncUploadCaster() {
107 if (isRunning()) {
108 wait();
109 }
110 if (_outSocket) {
111 delete _outSocket;
112 }
113}
114
115//
116////////////////////////////////////////////////////////////////////////////
117void bncUploadCaster::slotProxyAuthenticationRequired(const QNetworkProxy&,
118 QAuthenticator*) {
119 emit newMessage("slotProxyAuthenticationRequired", true);
120}
121
122// Endless Loop
123////////////////////////////////////////////////////////////////////////////
124void bncUploadCaster::run() {
125 while (true) {
126 if (_isToBeDeleted) {
127 QThread::quit();
128 deleteLater();
129 return;
130 }
131 open();
132 if (_outSocket && _outSocket->state() == QAbstractSocket::ConnectedState) {
133 QMutexLocker locker(&_mutex);
134 if (_outBuffer.size() > 0) {
135 if (_ntripVersion == "1") {
136 _outSocket->write(_outBuffer);
137 _outSocket->flush();
138 } else {
139 QString chunkSize = QString("%1").arg(_outBuffer.size(), 0, 16,
140 QLatin1Char('0'));
141 QByteArray chunkedData = chunkSize.toLatin1() + "\r\n" + _outBuffer
142 + "\r\n";
143 _outSocket->write(chunkedData);
144 _outSocket->flush();
145 }
146 emit newBytes(_mountpoint.toLatin1(), _outBuffer.size());
147 }
148 }
149 if (_rate == 0) {
150 {
151 QMutexLocker locker(&_mutex);
152 _outBuffer.clear();
153 }
154 msleep(100); //sleep 0.1 sec
155 } else {
156 sleep(_rate);
157 }
158 }
159}
160
161// Start the Communication with NTRIP Caster
162////////////////////////////////////////////////////////////////////////////
163void bncUploadCaster::open() {
164 const int timeOut = 5000; // 5 seconds
165 bncSslConfig sslConfig;
166 QByteArray msg;
167
168
169 if (_mountpoint.isEmpty()) {
170 return;
171 }
172
173 if (_outSocket != 0) {
174 if (_outSocket->state() == QAbstractSocket::ConnectedState) {
175 return;
176 } else {
177 emit(newMessage(
178 "Broadcaster: No connection for " + _mountpoint.toLatin1(), true));
179 }
180 }
181 delete _outSocket;
182 _outSocket = 0;
183
184 double minDt = pow(2.0, _sOpenTrial);
185 if (++_sOpenTrial > 4) {
186 _sOpenTrial = 4;
187 }
188 if (_outSocketOpenTime.isValid()
189 && _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
190 return;
191 } else {
192 _outSocketOpenTime = QDateTime::currentDateTime();
193 }
194
195 _outSocket = new QSslSocket();
196 _outSocket->setProxy(QNetworkProxy::NoProxy);
197 _outSocket->setSslConfiguration(sslConfig);
198 if (_sslIgnoreErrors) {
199 _outSocket->ignoreSslErrors();
200 emit(newMessage("BNC ignores SSL errors as configured", true));
201 }
202
203 if (_proxy) {
204 if (_ntripVersion == "1") {
205 emit(newMessage("No proxy support in Ntrip Version 1 upload!", true));
206 delete _outSocket;
207 _outSocket = 0;
208 return;
209 }
210 connect(_outSocket, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)),
211 this,SLOT(slotProxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)));
212
213 if (!connectToHost(_proxyOutHost, _proxyOutPort, false)) {
214 return;
215 }
216
217 if (_secure) {
218 msg = "CONNECT " + _casterOutHost.toLatin1() + ":"
219 + QString("%1").arg(_casterOutPort).toLatin1() + " HTTP/1.1\r\n"
220 + "Proxy-Connection: Keep-Alive\r\n"
221 + "Host: " + _casterOutHost.toLatin1() + "\r\n"
222 + "User-Agent: NTRIP BNC/" BNCVERSION " (" + BNC_OS + ")\r\n"
223 + "\r\n"; cout << msg.toStdString().c_str();
224 _outSocket->write(msg);
225 _outSocket->waitForBytesWritten();
226 _outSocket->waitForReadyRead();
227 QByteArray ans = _outSocket->readLine(); cout << ans.toStdString().c_str() << endl;
228 if (ans.indexOf("200") == -1) {
229 emit(newMessage(
230 "Proxy: Connection broken for " + _mountpoint.toLatin1()
231 + ": " + ans.left(ans.length() - 2), true));
232 delete _outSocket;
233 _outSocket = 0;
234 return;
235 } else {
236 emit(newMessage(
237 "Proxy: Connection opened for " + _mountpoint.toLatin1()
238 + ": " + ans.left(ans.length() - 2), true));
239 _sOpenTrial = 0;
240 _outSocket->setPeerVerifyName(_casterOutHost);
241 _outSocket->startClientEncryption();
242 if (!_outSocket->waitForEncrypted(timeOut)) {
243 emit(newMessage(
244 "Proxy: Encrypt timeout for " + _mountpoint.toLatin1() + " ("
245 + _casterOutHost.toLatin1() + ":"
246 + QString("%1) ").arg(_casterOutPort).toLatin1()
247 + _outSocket->errorString().toLatin1(), true));
248 delete _outSocket;
249 _outSocket = 0;
250 return;
251 } else {
252 emit(newMessage("Proxy: SSL handshake completed for " + _mountpoint.toLatin1(), true));
253 }
254 }
255 }
256 } else {
257 if (!connectToHost(_casterOutHost, _casterOutPort, _secure)) {
258 return;
259 }
260 }
261
262 if (_ntripVersion == "1") {
263 msg = "SOURCE " + _password.toLatin1() + " /" + _mountpoint.toLatin1()
264 + "\r\n" + "Source-Agent: NTRIP BNC/" BNCVERSION "\r\n\r\n";
265 } else {
266 msg = "POST " + _postExtension.toLatin1() + "/" + _mountpoint.toLatin1()
267 + " HTTP/1.1\r\n" + "Host: " + _casterOutHost.toLatin1() + "\r\n"
268 + "Ntrip-Version: Ntrip/2.0\r\n" + "Authorization: Basic "
269 + (_userName + ":" + _password).toLatin1().toBase64() + "\r\n"
270 + "User-Agent: NTRIP BNC/" BNCVERSION " (" + BNC_OS + ")\r\n"
271 + "Connection: close\r\n" + "Transfer-Encoding: chunked\r\n\r\n";
272 }
273 cout << msg.toStdString().c_str();
274 _outSocket->write(msg);
275 _outSocket->waitForBytesWritten();
276 _outSocket->waitForReadyRead();
277
278 QByteArray ans = _outSocket->readLine(); cout << "ans: " << ans.toStdString().c_str() << endl;
279
280 if (ans.indexOf("200") == -1) {
281 delete _outSocket;
282 _outSocket = 0;
283 emit(newMessage(
284 "Broadcaster: Connection broken for " + _mountpoint.toLatin1() + ": "
285 + ans.left(ans.length() - 2), true));
286 } else {
287 emit(newMessage(
288 "Broadcaster: Connection opened for " + _mountpoint.toLatin1() + ": "
289 + ans.left(ans.length() - 2), true));
290 _sOpenTrial = 0;
291 }
292}
293
294// Try connection to NTRIP Caster or Proxy
295////////////////////////////////////////////////////////////////////////////
296bool bncUploadCaster::connectToHost(QString outHost, int outPort, bool encrypted) {
297 const int timeOut = 5000; // 5 seconds
298 if (encrypted) {
299 _outSocket->connectToHostEncrypted(outHost, outPort);
300 if (!_outSocket->waitForEncrypted(timeOut)) {
301 emit(newMessage(
302 "Broadcaster: Connect timeout for " + _mountpoint.toLatin1() + " ("
303 + outHost.toLatin1() + ":"
304 + QString("%1) ").arg(outPort).toLatin1()
305 + _outSocket->errorString().toLatin1(), true));
306 delete _outSocket;
307 _outSocket = 0;
308 return false;
309 } else {
310 emit(newMessage("Broadcaster: SSL handshake completed for " + _mountpoint.toLatin1(), true));
311 }
312 } else {
313 _outSocket->connectToHost(outHost, outPort);
314 if (!_outSocket->waitForConnected(timeOut)) {
315 emit(newMessage(
316 "Broadcaster: Connect timeout for " + _mountpoint.toLatin1() + " ("
317 + outHost.toLatin1() + ":"
318 + QString("%1) ").arg(outPort).toLatin1()
319 + _outSocket->errorString().toLatin1(), true));
320 delete _outSocket;
321 _outSocket = 0;
322 return false;
323 }
324 }
325 return true;
326}
327
328
329
330
331
332
333
Note: See TracBrowser for help on using the repository browser.