[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 |
|
---|
[1123] | 72 | if (_mountpoint.isEmpty()) {
|
---|
| 73 | return;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[1060] | 76 | if (_outSocket != 0 &&
|
---|
| 77 | _outSocket->state() == QAbstractSocket::ConnectedState) {
|
---|
| 78 | return;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | delete _outSocket; _outSocket = 0;
|
---|
| 82 |
|
---|
| 83 | double minDt = exp2(_outSocketOpenTrial);
|
---|
| 84 | if (++_outSocketOpenTrial > 8) {
|
---|
| 85 | _outSocketOpenTrial = 8;
|
---|
| 86 | }
|
---|
| 87 | if (_outSocketOpenTime.isValid() &&
|
---|
| 88 | _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
|
---|
| 89 | return;
|
---|
| 90 | }
|
---|
| 91 | else {
|
---|
| 92 | _outSocketOpenTime = QDateTime::currentDateTime();
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | QSettings settings;
|
---|
| 96 | _outSocket = new QTcpSocket();
|
---|
| 97 | _outSocket->connectToHost(settings.value("outHost").toString(),
|
---|
| 98 | settings.value("outPort").toInt());
|
---|
| 99 |
|
---|
[1124] | 100 | const int timeOut = 5000; // 5 seconds
|
---|
[1060] | 101 | if (!_outSocket->waitForConnected(timeOut)) {
|
---|
| 102 | delete _outSocket;
|
---|
| 103 | _outSocket = 0;
|
---|
[1124] | 104 | emit(error("t_bnscaster::open Connect Timeout"));
|
---|
[1060] | 105 | return;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | QString password = settings.value("password").toString();
|
---|
| 109 |
|
---|
| 110 | QByteArray msg = "SOURCE " + password.toAscii() + " /" +
|
---|
| 111 | _mountpoint.toAscii() + "\r\n" +
|
---|
| 112 | "Source-Agent: NTRIP BNS/1.0\r\n\r\n";
|
---|
| 113 |
|
---|
| 114 | _outSocket->write(msg);
|
---|
| 115 | _outSocket->waitForBytesWritten();
|
---|
| 116 |
|
---|
| 117 | _outSocket->waitForReadyRead();
|
---|
| 118 | QByteArray ans = _outSocket->readLine();
|
---|
| 119 |
|
---|
| 120 | if (ans.indexOf("OK") == -1) {
|
---|
| 121 | delete _outSocket;
|
---|
| 122 | _outSocket = 0;
|
---|
[1124] | 123 | emit(newMessage("t_bnscaster::open socket deleted"));
|
---|
[1060] | 124 | }
|
---|
| 125 | else {
|
---|
[1124] | 126 | emit(newMessage("t_bnscaster::open socket OK"));
|
---|
[1060] | 127 | _outSocketOpenTrial = 0;
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | // Write buffer
|
---|
| 132 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 133 | void t_bnscaster::write(char* buffer, unsigned len) {
|
---|
| 134 | if (_outSocket) {
|
---|
| 135 | _outSocket->write(buffer, len);
|
---|
| 136 | _outSocket->flush();
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
[1065] | 139 |
|
---|
| 140 | // Print Ascii Output
|
---|
| 141 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 142 | void t_bnscaster::printAscii(const QString& line) {
|
---|
| 143 | if (_outStream) {
|
---|
| 144 | *_outStream << line;
|
---|
| 145 | _outStream->flush();
|
---|
| 146 | }
|
---|
| 147 | }
|
---|