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

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

* empty log message *

File size: 4.4 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}
31
32// Destructor
33////////////////////////////////////////////////////////////////////////////
34bncNetQueryRtp::~bncNetQueryRtp() {
35 delete _socket;
36}
37
38//
39////////////////////////////////////////////////////////////////////////////
40void bncNetQueryRtp::stop() {
41#ifndef sparc
42 if (_socket) {
43 _socket->abort();
44 }
45#endif
46 _status = finished;
47}
48
49//
50////////////////////////////////////////////////////////////////////////////
51void bncNetQueryRtp::waitForRequestResult(const QUrl&, QByteArray&) {
52}
53
54//
55////////////////////////////////////////////////////////////////////////////
56void bncNetQueryRtp::waitForReadyRead(QByteArray& outData) {
57}
58
59// Connect to Caster, send the Request
60////////////////////////////////////////////////////////////////////////////
61void bncNetQueryRtp::startRequest(const QUrl& url, const QByteArray& gga) {
62
63 const int timeOut = 5000;
64
65 _status = running;
66
67 delete _socket;
68 _socket = new QTcpSocket();
69
70 // Default scheme
71 // --------------
72 QUrl urlLoc(url);
73 urlLoc.setScheme("rtsp");
74
75 // Connect the Socket
76 // ------------------
77 QSettings settings;
78 QString proxyHost = settings.value("proxyHost").toString();
79 int proxyPort = settings.value("proxyPort").toInt();
80
81 if ( proxyHost.isEmpty() ) {
82 _socket->connectToHost(urlLoc.host(), urlLoc.port());
83 }
84 else {
85 _socket->connectToHost(proxyHost, proxyPort);
86 }
87
88 // Send Request 1
89 // --------------
90 if (_socket->waitForConnected(timeOut)) {
91 QString uName = QUrl::fromPercentEncoding(urlLoc.userName().toAscii());
92 QString passW = QUrl::fromPercentEncoding(urlLoc.password().toAscii());
93 QByteArray userAndPwd;
94
95 if(!uName.isEmpty() || !passW.isEmpty()) {
96 userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
97 passW.toAscii()).toBase64() + "\r\n";
98 }
99
100 QByteArray clientPort = "7777"; // TODO: make it an option
101
102 QByteArray reqStr;
103 reqStr = "SETUP " + urlLoc.toEncoded() + " RTSP/1.0\r\n"
104 + "Cseq: 1\r\n"
105 + "Ntrip-Version: Ntrip/2.0\r\n"
106 + "Ntrip-Component: Ntripclient\r\n"
107 + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
108 + "Transport: RTP/GNSS;unicast;client_port=" + clientPort + "\r\n"
109 + userAndPwd
110 + "\r\n";
111 _socket->write(reqStr, reqStr.length());
112
113 // Read Server Answer 1
114 // --------------------
115 if (_socket->waitForBytesWritten(timeOut)) {
116 if (_socket->waitForReadyRead(timeOut)) {
117 QTextStream in(_socket);
118 QByteArray session;
119 QString line = in.readLine();
120 while (!line.isEmpty()) {
121 cout << line.toAscii().data() << endl;;
122 if (line.indexOf("Session:") == 0) {
123 session = line.mid(9).toAscii();
124 }
125 line = in.readLine();
126 }
127
128 // Send Request 2
129 // --------------
130 if (!session.isEmpty()) {
131 reqStr = "PLAY " + urlLoc.toEncoded() + " RTSP/1.0\r\n"
132 + "Cseq: 2\r\n"
133 + "Session: " + session + "\r\n"
134 + "\r\n";
135 _socket->write(reqStr, reqStr.length());
136
137 cout << reqStr.data();
138
139 // Read Server Answer 2
140 // --------------------
141 if (_socket->waitForBytesWritten(timeOut)) {
142 if (_socket->waitForReadyRead(timeOut)) {
143 QTextStream in(_socket);
144 line = in.readLine();
145 while (!line.isEmpty()) {
146 cout << line.toAscii().data() << endl;
147 if (line.indexOf("200 OK") != -1) {
148 cout << "Connection Established" << endl;
149 return;
150 }
151 line = in.readLine();
152 }
153 }
154 }
155 }
156 }
157 }
158 }
159
160 delete _socket;
161 _socket = 0;
162 _status = error;
163}
164
Note: See TracBrowser for help on using the repository browser.