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

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

* empty log message *

File size: 5.0 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#include "RTCM/RTCM.h"
24#include "RTIGS/rtigs.h"
25
26using namespace std;
27
28const int timeOut = 30*1000;
29
30// Constructor
31////////////////////////////////////////////////////////////////////////////
32bncGetThread::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////////////////////////////////////////////////////////////////////////////
51bncGetThread::~bncGetThread() {
52 delete _socket;
53}
54
55// Connect to Caster, send the Request (static)
56////////////////////////////////////////////////////////////////////////////
57QTcpSocket* 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////////////////////////////////////////////////////////////////////////////
110void 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 return exit(1);
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////////////////////////////////////////////////////////////////////////////
184void bncGetThread::exit(int exitCode) {
185 if (exitCode!= 0) {
186 emit error(_mountPoint);
187 }
188 QThread::exit(exitCode);
189}
Note: See TracBrowser for help on using the repository browser.