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