[35] | 1 |
|
---|
| 2 | /* -------------------------------------------------------------------------
|
---|
[93] | 3 | * BKG NTRIP Client
|
---|
[35] | 4 | * -------------------------------------------------------------------------
|
---|
| 5 | *
|
---|
| 6 | * Class: bncGetThread
|
---|
| 7 | *
|
---|
| 8 | * Purpose: Thread that retrieves data from NTRIP caster
|
---|
| 9 | *
|
---|
| 10 | * Author: L. Mervart
|
---|
| 11 | *
|
---|
| 12 | * Created: 24-Dec-2005
|
---|
| 13 | *
|
---|
| 14 | * Changes:
|
---|
| 15 | *
|
---|
| 16 | * -----------------------------------------------------------------------*/
|
---|
| 17 |
|
---|
| 18 | #include <QFile>
|
---|
| 19 | #include <QTextStream>
|
---|
| 20 | #include <QtNetwork>
|
---|
| 21 |
|
---|
| 22 | #include "bncgetthread.h"
|
---|
[65] | 23 |
|
---|
[35] | 24 | #include "RTCM/RTCM.h"
|
---|
[65] | 25 | #include "RTCM3/rtcm3.h"
|
---|
[61] | 26 | #include "RTIGS/rtigs.h"
|
---|
[35] | 27 |
|
---|
| 28 | using namespace std;
|
---|
| 29 |
|
---|
| 30 | // Constructor
|
---|
| 31 | ////////////////////////////////////////////////////////////////////////////
|
---|
[88] | 32 | bncGetThread::bncGetThread(const QUrl& mountPoint, const QByteArray& format) {
|
---|
[136] | 33 | _decoder = 0;
|
---|
[35] | 34 | _mountPoint = mountPoint;
|
---|
[89] | 35 | _staID = mountPoint.path().mid(1).toAscii();
|
---|
[59] | 36 | _format = format;
|
---|
[35] | 37 | _socket = 0;
|
---|
[136] | 38 | _timeOut = 10*1000; // 10 seconds
|
---|
[138] | 39 | _nextSleep = 1; // 1 second
|
---|
[35] | 40 | }
|
---|
| 41 |
|
---|
| 42 | // Destructor
|
---|
| 43 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 44 | bncGetThread::~bncGetThread() {
|
---|
| 45 | delete _socket;
|
---|
[136] | 46 | delete _decoder;
|
---|
[35] | 47 | }
|
---|
| 48 |
|
---|
| 49 | // Connect to Caster, send the Request (static)
|
---|
| 50 | ////////////////////////////////////////////////////////////////////////////
|
---|
[136] | 51 | QTcpSocket* bncGetThread::request(const QUrl& mountPoint, int timeOut,
|
---|
| 52 | QString& msg) {
|
---|
[35] | 53 |
|
---|
[88] | 54 | // Connect the Socket
|
---|
| 55 | // ------------------
|
---|
| 56 | QSettings settings;
|
---|
| 57 | QString proxyHost = settings.value("proxyHost").toString();
|
---|
| 58 | int proxyPort = settings.value("proxyPort").toInt();
|
---|
| 59 |
|
---|
[35] | 60 | QTcpSocket* socket = new QTcpSocket();
|
---|
| 61 | if ( proxyHost.isEmpty() ) {
|
---|
[88] | 62 | socket->connectToHost(mountPoint.host(), mountPoint.port());
|
---|
[35] | 63 | }
|
---|
| 64 | else {
|
---|
| 65 | socket->connectToHost(proxyHost, proxyPort);
|
---|
| 66 | }
|
---|
| 67 | if (!socket->waitForConnected(timeOut)) {
|
---|
[82] | 68 | msg += "Connect timeout\n";
|
---|
[35] | 69 | delete socket;
|
---|
| 70 | return 0;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | // Send Request
|
---|
| 74 | // ------------
|
---|
[88] | 75 | QByteArray userAndPwd = mountPoint.userName().toAscii() + ":" +
|
---|
| 76 | mountPoint.password().toAscii();
|
---|
[92] | 77 |
|
---|
| 78 | QUrl hlp;
|
---|
| 79 | hlp.setScheme("http");
|
---|
| 80 | hlp.setHost(mountPoint.host());
|
---|
| 81 | hlp.setPort(mountPoint.port());
|
---|
| 82 | hlp.setPath(mountPoint.path());
|
---|
| 83 |
|
---|
| 84 | QByteArray reqStr = "GET " + hlp.toEncoded() +
|
---|
[88] | 85 | " HTTP/1.0\r\n"
|
---|
| 86 | "User-Agent: NTRIP BNC 1.0\r\n"
|
---|
| 87 | "Authorization: Basic " +
|
---|
| 88 | userAndPwd.toBase64() + "\r\n\r\n";
|
---|
[35] | 89 |
|
---|
[82] | 90 | msg += reqStr;
|
---|
[35] | 91 |
|
---|
| 92 | socket->write(reqStr, reqStr.length());
|
---|
| 93 |
|
---|
| 94 | if (!socket->waitForBytesWritten(timeOut)) {
|
---|
[82] | 95 | msg += "Write timeout\n";
|
---|
[35] | 96 | delete socket;
|
---|
| 97 | return 0;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | return socket;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[136] | 103 | // Init Run
|
---|
[35] | 104 | ////////////////////////////////////////////////////////////////////////////
|
---|
[138] | 105 | t_irc bncGetThread::initRun() {
|
---|
[35] | 106 |
|
---|
| 107 | // Send the Request
|
---|
| 108 | // ----------------
|
---|
[82] | 109 | QString msg;
|
---|
[88] | 110 |
|
---|
[136] | 111 | _socket = bncGetThread::request(_mountPoint, _timeOut, msg);
|
---|
[88] | 112 |
|
---|
[82] | 113 | emit(newMessage(msg.toAscii()));
|
---|
| 114 |
|
---|
[35] | 115 | if (!_socket) {
|
---|
[138] | 116 | return failure;
|
---|
[35] | 117 | }
|
---|
| 118 |
|
---|
| 119 | // Read Caster Response
|
---|
| 120 | // --------------------
|
---|
[136] | 121 | _socket->waitForReadyRead(_timeOut);
|
---|
[35] | 122 | if (_socket->canReadLine()) {
|
---|
| 123 | QString line = _socket->readLine();
|
---|
[146] | 124 | if (line.indexOf("Unauthorized") != -1) {
|
---|
| 125 | emit(newMessage(("Caster Response:\n" + line).toAscii()));
|
---|
| 126 | exit(1);
|
---|
[147] | 127 | return failure;
|
---|
[146] | 128 | }
|
---|
[35] | 129 | if (line.indexOf("ICY 200 OK") != 0) {
|
---|
[82] | 130 | emit(newMessage(("Wrong Caster Response:\n" + line).toAscii()));
|
---|
[144] | 131 | return failure;
|
---|
[35] | 132 | }
|
---|
| 133 | }
|
---|
| 134 | else {
|
---|
[82] | 135 | emit(newMessage("Response Timeout"));
|
---|
[138] | 136 | return failure;
|
---|
[35] | 137 | }
|
---|
| 138 |
|
---|
| 139 | // Instantiate the filter
|
---|
| 140 | // ----------------------
|
---|
[136] | 141 | if (!_decoder) {
|
---|
| 142 | if (_format.indexOf("RTCM_2") != -1) {
|
---|
| 143 | emit(newMessage("Get Data: " + _staID + " in RTCM 2.x format"));
|
---|
| 144 | _decoder = new RTCM('A',true);
|
---|
| 145 | }
|
---|
| 146 | else if (_format.indexOf("RTCM_3") != -1) {
|
---|
| 147 | emit(newMessage("Get Data: " + _staID + " in RTCM 3.0 format"));
|
---|
| 148 | _decoder = new rtcm3();
|
---|
| 149 | }
|
---|
| 150 | else if (_format.indexOf("RTIGS") != -1) {
|
---|
| 151 | emit(newMessage("Get Data: " + _staID + " in RTIGS format"));
|
---|
| 152 | _decoder = new rtigs();
|
---|
| 153 | }
|
---|
| 154 | else {
|
---|
| 155 | emit(newMessage(_staID + " Unknown data format " + _format));
|
---|
[138] | 156 | exit(1);
|
---|
[136] | 157 | }
|
---|
[60] | 158 | }
|
---|
[138] | 159 | return success;
|
---|
[136] | 160 | }
|
---|
[59] | 161 |
|
---|
[136] | 162 | // Run
|
---|
| 163 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 164 | void bncGetThread::run() {
|
---|
| 165 |
|
---|
[138] | 166 | if ( initRun() != success ) {
|
---|
| 167 | emit(newMessage("initRun failed, reconnecting"));
|
---|
| 168 | tryReconnect();
|
---|
| 169 | }
|
---|
[136] | 170 |
|
---|
[35] | 171 | // Read Incoming Data
|
---|
| 172 | // ------------------
|
---|
| 173 | while (true) {
|
---|
[145] | 174 |
|
---|
| 175 | if (_socket->state() != QAbstractSocket::ConnectedState) {
|
---|
| 176 | emit(newMessage("Socket not connected, reconnecting"));
|
---|
| 177 | tryReconnect();
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[136] | 180 | _socket->waitForReadyRead(_timeOut);
|
---|
[35] | 181 | qint64 nBytes = _socket->bytesAvailable();
|
---|
| 182 | if (nBytes > 0) {
|
---|
| 183 | char* data = new char[nBytes];
|
---|
| 184 | _socket->read(data, nBytes);
|
---|
[136] | 185 | _decoder->Decode(data, nBytes);
|
---|
[35] | 186 | delete data;
|
---|
[136] | 187 | for (list<Observation*>::iterator it = _decoder->m_lObsList.begin();
|
---|
| 188 | it != _decoder->m_lObsList.end(); it++) {
|
---|
[88] | 189 | emit newObs(_staID, *it);
|
---|
[35] | 190 | }
|
---|
[136] | 191 | _decoder->m_lObsList.clear();
|
---|
[35] | 192 | }
|
---|
| 193 | else {
|
---|
[136] | 194 | emit(newMessage("Data Timeout, reconnecting"));
|
---|
| 195 | tryReconnect();
|
---|
[35] | 196 | }
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | // Exit
|
---|
| 201 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 202 | void bncGetThread::exit(int exitCode) {
|
---|
| 203 | if (exitCode!= 0) {
|
---|
[88] | 204 | emit error(_staID);
|
---|
[35] | 205 | }
|
---|
| 206 | QThread::exit(exitCode);
|
---|
[148] | 207 | terminate();
|
---|
[35] | 208 | }
|
---|
[82] | 209 |
|
---|
[136] | 210 | // Try Re-Connect
|
---|
| 211 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 212 | void bncGetThread::tryReconnect() {
|
---|
[138] | 213 | while (1) {
|
---|
| 214 | delete _socket; _socket = 0;
|
---|
| 215 | sleep(_nextSleep);
|
---|
| 216 | if ( initRun() == success ) {
|
---|
| 217 | break;
|
---|
| 218 | }
|
---|
| 219 | else {
|
---|
| 220 | _nextSleep *= 2;
|
---|
[152] | 221 | if (_nextSleep > 60) {
|
---|
| 222 | _nextSleep = 60;
|
---|
| 223 | }
|
---|
[138] | 224 | }
|
---|
| 225 | }
|
---|
| 226 | _nextSleep = 1;
|
---|
[136] | 227 | }
|
---|