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

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

* empty log message *

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