1 | /* -------------------------------------------------------------------------
|
---|
2 | * BKG NTRIP Server
|
---|
3 | * -------------------------------------------------------------------------
|
---|
4 | *
|
---|
5 | * Class: bnscaster
|
---|
6 | *
|
---|
7 | * Purpose: Connection to NTRIP Caster
|
---|
8 | *
|
---|
9 | * Author: L. Mervart
|
---|
10 | *
|
---|
11 | * Created: 27-Aug-2008
|
---|
12 | *
|
---|
13 | * Changes:
|
---|
14 | *
|
---|
15 | * -----------------------------------------------------------------------*/
|
---|
16 |
|
---|
17 | #include <math.h>
|
---|
18 | #include "bnscaster.h"
|
---|
19 |
|
---|
20 | using namespace std;
|
---|
21 |
|
---|
22 | // Constructor
|
---|
23 | ////////////////////////////////////////////////////////////////////////////
|
---|
24 | t_bnscaster::t_bnscaster(const QString& mountpoint, const QString& outFileName,
|
---|
25 | const QString& refSys) {
|
---|
26 |
|
---|
27 | _mountpoint = mountpoint;
|
---|
28 | _outSocket = 0;
|
---|
29 | _outSocketOpenTrial = 0;
|
---|
30 |
|
---|
31 | QSettings settings;
|
---|
32 |
|
---|
33 | QIODevice::OpenMode oMode;
|
---|
34 | if (Qt::CheckState(settings.value("fileAppend").toInt()) == Qt::Checked) {
|
---|
35 | oMode = QIODevice::WriteOnly | QIODevice::Unbuffered | QIODevice::Append;
|
---|
36 | }
|
---|
37 | else {
|
---|
38 | oMode = QIODevice::WriteOnly | QIODevice::Unbuffered;
|
---|
39 | }
|
---|
40 |
|
---|
41 | if (outFileName.isEmpty()) {
|
---|
42 | _outFile = 0;
|
---|
43 | _outStream = 0;
|
---|
44 | }
|
---|
45 | else {
|
---|
46 | _outFile = new QFile(outFileName);
|
---|
47 | if (_outFile->open(oMode)) {
|
---|
48 | _outStream = new QTextStream(_outFile);
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | // Reference frame
|
---|
53 | // ---------------
|
---|
54 | _crdTrafo = false;
|
---|
55 | if (refSys == "ETRS89") {
|
---|
56 | _crdTrafo = true;
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | // Destructor
|
---|
61 | ////////////////////////////////////////////////////////////////////////////
|
---|
62 | t_bnscaster::~t_bnscaster() {
|
---|
63 | delete _outSocket;
|
---|
64 | delete _outStream;
|
---|
65 | delete _outFile;
|
---|
66 | }
|
---|
67 |
|
---|
68 | // Start the Communication with NTRIP Caster
|
---|
69 | ////////////////////////////////////////////////////////////////////////////
|
---|
70 | void t_bnscaster::open() {
|
---|
71 |
|
---|
72 | if (_mountpoint.isEmpty()) {
|
---|
73 | return;
|
---|
74 | }
|
---|
75 |
|
---|
76 | if (_outSocket != 0 &&
|
---|
77 | _outSocket->state() == QAbstractSocket::ConnectedState) {
|
---|
78 | return;
|
---|
79 | }
|
---|
80 |
|
---|
81 | delete _outSocket; _outSocket = 0;
|
---|
82 |
|
---|
83 | double minDt = exp2(_outSocketOpenTrial);
|
---|
84 | if (++_outSocketOpenTrial > 8) {
|
---|
85 | _outSocketOpenTrial = 8;
|
---|
86 | }
|
---|
87 | if (_outSocketOpenTime.isValid() &&
|
---|
88 | _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
|
---|
89 | return;
|
---|
90 | }
|
---|
91 | else {
|
---|
92 | _outSocketOpenTime = QDateTime::currentDateTime();
|
---|
93 | }
|
---|
94 |
|
---|
95 | QSettings settings;
|
---|
96 | _outSocket = new QTcpSocket();
|
---|
97 | _outSocket->connectToHost(settings.value("outHost").toString(),
|
---|
98 | settings.value("outPort").toInt());
|
---|
99 |
|
---|
100 | const int timeOut = 5000; // 5 seconds
|
---|
101 | if (!_outSocket->waitForConnected(timeOut)) {
|
---|
102 | delete _outSocket;
|
---|
103 | _outSocket = 0;
|
---|
104 | emit(error("t_bnscaster::open Connect Timeout"));
|
---|
105 | return;
|
---|
106 | }
|
---|
107 |
|
---|
108 | QString password = settings.value("password").toString();
|
---|
109 |
|
---|
110 | QByteArray msg = "SOURCE " + password.toAscii() + " /" +
|
---|
111 | _mountpoint.toAscii() + "\r\n" +
|
---|
112 | "Source-Agent: NTRIP BNS/1.0\r\n\r\n";
|
---|
113 |
|
---|
114 | _outSocket->write(msg);
|
---|
115 | _outSocket->waitForBytesWritten();
|
---|
116 |
|
---|
117 | _outSocket->waitForReadyRead();
|
---|
118 | QByteArray ans = _outSocket->readLine();
|
---|
119 |
|
---|
120 | if (ans.indexOf("OK") == -1) {
|
---|
121 | delete _outSocket;
|
---|
122 | _outSocket = 0;
|
---|
123 | // emit(newMessage("t_bnscaster::open socket deleted"));
|
---|
124 | emit(newMessage("Broadcaster: Connection broken")); // weber
|
---|
125 | }
|
---|
126 | else {
|
---|
127 | // emit(newMessage("t_bnscaster::open socket OK"));
|
---|
128 | emit(newMessage("Broadcaster: Connection opened")); // weber
|
---|
129 | _outSocketOpenTrial = 0;
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | // Write buffer
|
---|
134 | ////////////////////////////////////////////////////////////////////////////
|
---|
135 | void t_bnscaster::write(char* buffer, unsigned len) {
|
---|
136 | if (_outSocket) {
|
---|
137 | _outSocket->write(buffer, len);
|
---|
138 | _outSocket->flush();
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | // Print Ascii Output
|
---|
143 | ////////////////////////////////////////////////////////////////////////////
|
---|
144 | void t_bnscaster::printAscii(const QString& line) {
|
---|
145 | if (_outStream) {
|
---|
146 | *_outStream << line;
|
---|
147 | _outStream->flush();
|
---|
148 | }
|
---|
149 | }
|
---|