| 1 |
|
|---|
| 2 | /* -------------------------------------------------------------------------
|
|---|
| 3 | * BKG NTRIP Client
|
|---|
| 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"
|
|---|
| 23 | #include "bnctabledlg.h"
|
|---|
| 24 | #include "bncapp.h"
|
|---|
| 25 |
|
|---|
| 26 | #include "RTCM/RTCM2Decoder.h"
|
|---|
| 27 | #include "RTCM3/rtcm3.h"
|
|---|
| 28 | #include "RTIGS/rtigs.h"
|
|---|
| 29 |
|
|---|
| 30 | using namespace std;
|
|---|
| 31 |
|
|---|
| 32 | // Constructor
|
|---|
| 33 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 34 | bncGetThread::bncGetThread(const QUrl& mountPoint, const QByteArray& format) {
|
|---|
| 35 | _decoder = 0;
|
|---|
| 36 | _mountPoint = mountPoint;
|
|---|
| 37 | _staID = mountPoint.path().mid(1).toAscii();
|
|---|
| 38 | _format = format;
|
|---|
| 39 | _socket = 0;
|
|---|
| 40 | _timeOut = 20*1000; // 20 seconds
|
|---|
| 41 | _nextSleep = 1; // 1 second
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | // Destructor
|
|---|
| 45 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 46 | bncGetThread::~bncGetThread() {
|
|---|
| 47 | delete _socket;
|
|---|
| 48 | delete _decoder;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | // Connect to Caster, send the Request (static)
|
|---|
| 52 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 53 | QTcpSocket* bncGetThread::request(const QUrl& mountPoint, int timeOut,
|
|---|
| 54 | QString& msg) {
|
|---|
| 55 |
|
|---|
| 56 | // Connect the Socket
|
|---|
| 57 | // ------------------
|
|---|
| 58 | QSettings settings;
|
|---|
| 59 | QString proxyHost = settings.value("proxyHost").toString();
|
|---|
| 60 | int proxyPort = settings.value("proxyPort").toInt();
|
|---|
| 61 |
|
|---|
| 62 | QTcpSocket* socket = new QTcpSocket();
|
|---|
| 63 | if ( proxyHost.isEmpty() ) {
|
|---|
| 64 | socket->connectToHost(mountPoint.host(), mountPoint.port());
|
|---|
| 65 | }
|
|---|
| 66 | else {
|
|---|
| 67 | socket->connectToHost(proxyHost, proxyPort);
|
|---|
| 68 | }
|
|---|
| 69 | if (!socket->waitForConnected(timeOut)) {
|
|---|
| 70 | msg += "Connect timeout\n";
|
|---|
| 71 | delete socket;
|
|---|
| 72 | return 0;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | // Send Request
|
|---|
| 76 | // ------------
|
|---|
| 77 | QByteArray userAndPwd = mountPoint.userName().toAscii() + ":" +
|
|---|
| 78 | mountPoint.password().toAscii();
|
|---|
| 79 |
|
|---|
| 80 | QUrl hlp;
|
|---|
| 81 | hlp.setScheme("http");
|
|---|
| 82 | hlp.setHost(mountPoint.host());
|
|---|
| 83 | hlp.setPort(mountPoint.port());
|
|---|
| 84 | hlp.setPath(mountPoint.path());
|
|---|
| 85 |
|
|---|
| 86 | QByteArray reqStr;
|
|---|
| 87 | if ( proxyHost.isEmpty() ) {
|
|---|
| 88 | if (hlp.path().indexOf("/") != 0) hlp.setPath("/");
|
|---|
| 89 | reqStr = "GET " + hlp.path().toAscii() +
|
|---|
| 90 | " HTTP/1.0\r\n"
|
|---|
| 91 | "User-Agent: NTRIP BNC 1.0b\r\n"
|
|---|
| 92 | "Authorization: Basic " +
|
|---|
| 93 | userAndPwd.toBase64() + "\r\n\r\n";
|
|---|
| 94 | } else {
|
|---|
| 95 | reqStr = "GET " + hlp.toEncoded() +
|
|---|
| 96 | " HTTP/1.0\r\n"
|
|---|
| 97 | "User-Agent: NTRIP BNC 1.0b\r\n"
|
|---|
| 98 | "Authorization: Basic " +
|
|---|
| 99 | userAndPwd.toBase64() + "\r\n\r\n";
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | msg += reqStr;
|
|---|
| 103 |
|
|---|
| 104 | socket->write(reqStr, reqStr.length());
|
|---|
| 105 |
|
|---|
| 106 | if (!socket->waitForBytesWritten(timeOut)) {
|
|---|
| 107 | msg += "Write timeout\n";
|
|---|
| 108 | delete socket;
|
|---|
| 109 | return 0;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | return socket;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | // Init Run
|
|---|
| 116 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 117 | t_irc bncGetThread::initRun() {
|
|---|
| 118 |
|
|---|
| 119 | // Send the Request
|
|---|
| 120 | // ----------------
|
|---|
| 121 | QString msg;
|
|---|
| 122 |
|
|---|
| 123 | _socket = bncGetThread::request(_mountPoint, _timeOut, msg);
|
|---|
| 124 |
|
|---|
| 125 | //// emit(newMessage(msg.toAscii()));
|
|---|
| 126 |
|
|---|
| 127 | if (!_socket) {
|
|---|
| 128 | return failure;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | // Read Caster Response
|
|---|
| 132 | // --------------------
|
|---|
| 133 | _socket->waitForReadyRead(_timeOut);
|
|---|
| 134 | if (_socket->canReadLine()) {
|
|---|
| 135 | QString line = _socket->readLine();
|
|---|
| 136 | if (line.indexOf("Unauthorized") != -1) {
|
|---|
| 137 | QStringList table;
|
|---|
| 138 | bncTableDlg::getFullTable(_mountPoint.host(), _mountPoint.port(), table);
|
|---|
| 139 | QString net;
|
|---|
| 140 | QStringListIterator it(table);
|
|---|
| 141 | while (it.hasNext()) {
|
|---|
| 142 | QString line = it.next();
|
|---|
| 143 | if (line.indexOf("STR") == 0) {
|
|---|
| 144 | QStringList tags = line.split(";");
|
|---|
| 145 | if (tags.at(1) == _staID) {
|
|---|
| 146 | net = tags.at(7);
|
|---|
| 147 | break;
|
|---|
| 148 | }
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | QString reg;
|
|---|
| 153 | it.toFront();
|
|---|
| 154 | while (it.hasNext()) {
|
|---|
| 155 | QString line = it.next();
|
|---|
| 156 | if (line.indexOf("NET") == 0) {
|
|---|
| 157 | QStringList tags = line.split(";");
|
|---|
| 158 | if (tags.at(1) == net) {
|
|---|
| 159 | reg = tags.at(7);
|
|---|
| 160 | break;
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 | }
|
|---|
| 164 | emit(newMessage((_staID + ": Caster Response: " + line +
|
|---|
| 165 | " Adjust User-ID and Password Register, see"
|
|---|
| 166 | "\n " + reg).toAscii()));
|
|---|
| 167 | return fatal;
|
|---|
| 168 | }
|
|---|
| 169 | if (line.indexOf("ICY 200 OK") != 0) {
|
|---|
| 170 | emit(newMessage((_staID + ": Wrong Caster Response:\n" + line).toAscii()));
|
|---|
| 171 | return failure;
|
|---|
| 172 | }
|
|---|
| 173 | }
|
|---|
| 174 | else {
|
|---|
| 175 | emit(newMessage(_staID + ": Response Timeout"));
|
|---|
| 176 | return failure;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | // Instantiate the filter
|
|---|
| 180 | // ----------------------
|
|---|
| 181 | if (!_decoder) {
|
|---|
| 182 | if (_format.indexOf("RTCM_2") != -1) {
|
|---|
| 183 | emit(newMessage("Get Data: " + _staID + " in RTCM 2.x format"));
|
|---|
| 184 | _decoder = new RTCM2Decoder();
|
|---|
| 185 | }
|
|---|
| 186 | else if (_format.indexOf("RTCM_3") != -1) {
|
|---|
| 187 | emit(newMessage("Get Data: " + _staID + " in RTCM 3.0 format"));
|
|---|
| 188 | _decoder = new rtcm3();
|
|---|
| 189 | }
|
|---|
| 190 | else if (_format.indexOf("RTIGS") != -1) {
|
|---|
| 191 | emit(newMessage("Get Data: " + _staID + " in RTIGS format"));
|
|---|
| 192 | _decoder = new rtigs();
|
|---|
| 193 | }
|
|---|
| 194 | else {
|
|---|
| 195 | emit(newMessage(_staID + ": Unknown data format " + _format));
|
|---|
| 196 | return fatal;
|
|---|
| 197 | }
|
|---|
| 198 | }
|
|---|
| 199 | return success;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | // Run
|
|---|
| 203 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 204 | void bncGetThread::run() {
|
|---|
| 205 |
|
|---|
| 206 | t_irc irc = initRun();
|
|---|
| 207 |
|
|---|
| 208 | if (irc == fatal) {
|
|---|
| 209 | QThread::exit(1);
|
|---|
| 210 | return;
|
|---|
| 211 | }
|
|---|
| 212 | else if (irc != success) {
|
|---|
| 213 | emit(newMessage(_staID + ": initRun failed, reconnecting"));
|
|---|
| 214 | tryReconnect();
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | // Read Incoming Data
|
|---|
| 218 | // ------------------
|
|---|
| 219 | while (true) {
|
|---|
| 220 | try {
|
|---|
| 221 | if (_socket->state() != QAbstractSocket::ConnectedState) {
|
|---|
| 222 | emit(newMessage(_staID + ": Socket not connected, reconnecting"));
|
|---|
| 223 | tryReconnect();
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 | _socket->waitForReadyRead(_timeOut);
|
|---|
| 228 | qint64 nBytes = _socket->bytesAvailable();
|
|---|
| 229 | if (nBytes > 0) {
|
|---|
| 230 | char* data = new char[nBytes];
|
|---|
| 231 | _socket->read(data, nBytes);
|
|---|
| 232 | _decoder->Decode(data, nBytes);
|
|---|
| 233 | delete data;
|
|---|
| 234 | for (list<Observation*>::iterator it = _decoder->_obsList.begin();
|
|---|
| 235 | it != _decoder->_obsList.end(); it++) {
|
|---|
| 236 | emit newObs(_staID, *it);
|
|---|
| 237 | _global_caster->newObs(_staID, *it);
|
|---|
| 238 | }
|
|---|
| 239 | _decoder->_obsList.clear();
|
|---|
| 240 | }
|
|---|
| 241 | else {
|
|---|
| 242 | emit(newMessage(_staID + ": Data Timeout, reconnecting"));
|
|---|
| 243 | tryReconnect();
|
|---|
| 244 | }
|
|---|
| 245 | }
|
|---|
| 246 | catch (const char* msg) {
|
|---|
| 247 | emit(newMessage(_staID + msg));
|
|---|
| 248 | tryReconnect();
|
|---|
| 249 | }
|
|---|
| 250 | }
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | // Exit
|
|---|
| 254 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 255 | void bncGetThread::exit(int exitCode) {
|
|---|
| 256 | if (exitCode!= 0) {
|
|---|
| 257 | emit error(_staID);
|
|---|
| 258 | }
|
|---|
| 259 | QThread::exit(exitCode);
|
|---|
| 260 | terminate();
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | // Try Re-Connect
|
|---|
| 264 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 265 | void bncGetThread::tryReconnect() {
|
|---|
| 266 | while (1) {
|
|---|
| 267 | delete _socket; _socket = 0;
|
|---|
| 268 | sleep(_nextSleep);
|
|---|
| 269 | if ( initRun() == success ) {
|
|---|
| 270 | break;
|
|---|
| 271 | }
|
|---|
| 272 | else {
|
|---|
| 273 | _nextSleep *= 2;
|
|---|
| 274 | if (_nextSleep > 128) {
|
|---|
| 275 | _nextSleep = 128;
|
|---|
| 276 | }
|
|---|
| 277 | }
|
|---|
| 278 | }
|
|---|
| 279 | _nextSleep = 1;
|
|---|
| 280 | }
|
|---|