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

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

* empty log message *

File size: 4.5 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 rtcmFilter = new RTCM('A',true);
138
139 qWarning("Get Data: " + _mountPoint + " " + _format);
140
141 // Read Incoming Data
142 // ------------------
143 while (true) {
144 _socket->waitForReadyRead(timeOut);
145 qint64 nBytes = _socket->bytesAvailable();
146 if (nBytes > 0) {
147 char* data = new char[nBytes];
148 _socket->read(data, nBytes);
149 rtcmFilter->Decode(data, nBytes);
150 delete data;
151 for (list<Observation*>::iterator it = rtcmFilter->m_lObsList.begin();
152 it != rtcmFilter->m_lObsList.end(); it++) {
153 emit newObs(_mountPoint, *it);
154 }
155 rtcmFilter->m_lObsList.clear();
156 }
157 else {
158 qWarning("Data Timeout");
159 return exit(1);
160 }
161 }
162 delete rtcmFilter;
163}
164
165// Exit
166////////////////////////////////////////////////////////////////////////////
167void bncGetThread::exit(int exitCode) {
168 if (exitCode!= 0) {
169 emit error(_mountPoint);
170 }
171 QThread::exit(exitCode);
172}
Note: See TracBrowser for help on using the repository browser.