[1718] | 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>
|
---|
[1761] | 19 | #include <time.h>
|
---|
[1718] | 20 |
|
---|
| 21 | #include "bncnetqueryudp.h"
|
---|
| 22 | #include "bncsettings.h"
|
---|
| 23 |
|
---|
| 24 | using namespace std;
|
---|
| 25 |
|
---|
| 26 | #define BNCVERSION "1.7"
|
---|
[1761] | 27 | #define TIME_RESOLUTION 125
|
---|
[1718] | 28 |
|
---|
| 29 | // Constructor
|
---|
| 30 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 31 | bncNetQueryUdp::bncNetQueryUdp() {
|
---|
[1719] | 32 | _port = 0;
|
---|
[1718] | 33 | _udpSocket = 0;
|
---|
| 34 | _eventLoop = new QEventLoop(this);
|
---|
[1756] | 35 |
|
---|
[1761] | 36 | _keepAlive[ 0] = 128;
|
---|
| 37 | _keepAlive[ 1] = 96;
|
---|
| 38 | for (int ii = 2; ii <=11; ii++) {
|
---|
| 39 | _keepAlive[ii] = 0;
|
---|
[1756] | 40 | }
|
---|
[1718] | 41 | }
|
---|
| 42 |
|
---|
| 43 | // Destructor
|
---|
| 44 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 45 | bncNetQueryUdp::~bncNetQueryUdp() {
|
---|
| 46 | delete _eventLoop;
|
---|
| 47 | delete _udpSocket;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | //
|
---|
| 51 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 52 | void bncNetQueryUdp::stop() {
|
---|
| 53 | _eventLoop->quit();
|
---|
| 54 | _status = finished;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | //
|
---|
| 58 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 59 | void bncNetQueryUdp::slotKeepAlive() {
|
---|
[1756] | 60 | if (_udpSocket) {
|
---|
| 61 | _udpSocket->writeDatagram(_keepAlive, 12, _address, _port);
|
---|
| 62 | }
|
---|
[1761] | 63 | QTimer::singleShot(15000, this, SLOT(slotKeepAlive()));
|
---|
[1718] | 64 | }
|
---|
| 65 |
|
---|
| 66 | //
|
---|
| 67 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 68 | void bncNetQueryUdp::waitForRequestResult(const QUrl&, QByteArray&) {
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | //
|
---|
| 72 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 73 | void 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 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 94 | void bncNetQueryUdp::startRequest(const QUrl& url, const QByteArray& gga) {
|
---|
| 95 |
|
---|
| 96 | _status = running;
|
---|
| 97 |
|
---|
[1719] | 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 |
|
---|
[1718] | 110 | delete _udpSocket;
|
---|
| 111 | _udpSocket = new QUdpSocket();
|
---|
[1723] | 112 | _udpSocket->bind(0);
|
---|
[1718] | 113 | connect(_udpSocket, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
|
---|
| 114 |
|
---|
| 115 | QHostInfo hInfo = QHostInfo::fromName(url.host());
|
---|
| 116 |
|
---|
| 117 | if (!hInfo.addresses().isEmpty()) {
|
---|
[1719] | 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 |
|
---|
[1724] | 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"
|
---|
[1732] | 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";
|
---|
[1719] | 140 |
|
---|
[1729] | 141 | char rtpbuffer[12 + reqStr.size()];
|
---|
| 142 | rtpbuffer[0] = 128;
|
---|
| 143 | rtpbuffer[1] = 97;
|
---|
[1730] | 144 | for (int jj = 2; jj <= 11; jj++) {
|
---|
[1758] | 145 | rtpbuffer[jj] = _keepAlive[jj];
|
---|
[1730] | 146 | }
|
---|
[1729] | 147 | for (int ii = 0; ii < reqStr.size(); ii++) {
|
---|
| 148 | rtpbuffer[12+ii] = reqStr[ii];
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[1730] | 151 | _udpSocket->writeDatagram(rtpbuffer, 12 + reqStr.size(), _address, _port);
|
---|
[1761] | 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) {
|
---|
[1818] | 162 | _session = line.mid(9).toUInt();
|
---|
[1761] | 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()));
|
---|
[1718] | 173 | }
|
---|
| 174 | }
|
---|
| 175 |
|
---|