[1060] | 1 | /* -------------------------------------------------------------------------
|
---|
| 2 | * BKG NTRIP Server
|
---|
| 3 | * -------------------------------------------------------------------------
|
---|
| 4 | *
|
---|
| 5 | * Class: bnscaster
|
---|
| 6 | *
|
---|
| 7 | * Purpose: Connection to NTRIP Caster
|
---|
| 8 | *
|
---|
| 9 | * Author: L. Mervart
|
---|
| 10 | *
|
---|
| 11 | * Created: 27-Aug-2008
|
---|
| 12 | *
|
---|
| 13 | * Changes:
|
---|
| 14 | *
|
---|
| 15 | * -----------------------------------------------------------------------*/
|
---|
| 16 |
|
---|
| 17 | #include <math.h>
|
---|
| 18 | #include "bnscaster.h"
|
---|
| 19 |
|
---|
| 20 | using namespace std;
|
---|
| 21 |
|
---|
| 22 | // Constructor
|
---|
| 23 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1067] | 24 | t_bnscaster::t_bnscaster(const QString& mountpoint, const QString& outFileName,
|
---|
[1249] | 25 | const QString& refSys, const int ic) {
|
---|
[1067] | 26 |
|
---|
[1060] | 27 | _mountpoint = mountpoint;
|
---|
[1064] | 28 | _outSocket = 0;
|
---|
[1060] | 29 | _outSocketOpenTrial = 0;
|
---|
[1249] | 30 | _ic = ic;
|
---|
[1065] | 31 |
|
---|
| 32 | QSettings settings;
|
---|
| 33 |
|
---|
| 34 | QIODevice::OpenMode oMode;
|
---|
| 35 | if (Qt::CheckState(settings.value("fileAppend").toInt()) == Qt::Checked) {
|
---|
| 36 | oMode = QIODevice::WriteOnly | QIODevice::Unbuffered | QIODevice::Append;
|
---|
| 37 | }
|
---|
| 38 | else {
|
---|
| 39 | oMode = QIODevice::WriteOnly | QIODevice::Unbuffered;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | if (outFileName.isEmpty()) {
|
---|
| 43 | _outFile = 0;
|
---|
| 44 | _outStream = 0;
|
---|
| 45 | }
|
---|
| 46 | else {
|
---|
| 47 | _outFile = new QFile(outFileName);
|
---|
| 48 | if (_outFile->open(oMode)) {
|
---|
| 49 | _outStream = new QTextStream(_outFile);
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
[1066] | 52 |
|
---|
| 53 | // Reference frame
|
---|
| 54 | // ---------------
|
---|
| 55 | _crdTrafo = false;
|
---|
[1243] | 56 | if (refSys == "ETRF2000") {
|
---|
[1066] | 57 | _crdTrafo = true;
|
---|
| 58 | }
|
---|
[1060] | 59 | }
|
---|
| 60 |
|
---|
| 61 | // Destructor
|
---|
| 62 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 63 | t_bnscaster::~t_bnscaster() {
|
---|
| 64 | delete _outSocket;
|
---|
[1065] | 65 | delete _outStream;
|
---|
| 66 | delete _outFile;
|
---|
[1060] | 67 | }
|
---|
| 68 |
|
---|
| 69 | // Start the Communication with NTRIP Caster
|
---|
| 70 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 71 | void t_bnscaster::open() {
|
---|
| 72 |
|
---|
[1123] | 73 | if (_mountpoint.isEmpty()) {
|
---|
| 74 | return;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[1060] | 77 | if (_outSocket != 0 &&
|
---|
| 78 | _outSocket->state() == QAbstractSocket::ConnectedState) {
|
---|
| 79 | return;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | delete _outSocket; _outSocket = 0;
|
---|
| 83 |
|
---|
| 84 | double minDt = exp2(_outSocketOpenTrial);
|
---|
| 85 | if (++_outSocketOpenTrial > 8) {
|
---|
| 86 | _outSocketOpenTrial = 8;
|
---|
| 87 | }
|
---|
| 88 | if (_outSocketOpenTime.isValid() &&
|
---|
| 89 | _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
|
---|
| 90 | return;
|
---|
| 91 | }
|
---|
| 92 | else {
|
---|
| 93 | _outSocketOpenTime = QDateTime::currentDateTime();
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | QSettings settings;
|
---|
| 97 | _outSocket = new QTcpSocket();
|
---|
[1249] | 98 | QString password;
|
---|
| 99 | if (_ic == 1) {
|
---|
| 100 | _outSocket->connectToHost(settings.value("outHost1").toString(),
|
---|
| 101 | settings.value("outPort1").toInt());
|
---|
| 102 | password = settings.value("password1").toString();
|
---|
[1253] | 103 | }
|
---|
[1249] | 104 | if (_ic == 2) {
|
---|
| 105 | _outSocket->connectToHost(settings.value("outHost2").toString(),
|
---|
| 106 | settings.value("outPort2").toInt());
|
---|
| 107 | password = settings.value("password2").toString();
|
---|
| 108 | }
|
---|
[1060] | 109 |
|
---|
[1124] | 110 | const int timeOut = 5000; // 5 seconds
|
---|
[1060] | 111 | if (!_outSocket->waitForConnected(timeOut)) {
|
---|
| 112 | delete _outSocket;
|
---|
| 113 | _outSocket = 0;
|
---|
[1253] | 114 | emit(error("Broadcaster: Connect timeout"));
|
---|
[1060] | 115 | return;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | QByteArray msg = "SOURCE " + password.toAscii() + " /" +
|
---|
| 119 | _mountpoint.toAscii() + "\r\n" +
|
---|
| 120 | "Source-Agent: NTRIP BNS/1.0\r\n\r\n";
|
---|
| 121 |
|
---|
| 122 | _outSocket->write(msg);
|
---|
| 123 | _outSocket->waitForBytesWritten();
|
---|
| 124 |
|
---|
| 125 | _outSocket->waitForReadyRead();
|
---|
| 126 | QByteArray ans = _outSocket->readLine();
|
---|
| 127 |
|
---|
| 128 | if (ans.indexOf("OK") == -1) {
|
---|
| 129 | delete _outSocket;
|
---|
| 130 | _outSocket = 0;
|
---|
[1208] | 131 | // emit(newMessage("t_bnscaster::open socket deleted"));
|
---|
| 132 | emit(newMessage("Broadcaster: Connection broken")); // weber
|
---|
[1060] | 133 | }
|
---|
| 134 | else {
|
---|
[1208] | 135 | // emit(newMessage("t_bnscaster::open socket OK"));
|
---|
| 136 | emit(newMessage("Broadcaster: Connection opened")); // weber
|
---|
[1060] | 137 | _outSocketOpenTrial = 0;
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | // Write buffer
|
---|
| 142 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 143 | void t_bnscaster::write(char* buffer, unsigned len) {
|
---|
| 144 | if (_outSocket) {
|
---|
| 145 | _outSocket->write(buffer, len);
|
---|
| 146 | _outSocket->flush();
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
[1065] | 149 |
|
---|
| 150 | // Print Ascii Output
|
---|
| 151 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 152 | void t_bnscaster::printAscii(const QString& line) {
|
---|
| 153 | if (_outStream) {
|
---|
| 154 | *_outStream << line;
|
---|
| 155 | _outStream->flush();
|
---|
| 156 | }
|
---|
| 157 | }
|
---|