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