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

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

* empty log message *

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