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

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

* empty log message *

File size: 11.0 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#if QT_VERSION >= 0x040400
35 _manager = 0;
36 _reply = 0;
37#endif
38}
39
40// Destructor
41////////////////////////////////////////////////////////////////////////////
42bncSocket::~bncSocket() {
43 delete _socket;
44#if QT_VERSION >= 0x040400
45 _reply->deleteLater();
46 _manager->deleteLater();
47#endif
48}
49
50//
51////////////////////////////////////////////////////////////////////////////
52void bncSocket::connectToHost(const QString &hostName, quint16 port,
53 QIODevice::OpenMode mode) {
54#if QT_VERSION >= 0x040400
55 if (_reply) {
56 }
57 else
58#endif
59 if (_socket) {
60 _socket->connectToHost(hostName, port, mode);
61 }
62}
63
64//
65////////////////////////////////////////////////////////////////////////////
66bool bncSocket::waitForConnected(int msecs) {
67#if QT_VERSION >= 0x040400
68 if (_reply) {
69 return true;
70 }
71 else
72#endif
73 if (_socket) {
74 return _socket->waitForConnected(msecs);
75 }
76 else {
77 return false;
78 }
79}
80
81//
82////////////////////////////////////////////////////////////////////////////
83QAbstractSocket::SocketState bncSocket::state() const {
84#if QT_VERSION >= 0x040400
85 if (_reply) {
86 return QAbstractSocket::ConnectedState;
87 }
88 else
89#endif
90 if (_socket) {
91 return _socket->state();
92 }
93 else {
94 return QAbstractSocket::UnconnectedState;
95 }
96}
97
98//
99////////////////////////////////////////////////////////////////////////////
100void bncSocket::close() {
101#if QT_VERSION >= 0x040400
102 if (_reply) {
103 _reply->close();
104 }
105 else
106#endif
107 if (_socket) {
108 _socket->close();
109 }
110}
111
112//
113////////////////////////////////////////////////////////////////////////////
114qint64 bncSocket::bytesAvailable() const {
115#if QT_VERSION >= 0x040400
116 if (_reply) {
117 return _reply->bytesAvailable();
118 }
119 else
120#endif
121 if (_socket) {
122 return _socket->bytesAvailable();
123 }
124 else {
125 return 0;
126 }
127}
128
129//
130////////////////////////////////////////////////////////////////////////////
131bool bncSocket::canReadLine() const {
132#if QT_VERSION >= 0x040400
133 if (_reply) {
134 return _reply->canReadLine();
135 }
136 else
137#endif
138 if (_socket) {
139 return _socket->canReadLine();
140 }
141 else {
142 return false;
143 }
144}
145
146//
147////////////////////////////////////////////////////////////////////////////
148QByteArray bncSocket::readLine(qint64 maxlen) {
149#if QT_VERSION >= 0x040400
150 if (_reply) {
151 return _reply->readLine(maxlen);
152 }
153 else
154#endif
155 if (_socket) {
156 return _socket->readLine(maxlen);
157 }
158 else {
159 return "";
160 }
161}
162
163//
164////////////////////////////////////////////////////////////////////////////
165bool bncSocket::waitForReadyRead(int msecs) {
166#if QT_VERSION >= 0x040400
167 if (_reply) {
168 return _reply->waitForReadyRead(msecs);
169 }
170 else
171#endif
172 if (_socket) {
173 return _socket->waitForReadyRead(msecs);
174 }
175 else {
176 return false;
177 }
178}
179
180//
181////////////////////////////////////////////////////////////////////////////
182qint64 bncSocket::read(char* data, qint64 maxlen) {
183#if QT_VERSION >= 0x040400
184 if (_reply) {
185 return _reply->read(data, maxlen);
186 }
187 else
188#endif
189 if (_socket) {
190 return _socket->read(data, maxlen);
191 }
192 else {
193 return -1;
194 }
195}
196
197//
198////////////////////////////////////////////////////////////////////////////
199qint64 bncSocket::write(const char* data, qint64 len) {
200#if QT_VERSION >= 0x040400
201 if (_reply) {
202 return -1;
203 }
204 else
205#endif
206 if (_socket) {
207 return _socket->write(data, len);
208 }
209 else {
210 return -1;
211 }
212}
213
214//
215////////////////////////////////////////////////////////////////////////////
216bool bncSocket::waitForBytesWritten(int msecs) {
217#if QT_VERSION >= 0x040400
218 if (_reply) {
219 return _reply->waitForBytesWritten(msecs);
220 }
221 else
222#endif
223 if (_socket) {
224 return _socket->waitForBytesWritten(msecs);
225 }
226 else {
227 return false;
228 }
229}
230
231// Connect to Caster, send the Request
232////////////////////////////////////////////////////////////////////////////
233t_irc bncSocket::request(const QUrl& mountPoint, const QByteArray& latitude,
234 const QByteArray& longitude, const QByteArray& nmea,
235 const QByteArray& ntripVersion,
236 int timeOut, QString& msg) {
237
238 if (ntripVersion == "AUTO") {
239 emit newMessage("NTRIP Version AUTO not yet implemented", "true");
240 return failure;
241 }
242 else if (ntripVersion == "2") {
243 return request2(mountPoint, latitude, longitude, nmea, timeOut, msg);
244 }
245 else if (ntripVersion != "1") {
246 emit newMessage("Unknown NTRIP Version " + ntripVersion, "true");
247 return failure;
248 }
249
250 delete _socket;
251 _socket = new QTcpSocket();
252
253 // Connect the Socket
254 // ------------------
255 QSettings settings;
256 QString proxyHost = settings.value("proxyHost").toString();
257 int proxyPort = settings.value("proxyPort").toInt();
258
259 if ( proxyHost.isEmpty() ) {
260 _socket->connectToHost(mountPoint.host(), mountPoint.port());
261 }
262 else {
263 _socket->connectToHost(proxyHost, proxyPort);
264 }
265 if (!_socket->waitForConnected(timeOut)) {
266 msg += "Connect timeout\n";
267 delete _socket;
268 _socket = 0;
269 return failure;
270 }
271
272 // Send Request
273 // ------------
274 QString uName = QUrl::fromPercentEncoding(mountPoint.userName().toAscii());
275 QString passW = QUrl::fromPercentEncoding(mountPoint.password().toAscii());
276 QByteArray userAndPwd;
277
278 if(!uName.isEmpty() || !passW.isEmpty())
279 {
280 userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
281 passW.toAscii()).toBase64() + "\r\n";
282 }
283
284 QUrl hlp;
285 hlp.setScheme("http");
286 hlp.setHost(mountPoint.host());
287 hlp.setPort(mountPoint.port());
288 hlp.setPath(mountPoint.path());
289
290 QByteArray reqStr;
291 if ( proxyHost.isEmpty() ) {
292 if (hlp.path().indexOf("/") != 0) hlp.setPath("/");
293 reqStr = "GET " + hlp.path().toAscii() + " HTTP/1.0\r\n"
294 + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
295 + userAndPwd + "\r\n";
296 } else {
297 reqStr = "GET " + hlp.toEncoded() + " HTTP/1.0\r\n"
298 + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
299 + "Host: " + hlp.host().toAscii() + "\r\n"
300 + userAndPwd + "\r\n";
301 }
302
303 // NMEA string to handle VRS stream
304 // --------------------------------
305 double lat, lon;
306
307 lat = strtod(latitude,NULL);
308 lon = strtod(longitude,NULL);
309
310 if ((nmea == "yes") && (hlp.path().length() > 2) && (hlp.path().indexOf(".skl") < 0)) {
311 const char* flagN="N";
312 const char* flagE="E";
313 if (lon >180.) {lon=(lon-360.)*(-1.); flagE="W";}
314 if ((lon < 0.) && (lon >= -180.)) {lon=lon*(-1.); flagE="W";}
315 if (lon < -180.) {lon=(lon+360.); flagE="E";}
316 if (lat < 0.) {lat=lat*(-1.); flagN="S";}
317 QTime ttime(QDateTime::currentDateTime().toUTC().time());
318 int lat_deg = (int)lat;
319 double lat_min=(lat-lat_deg)*60.;
320 int lon_deg = (int)lon;
321 double lon_min=(lon-lon_deg)*60.;
322 int hh = 0 , mm = 0;
323 double ss = 0.0;
324 hh=ttime.hour();
325 mm=ttime.minute();
326 ss=(double)ttime.second()+0.001*ttime.msec();
327 QString gga;
328 gga += "GPGGA,";
329 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'));
330 gga += QString("%1%2,").arg((int)lat_deg,2, 10, QLatin1Char('0')).arg(lat_min, 7, 'f', 4, QLatin1Char('0'));
331 gga += flagN;
332 gga += QString(",%1%2,").arg((int)lon_deg,3, 10, QLatin1Char('0')).arg(lon_min, 7, 'f', 4, QLatin1Char('0'));
333 gga += flagE + QString(",1,05,1.00,+00100,M,10.000,M,,");
334 int xori;
335 char XOR = 0;
336 char *Buff =gga.toAscii().data();
337 int iLen = strlen(Buff);
338 for (xori = 0; xori < iLen; xori++) {
339 XOR ^= (char)Buff[xori];
340 }
341 gga += QString("*%1").arg(XOR, 2, 16, QLatin1Char('0'));
342 reqStr += "$";
343 reqStr += gga;
344 reqStr += "\r\n";
345 }
346
347 msg += reqStr;
348
349 _socket->write(reqStr, reqStr.length());
350
351 if (!_socket->waitForBytesWritten(timeOut)) {
352 msg += "Write timeout\n";
353 delete _socket;
354 _socket = 0;
355 return failure;
356 }
357
358 return success;
359}
360
361//
362////////////////////////////////////////////////////////////////////////////
363#if QT_VERSION >= 0x040400
364void bncSocket::slotReplyFinished() {
365 cout << "slotReplyFinished" << endl;
366 this->deleteLater();
367}
368#endif
369
370//
371////////////////////////////////////////////////////////////////////////////
372#if QT_VERSION >= 0x040400
373void bncSocket::slotReadyRead() {
374 cout << "slotReadyRead" << endl;
375}
376#endif
377
378//
379////////////////////////////////////////////////////////////////////////////
380#if QT_VERSION >= 0x040400
381void bncSocket::slotError(QNetworkReply::NetworkError) {
382 cout << "slotError " << _reply->error() << endl
383 << _reply->errorString().toAscii().data() << endl;
384}
385#endif
386
387//
388////////////////////////////////////////////////////////////////////////////
389#if QT_VERSION >= 0x040400
390void bncSocket::slotSslErrors(const QList<QSslError>&) {
391 cout << "slotSslError" << endl;
392}
393#endif
394
395// Connect to Caster NTRIP Version 2
396////////////////////////////////////////////////////////////////////////////
397t_irc bncSocket::request2(const QUrl& url, const QByteArray& latitude,
398 const QByteArray& longitude, const QByteArray& nmea,
399 int timeOut, QString& msg) {
400#if QT_VERSION < 0x040400
401 emit newMessage("NTRIP v2 requires Qt Version 4.4 or higher", "true");
402 return failure;
403#else
404 // Network Access Manager
405 // ----------------------
406 if (_manager == 0) {
407 _manager = new QNetworkAccessManager(this);
408 }
409 else {
410 return failure;
411 }
412
413 // Network Request
414 // ---------------
415 QNetworkRequest request;
416 request.setUrl(url);
417 request.setRawHeader("Host" , url.host().toAscii());
418 request.setRawHeader("Ntrip-Version", "NTRIP/2.0");
419 request.setRawHeader("User-Agent" , "NTRIP BNC/1.7");
420 if (!url.userName().isEmpty()) {
421 request.setRawHeader("Authorization", "Basic " +
422 (url.userName() + ":" + url.password()).toAscii().toBase64());
423 }
424 request.setRawHeader("Connection" , "close");
425
426 _reply = _manager->get(request);
427
428 connect(_reply, SIGNAL(finished()), this, SLOT(slotReplyFinished()));
429 connect(_reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
430 connect(_reply, SIGNAL(error(QNetworkReply::NetworkError)),
431 this, SLOT(slotError(QNetworkReply::NetworkError)));
432 connect(_reply, SIGNAL(sslErrors(const QList<QSslError>&)),
433 this, SLOT(slotSslErrors(const QList<QSslError>&)));
434
435 return success;
436#endif
437}
Note: See TracBrowser for help on using the repository browser.