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

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

Imported sources

File size: 4.3 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 = 10*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 _host = host;
37 _port = port;
38 _proxyHost = proxyHost;
39 _proxyPort = proxyPort;
40 _mountPoint = mountPoint;
41 _user = user;
42 _password = password;
43 _socket = 0;
44}
45
46// Destructor
47////////////////////////////////////////////////////////////////////////////
48bncGetThread::~bncGetThread() {
49 delete _socket;
50}
51
52// Connect to Caster, send the Request (static)
53////////////////////////////////////////////////////////////////////////////
54QTcpSocket* bncGetThread::request(const QString& host, int port,
55 const QString& proxyHost, int proxyPort,
56 const QByteArray& mountPoint,
57 const QByteArray& user,
58 const QByteArray& password) {
59
60 QTcpSocket* socket = new QTcpSocket();
61
62 QByteArray l_mountPoint = mountPoint;
63
64 if ( proxyHost.isEmpty() ) {
65 socket->connectToHost(host, port);
66 }
67 else {
68 socket->connectToHost(proxyHost, proxyPort);
69 if (!proxyHost.isEmpty()) {
70 QUrl proxyUrl;
71 proxyUrl.setScheme("http");
72 proxyUrl.setHost(host);
73 proxyUrl.setPort(port);
74 l_mountPoint = proxyUrl.resolved(QUrl(mountPoint)).toEncoded();
75 }
76 }
77
78 if (!socket->waitForConnected(timeOut)) {
79 qWarning("Connect timeout");
80 delete socket;
81 return 0;
82 }
83
84 // Send Request
85 // ------------
86 QByteArray userAndPwd = user + ":" + password;
87 QByteArray reqStr = "GET " + l_mountPoint + " HTTP/1.0\r\n"
88 "User-Agent: NTRIP BNC 1.0\r\n"
89 "Authorization: Basic " + userAndPwd.toBase64() +
90 "\r\n\r\n";
91
92 qWarning(reqStr);
93
94 socket->write(reqStr, reqStr.length());
95
96 if (!socket->waitForBytesWritten(timeOut)) {
97 qWarning("Write timeout");
98 delete socket;
99 return 0;
100 }
101
102 return socket;
103}
104
105// Run
106////////////////////////////////////////////////////////////////////////////
107void bncGetThread::run() {
108
109 // Send the Request
110 // ----------------
111 _socket = bncGetThread::request(_host, _port, _proxyHost, _proxyPort,
112 _mountPoint, _user, _password);
113 if (!_socket) {
114 return exit(1);
115 }
116
117 // Read Caster Response
118 // --------------------
119 _socket->waitForReadyRead(timeOut);
120 if (_socket->canReadLine()) {
121 QString line = _socket->readLine();
122 if (line.indexOf("ICY 200 OK") != 0) {
123 qWarning(("Wrong Caster Response:\n" + line).toAscii());
124 return exit(1);
125 }
126 }
127 else {
128 qWarning("Response Timeout");
129 return exit(1);
130 }
131
132 // Instantiate the filter
133 // ----------------------
134 RTCM rtcmFilter('A',true);
135
136 // Read Incoming Data
137 // ------------------
138 while (true) {
139 _socket->waitForReadyRead(timeOut);
140 qint64 nBytes = _socket->bytesAvailable();
141 if (nBytes > 0) {
142 char* data = new char[nBytes];
143 _socket->read(data, nBytes);
144 rtcmFilter.Decode(data, nBytes);
145 delete data;
146 for (list<Observation*>::iterator it = rtcmFilter.m_lObsList.begin();
147 it != rtcmFilter.m_lObsList.end(); it++) {
148 emit newObs(_mountPoint, *it);
149 }
150 rtcmFilter.m_lObsList.clear();
151 }
152 else {
153 qWarning("Data Timeout");
154 return exit(1);
155 }
156 }
157}
158
159// Exit
160////////////////////////////////////////////////////////////////////////////
161void bncGetThread::exit(int exitCode) {
162 if (exitCode!= 0) {
163 emit error(_mountPoint);
164 }
165 QThread::exit(exitCode);
166}
Note: See TracBrowser for help on using the repository browser.