| 1 | /* -------------------------------------------------------------------------
|
|---|
| 2 | * BKG NTRIP Client
|
|---|
| 3 | * -------------------------------------------------------------------------
|
|---|
| 4 | *
|
|---|
| 5 | * Class: bncNetQueryV0
|
|---|
| 6 | *
|
|---|
| 7 | * Purpose: TCP/IP Network Requests, no NTRIP
|
|---|
| 8 | *
|
|---|
| 9 | * Author: G. Weber
|
|---|
| 10 | *
|
|---|
| 11 | * Created: 19-Feb-2009
|
|---|
| 12 | *
|
|---|
| 13 | * Changes:
|
|---|
| 14 | *
|
|---|
| 15 | * -----------------------------------------------------------------------*/
|
|---|
| 16 |
|
|---|
| 17 | #include <iostream>
|
|---|
| 18 | #include <iomanip>
|
|---|
| 19 |
|
|---|
| 20 | #include "bncnetqueryv0.h"
|
|---|
| 21 | #include "bncsettings.h"
|
|---|
| 22 | #include "bncversion.h"
|
|---|
| 23 |
|
|---|
| 24 | using namespace std;
|
|---|
| 25 |
|
|---|
| 26 | // Constructor
|
|---|
| 27 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 28 | bncNetQueryV0::bncNetQueryV0() {
|
|---|
| 29 | _socket = 0;
|
|---|
| 30 | _timeOut = 120000;
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | // Destructor
|
|---|
| 34 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 35 | bncNetQueryV0::~bncNetQueryV0() {
|
|---|
| 36 | delete _socket;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | //
|
|---|
| 40 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 41 | void bncNetQueryV0::stop() {
|
|---|
| 42 | #ifndef sparc
|
|---|
| 43 | if (_socket) {
|
|---|
| 44 | _socket->abort();
|
|---|
| 45 | }
|
|---|
| 46 | #endif
|
|---|
| 47 | _status = finished;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | //
|
|---|
| 51 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 52 | void bncNetQueryV0::waitForRequestResult(const QUrl&, QByteArray&) {
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | //
|
|---|
| 56 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 57 | void bncNetQueryV0::waitForReadyRead(QByteArray& outData) {
|
|---|
| 58 | if (_socket && _socket->state() == QAbstractSocket::ConnectedState) {
|
|---|
| 59 | while (true) {
|
|---|
| 60 | int nBytes = _socket->bytesAvailable();
|
|---|
| 61 | if (nBytes > 0) {
|
|---|
| 62 | outData = _socket->readAll();
|
|---|
| 63 | return;
|
|---|
| 64 | }
|
|---|
| 65 | else if (!_socket->waitForReadyRead(_timeOut)) {
|
|---|
| 66 | delete _socket;
|
|---|
| 67 | _socket = 0;
|
|---|
| 68 | _status = error;
|
|---|
| 69 | emit newMessage(_url.path().toAscii() + " read timeout", true);
|
|---|
| 70 | return;
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 | }
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | // Connect to Caster, send the Request
|
|---|
| 77 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 78 | void bncNetQueryV0::startRequest(const QUrl& url, const QByteArray& /* gga */) {
|
|---|
| 79 |
|
|---|
| 80 | _status = running;
|
|---|
| 81 |
|
|---|
| 82 | delete _socket;
|
|---|
| 83 | _socket = new QTcpSocket();
|
|---|
| 84 |
|
|---|
| 85 | // Default scheme and path
|
|---|
| 86 | // -----------------------
|
|---|
| 87 | _url = url;
|
|---|
| 88 | if (_url.scheme().isEmpty()) {
|
|---|
| 89 | _url.setScheme("http");
|
|---|
| 90 | }
|
|---|
| 91 | if (_url.path().isEmpty()) {
|
|---|
| 92 | _url.setPath("/");
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | // Connect the Socket
|
|---|
| 96 | // ------------------
|
|---|
| 97 | bncSettings settings;
|
|---|
| 98 |
|
|---|
| 99 | _socket->connectToHost(_url.host(), _url.port());
|
|---|
| 100 | if (!_socket->waitForConnected(_timeOut)) {
|
|---|
| 101 | delete _socket;
|
|---|
| 102 | _socket = 0;
|
|---|
| 103 | _status = error;
|
|---|
| 104 | emit(newMessage(_url.path().toAscii().replace(0,1,"")
|
|---|
| 105 | + ": Connect timeout, reconnecting", true));
|
|---|
| 106 | return;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | // Read Caster Response
|
|---|
| 110 | // --------------------
|
|---|
| 111 | QStringList response;
|
|---|
| 112 | while (true) {
|
|---|
| 113 | if (!_socket->waitForReadyRead(_timeOut)) {
|
|---|
| 114 | delete _socket;
|
|---|
| 115 | _socket = 0;
|
|---|
| 116 | _status = error;
|
|---|
| 117 | emit newMessage(_url.path().toAscii().replace(0,1,"")
|
|---|
| 118 | + ": Read timeout", true);
|
|---|
| 119 | return;
|
|---|
| 120 | }
|
|---|
| 121 | if (_socket->canReadLine()) {
|
|---|
| 122 | QString line = _socket->readLine();
|
|---|
| 123 | response.push_back(line);
|
|---|
| 124 | response.clear();
|
|---|
| 125 | break;
|
|---|
| 126 | }
|
|---|
| 127 | }
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|