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 "bncsocket.h"
|
---|
21 | #include "bncapp.h"
|
---|
22 | #include "bncutils.h"
|
---|
23 |
|
---|
24 | using namespace std;
|
---|
25 |
|
---|
26 | #define BNCVERSION "1.7"
|
---|
27 |
|
---|
28 | // Constructor
|
---|
29 | ////////////////////////////////////////////////////////////////////////////
|
---|
30 | bncNetQueryV1::bncNetQueryV1() {
|
---|
31 | _socket = 0;
|
---|
32 | }
|
---|
33 |
|
---|
34 | // Destructor
|
---|
35 | ////////////////////////////////////////////////////////////////////////////
|
---|
36 | bncNetQueryV1::~bncNetQueryV1() {
|
---|
37 | delete _socket;
|
---|
38 | }
|
---|
39 |
|
---|
40 | //
|
---|
41 | ////////////////////////////////////////////////////////////////////////////
|
---|
42 | void bncNetQueryV1::close() {
|
---|
43 | if (_socket) {
|
---|
44 | _socket->close();
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | //
|
---|
49 | ////////////////////////////////////////////////////////////////////////////
|
---|
50 | qint64 bncNetQueryV1::bytesAvailable() const {
|
---|
51 | if (_socket) {
|
---|
52 | return _socket->bytesAvailable();
|
---|
53 | }
|
---|
54 | else {
|
---|
55 | return 0;
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | //
|
---|
60 | ////////////////////////////////////////////////////////////////////////////
|
---|
61 | bool bncNetQueryV1::canReadLine() const {
|
---|
62 | if (_socket) {
|
---|
63 | return _socket->canReadLine();
|
---|
64 | }
|
---|
65 | else {
|
---|
66 | return false;
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | //
|
---|
71 | ////////////////////////////////////////////////////////////////////////////
|
---|
72 | QByteArray bncNetQueryV1::readLine() {
|
---|
73 | if (_socket) {
|
---|
74 | return _socket->readLine();
|
---|
75 | }
|
---|
76 | else {
|
---|
77 | return "";
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | //
|
---|
82 | ////////////////////////////////////////////////////////////////////////////
|
---|
83 | void bncNetQueryV1::waitForReadyRead(int msecs) {
|
---|
84 | if (_socket) {
|
---|
85 | _socket->waitForReadyRead(msecs);
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | //
|
---|
90 | ////////////////////////////////////////////////////////////////////////////
|
---|
91 | QByteArray bncNetQueryV1::read(qint64 maxSize) {
|
---|
92 | if (_socket) {
|
---|
93 | return _socket->read(maxSize);
|
---|
94 | }
|
---|
95 | else {
|
---|
96 | return "";
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | // Connect to Caster, send the Request
|
---|
101 | ////////////////////////////////////////////////////////////////////////////
|
---|
102 | t_irc bncNetQueryV1::request(const QUrl& mountPoint, const QByteArray& latitude,
|
---|
103 | const QByteArray& longitude, const QByteArray& nmea,
|
---|
104 | const QByteArray& ntripVersion,
|
---|
105 | int timeOut, QString& msg) {
|
---|
106 |
|
---|
107 | delete _socket;
|
---|
108 | _socket = new QTcpSocket();
|
---|
109 |
|
---|
110 | // Connect the Socket
|
---|
111 | // ------------------
|
---|
112 | QSettings settings;
|
---|
113 | QString proxyHost = settings.value("proxyHost").toString();
|
---|
114 | int proxyPort = settings.value("proxyPort").toInt();
|
---|
115 |
|
---|
116 | if ( proxyHost.isEmpty() ) {
|
---|
117 | _socket->connectToHost(mountPoint.host(), mountPoint.port());
|
---|
118 | }
|
---|
119 | else {
|
---|
120 | _socket->connectToHost(proxyHost, proxyPort);
|
---|
121 | }
|
---|
122 | if (!_socket->waitForConnected(timeOut)) {
|
---|
123 | msg += "Connect timeout\n";
|
---|
124 | delete _socket;
|
---|
125 | _socket = 0;
|
---|
126 | return failure;
|
---|
127 | }
|
---|
128 |
|
---|
129 | // Send Request
|
---|
130 | // ------------
|
---|
131 | QString uName = QUrl::fromPercentEncoding(mountPoint.userName().toAscii());
|
---|
132 | QString passW = QUrl::fromPercentEncoding(mountPoint.password().toAscii());
|
---|
133 | QByteArray userAndPwd;
|
---|
134 |
|
---|
135 | if(!uName.isEmpty() || !passW.isEmpty())
|
---|
136 | {
|
---|
137 | userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
|
---|
138 | passW.toAscii()).toBase64() + "\r\n";
|
---|
139 | }
|
---|
140 |
|
---|
141 | QUrl hlp;
|
---|
142 | hlp.setScheme("http");
|
---|
143 | hlp.setHost(mountPoint.host());
|
---|
144 | hlp.setPort(mountPoint.port());
|
---|
145 | hlp.setPath(mountPoint.path());
|
---|
146 |
|
---|
147 | QByteArray reqStr;
|
---|
148 | if ( proxyHost.isEmpty() ) {
|
---|
149 | if (hlp.path().indexOf("/") != 0) hlp.setPath("/");
|
---|
150 | reqStr = "GET " + hlp.path().toAscii() + " HTTP/1.0\r\n"
|
---|
151 | + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
|
---|
152 | + userAndPwd + "\r\n";
|
---|
153 | } else {
|
---|
154 | reqStr = "GET " + hlp.toEncoded() + " HTTP/1.0\r\n"
|
---|
155 | + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
|
---|
156 | + "Host: " + hlp.host().toAscii() + "\r\n"
|
---|
157 | + userAndPwd + "\r\n";
|
---|
158 | }
|
---|
159 |
|
---|
160 | // NMEA string to handle VRS stream
|
---|
161 | // --------------------------------
|
---|
162 | if ((nmea == "yes") && (hlp.path().length() > 2) && (hlp.path().indexOf(".skl") < 0)) {
|
---|
163 | reqStr += "$" + ggaString(latitude, longitude) + "\r\n";
|
---|
164 | }
|
---|
165 |
|
---|
166 | msg += reqStr;
|
---|
167 |
|
---|
168 | _socket->write(reqStr, reqStr.length());
|
---|
169 |
|
---|
170 | if (!_socket->waitForBytesWritten(timeOut)) {
|
---|
171 | msg += "Write timeout\n";
|
---|
172 | delete _socket;
|
---|
173 | _socket = 0;
|
---|
174 | return failure;
|
---|
175 | }
|
---|
176 |
|
---|
177 | return success;
|
---|
178 | }
|
---|
179 |
|
---|