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

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

* empty log message *

File size: 5.0 KB
Line 
1
2/* -------------------------------------------------------------------------
3 * BKG 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
24#include "RTCM/RTCM.h"
25#include "RTCM3/rtcm3.h"
26#include "RTIGS/rtigs.h"
27
28using namespace std;
29
30// Constructor
31////////////////////////////////////////////////////////////////////////////
32bncGetThread::bncGetThread(const QUrl& mountPoint, const QByteArray& format) {
33 _decoder = 0;
34 _mountPoint = mountPoint;
35 _staID = mountPoint.path().mid(1).toAscii();
36 _format = format;
37 _socket = 0;
38 _timeOut = 10*1000; // 10 seconds
39}
40
41// Destructor
42////////////////////////////////////////////////////////////////////////////
43bncGetThread::~bncGetThread() {
44 delete _socket;
45 delete _decoder;
46}
47
48// Connect to Caster, send the Request (static)
49////////////////////////////////////////////////////////////////////////////
50QTcpSocket* bncGetThread::request(const QUrl& mountPoint, int timeOut,
51 QString& msg) {
52
53 // Connect the Socket
54 // ------------------
55 QSettings settings;
56 QString proxyHost = settings.value("proxyHost").toString();
57 int proxyPort = settings.value("proxyPort").toInt();
58
59 QTcpSocket* socket = new QTcpSocket();
60 if ( proxyHost.isEmpty() ) {
61 socket->connectToHost(mountPoint.host(), mountPoint.port());
62 }
63 else {
64 socket->connectToHost(proxyHost, proxyPort);
65 }
66 if (!socket->waitForConnected(timeOut)) {
67 msg += "Connect timeout\n";
68 delete socket;
69 return 0;
70 }
71
72 // Send Request
73 // ------------
74 QByteArray userAndPwd = mountPoint.userName().toAscii() + ":" +
75 mountPoint.password().toAscii();
76
77 QUrl hlp;
78 hlp.setScheme("http");
79 hlp.setHost(mountPoint.host());
80 hlp.setPort(mountPoint.port());
81 hlp.setPath(mountPoint.path());
82
83 QByteArray reqStr = "GET " + hlp.toEncoded() +
84 " HTTP/1.0\r\n"
85 "User-Agent: NTRIP BNC 1.0\r\n"
86 "Authorization: Basic " +
87 userAndPwd.toBase64() + "\r\n\r\n";
88
89 msg += reqStr;
90
91 socket->write(reqStr, reqStr.length());
92
93 if (!socket->waitForBytesWritten(timeOut)) {
94 msg += "Write timeout\n";
95 delete socket;
96 return 0;
97 }
98
99 return socket;
100}
101
102// Init Run
103////////////////////////////////////////////////////////////////////////////
104void bncGetThread::initRun() {
105
106 // Send the Request
107 // ----------------
108 QString msg;
109
110 _socket = bncGetThread::request(_mountPoint, _timeOut, msg);
111
112 emit(newMessage(msg.toAscii()));
113
114 if (!_socket) {
115 return exit(1);
116 }
117
118 // Read Caster Response
119 // --------------------
120 _socket->waitForReadyRead(_timeOut);
121 if (_socket->canReadLine()) {
122 QString line = _socket->readLine();
123 if (line.indexOf("ICY 200 OK") != 0) {
124 emit(newMessage(("Wrong Caster Response:\n" + line).toAscii()));
125 return exit(1);
126 }
127 }
128 else {
129 emit(newMessage("Response Timeout"));
130 return exit(1);
131 }
132
133 // Instantiate the filter
134 // ----------------------
135 if (!_decoder) {
136 if (_format.indexOf("RTCM_2") != -1) {
137 emit(newMessage("Get Data: " + _staID + " in RTCM 2.x format"));
138 _decoder = new RTCM('A',true);
139 }
140 else if (_format.indexOf("RTCM_3") != -1) {
141 emit(newMessage("Get Data: " + _staID + " in RTCM 3.0 format"));
142 _decoder = new rtcm3();
143 }
144 else if (_format.indexOf("RTIGS") != -1) {
145 emit(newMessage("Get Data: " + _staID + " in RTIGS format"));
146 _decoder = new rtigs();
147 }
148 else {
149 emit(newMessage(_staID + " Unknown data format " + _format));
150 return exit(1);
151 }
152 }
153}
154
155// Run
156////////////////////////////////////////////////////////////////////////////
157void bncGetThread::run() {
158
159 initRun();
160
161 // Read Incoming Data
162 // ------------------
163 while (true) {
164 _socket->waitForReadyRead(_timeOut);
165 qint64 nBytes = _socket->bytesAvailable();
166 if (nBytes > 0) {
167 char* data = new char[nBytes];
168 _socket->read(data, nBytes);
169 _decoder->Decode(data, nBytes);
170 delete data;
171 for (list<Observation*>::iterator it = _decoder->m_lObsList.begin();
172 it != _decoder->m_lObsList.end(); it++) {
173 emit newObs(_staID, *it);
174 }
175 _decoder->m_lObsList.clear();
176 }
177 else {
178 emit(newMessage("Data Timeout, reconnecting"));
179 tryReconnect();
180 }
181 }
182}
183
184// Exit
185////////////////////////////////////////////////////////////////////////////
186void bncGetThread::exit(int exitCode) {
187 if (exitCode!= 0) {
188 emit error(_staID);
189 }
190 QThread::exit(exitCode);
191}
192
193// Try Re-Connect
194////////////////////////////////////////////////////////////////////////////
195void bncGetThread::tryReconnect() {
196
197}
Note: See TracBrowser for help on using the repository browser.