[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 | *
|
---|
[7661] | 13 | * Changes:
|
---|
[3172] | 14 | *
|
---|
| 15 | * -----------------------------------------------------------------------*/
|
---|
| 16 |
|
---|
| 17 | #include <math.h>
|
---|
[7661] | 18 | #include "bncuploadcaster.h"
|
---|
[3172] | 19 | #include "bncversion.h"
|
---|
[5070] | 20 | #include "bnccore.h"
|
---|
[3235] | 21 | #include "bnctableitem.h"
|
---|
[9707] | 22 | #include "bncsettings.h"
|
---|
[3172] | 23 |
|
---|
| 24 | using namespace std;
|
---|
| 25 |
|
---|
| 26 | // Constructor
|
---|
| 27 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 28 | bncUploadCaster::bncUploadCaster(const QString& mountpoint,
|
---|
| 29 | const QString& outHost, int outPort,
|
---|
[8275] | 30 | const QString& ntripVersion,
|
---|
| 31 | const QString& userName, const QString& password,
|
---|
| 32 | int iRow,
|
---|
[3273] | 33 | int rate) {
|
---|
[3224] | 34 | _mountpoint = mountpoint;
|
---|
[9707] | 35 | _casterOutHost = outHost;
|
---|
| 36 | _casterOutPort = outPort;
|
---|
[8275] | 37 | _ntripVersion = ntripVersion;
|
---|
| 38 | _userName = userName;
|
---|
[3224] | 39 | _password = password;
|
---|
| 40 | _outSocket = 0;
|
---|
| 41 | _sOpenTrial = 0;
|
---|
[3233] | 42 | _iRow = iRow;
|
---|
[3273] | 43 | _rate = rate;
|
---|
[9707] | 44 |
|
---|
[4809] | 45 | if (_rate < 0) {
|
---|
| 46 | _rate = 0;
|
---|
[3273] | 47 | }
|
---|
| 48 | else if (_rate > 60) {
|
---|
| 49 | _rate = 60;
|
---|
| 50 | }
|
---|
[3207] | 51 | _isToBeDeleted = false;
|
---|
[3235] | 52 |
|
---|
[7661] | 53 | connect(this, SIGNAL(newMessage(QByteArray,bool)),
|
---|
[5068] | 54 | BNC_CORE, SLOT(slotMessage(const QByteArray,bool)));
|
---|
[3235] | 55 |
|
---|
[5068] | 56 | if (BNC_CORE->_uploadTableItems.find(_iRow) != BNC_CORE->_uploadTableItems.end()){
|
---|
[7661] | 57 | connect(this, SIGNAL(newBytes(QByteArray,double)),
|
---|
| 58 | BNC_CORE->_uploadTableItems.value(iRow),
|
---|
[3236] | 59 | SLOT(slotNewBytes(const QByteArray,double)));
|
---|
[3235] | 60 | }
|
---|
[8733] | 61 | if (BNC_CORE->_uploadEphTableItems.find(_iRow) != BNC_CORE->_uploadEphTableItems.end()){
|
---|
| 62 | connect(this, SIGNAL(newBytes(QByteArray,double)),
|
---|
| 63 | BNC_CORE->_uploadEphTableItems.value(iRow),
|
---|
| 64 | SLOT(slotNewBytes(const QByteArray,double)));
|
---|
| 65 | }
|
---|
[9714] | 66 |
|
---|
| 67 | bncSettings settings;
|
---|
| 68 | _sslIgnoreErrors = (Qt::CheckState(settings.value("sslIgnoreErrors").toInt()) == Qt::Checked);
|
---|
| 69 | _proxyHost = settings.value("proxyHost").toString();
|
---|
| 70 | _proxyPort = settings.value("proxyPort").toInt();
|
---|
| 71 | (_proxyHost.isEmpty()) ? _proxy = false : _proxy = true;
|
---|
| 72 |
|
---|
| 73 | if (_ntripVersion == "2s") {
|
---|
| 74 | _secure = true;
|
---|
| 75 | if (!QSslSocket::supportsSsl()) {
|
---|
| 76 | emit(newMessage("For SSL support please install OpenSSL run-time libraries: Ntrip Version 2 is tried", true));
|
---|
| 77 | _secure = false;
|
---|
| 78 | _ntripVersion == "2";
|
---|
| 79 | }
|
---|
| 80 | } else {
|
---|
| 81 | _secure = false;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | if (_secure) {
|
---|
| 85 | _casterOutPort = 443;
|
---|
| 86 | (_proxy) ? _postExtension = QString("https://%1:%2").arg(_casterOutHost).arg(_casterOutPort) : _postExtension = "";
|
---|
| 87 | }
|
---|
| 88 | else {
|
---|
| 89 | (_proxy) ? _postExtension = QString("http://%1:%2").arg(_casterOutHost).arg(_casterOutPort) : _postExtension = "";
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[3172] | 92 | }
|
---|
| 93 |
|
---|
[3207] | 94 | // Safe Desctructor
|
---|
| 95 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 96 | void bncUploadCaster::deleteSafely() {
|
---|
| 97 | _isToBeDeleted = true;
|
---|
[3208] | 98 | if (!isRunning()) {
|
---|
| 99 | delete this;
|
---|
| 100 | }
|
---|
[3207] | 101 | }
|
---|
| 102 |
|
---|
[3172] | 103 | // Destructor
|
---|
| 104 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 105 | bncUploadCaster::~bncUploadCaster() {
|
---|
[3208] | 106 | if (isRunning()) {
|
---|
| 107 | wait();
|
---|
| 108 | }
|
---|
[7661] | 109 | if (_outSocket) {
|
---|
| 110 | delete _outSocket;
|
---|
| 111 | }
|
---|
[3172] | 112 | }
|
---|
| 113 |
|
---|
[9707] | 114 | //
|
---|
| 115 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 116 | void bncUploadCaster::slotProxyAuthenticationRequired(const QNetworkProxy&,
|
---|
| 117 | QAuthenticator*) {
|
---|
| 118 | emit newMessage("slotProxyAuthenticationRequired", true);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 |
|
---|
| 122 | /* TSL/SSL
|
---|
| 123 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 124 | void bncUploadCaster::slotSslErrors(QList<QSslError> errors) {
|
---|
| 125 |
|
---|
| 126 | QString msg = "SSL Error\n";
|
---|
| 127 | QSslCertificate cert = _outSocket->sslConfiguration().peerCertificate();
|
---|
| 128 | if (!cert.isNull()) {
|
---|
| 129 | msg += QString("Server Certificate Issued by:\n"
|
---|
| 130 | "%1\n%2\nCannot be verified\n")
|
---|
| 131 | #if QT_VERSION >= 0x050000
|
---|
| 132 | .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName).at(0))
|
---|
| 133 | .arg(cert.issuerInfo(QSslCertificate::Organization).at(0));
|
---|
| 134 | #else
|
---|
| 135 | .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName))
|
---|
| 136 | .arg(cert.issuerInfo(QSslCertificate::Organization));
|
---|
| 137 | #endif
|
---|
| 138 | }
|
---|
| 139 | QListIterator<QSslError> it(errors);
|
---|
| 140 | while (it.hasNext()) {
|
---|
| 141 | const QSslError& err = it.next();
|
---|
| 142 | msg += "\n" + err.errorString();
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | BNC_CORE->slotMessage(msg.toLatin1(), true);
|
---|
| 146 |
|
---|
| 147 | if (_sslIgnoreErrors) {
|
---|
| 148 | _outSocket->ignoreSslErrors();
|
---|
| 149 | }
|
---|
| 150 | else {
|
---|
| 151 | deleteSafely();
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 | */
|
---|
| 155 |
|
---|
[3226] | 156 | // Endless Loop
|
---|
| 157 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 158 | void bncUploadCaster::run() {
|
---|
| 159 | while (true) {
|
---|
| 160 | if (_isToBeDeleted) {
|
---|
| 161 | QThread::quit();
|
---|
| 162 | deleteLater();
|
---|
| 163 | return;
|
---|
| 164 | }
|
---|
| 165 | open();
|
---|
| 166 | if (_outSocket && _outSocket->state() == QAbstractSocket::ConnectedState) {
|
---|
| 167 | QMutexLocker locker(&_mutex);
|
---|
[4808] | 168 | if (_outBuffer.size() > 0) {
|
---|
[9707] | 169 | if (_ntripVersion == "1") {
|
---|
| 170 | _outSocket->write(_outBuffer);
|
---|
| 171 | _outSocket->flush();
|
---|
| 172 | }
|
---|
| 173 | else {
|
---|
| 174 | QString chunkSize = QString("%1").arg(_outBuffer.size(),0,16,QLatin1Char('0'));
|
---|
| 175 | QByteArray chunkedData = chunkSize.toLatin1() + "\r\n" + _outBuffer + "\r\n";
|
---|
| 176 | _outSocket->write(chunkedData);
|
---|
| 177 | _outSocket->flush();
|
---|
| 178 | }
|
---|
[8204] | 179 | emit newBytes(_mountpoint.toLatin1(), _outBuffer.size());
|
---|
[4808] | 180 | }
|
---|
[3226] | 181 | }
|
---|
[4809] | 182 | if (_rate == 0) {
|
---|
[4985] | 183 | {
|
---|
| 184 | QMutexLocker locker(&_mutex);
|
---|
| 185 | _outBuffer.clear();
|
---|
| 186 | }
|
---|
[8708] | 187 | msleep(100); //sleep 0.1 sec
|
---|
[4809] | 188 | }
|
---|
| 189 | else {
|
---|
| 190 | sleep(_rate);
|
---|
| 191 | }
|
---|
[3226] | 192 | }
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[3172] | 195 | // Start the Communication with NTRIP Caster
|
---|
| 196 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 197 | void bncUploadCaster::open() {
|
---|
| 198 |
|
---|
| 199 | if (_mountpoint.isEmpty()) {
|
---|
| 200 | return;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[9712] | 203 | if (_outSocket != 0) {
|
---|
| 204 | if (_outSocket->state() == QAbstractSocket::ConnectedState) {
|
---|
| 205 | return;
|
---|
| 206 | }
|
---|
| 207 | else {
|
---|
| 208 | emit(newMessage("Broadcaster: No connection for " + _mountpoint.toLatin1(), true));
|
---|
| 209 | }
|
---|
[3172] | 210 | }
|
---|
[9712] | 211 |
|
---|
[3172] | 212 | delete _outSocket; _outSocket = 0;
|
---|
| 213 |
|
---|
| 214 | double minDt = pow(2.0,_sOpenTrial);
|
---|
| 215 | if (++_sOpenTrial > 4) {
|
---|
| 216 | _sOpenTrial = 4;
|
---|
| 217 | }
|
---|
| 218 | if (_outSocketOpenTime.isValid() &&
|
---|
| 219 | _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
|
---|
| 220 | return;
|
---|
| 221 | }
|
---|
| 222 | else {
|
---|
| 223 | _outSocketOpenTime = QDateTime::currentDateTime();
|
---|
| 224 | }
|
---|
| 225 |
|
---|
[9707] | 226 | _outSocket = new QSslSocket();
|
---|
[9714] | 227 | const int timeOut = 5000; // 5 seconds
|
---|
[9707] | 228 | if (_sslIgnoreErrors) {
|
---|
| 229 | _outSocket->ignoreSslErrors();
|
---|
| 230 | }
|
---|
[9714] | 231 | _outSocket->setProxy(QNetworkProxy::NoProxy);
|
---|
[9712] | 232 | _outHost = _casterOutHost;
|
---|
| 233 | _outPort = _casterOutPort;
|
---|
[9714] | 234 | if (_proxy) {
|
---|
[9712] | 235 | _outHost = _proxyHost;
|
---|
| 236 | _outPort = _proxyPort;
|
---|
[9714] | 237 | connect(_outSocket, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)),
|
---|
[9712] | 238 | this, SLOT(slotProxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)));
|
---|
[9714] | 239 | if (_ntripVersion == "1") {
|
---|
| 240 | emit(newMessage("No proxy support in Ntrip Version 1 upload!",true));
|
---|
| 241 | }
|
---|
[9707] | 242 | }
|
---|
[3172] | 243 |
|
---|
[9714] | 244 | if (!_secure || _proxy) {
|
---|
[9707] | 245 | _outSocket->connectToHost(_outHost, _outPort);
|
---|
[9713] | 246 | if (!_outSocket->waitForConnected(timeOut)) {
|
---|
| 247 | emit(newMessage("Broadcaster: Connect timeout for " + _mountpoint.toLatin1()
|
---|
| 248 | + " (" + _outHost.toLatin1()+ ":" + QString("%1) ").arg(_outPort).toLatin1()
|
---|
| 249 | + _outSocket->errorString().toLatin1(), true));
|
---|
| 250 | delete _outSocket;
|
---|
| 251 | _outSocket = 0;
|
---|
| 252 | return;
|
---|
| 253 | }
|
---|
[9707] | 254 | }
|
---|
| 255 | else {
|
---|
[9713] | 256 | _outSocket->connectToHostEncrypted(_outHost, _outPort);
|
---|
| 257 | if (!_outSocket->waitForEncrypted(timeOut)) {
|
---|
| 258 | emit(newMessage("Broadcaster: Connect timeout for " + _mountpoint.toLatin1()
|
---|
| 259 | + " (" + _outHost.toLatin1()+ ":" + QString("%1) ").arg(_outPort).toLatin1()
|
---|
| 260 | + _outSocket->errorString().toLatin1(), true));
|
---|
| 261 | delete _outSocket;
|
---|
| 262 | _outSocket = 0;
|
---|
| 263 | return;
|
---|
| 264 | }
|
---|
| 265 | }
|
---|
[9707] | 266 |
|
---|
| 267 | QByteArray msg;
|
---|
| 268 | if (_ntripVersion == "1") {
|
---|
| 269 | msg = "SOURCE " + _password.toLatin1() + " /" + _mountpoint.toLatin1() + "\r\n" +
|
---|
| 270 | "Source-Agent: NTRIP BNC/" BNCVERSION "\r\n\r\n";
|
---|
| 271 | }
|
---|
| 272 | else {
|
---|
| 273 | msg = "POST " + _postExtension.toLatin1() + "/" + _mountpoint.toLatin1() + " HTTP/1.1\r\n" +
|
---|
| 274 | "Host: " + _casterOutHost.toLatin1() + "\r\n" +
|
---|
| 275 | "Ntrip-Version: Ntrip/2.0\r\n" +
|
---|
| 276 | "Authorization: Basic " + (_userName + ":" + _password).toLatin1().toBase64() + "\r\n" +
|
---|
| 277 | "User-Agent: NTRIP BNC/" BNCVERSION " (" + BNC_OS + ")\r\n" +
|
---|
| 278 | "Connection: close\r\n" +
|
---|
| 279 | "Transfer-Encoding: chunked\r\n\r\n";
|
---|
| 280 | }
|
---|
| 281 | cout << msg.toStdString().c_str();
|
---|
[3172] | 282 | _outSocket->write(msg);
|
---|
| 283 | _outSocket->waitForBytesWritten();
|
---|
| 284 |
|
---|
| 285 | _outSocket->waitForReadyRead();
|
---|
[9707] | 286 | QByteArray ans = _outSocket->readLine();cout << ans.toStdString().c_str() << endl;
|
---|
[3172] | 287 |
|
---|
| 288 | if (ans.indexOf("OK") == -1) {
|
---|
| 289 | delete _outSocket;
|
---|
| 290 | _outSocket = 0;
|
---|
[9707] | 291 | emit(newMessage("Broadcaster: Connection broken for " + _mountpoint.toLatin1() + ": " + ans.left(ans.length()-2), true));
|
---|
[3172] | 292 | }
|
---|
| 293 | else {
|
---|
[9712] | 294 | emit(newMessage("Broadcaster: Connection opened for " + _mountpoint.toLatin1() + ": " + ans.left(ans.length()-2), true));
|
---|
[3172] | 295 | _sOpenTrial = 0;
|
---|
| 296 | }
|
---|
| 297 | }
|
---|
| 298 |
|
---|