source: ntrip/trunk/BNC/bncnetqueryrtp.cpp@ 1415

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

* empty log message *

File size: 4.9 KB
Line 
1/* -------------------------------------------------------------------------
2 * BKG NTRIP Client
3 * -------------------------------------------------------------------------
4 *
5 * Class: bncNetQueryRtp
6 *
7 * Purpose: Blocking Network Requests (NTRIP Version 2 with RTSP)
8 *
9 * Author: L. Mervart
10 *
11 * Created: 27-Dec-2008
12 *
13 * Changes:
14 *
15 * -----------------------------------------------------------------------*/
16
17#include <iostream>
18#include <iomanip>
19
20#include "bncnetqueryrtp.h"
21
22using namespace std;
23
24#define BNCVERSION "1.7"
25
26// Constructor
27////////////////////////////////////////////////////////////////////////////
28bncNetQueryRtp::bncNetQueryRtp() {
29 _socket = 0;
30 _udpSocket = 0;
31 _eventLoop = new QEventLoop(this);
32}
33
34// Destructor
35////////////////////////////////////////////////////////////////////////////
36bncNetQueryRtp::~bncNetQueryRtp() {
37 delete _eventLoop;
38 delete _socket;
39 delete _udpSocket;
40}
41
42//
43////////////////////////////////////////////////////////////////////////////
44void bncNetQueryRtp::stop() {
45 _eventLoop->quit();
46 _status = finished;
47}
48
49//
50////////////////////////////////////////////////////////////////////////////
51void bncNetQueryRtp::waitForRequestResult(const QUrl&, QByteArray&) {
52}
53
54//
55////////////////////////////////////////////////////////////////////////////
56void bncNetQueryRtp::waitForReadyRead(QByteArray& outData) {
57
58 // Wait Loop
59 // ---------
60 if (!_udpSocket->hasPendingDatagrams()) {
61 _eventLoop->exec();
62 }
63
64 // Append Data
65 // -----------
66 QByteArray datagram;
67 datagram.resize(_udpSocket->pendingDatagramSize());
68 _udpSocket->readDatagram(datagram.data(), datagram.size());
69
70 cout << "Read Datagram: size = " << datagram.size() << endl;
71
72 outData.append(datagram);
73}
74
75// Connect to Caster, send the Request
76////////////////////////////////////////////////////////////////////////////
77void bncNetQueryRtp::startRequest(const QUrl& url, const QByteArray& gga) {
78
79 const int timeOut = 5000;
80
81 _status = running;
82
83 delete _socket;
84 _socket = new QTcpSocket();
85
86 // Default scheme
87 // --------------
88 QUrl urlLoc(url);
89 urlLoc.setScheme("rtsp");
90
91 // Connect the Socket
92 // ------------------
93 QSettings settings;
94 QString proxyHost = settings.value("proxyHost").toString();
95 int proxyPort = settings.value("proxyPort").toInt();
96
97 if ( proxyHost.isEmpty() ) {
98 _socket->connectToHost(urlLoc.host(), urlLoc.port());
99 }
100 else {
101 _socket->connectToHost(proxyHost, proxyPort);
102 }
103
104 // Send Request 1
105 // --------------
106 if (_socket->waitForConnected(timeOut)) {
107 QString uName = QUrl::fromPercentEncoding(urlLoc.userName().toAscii());
108 QString passW = QUrl::fromPercentEncoding(urlLoc.password().toAscii());
109 QByteArray userAndPwd;
110
111 if(!uName.isEmpty() || !passW.isEmpty()) {
112 userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
113 passW.toAscii()).toBase64() + "\r\n";
114 }
115
116 QByteArray clientPort = "7777"; // TODO: make it an option
117 delete _udpSocket;
118 _udpSocket = new QUdpSocket();
119 _udpSocket->bind(clientPort.toInt());
120 connect(_udpSocket, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
121
122 QByteArray reqStr;
123 reqStr = "SETUP " + urlLoc.toEncoded() + " RTSP/1.0\r\n"
124 + "Cseq: 1\r\n"
125 + "Ntrip-Version: Ntrip/2.0\r\n"
126 + "Ntrip-Component: Ntripclient\r\n"
127 + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
128 + "Transport: RTP/GNSS;unicast;client_port=" + clientPort + "\r\n"
129 + userAndPwd
130 + "\r\n";
131 _socket->write(reqStr, reqStr.length());
132
133 // Read Server Answer 1
134 // --------------------
135 if (_socket->waitForBytesWritten(timeOut)) {
136 if (_socket->waitForReadyRead(timeOut)) {
137 QTextStream in(_socket);
138 QByteArray session;
139 QString line = in.readLine();
140 while (!line.isEmpty()) {
141 if (line.indexOf("Session:") == 0) {
142 session = line.mid(9).toAscii();
143 break;
144 }
145 line = in.readLine();
146 }
147
148 // Send Request 2
149 // --------------
150 if (!session.isEmpty()) {
151 reqStr = "PLAY " + urlLoc.toEncoded() + " RTSP/1.0\r\n"
152 + "Cseq: 2\r\n"
153 + "Session: " + session + "\r\n"
154 + "\r\n";
155 _socket->write(reqStr, reqStr.length());
156
157 // Read Server Answer 2
158 // --------------------
159 if (_socket->waitForBytesWritten(timeOut)) {
160 if (_socket->waitForReadyRead(timeOut)) {
161 QTextStream in(_socket);
162 line = in.readLine();
163 while (!line.isEmpty()) {
164 if (line.indexOf("200 OK") != -1) {
165 cout << "Connection Established" << endl;
166 return;
167 }
168 line = in.readLine();
169 }
170 }
171 }
172 }
173 }
174 }
175 }
176
177 delete _socket;
178 _socket = 0;
179 _status = error;
180}
181
Note: See TracBrowser for help on using the repository browser.