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

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

* empty log message *

File size: 4.6 KB
RevLine 
[35]1
2/* -------------------------------------------------------------------------
[93]3 * BKG NTRIP Client
[35]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
[87]30const int timeOut = 300*1000;
[35]31
32// Constructor
33////////////////////////////////////////////////////////////////////////////
[88]34bncGetThread::bncGetThread(const QUrl& mountPoint, const QByteArray& format) {
[35]35 _mountPoint = mountPoint;
[89]36 _staID = mountPoint.path().mid(1).toAscii();
[59]37 _format = format;
[35]38 _socket = 0;
39}
40
41// Destructor
42////////////////////////////////////////////////////////////////////////////
43bncGetThread::~bncGetThread() {
44 delete _socket;
45}
46
47// Connect to Caster, send the Request (static)
48////////////////////////////////////////////////////////////////////////////
[88]49QTcpSocket* bncGetThread::request(const QUrl& mountPoint, QString& msg) {
[35]50
[88]51 // Connect the Socket
52 // ------------------
53 QSettings settings;
54 QString proxyHost = settings.value("proxyHost").toString();
55 int proxyPort = settings.value("proxyPort").toInt();
56
[35]57 QTcpSocket* socket = new QTcpSocket();
58 if ( proxyHost.isEmpty() ) {
[88]59 socket->connectToHost(mountPoint.host(), mountPoint.port());
[35]60 }
61 else {
62 socket->connectToHost(proxyHost, proxyPort);
63 }
64 if (!socket->waitForConnected(timeOut)) {
[82]65 msg += "Connect timeout\n";
[35]66 delete socket;
67 return 0;
68 }
69
70 // Send Request
71 // ------------
[88]72 QByteArray userAndPwd = mountPoint.userName().toAscii() + ":" +
73 mountPoint.password().toAscii();
[92]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() +
[88]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";
[35]86
[82]87 msg += reqStr;
[35]88
89 socket->write(reqStr, reqStr.length());
90
91 if (!socket->waitForBytesWritten(timeOut)) {
[82]92 msg += "Write timeout\n";
[35]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 // ----------------
[82]106 QString msg;
[88]107
108 _socket = bncGetThread::request(_mountPoint, msg);
109
[82]110 emit(newMessage(msg.toAscii()));
111
[35]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) {
[82]122 emit(newMessage(("Wrong Caster Response:\n" + line).toAscii()));
[35]123 return exit(1);
124 }
125 }
126 else {
[82]127 emit(newMessage("Response Timeout"));
[35]128 return exit(1);
129 }
130
131 // Instantiate the filter
132 // ----------------------
[61]133 GPSDecoder* decoder;
[35]134
[60]135 if (_format.indexOf("RTCM_2") != -1) {
[88]136 emit(newMessage("Get Data: " + _staID + " in RTCM 2.x format"));
[61]137 decoder = new RTCM('A',true);
[60]138 }
139 else if (_format.indexOf("RTCM_3") != -1) {
[88]140 emit(newMessage("Get Data: " + _staID + " in RTCM 3.0 format"));
[65]141 decoder = new rtcm3();
[60]142 }
143 else if (_format.indexOf("RTIGS") != -1) {
[88]144 emit(newMessage("Get Data: " + _staID + " in RTIGS format"));
[61]145 decoder = new rtigs();
[60]146 }
147 else {
[88]148 emit(newMessage(_staID + " Unknown data format " + _format));
[60]149 return exit(1);
150 }
[59]151
[35]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);
[61]160 decoder->Decode(data, nBytes);
[35]161 delete data;
[61]162 for (list<Observation*>::iterator it = decoder->m_lObsList.begin();
163 it != decoder->m_lObsList.end(); it++) {
[88]164 emit newObs(_staID, *it);
[35]165 }
[61]166 decoder->m_lObsList.clear();
[35]167 }
168 else {
[82]169 emit(newMessage("Data Timeout"));
[35]170 return exit(1);
171 }
172 }
[61]173 delete decoder;
[35]174}
175
176// Exit
177////////////////////////////////////////////////////////////////////////////
178void bncGetThread::exit(int exitCode) {
179 if (exitCode!= 0) {
[88]180 emit error(_staID);
[35]181 }
182 QThread::exit(exitCode);
183}
[82]184
Note: See TracBrowser for help on using the repository browser.