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