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

Last change on this file since 1362 was 1362, checked in by mervart, 17 years ago

* empty log message *

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