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

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