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

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

* empty log message *

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