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

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

* empty log message *

File size: 4.8 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 outData.append(datagram);
70}
71
72// Connect to Caster, send the Request
73////////////////////////////////////////////////////////////////////////////
74void bncNetQueryRtp::startRequest(const QUrl& url, const QByteArray& gga) {
75
76 const int timeOut = 5000;
77
78 _status = running;
79
80 delete _socket;
81 _socket = new QTcpSocket();
82
83 // Default scheme
84 // --------------
85 QUrl urlLoc(url);
86 urlLoc.setScheme("rtsp");
87
88 // Connect the Socket
89 // ------------------
90 QSettings settings;
91 QString proxyHost = settings.value("proxyHost").toString();
92 int proxyPort = settings.value("proxyPort").toInt();
93
94 if ( proxyHost.isEmpty() ) {
95 _socket->connectToHost(urlLoc.host(), urlLoc.port());
96 }
97 else {
98 _socket->connectToHost(proxyHost, proxyPort);
99 }
100
101 // Send Request 1
102 // --------------
103 if (_socket->waitForConnected(timeOut)) {
104 QString uName = QUrl::fromPercentEncoding(urlLoc.userName().toAscii());
105 QString passW = QUrl::fromPercentEncoding(urlLoc.password().toAscii());
106 QByteArray userAndPwd;
107
108 if(!uName.isEmpty() || !passW.isEmpty()) {
109 userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
110 passW.toAscii()).toBase64() + "\r\n";
111 }
112
113 QByteArray clientPort = "7777"; // TODO: make it an option
114 delete _udpSocket;
115 _udpSocket = new QUdpSocket();
116 _udpSocket->bind(clientPort.toInt());
117 connect(_udpSocket, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
118
119 QByteArray reqStr;
120 reqStr = "SETUP " + urlLoc.toEncoded() + " RTSP/1.0\r\n"
121 + "Cseq: 1\r\n"
122 + "Ntrip-Version: Ntrip/2.0\r\n"
123 + "Ntrip-Component: Ntripclient\r\n"
124 + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
125 + "Transport: RTP/GNSS;unicast;client_port=" + clientPort + "\r\n"
126 + userAndPwd
127 + "\r\n";
128 _socket->write(reqStr, reqStr.length());
129
130 // Read Server Answer 1
131 // --------------------
132 if (_socket->waitForBytesWritten(timeOut)) {
133 if (_socket->waitForReadyRead(timeOut)) {
134 QTextStream in(_socket);
135 QByteArray session;
136 QString line = in.readLine();
137 while (!line.isEmpty()) {
138 if (line.indexOf("Session:") == 0) {
139 session = line.mid(9).toAscii();
140 break;
141 }
142 line = in.readLine();
143 }
144
145 // Send Request 2
146 // --------------
147 if (!session.isEmpty()) {
148 reqStr = "PLAY " + urlLoc.toEncoded() + " RTSP/1.0\r\n"
149 + "Cseq: 2\r\n"
150 + "Session: " + session + "\r\n"
151 + "\r\n";
152 _socket->write(reqStr, reqStr.length());
153
154 // Read Server Answer 2
155 // --------------------
156 if (_socket->waitForBytesWritten(timeOut)) {
157 if (_socket->waitForReadyRead(timeOut)) {
158 QTextStream in(_socket);
159 line = in.readLine();
160 while (!line.isEmpty()) {
161 if (line.indexOf("200 OK") != -1) {
162 cout << "Connection Established" << endl;
163 return;
164 }
165 line = in.readLine();
166 }
167 }
168 }
169 }
170 }
171 }
172 }
173
174 delete _socket;
175 _socket = 0;
176 _status = error;
177}
178
Note: See TracBrowser for help on using the repository browser.