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

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

* empty log message *

File size: 3.4 KB
Line 
1/* -------------------------------------------------------------------------
2 * BKG NTRIP Client
3 * -------------------------------------------------------------------------
4 *
5 * Class: bncNetQueryV1
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
20#include "bncnetqueryv1.h"
21
22using namespace std;
23
24#define BNCVERSION "1.7"
25
26// Constructor
27////////////////////////////////////////////////////////////////////////////
28bncNetQueryV1::bncNetQueryV1() {
29 _socket = 0;
30}
31
32// Destructor
33////////////////////////////////////////////////////////////////////////////
34bncNetQueryV1::~bncNetQueryV1() {
35 delete _socket;
36}
37
38//
39////////////////////////////////////////////////////////////////////////////
40void bncNetQueryV1::stop() {
41 if (_socket) {
42 _socket->abort();
43 }
44}
45
46//
47////////////////////////////////////////////////////////////////////////////
48void bncNetQueryV1::waitForRequestResult(const QUrl&, QByteArray&) {
49}
50
51//
52////////////////////////////////////////////////////////////////////////////
53void bncNetQueryV1::waitForReadyRead(QByteArray& outData) {
54 if (_socket) {
55 if (_socket->waitForReadyRead()) {
56 outData = _socket->readAll();
57 }
58 }
59}
60
61// Connect to Caster, send the Request
62////////////////////////////////////////////////////////////////////////////
63void bncNetQueryV1::startRequest(const QUrl& url, const QByteArray& gga) {
64
65 const int timeOut = 5000;
66
67 _status = running;
68
69 delete _socket;
70 _socket = new QTcpSocket();
71
72 // Default scheme and path
73 // -----------------------
74 QUrl urlLoc(url);
75 if (urlLoc.scheme().isEmpty()) {
76 urlLoc.setScheme("http");
77 }
78 if (urlLoc.path().isEmpty()) {
79 urlLoc.setPath("/");
80 }
81
82 // Connect the Socket
83 // ------------------
84 QSettings settings;
85 QString proxyHost = settings.value("proxyHost").toString();
86 int proxyPort = settings.value("proxyPort").toInt();
87
88 if ( proxyHost.isEmpty() ) {
89 _socket->connectToHost(urlLoc.host(), urlLoc.port());
90 }
91 else {
92 _socket->connectToHost(proxyHost, proxyPort);
93 }
94 if (!_socket->waitForConnected(timeOut)) {
95 delete _socket;
96 _socket = 0;
97 _status = error;
98 return;
99 }
100
101 // Send Request
102 // ------------
103 QString uName = QUrl::fromPercentEncoding(urlLoc.userName().toAscii());
104 QString passW = QUrl::fromPercentEncoding(urlLoc.password().toAscii());
105 QByteArray userAndPwd;
106
107 if(!uName.isEmpty() || !passW.isEmpty()) {
108 userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
109 passW.toAscii()).toBase64() + "\r\n";
110 }
111
112 QByteArray reqStr;
113 if ( proxyHost.isEmpty() ) {
114 if (urlLoc.path().indexOf("/") != 0) urlLoc.setPath("/");
115 reqStr = "GET " + urlLoc.path().toAscii() + " HTTP/1.0\r\n"
116 + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
117 + userAndPwd + "\r\n";
118 } else {
119 reqStr = "GET " + urlLoc.toEncoded() + " HTTP/1.0\r\n"
120 + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
121 + "Host: " + urlLoc.host().toAscii() + "\r\n"
122 + userAndPwd + "\r\n";
123 }
124
125 // NMEA string to handle VRS stream
126 // --------------------------------
127 if (!gga.isEmpty()) {
128 reqStr += gga + "\r\n";
129 }
130
131 _socket->write(reqStr, reqStr.length());
132
133 if (!_socket->waitForBytesWritten(timeOut)) {
134 delete _socket;
135 _socket = 0;
136 _status = error;
137 }
138}
139
Note: See TracBrowser for help on using the repository browser.