[1378] | 1 | /* -------------------------------------------------------------------------
|
---|
| 2 | * BKG NTRIP Client
|
---|
| 3 | * -------------------------------------------------------------------------
|
---|
| 4 | *
|
---|
| 5 | * Class: bncNetQueryV2
|
---|
| 6 | *
|
---|
| 7 | * Purpose: Blocking Network Requests (NTRIP Version 2)
|
---|
| 8 | *
|
---|
| 9 | * Author: L. Mervart
|
---|
| 10 | *
|
---|
| 11 | * Created: 27-Dec-2008
|
---|
| 12 | *
|
---|
| 13 | * Changes:
|
---|
| 14 | *
|
---|
| 15 | * -----------------------------------------------------------------------*/
|
---|
| 16 |
|
---|
[1583] | 17 | #include <iostream>
|
---|
| 18 |
|
---|
[1379] | 19 | #include "bncnetqueryv2.h"
|
---|
[1535] | 20 | #include "bncsettings.h"
|
---|
[1378] | 21 |
|
---|
| 22 | #define BNCVERSION "1.7"
|
---|
| 23 |
|
---|
| 24 | // Constructor
|
---|
| 25 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 26 | bncNetQueryV2::bncNetQueryV2() {
|
---|
| 27 | _manager = new QNetworkAccessManager(this);
|
---|
[1405] | 28 | connect(_manager, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&,
|
---|
| 29 | QAuthenticator*)),
|
---|
| 30 | this, SLOT(slotProxyAuthenticationRequired(const QNetworkProxy&,
|
---|
| 31 | QAuthenticator*)));
|
---|
[1378] | 32 | _reply = 0;
|
---|
| 33 | _eventLoop = new QEventLoop(this);
|
---|
[1583] | 34 | _firstData = true;
|
---|
[1378] | 35 | _status = init;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | // Destructor
|
---|
| 39 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 40 | bncNetQueryV2::~bncNetQueryV2() {
|
---|
| 41 | delete _eventLoop;
|
---|
| 42 | delete _reply;
|
---|
| 43 | delete _manager;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[1391] | 46 | // Stop (quit even loop)
|
---|
[1390] | 47 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 48 | void bncNetQueryV2::stop() {
|
---|
[1393] | 49 | if (_reply) {
|
---|
[1398] | 50 | _reply->disconnect(SIGNAL(error(QNetworkReply::NetworkError)));
|
---|
[1395] | 51 | _reply->abort();
|
---|
[1393] | 52 | }
|
---|
[1394] | 53 | _eventLoop->quit();
|
---|
[1398] | 54 | _status = finished;
|
---|
[1390] | 55 | }
|
---|
| 56 |
|
---|
[1378] | 57 | // Error
|
---|
| 58 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 59 | void bncNetQueryV2::slotError(QNetworkReply::NetworkError) {
|
---|
| 60 | _status = error;
|
---|
[1649] | 61 | emit newMessage(_url.path().toAscii().replace(0,1,"")
|
---|
| 62 | + ": NetQuery, " + _reply->errorString().toAscii(), true);
|
---|
[1394] | 63 | _eventLoop->quit();
|
---|
[1378] | 64 | }
|
---|
| 65 |
|
---|
[1389] | 66 | // End of Request
|
---|
| 67 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1378] | 68 | void bncNetQueryV2::slotFinished() {
|
---|
| 69 | if (_status != error) {
|
---|
| 70 | _status = finished;
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[1405] | 74 | //
|
---|
| 75 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 76 | void bncNetQueryV2::slotProxyAuthenticationRequired(const QNetworkProxy&,
|
---|
| 77 | QAuthenticator*) {
|
---|
| 78 | emit newMessage("slotProxyAuthenticationRequired", true);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[1389] | 81 | // Start request, block till the next read
|
---|
[1378] | 82 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1380] | 83 | void bncNetQueryV2::startRequest(const QUrl& url, const QByteArray& gga) {
|
---|
[1389] | 84 | startRequestPrivate(url, gga, false);
|
---|
[1378] | 85 | }
|
---|
| 86 |
|
---|
[1389] | 87 | // Start Request (Private Method)
|
---|
[1378] | 88 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1389] | 89 | void bncNetQueryV2::startRequestPrivate(const QUrl& url, const QByteArray& gga,
|
---|
| 90 | bool full) {
|
---|
[1378] | 91 |
|
---|
| 92 | _status = running;
|
---|
| 93 |
|
---|
| 94 | // Default scheme and path
|
---|
| 95 | // -----------------------
|
---|
[1509] | 96 | _url = url;
|
---|
| 97 | if (_url.scheme().isEmpty()) {
|
---|
| 98 | _url.setScheme("http");
|
---|
[1378] | 99 | }
|
---|
[1509] | 100 | if (_url.path().isEmpty()) {
|
---|
| 101 | _url.setPath("/");
|
---|
[1378] | 102 | }
|
---|
| 103 |
|
---|
[1404] | 104 | // Proxy Settings
|
---|
| 105 | // --------------
|
---|
[1535] | 106 | bncSettings settings;
|
---|
[1404] | 107 | QString proxyHost = settings.value("proxyHost").toString();
|
---|
| 108 | int proxyPort = settings.value("proxyPort").toInt();
|
---|
| 109 |
|
---|
| 110 | if (!proxyHost.isEmpty()) {
|
---|
| 111 | QNetworkProxy proxy(QNetworkProxy::HttpProxy, proxyHost, proxyPort);
|
---|
| 112 | _manager->setProxy(proxy);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[1378] | 115 | // Network Request
|
---|
| 116 | // ---------------
|
---|
| 117 | QNetworkRequest request;
|
---|
[1509] | 118 | request.setUrl(_url);
|
---|
| 119 | request.setRawHeader("Host" , _url.host().toAscii());
|
---|
[1463] | 120 | request.setRawHeader("Ntrip-Version", "Ntrip/2.0");
|
---|
[1378] | 121 | request.setRawHeader("User-Agent" , "NTRIP BNC/1.7");
|
---|
[1509] | 122 | if (!_url.userName().isEmpty()) {
|
---|
| 123 | QString uName = QUrl::fromPercentEncoding(_url.userName().toAscii());
|
---|
| 124 | QString passW = QUrl::fromPercentEncoding(_url.password().toAscii());
|
---|
[1378] | 125 | request.setRawHeader("Authorization", "Basic " +
|
---|
[1509] | 126 | (uName + ":" + passW).toAscii().toBase64());
|
---|
[1378] | 127 | }
|
---|
[1389] | 128 | if (!gga.isEmpty()) {
|
---|
| 129 | request.setRawHeader("Ntrip-GGA", gga);
|
---|
| 130 | }
|
---|
[1378] | 131 | request.setRawHeader("Connection" , "close");
|
---|
| 132 |
|
---|
| 133 | _reply = _manager->get(request);
|
---|
| 134 |
|
---|
| 135 | // Connect Signals
|
---|
| 136 | // ---------------
|
---|
| 137 | connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
|
---|
| 138 | connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(quit()));
|
---|
| 139 | if (!full) {
|
---|
| 140 | connect(_reply, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
|
---|
| 141 | }
|
---|
| 142 | connect(_reply, SIGNAL(error(QNetworkReply::NetworkError)),
|
---|
| 143 | this, SLOT(slotError(QNetworkReply::NetworkError)));
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | // Start Request, wait for its completion
|
---|
| 147 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 148 | void bncNetQueryV2::waitForRequestResult(const QUrl& url, QByteArray& outData) {
|
---|
| 149 |
|
---|
| 150 | // Send Request
|
---|
| 151 | // ------------
|
---|
[1389] | 152 | startRequestPrivate(url, "", true);
|
---|
[1378] | 153 |
|
---|
| 154 | // Wait Loop
|
---|
| 155 | // ---------
|
---|
[1394] | 156 | _eventLoop->exec();
|
---|
[1378] | 157 |
|
---|
| 158 | // Copy Data and Return
|
---|
| 159 | // --------------------
|
---|
| 160 | outData = _reply->readAll();
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | // Wait for next data
|
---|
| 164 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 165 | void bncNetQueryV2::waitForReadyRead(QByteArray& outData) {
|
---|
| 166 |
|
---|
| 167 | // Wait Loop
|
---|
| 168 | // ---------
|
---|
| 169 | if (!_reply->bytesAvailable()) {
|
---|
[1394] | 170 | _eventLoop->exec();
|
---|
[1378] | 171 | }
|
---|
| 172 |
|
---|
| 173 | // Append Data
|
---|
| 174 | // -----------
|
---|
| 175 | outData.append(_reply->readAll());
|
---|
[1583] | 176 |
|
---|
| 177 | if (_firstData) {
|
---|
| 178 | _firstData = false;
|
---|
[1648] | 179 | if (outData.indexOf("SOURCETABLE") != -1 ||
|
---|
| 180 | outData.indexOf("not found") != -1) {
|
---|
[1583] | 181 | _reply->disconnect(SIGNAL(error(QNetworkReply::NetworkError)));
|
---|
| 182 | _reply->abort();
|
---|
| 183 | _eventLoop->quit();
|
---|
| 184 | _status = error;
|
---|
[1649] | 185 | emit newMessage(_url.path().toAscii().replace(0,1,"")
|
---|
[1652] | 186 | + ": NetQuery, wrong mountpoint", true);
|
---|
[1583] | 187 | }
|
---|
| 188 | }
|
---|
[1378] | 189 | }
|
---|