source: ntrip/trunk/BNC/src/bncnetqueryudp.cpp@ 8577

Last change on this file since 8577 was 8252, checked in by stoecker, 7 years ago

see #105 - reenable Qt4 build options, drop generic version dependend includes, replace by direct requirements, remaining QtCore lines should also be replaced

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