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

Last change on this file since 1348 was 1348, checked in by mervart, 16 years ago

* empty log message *

File size: 6.9 KB
Line 
1/* -------------------------------------------------------------------------
2 * BKG NTRIP Client
3 * -------------------------------------------------------------------------
4 *
5 * Class: bncSocket
6 *
7 * Purpose: Subclass QIODevice (QTcpSocket, QNetworkReply)
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
22using namespace std;
23
24#define BNCVERSION "1.7"
25
26// Constructor
27////////////////////////////////////////////////////////////////////////////
28bncSocket::bncSocket() {
29 _socket = 0;
30}
31
32// Destructor
33////////////////////////////////////////////////////////////////////////////
34bncSocket::~bncSocket() {
35 delete _socket;
36}
37
38//
39////////////////////////////////////////////////////////////////////////////
40void bncSocket::connectToHost(const QString &hostName, quint16 port,
41 QIODevice::OpenMode mode) {
42 if (_socket) {
43 _socket->connectToHost(hostName, port, mode);
44 }
45}
46
47//
48////////////////////////////////////////////////////////////////////////////
49bool bncSocket::waitForConnected(int msecs) {
50 if (_socket) {
51 return _socket->waitForConnected(msecs);
52 }
53 else {
54 return false;
55 }
56}
57
58//
59////////////////////////////////////////////////////////////////////////////
60QAbstractSocket::SocketState bncSocket::state() const {
61 if (_socket) {
62 return _socket->state();
63 }
64 else {
65 return QAbstractSocket::UnconnectedState;
66 }
67}
68
69//
70////////////////////////////////////////////////////////////////////////////
71void bncSocket::close() {
72 if (_socket) {
73 _socket->close();
74 }
75}
76
77//
78////////////////////////////////////////////////////////////////////////////
79qint64 bncSocket::bytesAvailable() const {
80 if (_socket) {
81 return _socket->bytesAvailable();
82 }
83 else {
84 return 0;
85 }
86}
87
88//
89////////////////////////////////////////////////////////////////////////////
90bool bncSocket::canReadLine() const {
91 if (_socket) {
92 return _socket->canReadLine();
93 }
94 else {
95 return false;
96 }
97}
98
99//
100////////////////////////////////////////////////////////////////////////////
101QByteArray bncSocket::readLine(qint64 maxlen) {
102 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//
133////////////////////////////////////////////////////////////////////////////
134qint64 bncSocket::write(const char* data, qint64 len) {
135 if (_socket) {
136 return _socket->write(data, len);
137 }
138 else {
139 return -1;
140 }
141}
142
143//
144////////////////////////////////////////////////////////////////////////////
145bool bncSocket::waitForBytesWritten(int msecs) {
146 if (_socket) {
147 return _socket->waitForBytesWritten(msecs);
148 }
149 else {
150 return false;
151 }
152}
153
154// Connect to Caster, send the Request (static)
155////////////////////////////////////////////////////////////////////////////
156t_irc bncSocket::request(const QUrl& mountPoint, const QByteArray& latitude,
157 const QByteArray& longitude, const QByteArray& nmea,
158 int timeOut, QString& msg) {
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
Note: See TracBrowser for help on using the repository browser.