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

Last change on this file since 1747 was 1732, checked in by mervart, 16 years ago

* empty log message *

File size: 3.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
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 }
78}
79
80// Connect to Caster, send the Request
81////////////////////////////////////////////////////////////////////////////
82void bncNetQueryUdp::startRequest(const QUrl& url, const QByteArray& gga) {
83
84 _status = running;
85
86 // Default scheme and path
87 // -----------------------
88 _url = url;
89 if (_url.scheme().isEmpty()) {
90 _url.setScheme("http");
91 }
92 if (_url.path().isEmpty()) {
93 _url.setPath("/");
94 }
95
96 _port = _url.port();
97
98 delete _udpSocket;
99 _udpSocket = new QUdpSocket();
100 _udpSocket->bind(0);
101 connect(_udpSocket, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
102
103 QHostInfo hInfo = QHostInfo::fromName(url.host());
104
105 if (!hInfo.addresses().isEmpty()) {
106
107 _address = hInfo.addresses().first();
108
109 // Send Request
110 // ------------
111 QString uName = QUrl::fromPercentEncoding(_url.userName().toAscii());
112 QString passW = QUrl::fromPercentEncoding(_url.password().toAscii());
113 QByteArray userAndPwd;
114
115 if(!uName.isEmpty() || !passW.isEmpty()) {
116 userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
117 passW.toAscii()).toBase64() + "\r\n";
118 }
119
120 QByteArray reqStr = "GET " + _url.path().toAscii() + " HTTP/1.1\r\n"
121 + "Host: " + _url.host().toAscii() + "\r\n"
122 + "Ntrip-Version: Ntrip/2.0\r\n"
123 + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n";
124 if (!gga.isEmpty()) {
125 reqStr += "Ntrip-GGA: " + gga + "\r\n";
126 }
127 reqStr += userAndPwd + "Connection: close\r\n\r\n";
128
129 char rtpbuffer[12 + reqStr.size()];
130 rtpbuffer[0] = 128;
131 rtpbuffer[1] = 97;
132 for (int jj = 2; jj <= 11; jj++) {
133 rtpbuffer[jj] = 0;
134 }
135 for (int ii = 0; ii < reqStr.size(); ii++) {
136 rtpbuffer[12+ii] = reqStr[ii];
137 }
138
139 _udpSocket->writeDatagram(rtpbuffer, 12 + reqStr.size(), _address, _port);
140 }
141}
142
Note: See TracBrowser for help on using the repository browser.