[1372] | 1 | /* -------------------------------------------------------------------------
|
---|
| 2 | * BKG NTRIP Client
|
---|
| 3 | * -------------------------------------------------------------------------
|
---|
| 4 | *
|
---|
| 5 | * Class: bncNetQuery
|
---|
| 6 | *
|
---|
| 7 | * Purpose: Blocking Network Requests
|
---|
| 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 "bncnetquery.h"
|
---|
| 21 | #include "bncapp.h"
|
---|
| 22 |
|
---|
| 23 | using namespace std;
|
---|
| 24 |
|
---|
| 25 | #define BNCVERSION "1.7"
|
---|
| 26 |
|
---|
| 27 | // Constructor
|
---|
| 28 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 29 | bncNetQuery::bncNetQuery() {
|
---|
| 30 |
|
---|
| 31 | connect(this, SIGNAL(newMessage(QByteArray,bool)),
|
---|
| 32 | (bncApp*) qApp, SLOT(slotMessage(const QByteArray,bool)));
|
---|
| 33 |
|
---|
| 34 | _manager = new QNetworkAccessManager(this);
|
---|
| 35 | _reply = 0;
|
---|
| 36 | _eventLoop = new QEventLoop(this);
|
---|
[1374] | 37 |
|
---|
| 38 | _status = init;
|
---|
[1372] | 39 | }
|
---|
| 40 |
|
---|
| 41 | // Destructor
|
---|
| 42 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 43 | bncNetQuery::~bncNetQuery() {
|
---|
| 44 | delete _eventLoop;
|
---|
| 45 | delete _reply;
|
---|
| 46 | delete _manager;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | // Error
|
---|
| 50 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 51 | void bncNetQuery::slotError(QNetworkReply::NetworkError) {
|
---|
[1374] | 52 | _status = error;
|
---|
[1372] | 53 | cout << "slotError " << endl;
|
---|
| 54 | emit newMessage("slotError " + _reply->errorString().toAscii(), true);
|
---|
| 55 | _eventLoop->quit();
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[1373] | 58 | void bncNetQuery::slotFinished() {
|
---|
[1374] | 59 | if (_status != error) {
|
---|
| 60 | _status = finished;
|
---|
| 61 | }
|
---|
[1373] | 62 | cout << "slotFinished" << endl;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | void bncNetQuery::slotReadyRead() {
|
---|
| 66 | cout << "slotReadyRead" << endl;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[1372] | 69 | // Start request, block till the next read (public)
|
---|
| 70 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1374] | 71 | void bncNetQuery::startRequest(const QUrl& url) {
|
---|
[1376] | 72 | startRequest(url, false);
|
---|
[1372] | 73 | }
|
---|
| 74 |
|
---|
| 75 | // Start request
|
---|
| 76 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1374] | 77 | void bncNetQuery::startRequest(const QUrl& url, bool full) {
|
---|
[1372] | 78 |
|
---|
[1374] | 79 | _status = running;
|
---|
| 80 |
|
---|
[1372] | 81 | // Default scheme and path
|
---|
| 82 | // -----------------------
|
---|
| 83 | QUrl urlLoc(url);
|
---|
| 84 | if (urlLoc.scheme().isEmpty()) {
|
---|
| 85 | urlLoc.setScheme("http");
|
---|
| 86 | }
|
---|
| 87 | if (urlLoc.path().isEmpty()) {
|
---|
| 88 | urlLoc.setPath("/");
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | // Network Request
|
---|
| 92 | // ---------------
|
---|
| 93 | QNetworkRequest request;
|
---|
| 94 | request.setUrl(urlLoc);
|
---|
| 95 | request.setRawHeader("Host" , urlLoc.host().toAscii());
|
---|
| 96 | request.setRawHeader("Ntrip-Version", "NTRIP/2.0");
|
---|
| 97 | request.setRawHeader("User-Agent" , "NTRIP BNC/1.7");
|
---|
| 98 | if (!urlLoc.userName().isEmpty()) {
|
---|
| 99 | request.setRawHeader("Authorization", "Basic " +
|
---|
| 100 | (urlLoc.userName() + ":" + urlLoc.password()).toAscii().toBase64());
|
---|
| 101 | }
|
---|
| 102 | request.setRawHeader("Connection" , "close");
|
---|
| 103 |
|
---|
| 104 | _reply = _manager->get(request);
|
---|
| 105 |
|
---|
| 106 | // Connect Signals
|
---|
| 107 | // ---------------
|
---|
[1373] | 108 | connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
|
---|
| 109 | connect(_reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
|
---|
| 110 |
|
---|
[1372] | 111 | connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(quit()));
|
---|
| 112 | if (!full) {
|
---|
| 113 | connect(_reply, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
|
---|
| 114 | }
|
---|
| 115 | connect(_reply, SIGNAL(error(QNetworkReply::NetworkError)),
|
---|
| 116 | this, SLOT(slotError(QNetworkReply::NetworkError)));
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | // Start Request, wait for its completion
|
---|
| 120 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1374] | 121 | void bncNetQuery::waitForRequestResult(const QUrl& url, QByteArray& outData) {
|
---|
[1372] | 122 |
|
---|
| 123 | // Send Request
|
---|
| 124 | // ------------
|
---|
[1376] | 125 | startRequest(url, true);
|
---|
[1372] | 126 |
|
---|
| 127 | // Wait Loop
|
---|
| 128 | // ---------
|
---|
| 129 | _eventLoop->exec();
|
---|
| 130 |
|
---|
| 131 | // Copy Data and Return
|
---|
| 132 | // --------------------
|
---|
| 133 | outData = _reply->readAll();
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | // Wait for next data
|
---|
| 137 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1374] | 138 | void bncNetQuery::waitForReadyRead(QByteArray& outData) {
|
---|
[1372] | 139 |
|
---|
| 140 | // Wait Loop
|
---|
| 141 | // ---------
|
---|
| 142 | if (!_reply->bytesAvailable()) {
|
---|
| 143 | _eventLoop->exec();
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[1374] | 146 | // Append Data
|
---|
| 147 | // -----------
|
---|
[1372] | 148 | outData.append(_reply->readAll());
|
---|
| 149 | }
|
---|