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