| 1 | /* -------------------------------------------------------------------------
|
|---|
| 2 | * BKG NTRIP Client
|
|---|
| 3 | * -------------------------------------------------------------------------
|
|---|
| 4 | *
|
|---|
| 5 | * Class: bncNetQueryV1
|
|---|
| 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 |
|
|---|
| 20 | #include "bncnetqueryv1.h"
|
|---|
| 21 |
|
|---|
| 22 | using namespace std;
|
|---|
| 23 |
|
|---|
| 24 | #define BNCVERSION "1.7"
|
|---|
| 25 |
|
|---|
| 26 | // Constructor
|
|---|
| 27 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 28 | bncNetQueryV1::bncNetQueryV1() {
|
|---|
| 29 | _socket = 0;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | // Destructor
|
|---|
| 33 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 34 | bncNetQueryV1::~bncNetQueryV1() {
|
|---|
| 35 | delete _socket;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | //
|
|---|
| 39 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 40 | void bncNetQueryV1::stop() {
|
|---|
| 41 | if (_socket) {
|
|---|
| 42 | _socket->abort();
|
|---|
| 43 | }
|
|---|
| 44 | _status = finished;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | //
|
|---|
| 48 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 49 | void bncNetQueryV1::waitForRequestResult(const QUrl&, QByteArray&) {
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | //
|
|---|
| 53 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 54 | void bncNetQueryV1::waitForReadyRead(QByteArray& outData) {
|
|---|
| 55 | if (_socket) {
|
|---|
| 56 | if (_socket->waitForReadyRead()) {
|
|---|
| 57 | outData = _socket->readAll();
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | // Connect to Caster, send the Request
|
|---|
| 63 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 64 | void bncNetQueryV1::startRequest(const QUrl& url, const QByteArray& gga) {
|
|---|
| 65 |
|
|---|
| 66 | const int timeOut = 5000;
|
|---|
| 67 |
|
|---|
| 68 | _status = running;
|
|---|
| 69 |
|
|---|
| 70 | delete _socket;
|
|---|
| 71 | _socket = new QTcpSocket();
|
|---|
| 72 |
|
|---|
| 73 | // Default scheme and path
|
|---|
| 74 | // -----------------------
|
|---|
| 75 | QUrl urlLoc(url);
|
|---|
| 76 | if (urlLoc.scheme().isEmpty()) {
|
|---|
| 77 | urlLoc.setScheme("http");
|
|---|
| 78 | }
|
|---|
| 79 | if (urlLoc.path().isEmpty()) {
|
|---|
| 80 | urlLoc.setPath("/");
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | // Connect the Socket
|
|---|
| 84 | // ------------------
|
|---|
| 85 | QSettings settings;
|
|---|
| 86 | QString proxyHost = settings.value("proxyHost").toString();
|
|---|
| 87 | int proxyPort = settings.value("proxyPort").toInt();
|
|---|
| 88 |
|
|---|
| 89 | if ( proxyHost.isEmpty() ) {
|
|---|
| 90 | _socket->connectToHost(urlLoc.host(), urlLoc.port());
|
|---|
| 91 | }
|
|---|
| 92 | else {
|
|---|
| 93 | _socket->connectToHost(proxyHost, proxyPort);
|
|---|
| 94 | }
|
|---|
| 95 | if (!_socket->waitForConnected(timeOut)) {
|
|---|
| 96 | delete _socket;
|
|---|
| 97 | _socket = 0;
|
|---|
| 98 | _status = error;
|
|---|
| 99 | return;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | // Send Request
|
|---|
| 103 | // ------------
|
|---|
| 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 reqStr;
|
|---|
| 114 | if ( proxyHost.isEmpty() ) {
|
|---|
| 115 | if (urlLoc.path().indexOf("/") != 0) urlLoc.setPath("/");
|
|---|
| 116 | reqStr = "GET " + urlLoc.path().toAscii() + " HTTP/1.0\r\n"
|
|---|
| 117 | + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
|
|---|
| 118 | + userAndPwd + "\r\n";
|
|---|
| 119 | } else {
|
|---|
| 120 | reqStr = "GET " + urlLoc.toEncoded() + " HTTP/1.0\r\n"
|
|---|
| 121 | + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
|
|---|
| 122 | + "Host: " + urlLoc.host().toAscii() + "\r\n"
|
|---|
| 123 | + userAndPwd + "\r\n";
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | // NMEA string to handle VRS stream
|
|---|
| 127 | // --------------------------------
|
|---|
| 128 | if (!gga.isEmpty()) {
|
|---|
| 129 | reqStr += gga + "\r\n";
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | _socket->write(reqStr, reqStr.length());
|
|---|
| 133 |
|
|---|
| 134 | if (!_socket->waitForBytesWritten(timeOut)) {
|
|---|
| 135 | delete _socket;
|
|---|
| 136 | _socket = 0;
|
|---|
| 137 | _status = error;
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|