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

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

* empty log message *

File size: 5.2 KB
RevLine 
[35]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"
[65]23
[35]24#include "RTCM/RTCM.h"
[65]25#include "RTCM3/rtcm3.h"
[61]26#include "RTIGS/rtigs.h"
[35]27
28using namespace std;
29
[36]30const int timeOut = 30*1000;
[35]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,
[59]38 const QByteArray& password,
39 const QByteArray& format) {
[35]40 _host = host;
41 _port = port;
42 _proxyHost = proxyHost;
43 _proxyPort = proxyPort;
44 _mountPoint = mountPoint;
45 _user = user;
46 _password = password;
[59]47 _format = format;
[35]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,
[82]63 const QByteArray& password,
64 QString& msg) {
[35]65
[82]66 msg.clear();
67
[35]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)) {
[82]87 msg += "Connect timeout\n";
[35]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
[82]100 msg += reqStr;
[35]101
102 socket->write(reqStr, reqStr.length());
103
104 if (!socket->waitForBytesWritten(timeOut)) {
[82]105 msg += "Write timeout\n";
[35]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 // ----------------
[82]119 QString msg;
[35]120 _socket = bncGetThread::request(_host, _port, _proxyHost, _proxyPort,
[82]121 _mountPoint, _user, _password, msg);
122 emit(newMessage(msg.toAscii()));
123
[35]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) {
[82]134 emit(newMessage(("Wrong Caster Response:\n" + line).toAscii()));
[35]135 return exit(1);
136 }
137 }
138 else {
[82]139 emit(newMessage("Response Timeout"));
[35]140 return exit(1);
141 }
142
143 // Instantiate the filter
144 // ----------------------
[61]145 GPSDecoder* decoder;
[35]146
[60]147 if (_format.indexOf("RTCM_2") != -1) {
[82]148 emit(newMessage("Get Data: " + _mountPoint + " in RTCM 2.x format"));
[61]149 decoder = new RTCM('A',true);
[60]150 }
151 else if (_format.indexOf("RTCM_3") != -1) {
[82]152 emit(newMessage("Get Data: " + _mountPoint + " in RTCM 3.0 format"));
[65]153 decoder = new rtcm3();
[60]154 }
155 else if (_format.indexOf("RTIGS") != -1) {
[82]156 emit(newMessage("Get Data: " + _mountPoint + " in RTIGS format"));
[61]157 decoder = new rtigs();
[60]158 }
159 else {
[82]160 emit(newMessage(_mountPoint + " Unknown data format " + _format));
[60]161 return exit(1);
162 }
[59]163
[35]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);
[61]172 decoder->Decode(data, nBytes);
[35]173 delete data;
[61]174 for (list<Observation*>::iterator it = decoder->m_lObsList.begin();
175 it != decoder->m_lObsList.end(); it++) {
[35]176 emit newObs(_mountPoint, *it);
177 }
[61]178 decoder->m_lObsList.clear();
[35]179 }
180 else {
[82]181 emit(newMessage("Data Timeout"));
[35]182 return exit(1);
183 }
184 }
[61]185 delete decoder;
[35]186}
187
188// Exit
189////////////////////////////////////////////////////////////////////////////
190void bncGetThread::exit(int exitCode) {
191 if (exitCode!= 0) {
192 emit error(_mountPoint);
193 }
194 QThread::exit(exitCode);
195}
[82]196
Note: See TracBrowser for help on using the repository browser.