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