source: ntrip/trunk/BNC/bncnetqueryv1.cpp@ 1500

Last change on this file since 1500 was 1500, checked in by mervart, 15 years ago

* empty log message *

File size: 4.3 KB
RevLine 
[1382]1/* -------------------------------------------------------------------------
2 * BKG NTRIP Client
3 * -------------------------------------------------------------------------
4 *
[1383]5 * Class: bncNetQueryV1
[1382]6 *
7 * Purpose: Blocking Network Requests (NTRIP Version 1)
8 *
9 * Author: L. Mervart
10 *
11 * Created: 27-Dec-2008
12 *
13 * Changes:
14 *
15 * -----------------------------------------------------------------------*/
16
17#include <iostream>
18#include <iomanip>
19
[1385]20#include "bncnetqueryv1.h"
[1382]21
22using namespace std;
23
24#define BNCVERSION "1.7"
25
26// Constructor
27////////////////////////////////////////////////////////////////////////////
[1383]28bncNetQueryV1::bncNetQueryV1() {
[1500]29 _socket = 0;
30 _timeOut = 20000;
[1382]31}
32
33// Destructor
34////////////////////////////////////////////////////////////////////////////
[1383]35bncNetQueryV1::~bncNetQueryV1() {
[1382]36 delete _socket;
37}
38
39//
40////////////////////////////////////////////////////////////////////////////
[1390]41void bncNetQueryV1::stop() {
[1408]42#ifndef sparc
43 if (_socket) {
44 _socket->abort();
45 }
46#endif
[1403]47 _status = finished;
[1390]48}
49
50//
51////////////////////////////////////////////////////////////////////////////
[1402]52void bncNetQueryV1::waitForRequestResult(const QUrl&, QByteArray&) {
[1382]53}
54
55//
56////////////////////////////////////////////////////////////////////////////
[1384]57void bncNetQueryV1::waitForReadyRead(QByteArray& outData) {
[1500]58 if (_socket && _socket->state() == QAbstractSocket::ConnectedState) {
59 while (true) {
60 int nBytes = _socket->bytesAvailable();
61 if (nBytes > 0) {
62 outData = _socket->readAll();
63 return;
64 }
65 else if (!_socket->waitForReadyRead(_timeOut)) {
66 delete _socket;
67 _socket = 0;
68 _status = error;
69 emit newMessage(_url.path().toAscii() + " read timeout", true);
70 return;
71 }
[1402]72 }
73 }
[1382]74}
75
76// Connect to Caster, send the Request
77////////////////////////////////////////////////////////////////////////////
[1384]78void bncNetQueryV1::startRequest(const QUrl& url, const QByteArray& gga) {
[1382]79
[1385]80 _status = running;
81
[1382]82 delete _socket;
83 _socket = new QTcpSocket();
84
[1385]85 // Default scheme and path
86 // -----------------------
[1500]87 _url = url;
88 if (_url.scheme().isEmpty()) {
89 _url.setScheme("http");
[1385]90 }
[1500]91 if (_url.path().isEmpty()) {
92 _url.setPath("/");
[1385]93 }
94
[1382]95 // Connect the Socket
96 // ------------------
97 QSettings settings;
98 QString proxyHost = settings.value("proxyHost").toString();
99 int proxyPort = settings.value("proxyPort").toInt();
100
101 if ( proxyHost.isEmpty() ) {
[1500]102 _socket->connectToHost(_url.host(), _url.port());
[1382]103 }
104 else {
105 _socket->connectToHost(proxyHost, proxyPort);
106 }
[1500]107 if (!_socket->waitForConnected(_timeOut)) {
[1382]108 delete _socket;
109 _socket = 0;
[1385]110 _status = error;
111 return;
[1382]112 }
113
114 // Send Request
115 // ------------
[1500]116 QString uName = QUrl::fromPercentEncoding(_url.userName().toAscii());
117 QString passW = QUrl::fromPercentEncoding(_url.password().toAscii());
[1382]118 QByteArray userAndPwd;
119
[1385]120 if(!uName.isEmpty() || !passW.isEmpty()) {
[1382]121 userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
122 passW.toAscii()).toBase64() + "\r\n";
123 }
124
125 QByteArray reqStr;
126 if ( proxyHost.isEmpty() ) {
[1500]127 if (_url.path().indexOf("/") != 0) _url.setPath("/");
128 reqStr = "GET " + _url.path().toAscii() + " HTTP/1.0\r\n"
[1382]129 + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
130 + userAndPwd + "\r\n";
131 } else {
[1500]132 reqStr = "GET " + _url.toEncoded() + " HTTP/1.0\r\n"
[1382]133 + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
[1500]134 + "Host: " + _url.host().toAscii() + "\r\n"
[1382]135 + userAndPwd + "\r\n";
136 }
137
138 // NMEA string to handle VRS stream
139 // --------------------------------
[1385]140 if (!gga.isEmpty()) {
141 reqStr += gga + "\r\n";
[1382]142 }
143
144 _socket->write(reqStr, reqStr.length());
145
[1500]146 if (!_socket->waitForBytesWritten(_timeOut)) {
[1382]147 delete _socket;
148 _socket = 0;
[1385]149 _status = error;
[1500]150 emit newMessage(_url.path().toAscii() + " write timeout", true);
[1498]151 return;
[1382]152 }
[1498]153
154 // Read Caster Response
155 // --------------------
156 while (true) {
[1500]157 if (!_socket->waitForReadyRead(_timeOut)) {
[1498]158 delete _socket;
159 _socket = 0;
160 _status = error;
[1500]161 emit newMessage(_url.path().toAscii() + " read timeout", true);
[1498]162 return;
163 }
164 if (_socket->canReadLine()) {
165 QString line = _socket->readLine();
166 if (line.indexOf("ICY 200 OK") != -1) {
167 break;
168 }
169 }
170 }
[1382]171}
172
Note: See TracBrowser for help on using the repository browser.