[1382] | 1 | /* -------------------------------------------------------------------------
|
---|
| 2 | * BKG NTRIP Client
|
---|
| 3 | * -------------------------------------------------------------------------
|
---|
| 4 | *
|
---|
[1383] | 5 | * Class: bncNetQueryV1
|
---|
[1382] | 6 | *
|
---|
| 7 | * Purpose: Blocking Network Requests (NTRIP Version 1)
|
---|
| 8 | *
|
---|
| 9 | * Author: L. Mervart
|
---|
| 10 | *
|
---|
| 11 | * Created: 27-Dec-2008
|
---|
| 12 | *
|
---|
| 13 | * Changes:
|
---|
| 14 | *
|
---|
| 15 | * -----------------------------------------------------------------------*/
|
---|
| 16 |
|
---|
| 17 | #include <iostream>
|
---|
| 18 | #include <iomanip>
|
---|
| 19 |
|
---|
[1385] | 20 | #include "bncnetqueryv1.h"
|
---|
[1382] | 21 |
|
---|
| 22 | using namespace std;
|
---|
| 23 |
|
---|
| 24 | #define BNCVERSION "1.7"
|
---|
| 25 |
|
---|
| 26 | // Constructor
|
---|
| 27 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1383] | 28 | bncNetQueryV1::bncNetQueryV1() {
|
---|
[1403] | 29 | _socket = 0;
|
---|
[1382] | 30 | }
|
---|
| 31 |
|
---|
| 32 | // Destructor
|
---|
| 33 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1383] | 34 | bncNetQueryV1::~bncNetQueryV1() {
|
---|
[1382] | 35 | delete _socket;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | //
|
---|
| 39 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1390] | 40 | void bncNetQueryV1::stop() {
|
---|
[1408] | 41 | #ifndef sparc
|
---|
| 42 | if (_socket) {
|
---|
| 43 | _socket->abort();
|
---|
| 44 | }
|
---|
| 45 | #endif
|
---|
[1403] | 46 | _status = finished;
|
---|
[1390] | 47 | }
|
---|
| 48 |
|
---|
| 49 | //
|
---|
| 50 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1402] | 51 | void bncNetQueryV1::waitForRequestResult(const QUrl&, QByteArray&) {
|
---|
[1382] | 52 | }
|
---|
| 53 |
|
---|
| 54 | //
|
---|
| 55 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1384] | 56 | void bncNetQueryV1::waitForReadyRead(QByteArray& outData) {
|
---|
[1402] | 57 | if (_socket) {
|
---|
| 58 | if (_socket->waitForReadyRead()) {
|
---|
| 59 | outData = _socket->readAll();
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
[1382] | 62 | }
|
---|
| 63 |
|
---|
| 64 | // Connect to Caster, send the Request
|
---|
| 65 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1384] | 66 | void bncNetQueryV1::startRequest(const QUrl& url, const QByteArray& gga) {
|
---|
[1382] | 67 |
|
---|
[1498] | 68 | const int timeOut = 20000;
|
---|
[1385] | 69 |
|
---|
| 70 | _status = running;
|
---|
| 71 |
|
---|
[1382] | 72 | delete _socket;
|
---|
| 73 | _socket = new QTcpSocket();
|
---|
| 74 |
|
---|
[1385] | 75 | // Default scheme and path
|
---|
| 76 | // -----------------------
|
---|
| 77 | QUrl urlLoc(url);
|
---|
| 78 | if (urlLoc.scheme().isEmpty()) {
|
---|
| 79 | urlLoc.setScheme("http");
|
---|
| 80 | }
|
---|
| 81 | if (urlLoc.path().isEmpty()) {
|
---|
| 82 | urlLoc.setPath("/");
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[1382] | 85 | // Connect the Socket
|
---|
| 86 | // ------------------
|
---|
| 87 | QSettings settings;
|
---|
| 88 | QString proxyHost = settings.value("proxyHost").toString();
|
---|
| 89 | int proxyPort = settings.value("proxyPort").toInt();
|
---|
| 90 |
|
---|
| 91 | if ( proxyHost.isEmpty() ) {
|
---|
[1385] | 92 | _socket->connectToHost(urlLoc.host(), urlLoc.port());
|
---|
[1382] | 93 | }
|
---|
| 94 | else {
|
---|
| 95 | _socket->connectToHost(proxyHost, proxyPort);
|
---|
| 96 | }
|
---|
| 97 | if (!_socket->waitForConnected(timeOut)) {
|
---|
| 98 | delete _socket;
|
---|
| 99 | _socket = 0;
|
---|
[1385] | 100 | _status = error;
|
---|
| 101 | return;
|
---|
[1382] | 102 | }
|
---|
| 103 |
|
---|
| 104 | // Send Request
|
---|
| 105 | // ------------
|
---|
[1385] | 106 | QString uName = QUrl::fromPercentEncoding(urlLoc.userName().toAscii());
|
---|
| 107 | QString passW = QUrl::fromPercentEncoding(urlLoc.password().toAscii());
|
---|
[1382] | 108 | QByteArray userAndPwd;
|
---|
| 109 |
|
---|
[1385] | 110 | if(!uName.isEmpty() || !passW.isEmpty()) {
|
---|
[1382] | 111 | userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
|
---|
| 112 | passW.toAscii()).toBase64() + "\r\n";
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | QByteArray reqStr;
|
---|
| 116 | if ( proxyHost.isEmpty() ) {
|
---|
[1385] | 117 | if (urlLoc.path().indexOf("/") != 0) urlLoc.setPath("/");
|
---|
| 118 | reqStr = "GET " + urlLoc.path().toAscii() + " HTTP/1.0\r\n"
|
---|
[1382] | 119 | + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
|
---|
| 120 | + userAndPwd + "\r\n";
|
---|
| 121 | } else {
|
---|
[1385] | 122 | reqStr = "GET " + urlLoc.toEncoded() + " HTTP/1.0\r\n"
|
---|
[1382] | 123 | + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
|
---|
[1385] | 124 | + "Host: " + urlLoc.host().toAscii() + "\r\n"
|
---|
[1382] | 125 | + userAndPwd + "\r\n";
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | // NMEA string to handle VRS stream
|
---|
| 129 | // --------------------------------
|
---|
[1385] | 130 | if (!gga.isEmpty()) {
|
---|
| 131 | reqStr += gga + "\r\n";
|
---|
[1382] | 132 | }
|
---|
| 133 |
|
---|
| 134 | _socket->write(reqStr, reqStr.length());
|
---|
| 135 |
|
---|
| 136 | if (!_socket->waitForBytesWritten(timeOut)) {
|
---|
| 137 | delete _socket;
|
---|
| 138 | _socket = 0;
|
---|
[1385] | 139 | _status = error;
|
---|
[1498] | 140 | emit newMessage("bncnetqueryv1: write timeout", true);
|
---|
| 141 | return;
|
---|
[1382] | 142 | }
|
---|
[1498] | 143 |
|
---|
| 144 | // Read Caster Response
|
---|
| 145 | // --------------------
|
---|
| 146 | while (true) {
|
---|
| 147 | if (!_socket->waitForReadyRead(timeOut)) {
|
---|
| 148 | delete _socket;
|
---|
| 149 | _socket = 0;
|
---|
| 150 | _status = error;
|
---|
| 151 | emit newMessage("bncnetqueryv1: read timeout", true);
|
---|
| 152 | return;
|
---|
| 153 | }
|
---|
| 154 | if (_socket->canReadLine()) {
|
---|
| 155 | QString line = _socket->readLine();
|
---|
| 156 | cout << ">" << line.toAscii().data() << "<" << endl;
|
---|
| 157 | if (line.indexOf("ICY 200 OK") != -1) {
|
---|
| 158 | break;
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
[1382] | 162 | }
|
---|
| 163 |
|
---|