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