source: ntrip/trunk/BNC/bncnetqueryudp.cpp@ 1891

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

* empty log message *

File size: 4.6 KB
Line 
1/* -------------------------------------------------------------------------
2 * BKG NTRIP Client
3 * -------------------------------------------------------------------------
4 *
5 * Class: bncNetQueryUdp
6 *
7 * Purpose: Blocking Network Requests (NTRIP Version 2 with plain UDP)
8 *
9 * Author: L. Mervart
10 *
11 * Created: 04-Feb-2009
12 *
13 * Changes:
14 *
15 * -----------------------------------------------------------------------*/
16
17#include <iostream>
18#include <iomanip>
19#include <time.h>
20
21#include "bncnetqueryudp.h"
22#include "bncsettings.h"
23
24using namespace std;
25
26#define BNCVERSION "1.7"
27#define TIME_RESOLUTION 125
28
29// Constructor
30////////////////////////////////////////////////////////////////////////////
31bncNetQueryUdp::bncNetQueryUdp() {
32 _port = 0;
33 _udpSocket = 0;
34 _eventLoop = new QEventLoop(this);
35
36 _keepAlive[ 0] = 128;
37 _keepAlive[ 1] = 96;
38 for (int ii = 2; ii <=11; ii++) {
39 _keepAlive[ii] = 0;
40 }
41}
42
43// Destructor
44////////////////////////////////////////////////////////////////////////////
45bncNetQueryUdp::~bncNetQueryUdp() {
46 delete _eventLoop;
47 delete _udpSocket;
48}
49
50//
51////////////////////////////////////////////////////////////////////////////
52void bncNetQueryUdp::stop() {
53 _eventLoop->quit();
54 _status = finished;
55}
56
57//
58////////////////////////////////////////////////////////////////////////////
59void bncNetQueryUdp::slotKeepAlive() {
60 if (_udpSocket) {
61 _udpSocket->writeDatagram(_keepAlive, 12, _address, _port);
62 }
63 QTimer::singleShot(15000, this, SLOT(slotKeepAlive()));
64}
65
66//
67////////////////////////////////////////////////////////////////////////////
68void bncNetQueryUdp::waitForRequestResult(const QUrl&, QByteArray&) {
69}
70
71//
72////////////////////////////////////////////////////////////////////////////
73void bncNetQueryUdp::waitForReadyRead(QByteArray& outData) {
74
75 // Wait Loop
76 // ---------
77 if (!_udpSocket->hasPendingDatagrams()) {
78 _eventLoop->exec();
79 }
80
81 // Append Data
82 // -----------
83 QByteArray datagram;
84 datagram.resize(_udpSocket->pendingDatagramSize());
85 _udpSocket->readDatagram(datagram.data(), datagram.size());
86
87 if (datagram.size() > 12) {
88 outData.append(datagram.mid(12));
89 }
90}
91
92// Connect to Caster, send the Request
93////////////////////////////////////////////////////////////////////////////
94void bncNetQueryUdp::startRequest(const QUrl& url, const QByteArray& gga) {
95
96 _status = running;
97
98 // Default scheme and path
99 // -----------------------
100 _url = url;
101 if (_url.scheme().isEmpty()) {
102 _url.setScheme("http");
103 }
104 if (_url.path().isEmpty()) {
105 _url.setPath("/");
106 }
107
108 _port = _url.port();
109
110 delete _udpSocket;
111 _udpSocket = new QUdpSocket();
112 _udpSocket->bind(0);
113 connect(_udpSocket, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
114
115 QHostInfo hInfo = QHostInfo::fromName(url.host());
116
117 if (!hInfo.addresses().isEmpty()) {
118
119 _address = hInfo.addresses().first();
120
121 // Send Request
122 // ------------
123 QString uName = QUrl::fromPercentEncoding(_url.userName().toAscii());
124 QString passW = QUrl::fromPercentEncoding(_url.password().toAscii());
125 QByteArray userAndPwd;
126
127 if(!uName.isEmpty() || !passW.isEmpty()) {
128 userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
129 passW.toAscii()).toBase64() + "\r\n";
130 }
131
132 QByteArray reqStr = "GET " + _url.path().toAscii() + " HTTP/1.1\r\n"
133 + "Host: " + _url.host().toAscii() + "\r\n"
134 + "Ntrip-Version: Ntrip/2.0\r\n"
135 + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n";
136 if (!gga.isEmpty()) {
137 reqStr += "Ntrip-GGA: " + gga + "\r\n";
138 }
139 reqStr += userAndPwd + "Connection: close\r\n\r\n";
140
141 char rtpbuffer[12 + reqStr.size()];
142 rtpbuffer[0] = 128;
143 rtpbuffer[1] = 97;
144 for (int jj = 2; jj <= 11; jj++) {
145 rtpbuffer[jj] = _keepAlive[jj];
146 }
147 for (int ii = 0; ii < reqStr.size(); ii++) {
148 rtpbuffer[12+ii] = reqStr[ii];
149 }
150
151 _udpSocket->writeDatagram(rtpbuffer, 12 + reqStr.size(), _address, _port);
152
153 // Wait for Reply, read Session Number
154 // -----------------------------------
155 QByteArray repl;
156 waitForReadyRead(repl);
157
158 QTextStream in(repl);
159 QString line = in.readLine();
160 while (!line.isEmpty()) {
161 if (line.indexOf("Session:") == 0) {
162 _session = line.mid(9).toUInt();
163 _keepAlive[ 8] = (_session >> 24) & 0xFF;
164 _keepAlive[ 9] = (_session >> 16) & 0xFF;
165 _keepAlive[10] = (_session >> 8) & 0xFF;
166 _keepAlive[11] = (_session) & 0xFF;
167 break;
168 }
169 line = in.readLine();
170 }
171
172 QTimer::singleShot(15000, this, SLOT(slotKeepAlive()));
173 }
174}
175
Note: See TracBrowser for help on using the repository browser.