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 "bncsocket.h"
|
---|
21 | #include "bncapp.h"
|
---|
22 | #include "bncutils.h"
|
---|
23 |
|
---|
24 | using namespace std;
|
---|
25 |
|
---|
26 | #define BNCVERSION "1.7"
|
---|
27 |
|
---|
28 | // Constructor
|
---|
29 | ////////////////////////////////////////////////////////////////////////////
|
---|
30 | bncNetQueryV1::bncNetQueryV1() {
|
---|
31 | _socket = 0;
|
---|
32 | }
|
---|
33 |
|
---|
34 | // Destructor
|
---|
35 | ////////////////////////////////////////////////////////////////////////////
|
---|
36 | bncNetQueryV1::~bncNetQueryV1() {
|
---|
37 | delete _socket;
|
---|
38 | }
|
---|
39 |
|
---|
40 | //
|
---|
41 | ////////////////////////////////////////////////////////////////////////////
|
---|
42 | void bncNetQueryV1::waitForRequestResult(const QUrl& url, QByteArray& outData) {
|
---|
43 | }
|
---|
44 |
|
---|
45 | //
|
---|
46 | ////////////////////////////////////////////////////////////////////////////
|
---|
47 | void bncNetQueryV1::waitForReadyRead(QByteArray& outData) {
|
---|
48 |
|
---|
49 | }
|
---|
50 |
|
---|
51 | // Connect to Caster, send the Request
|
---|
52 | ////////////////////////////////////////////////////////////////////////////
|
---|
53 | void bncNetQueryV1::startRequest(const QUrl& url, const QByteArray& gga) {
|
---|
54 |
|
---|
55 | delete _socket;
|
---|
56 | _socket = new QTcpSocket();
|
---|
57 |
|
---|
58 | // Connect the Socket
|
---|
59 | // ------------------
|
---|
60 | QSettings settings;
|
---|
61 | QString proxyHost = settings.value("proxyHost").toString();
|
---|
62 | int proxyPort = settings.value("proxyPort").toInt();
|
---|
63 |
|
---|
64 | if ( proxyHost.isEmpty() ) {
|
---|
65 | _socket->connectToHost(mountPoint.host(), mountPoint.port());
|
---|
66 | }
|
---|
67 | else {
|
---|
68 | _socket->connectToHost(proxyHost, proxyPort);
|
---|
69 | }
|
---|
70 | if (!_socket->waitForConnected(timeOut)) {
|
---|
71 | msg += "Connect timeout\n";
|
---|
72 | delete _socket;
|
---|
73 | _socket = 0;
|
---|
74 | return failure;
|
---|
75 | }
|
---|
76 |
|
---|
77 | // Send Request
|
---|
78 | // ------------
|
---|
79 | QString uName = QUrl::fromPercentEncoding(mountPoint.userName().toAscii());
|
---|
80 | QString passW = QUrl::fromPercentEncoding(mountPoint.password().toAscii());
|
---|
81 | QByteArray userAndPwd;
|
---|
82 |
|
---|
83 | if(!uName.isEmpty() || !passW.isEmpty())
|
---|
84 | {
|
---|
85 | userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
|
---|
86 | passW.toAscii()).toBase64() + "\r\n";
|
---|
87 | }
|
---|
88 |
|
---|
89 | QUrl hlp;
|
---|
90 | hlp.setScheme("http");
|
---|
91 | hlp.setHost(mountPoint.host());
|
---|
92 | hlp.setPort(mountPoint.port());
|
---|
93 | hlp.setPath(mountPoint.path());
|
---|
94 |
|
---|
95 | QByteArray reqStr;
|
---|
96 | if ( proxyHost.isEmpty() ) {
|
---|
97 | if (hlp.path().indexOf("/") != 0) hlp.setPath("/");
|
---|
98 | reqStr = "GET " + hlp.path().toAscii() + " HTTP/1.0\r\n"
|
---|
99 | + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
|
---|
100 | + userAndPwd + "\r\n";
|
---|
101 | } else {
|
---|
102 | reqStr = "GET " + hlp.toEncoded() + " HTTP/1.0\r\n"
|
---|
103 | + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
|
---|
104 | + "Host: " + hlp.host().toAscii() + "\r\n"
|
---|
105 | + userAndPwd + "\r\n";
|
---|
106 | }
|
---|
107 |
|
---|
108 | // NMEA string to handle VRS stream
|
---|
109 | // --------------------------------
|
---|
110 | if ((nmea == "yes") && (hlp.path().length() > 2) && (hlp.path().indexOf(".skl") < 0)) {
|
---|
111 | reqStr += ggaString(latitude, longitude) + "\r\n";
|
---|
112 | }
|
---|
113 |
|
---|
114 | msg += reqStr;
|
---|
115 |
|
---|
116 | _socket->write(reqStr, reqStr.length());
|
---|
117 |
|
---|
118 | if (!_socket->waitForBytesWritten(timeOut)) {
|
---|
119 | msg += "Write timeout\n";
|
---|
120 | delete _socket;
|
---|
121 | _socket = 0;
|
---|
122 | return failure;
|
---|
123 | }
|
---|
124 |
|
---|
125 | return success;
|
---|
126 | }
|
---|
127 |
|
---|