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

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

* empty log message *

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