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

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