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);
|
---|
37 |
|
---|
38 | _status = init;
|
---|
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) {
|
---|
52 | _status = error;
|
---|
53 | cout << "slotError " << endl;
|
---|
54 | emit newMessage("slotError " + _reply->errorString().toAscii(), true);
|
---|
55 | _eventLoop->quit();
|
---|
56 | }
|
---|
57 |
|
---|
58 | void bncNetQuery::slotFinished() {
|
---|
59 | if (_status != error) {
|
---|
60 | _status = finished;
|
---|
61 | }
|
---|
62 | cout << "slotFinished" << endl;
|
---|
63 | }
|
---|
64 |
|
---|
65 | void bncNetQuery::slotReadyRead() {
|
---|
66 | cout << "slotReadyRead" << endl;
|
---|
67 | }
|
---|
68 |
|
---|
69 | // Start request, block till the next read (public)
|
---|
70 | ////////////////////////////////////////////////////////////////////////////
|
---|
71 | void bncNetQuery::startRequest(const QUrl& url) {
|
---|
72 | startRequest(url, false);
|
---|
73 | }
|
---|
74 |
|
---|
75 | // Start request
|
---|
76 | ////////////////////////////////////////////////////////////////////////////
|
---|
77 | void bncNetQuery::startRequest(const QUrl& url, bool full) {
|
---|
78 |
|
---|
79 | _status = running;
|
---|
80 |
|
---|
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 | // ---------------
|
---|
108 | connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
|
---|
109 | connect(_reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
|
---|
110 |
|
---|
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 | ////////////////////////////////////////////////////////////////////////////
|
---|
121 | void bncNetQuery::waitForRequestResult(const QUrl& url, QByteArray& outData) {
|
---|
122 |
|
---|
123 | // Send Request
|
---|
124 | // ------------
|
---|
125 | startRequest(url, true);
|
---|
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 | ////////////////////////////////////////////////////////////////////////////
|
---|
138 | void bncNetQuery::waitForReadyRead(QByteArray& outData) {
|
---|
139 |
|
---|
140 | // Wait Loop
|
---|
141 | // ---------
|
---|
142 | if (!_reply->bytesAvailable()) {
|
---|
143 | _eventLoop->exec();
|
---|
144 | }
|
---|
145 |
|
---|
146 | // Append Data
|
---|
147 | // -----------
|
---|
148 | outData.append(_reply->readAll());
|
---|
149 | }
|
---|