| 1 |
|
|---|
| 2 | /* -------------------------------------------------------------------------
|
|---|
| 3 | * Bernese 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 |
|
|---|
| 24 | #include "RTCM/RTCM.h"
|
|---|
| 25 | #include "RTCM3/rtcm3.h"
|
|---|
| 26 | #include "RTIGS/rtigs.h"
|
|---|
| 27 |
|
|---|
| 28 | using namespace std;
|
|---|
| 29 |
|
|---|
| 30 | const int timeOut = 30*1000;
|
|---|
| 31 |
|
|---|
| 32 | // Constructor
|
|---|
| 33 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 34 | bncGetThread::bncGetThread(const QString& host, int port,
|
|---|
| 35 | const QString& proxyHost, int proxyPort,
|
|---|
| 36 | const QByteArray& mountPoint,
|
|---|
| 37 | const QByteArray& user,
|
|---|
| 38 | const QByteArray& password,
|
|---|
| 39 | const QByteArray& format) {
|
|---|
| 40 | _host = host;
|
|---|
| 41 | _port = port;
|
|---|
| 42 | _proxyHost = proxyHost;
|
|---|
| 43 | _proxyPort = proxyPort;
|
|---|
| 44 | _mountPoint = mountPoint;
|
|---|
| 45 | _user = user;
|
|---|
| 46 | _password = password;
|
|---|
| 47 | _format = format;
|
|---|
| 48 | _socket = 0;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | // Destructor
|
|---|
| 52 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 53 | bncGetThread::~bncGetThread() {
|
|---|
| 54 | delete _socket;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | // Connect to Caster, send the Request (static)
|
|---|
| 58 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 59 | QTcpSocket* bncGetThread::request(const QString& host, int port,
|
|---|
| 60 | const QString& proxyHost, int proxyPort,
|
|---|
| 61 | const QByteArray& mountPoint,
|
|---|
| 62 | const QByteArray& user,
|
|---|
| 63 | const QByteArray& password) {
|
|---|
| 64 |
|
|---|
| 65 | QTcpSocket* socket = new QTcpSocket();
|
|---|
| 66 |
|
|---|
| 67 | QByteArray l_mountPoint = mountPoint;
|
|---|
| 68 |
|
|---|
| 69 | if ( proxyHost.isEmpty() ) {
|
|---|
| 70 | socket->connectToHost(host, port);
|
|---|
| 71 | }
|
|---|
| 72 | else {
|
|---|
| 73 | socket->connectToHost(proxyHost, proxyPort);
|
|---|
| 74 | if (!proxyHost.isEmpty()) {
|
|---|
| 75 | QUrl proxyUrl;
|
|---|
| 76 | proxyUrl.setScheme("http");
|
|---|
| 77 | proxyUrl.setHost(host);
|
|---|
| 78 | proxyUrl.setPort(port);
|
|---|
| 79 | l_mountPoint = proxyUrl.resolved(QUrl(mountPoint)).toEncoded();
|
|---|
| 80 | }
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | if (!socket->waitForConnected(timeOut)) {
|
|---|
| 84 | qWarning("Connect timeout");
|
|---|
| 85 | delete socket;
|
|---|
| 86 | return 0;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | // Send Request
|
|---|
| 90 | // ------------
|
|---|
| 91 | QByteArray userAndPwd = user + ":" + password;
|
|---|
| 92 | QByteArray reqStr = "GET " + l_mountPoint + " HTTP/1.0\r\n"
|
|---|
| 93 | "User-Agent: NTRIP BNC 1.0\r\n"
|
|---|
| 94 | "Authorization: Basic " + userAndPwd.toBase64() +
|
|---|
| 95 | "\r\n\r\n";
|
|---|
| 96 |
|
|---|
| 97 | qWarning(reqStr);
|
|---|
| 98 |
|
|---|
| 99 | socket->write(reqStr, reqStr.length());
|
|---|
| 100 |
|
|---|
| 101 | if (!socket->waitForBytesWritten(timeOut)) {
|
|---|
| 102 | qWarning("Write timeout");
|
|---|
| 103 | delete socket;
|
|---|
| 104 | return 0;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | return socket;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | // Run
|
|---|
| 111 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 112 | void bncGetThread::run() {
|
|---|
| 113 |
|
|---|
| 114 | // Send the Request
|
|---|
| 115 | // ----------------
|
|---|
| 116 | _socket = bncGetThread::request(_host, _port, _proxyHost, _proxyPort,
|
|---|
| 117 | _mountPoint, _user, _password);
|
|---|
| 118 | if (!_socket) {
|
|---|
| 119 | return exit(1);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | // Read Caster Response
|
|---|
| 123 | // --------------------
|
|---|
| 124 | _socket->waitForReadyRead(timeOut);
|
|---|
| 125 | if (_socket->canReadLine()) {
|
|---|
| 126 | QString line = _socket->readLine();
|
|---|
| 127 | if (line.indexOf("ICY 200 OK") != 0) {
|
|---|
| 128 | qWarning(("Wrong Caster Response:\n" + line).toAscii());
|
|---|
| 129 | return exit(1);
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 | else {
|
|---|
| 133 | qWarning("Response Timeout");
|
|---|
| 134 | return exit(1);
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | // Instantiate the filter
|
|---|
| 138 | // ----------------------
|
|---|
| 139 | GPSDecoder* decoder;
|
|---|
| 140 |
|
|---|
| 141 | if (_format.indexOf("RTCM_2") != -1) {
|
|---|
| 142 | qWarning("Get Data: " + _mountPoint + " in RTCM 2.x format");
|
|---|
| 143 | decoder = new RTCM('A',true);
|
|---|
| 144 | }
|
|---|
| 145 | else if (_format.indexOf("RTCM_3") != -1) {
|
|---|
| 146 | qWarning("Get Data: " + _mountPoint + " in RTCM 3.0 format");
|
|---|
| 147 | decoder = new rtcm3();
|
|---|
| 148 | }
|
|---|
| 149 | else if (_format.indexOf("RTIGS") != -1) {
|
|---|
| 150 | qWarning("Get Data: " + _mountPoint + " in RTIGS format");
|
|---|
| 151 | decoder = new rtigs();
|
|---|
| 152 | }
|
|---|
| 153 | else {
|
|---|
| 154 | qWarning(_mountPoint + " Unknown data format " + _format);
|
|---|
| 155 | return exit(1);
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | // Read Incoming Data
|
|---|
| 159 | // ------------------
|
|---|
| 160 | while (true) {
|
|---|
| 161 | _socket->waitForReadyRead(timeOut);
|
|---|
| 162 | qint64 nBytes = _socket->bytesAvailable();
|
|---|
| 163 | if (nBytes > 0) {
|
|---|
| 164 | char* data = new char[nBytes];
|
|---|
| 165 | _socket->read(data, nBytes);
|
|---|
| 166 | decoder->Decode(data, nBytes);
|
|---|
| 167 | delete data;
|
|---|
| 168 | for (list<Observation*>::iterator it = decoder->m_lObsList.begin();
|
|---|
| 169 | it != decoder->m_lObsList.end(); it++) {
|
|---|
| 170 | emit newObs(_mountPoint, *it);
|
|---|
| 171 | }
|
|---|
| 172 | decoder->m_lObsList.clear();
|
|---|
| 173 | }
|
|---|
| 174 | else {
|
|---|
| 175 | qWarning("Data Timeout");
|
|---|
| 176 | return exit(1);
|
|---|
| 177 | }
|
|---|
| 178 | }
|
|---|
| 179 | delete decoder;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | // Exit
|
|---|
| 183 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 184 | void bncGetThread::exit(int exitCode) {
|
|---|
| 185 | if (exitCode!= 0) {
|
|---|
| 186 | emit error(_mountPoint);
|
|---|
| 187 | }
|
|---|
| 188 | QThread::exit(exitCode);
|
|---|
| 189 | }
|
|---|