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

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

* empty log message *

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