source: ntrip/trunk/BNC/bncgetthread.cpp@ 82

Last change on this file since 82 was 82, checked in by mervart, 18 years ago

* empty log message *

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