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