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

Last change on this file since 91 was 90, checked in by mervart, 19 years ago

* empty log message *

File size: 4.4 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
[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();
74 QByteArray reqStr = "GET " + mountPoint.path().toAscii() +
75 " HTTP/1.0\r\n"
76 "User-Agent: NTRIP BNC 1.0\r\n"
77 "Authorization: Basic " +
78 userAndPwd.toBase64() + "\r\n\r\n";
[35]79
[82]80 msg += reqStr;
[35]81
82 socket->write(reqStr, reqStr.length());
83
84 if (!socket->waitForBytesWritten(timeOut)) {
[82]85 msg += "Write timeout\n";
[35]86 delete socket;
87 return 0;
88 }
89
90 return socket;
91}
92
93// Run
94////////////////////////////////////////////////////////////////////////////
95void bncGetThread::run() {
96
97 // Send the Request
98 // ----------------
[82]99 QString msg;
[88]100
101 _socket = bncGetThread::request(_mountPoint, msg);
102
[82]103 emit(newMessage(msg.toAscii()));
104
[35]105 if (!_socket) {
106 return exit(1);
107 }
108
109 // Read Caster Response
110 // --------------------
111 _socket->waitForReadyRead(timeOut);
112 if (_socket->canReadLine()) {
113 QString line = _socket->readLine();
114 if (line.indexOf("ICY 200 OK") != 0) {
[82]115 emit(newMessage(("Wrong Caster Response:\n" + line).toAscii()));
[35]116 return exit(1);
117 }
118 }
119 else {
[82]120 emit(newMessage("Response Timeout"));
[35]121 return exit(1);
122 }
123
124 // Instantiate the filter
125 // ----------------------
[61]126 GPSDecoder* decoder;
[35]127
[60]128 if (_format.indexOf("RTCM_2") != -1) {
[88]129 emit(newMessage("Get Data: " + _staID + " in RTCM 2.x format"));
[61]130 decoder = new RTCM('A',true);
[60]131 }
132 else if (_format.indexOf("RTCM_3") != -1) {
[88]133 emit(newMessage("Get Data: " + _staID + " in RTCM 3.0 format"));
[65]134 decoder = new rtcm3();
[60]135 }
136 else if (_format.indexOf("RTIGS") != -1) {
[88]137 emit(newMessage("Get Data: " + _staID + " in RTIGS format"));
[61]138 decoder = new rtigs();
[60]139 }
140 else {
[88]141 emit(newMessage(_staID + " Unknown data format " + _format));
[60]142 return exit(1);
143 }
[59]144
[35]145 // Read Incoming Data
146 // ------------------
147 while (true) {
148 _socket->waitForReadyRead(timeOut);
149 qint64 nBytes = _socket->bytesAvailable();
150 if (nBytes > 0) {
151 char* data = new char[nBytes];
152 _socket->read(data, nBytes);
[61]153 decoder->Decode(data, nBytes);
[35]154 delete data;
[61]155 for (list<Observation*>::iterator it = decoder->m_lObsList.begin();
156 it != decoder->m_lObsList.end(); it++) {
[88]157 emit newObs(_staID, *it);
[35]158 }
[61]159 decoder->m_lObsList.clear();
[35]160 }
161 else {
[82]162 emit(newMessage("Data Timeout"));
[35]163 return exit(1);
164 }
165 }
[61]166 delete decoder;
[35]167}
168
169// Exit
170////////////////////////////////////////////////////////////////////////////
171void bncGetThread::exit(int exitCode) {
172 if (exitCode!= 0) {
[88]173 emit error(_staID);
[35]174 }
175 QThread::exit(exitCode);
176}
[82]177
Note: See TracBrowser for help on using the repository browser.