| 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 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 24 | t_bnscaster::t_bnscaster(const QString& mountpoint) {
|
|---|
| 25 | _mountpoint = mountpoint;
|
|---|
| 26 | _outSocket = 0;
|
|---|
| 27 | _outSocketOpenTrial = 0;
|
|---|
| 28 |
|
|---|
| 29 | QSettings settings;
|
|---|
| 30 |
|
|---|
| 31 | QIODevice::OpenMode oMode;
|
|---|
| 32 | if (Qt::CheckState(settings.value("fileAppend").toInt()) == Qt::Checked) {
|
|---|
| 33 | oMode = QIODevice::WriteOnly | QIODevice::Unbuffered | QIODevice::Append;
|
|---|
| 34 | }
|
|---|
| 35 | else {
|
|---|
| 36 | oMode = QIODevice::WriteOnly | QIODevice::Unbuffered;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | QString outFileName = settings.value("outFile").toString();
|
|---|
| 40 | if (outFileName.isEmpty()) {
|
|---|
| 41 | _outFile = 0;
|
|---|
| 42 | _outStream = 0;
|
|---|
| 43 | }
|
|---|
| 44 | else {
|
|---|
| 45 | _outFile = new QFile(outFileName);
|
|---|
| 46 | if (_outFile->open(oMode)) {
|
|---|
| 47 | _outStream = new QTextStream(_outFile);
|
|---|
| 48 | }
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | // Reference frame
|
|---|
| 52 | // ---------------
|
|---|
| 53 | _crdTrafo = false;
|
|---|
| 54 | if (settings.value("refSys").toString() == "ETRS89") {
|
|---|
| 55 | _crdTrafo = true;
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | // Destructor
|
|---|
| 60 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 61 | t_bnscaster::~t_bnscaster() {
|
|---|
| 62 | delete _outSocket;
|
|---|
| 63 | delete _outStream;
|
|---|
| 64 | delete _outFile;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | // Start the Communication with NTRIP Caster
|
|---|
| 68 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 69 | void t_bnscaster::open() {
|
|---|
| 70 |
|
|---|
| 71 | if (_outSocket != 0 &&
|
|---|
| 72 | _outSocket->state() == QAbstractSocket::ConnectedState) {
|
|---|
| 73 | return;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | delete _outSocket; _outSocket = 0;
|
|---|
| 77 |
|
|---|
| 78 | double minDt = exp2(_outSocketOpenTrial);
|
|---|
| 79 | if (++_outSocketOpenTrial > 8) {
|
|---|
| 80 | _outSocketOpenTrial = 8;
|
|---|
| 81 | }
|
|---|
| 82 | if (_outSocketOpenTime.isValid() &&
|
|---|
| 83 | _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
|
|---|
| 84 | return;
|
|---|
| 85 | }
|
|---|
| 86 | else {
|
|---|
| 87 | _outSocketOpenTime = QDateTime::currentDateTime();
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | QSettings settings;
|
|---|
| 91 | _outSocket = new QTcpSocket();
|
|---|
| 92 | _outSocket->connectToHost(settings.value("outHost").toString(),
|
|---|
| 93 | settings.value("outPort").toInt());
|
|---|
| 94 |
|
|---|
| 95 | const int timeOut = 100; // 0.1 seconds
|
|---|
| 96 | if (!_outSocket->waitForConnected(timeOut)) {
|
|---|
| 97 | delete _outSocket;
|
|---|
| 98 | _outSocket = 0;
|
|---|
| 99 | emit(error("bns::openCaster Connect Timeout"));
|
|---|
| 100 | return;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | QString password = settings.value("password").toString();
|
|---|
| 104 |
|
|---|
| 105 | QByteArray msg = "SOURCE " + password.toAscii() + " /" +
|
|---|
| 106 | _mountpoint.toAscii() + "\r\n" +
|
|---|
| 107 | "Source-Agent: NTRIP BNS/1.0\r\n\r\n";
|
|---|
| 108 |
|
|---|
| 109 | _outSocket->write(msg);
|
|---|
| 110 | _outSocket->waitForBytesWritten();
|
|---|
| 111 |
|
|---|
| 112 | _outSocket->waitForReadyRead();
|
|---|
| 113 | QByteArray ans = _outSocket->readLine();
|
|---|
| 114 |
|
|---|
| 115 | if (ans.indexOf("OK") == -1) {
|
|---|
| 116 | delete _outSocket;
|
|---|
| 117 | _outSocket = 0;
|
|---|
| 118 | emit(newMessage("bns::openCaster socket deleted"));
|
|---|
| 119 | }
|
|---|
| 120 | else {
|
|---|
| 121 | emit(newMessage("bns::openCaster socket OK"));
|
|---|
| 122 | _outSocketOpenTrial = 0;
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | // Write buffer
|
|---|
| 127 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 128 | void t_bnscaster::write(char* buffer, unsigned len) {
|
|---|
| 129 | if (_outSocket) {
|
|---|
| 130 | _outSocket->write(buffer, len);
|
|---|
| 131 | _outSocket->flush();
|
|---|
| 132 | }
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | // Print Ascii Output
|
|---|
| 136 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 137 | void t_bnscaster::printAscii(const QString& line) {
|
|---|
| 138 | if (_outStream) {
|
|---|
| 139 | *_outStream << line;
|
|---|
| 140 | _outStream->flush();
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|