source: ntrip/trunk/BNC/bncsocket.cpp@ 1357

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

* empty log message *

File size: 9.6 KB
Line 
1/* -------------------------------------------------------------------------
2 * BKG NTRIP Client
3 * -------------------------------------------------------------------------
4 *
5 * Class: bncSocket
6 *
7 * Purpose: Combines QTcpSocket (NTRIP v1) and QNetworkReply (NTRIP v2)
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
23using namespace std;
24
25#define BNCVERSION "1.7"
26
27// Constructor
28////////////////////////////////////////////////////////////////////////////
29bncSocket::bncSocket() {
30 bncApp* app = (bncApp*) qApp;
31 app->connect(this, SIGNAL(newMessage(QByteArray,bool)),
32 app, SLOT(slotMessage(const QByteArray,bool)));
33 _socket = 0;
34 _http = 0;
35}
36
37// Destructor
38////////////////////////////////////////////////////////////////////////////
39bncSocket::~bncSocket() {
40 cout << "~bncSocket" << endl;
41 delete _socket;
42 delete _http;
43}
44
45//
46////////////////////////////////////////////////////////////////////////////
47void bncSocket::connectToHost(const QString &hostName, quint16 port,
48 QIODevice::OpenMode mode) {
49 if (_socket) {
50 _socket->connectToHost(hostName, port, mode);
51 }
52}
53
54//
55////////////////////////////////////////////////////////////////////////////
56bool bncSocket::waitForConnected(int msecs) {
57 if (_socket) {
58 return _socket->waitForConnected(msecs);
59 }
60 else {
61 return false;
62 }
63}
64
65//
66////////////////////////////////////////////////////////////////////////////
67QAbstractSocket::SocketState bncSocket::state() const {
68 if (_socket) {
69 return _socket->state();
70 }
71 else {
72 return QAbstractSocket::UnconnectedState;
73 }
74}
75
76//
77////////////////////////////////////////////////////////////////////////////
78void bncSocket::close() {
79 if (_socket) {
80 _socket->close();
81 }
82}
83
84//
85////////////////////////////////////////////////////////////////////////////
86qint64 bncSocket::bytesAvailable() const {
87 if (_socket) {
88 return _socket->bytesAvailable();
89 }
90 else {
91 return 0;
92 }
93}
94
95//
96////////////////////////////////////////////////////////////////////////////
97bool bncSocket::canReadLine() const {
98 if (_socket) {
99 return _socket->canReadLine();
100 }
101 else {
102 return false;
103 }
104}
105
106//
107////////////////////////////////////////////////////////////////////////////
108QByteArray bncSocket::readLine(qint64 maxlen) {
109 if (_socket) {
110 return _socket->readLine(maxlen);
111 }
112 else {
113 return "";
114 }
115}
116
117//
118////////////////////////////////////////////////////////////////////////////
119bool bncSocket::waitForReadyRead(int msecs) {
120 if (_socket) {
121 return _socket->waitForReadyRead(msecs);
122 }
123 else {
124 return false;
125 }
126}
127
128//
129////////////////////////////////////////////////////////////////////////////
130qint64 bncSocket::read(char* data, qint64 maxlen) {
131 if (_socket) {
132 return _socket->read(data, maxlen);
133 }
134 else {
135 return -1;
136 }
137}
138
139//
140////////////////////////////////////////////////////////////////////////////
141qint64 bncSocket::write(const char* data, qint64 len) {
142 if (_socket) {
143 return _socket->write(data, len);
144 }
145 else {
146 return -1;
147 }
148}
149
150//
151////////////////////////////////////////////////////////////////////////////
152bool bncSocket::waitForBytesWritten(int msecs) {
153 if (_socket) {
154 return _socket->waitForBytesWritten(msecs);
155 }
156 else {
157 return false;
158 }
159}
160
161// Connect to Caster, send the Request
162////////////////////////////////////////////////////////////////////////////
163t_irc bncSocket::request(const QUrl& mountPoint, const QByteArray& latitude,
164 const QByteArray& longitude, const QByteArray& nmea,
165 const QByteArray& ntripVersion,
166 int timeOut, QString& msg) {
167
168 if (ntripVersion == "AUTO") {
169 emit newMessage("NTRIP Version AUTO not yet implemented", "true");
170 return failure;
171 }
172 else if (ntripVersion == "2") {
173 return request2(mountPoint, latitude, longitude, nmea, timeOut, msg);
174 }
175 else if (ntripVersion != "1") {
176 emit newMessage("Unknown NTRIP Version " + ntripVersion, "true");
177 return failure;
178 }
179
180 delete _socket;
181 _socket = new QTcpSocket();
182
183 // Connect the Socket
184 // ------------------
185 QSettings settings;
186 QString proxyHost = settings.value("proxyHost").toString();
187 int proxyPort = settings.value("proxyPort").toInt();
188
189 if ( proxyHost.isEmpty() ) {
190 _socket->connectToHost(mountPoint.host(), mountPoint.port());
191 }
192 else {
193 _socket->connectToHost(proxyHost, proxyPort);
194 }
195 if (!_socket->waitForConnected(timeOut)) {
196 msg += "Connect timeout\n";
197 delete _socket;
198 _socket = 0;
199 return failure;
200 }
201
202 // Send Request
203 // ------------
204 QString uName = QUrl::fromPercentEncoding(mountPoint.userName().toAscii());
205 QString passW = QUrl::fromPercentEncoding(mountPoint.password().toAscii());
206 QByteArray userAndPwd;
207
208 if(!uName.isEmpty() || !passW.isEmpty())
209 {
210 userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
211 passW.toAscii()).toBase64() + "\r\n";
212 }
213
214 QUrl hlp;
215 hlp.setScheme("http");
216 hlp.setHost(mountPoint.host());
217 hlp.setPort(mountPoint.port());
218 hlp.setPath(mountPoint.path());
219
220 QByteArray reqStr;
221 if ( proxyHost.isEmpty() ) {
222 if (hlp.path().indexOf("/") != 0) hlp.setPath("/");
223 reqStr = "GET " + hlp.path().toAscii() + " HTTP/1.0\r\n"
224 + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
225 + userAndPwd + "\r\n";
226 } else {
227 reqStr = "GET " + hlp.toEncoded() + " HTTP/1.0\r\n"
228 + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
229 + "Host: " + hlp.host().toAscii() + "\r\n"
230 + userAndPwd + "\r\n";
231 }
232
233 // NMEA string to handle VRS stream
234 // --------------------------------
235 double lat, lon;
236
237 lat = strtod(latitude,NULL);
238 lon = strtod(longitude,NULL);
239
240 if ((nmea == "yes") && (hlp.path().length() > 2) && (hlp.path().indexOf(".skl") < 0)) {
241 const char* flagN="N";
242 const char* flagE="E";
243 if (lon >180.) {lon=(lon-360.)*(-1.); flagE="W";}
244 if ((lon < 0.) && (lon >= -180.)) {lon=lon*(-1.); flagE="W";}
245 if (lon < -180.) {lon=(lon+360.); flagE="E";}
246 if (lat < 0.) {lat=lat*(-1.); flagN="S";}
247 QTime ttime(QDateTime::currentDateTime().toUTC().time());
248 int lat_deg = (int)lat;
249 double lat_min=(lat-lat_deg)*60.;
250 int lon_deg = (int)lon;
251 double lon_min=(lon-lon_deg)*60.;
252 int hh = 0 , mm = 0;
253 double ss = 0.0;
254 hh=ttime.hour();
255 mm=ttime.minute();
256 ss=(double)ttime.second()+0.001*ttime.msec();
257 QString gga;
258 gga += "GPGGA,";
259 gga += QString("%1%2%3,").arg((int)hh, 2, 10, QLatin1Char('0')).arg((int)mm, 2, 10, QLatin1Char('0')).arg((int)ss, 2, 10, QLatin1Char('0'));
260 gga += QString("%1%2,").arg((int)lat_deg,2, 10, QLatin1Char('0')).arg(lat_min, 7, 'f', 4, QLatin1Char('0'));
261 gga += flagN;
262 gga += QString(",%1%2,").arg((int)lon_deg,3, 10, QLatin1Char('0')).arg(lon_min, 7, 'f', 4, QLatin1Char('0'));
263 gga += flagE + QString(",1,05,1.00,+00100,M,10.000,M,,");
264 int xori;
265 char XOR = 0;
266 char *Buff =gga.toAscii().data();
267 int iLen = strlen(Buff);
268 for (xori = 0; xori < iLen; xori++) {
269 XOR ^= (char)Buff[xori];
270 }
271 gga += QString("*%1").arg(XOR, 2, 16, QLatin1Char('0'));
272 reqStr += "$";
273 reqStr += gga;
274 reqStr += "\r\n";
275 }
276
277 msg += reqStr;
278
279 _socket->write(reqStr, reqStr.length());
280
281 if (!_socket->waitForBytesWritten(timeOut)) {
282 msg += "Write timeout\n";
283 delete _socket;
284 _socket = 0;
285 return failure;
286 }
287
288 return success;
289}
290
291//
292////////////////////////////////////////////////////////////////////////////
293void bncSocket::slotRequestFinished(int id, bool error) {
294 cout << "slotRequestFinished " << id << " " << error << endl;
295 this->deleteLater();
296}
297
298//
299////////////////////////////////////////////////////////////////////////////
300void bncSocket::slotReadyRead(const QHttpResponseHeader&) {
301 cout << "slotReadyRead" << endl;
302}
303
304//
305////////////////////////////////////////////////////////////////////////////
306void bncSocket::slotSslErrors(const QList<QSslError>&) {
307 cout << "slotSslError" << endl;
308}
309
310//
311////////////////////////////////////////////////////////////////////////////
312void bncSocket::slotDone(bool error) {
313 cout << "slotDone " << error << endl;
314}
315
316// Connect to Caster NTRIP Version 2
317////////////////////////////////////////////////////////////////////////////
318t_irc bncSocket::request2(const QUrl& url, const QByteArray& latitude,
319 const QByteArray& longitude, const QByteArray& nmea,
320 int timeOut, QString& msg) {
321
322 delete _socket;
323 _socket = new QTcpSocket();
324
325 delete _http;
326 _http = new QHttp();
327
328 _http->setSocket(_socket);
329
330 // Network Request
331 // ---------------
332 QHttpRequestHeader request("GET", url.path());
333 request.addValue("Host" , url.host().toAscii());
334 request.addValue("Ntrip-Version", "NTRIP/2.0");
335 request.addValue("User-Agent" , "NTRIP BNC/1.7");
336 if (!url.userName().isEmpty()) {
337 request.addValue("Authorization", "Basic " +
338 (url.userName() + ":" + url.password()).toAscii().toBase64());
339 }
340 request.addValue("Connection" , "close");
341
342
343 connect(_http, SIGNAL(done(bool)), this, SLOT(slotDone(bool)));
344 connect(_http, SIGNAL(requestFinished(int, bool)),
345 this, SLOT(slotRequestFinished(int, bool)));
346 connect(_http, SIGNAL(readyRead(const QHttpResponseHeader&)),
347 this, SLOT(slotReadyRead(const QHttpResponseHeader&)));
348 connect(_http, SIGNAL(sslErrors(const QList<QSslError>&)),
349 this, SLOT(slotSslErrors(const QList<QSslError>&)));
350
351 return success;
352}
Note: See TracBrowser for help on using the repository browser.