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