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 | #include "bnssettings.h"
|
---|
20 |
|
---|
21 | using namespace std;
|
---|
22 |
|
---|
23 | // Constructor
|
---|
24 | ////////////////////////////////////////////////////////////////////////////
|
---|
25 | t_bnscaster::t_bnscaster(const QString& mountpoint, const QString& outFileName,
|
---|
26 | int ic) {
|
---|
27 |
|
---|
28 | bnsSettings settings;
|
---|
29 |
|
---|
30 | _mountpoint = mountpoint;
|
---|
31 | _ic = ic;
|
---|
32 | _outSocket = 0;
|
---|
33 | _sOpenTrial = 0;
|
---|
34 |
|
---|
35 | if (outFileName.isEmpty()) {
|
---|
36 | _outFile = 0;
|
---|
37 | _outStream = 0;
|
---|
38 | }
|
---|
39 | else {
|
---|
40 | _outFile = new QFile(outFileName);
|
---|
41 |
|
---|
42 | QIODevice::OpenMode oMode;
|
---|
43 | if (Qt::CheckState(settings.value("fileAppend").toInt()) == Qt::Checked) {
|
---|
44 | oMode = QIODevice::WriteOnly | QIODevice::Unbuffered | QIODevice::Append;
|
---|
45 | }
|
---|
46 | else {
|
---|
47 | oMode = QIODevice::WriteOnly | QIODevice::Unbuffered;
|
---|
48 | }
|
---|
49 |
|
---|
50 | if (_outFile->open(oMode)) {
|
---|
51 | _outStream = new QTextStream(_outFile);
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | // Reference frame
|
---|
56 | // ---------------
|
---|
57 | _crdTrafo = settings.value(QString("refSys_%1").arg(_ic)).toString();
|
---|
58 |
|
---|
59 | if ( Qt::CheckState(settings.value(QString("CoM_%1").arg(_ic)).toInt())
|
---|
60 | == Qt::Checked ) {
|
---|
61 | _CoM = true;
|
---|
62 | }
|
---|
63 | else {
|
---|
64 | _CoM = false;
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | // Constructor
|
---|
69 | ////////////////////////////////////////////////////////////////////////////
|
---|
70 | t_bnscaster::t_bnscaster(const QString& mountpoint) {
|
---|
71 |
|
---|
72 | bnsSettings settings;
|
---|
73 |
|
---|
74 | _mountpoint = mountpoint;
|
---|
75 | _ic = 0;
|
---|
76 | _outSocket = 0;
|
---|
77 | _sOpenTrial = 0;
|
---|
78 | _outFile = 0;
|
---|
79 | _outStream = 0;
|
---|
80 | _crdTrafo = "";
|
---|
81 | _CoM = false;
|
---|
82 | }
|
---|
83 |
|
---|
84 | // Destructor
|
---|
85 | ////////////////////////////////////////////////////////////////////////////
|
---|
86 | t_bnscaster::~t_bnscaster() {
|
---|
87 | delete _outSocket;
|
---|
88 | delete _outStream;
|
---|
89 | delete _outFile;
|
---|
90 | }
|
---|
91 |
|
---|
92 | // Start the Communication with NTRIP Caster
|
---|
93 | ////////////////////////////////////////////////////////////////////////////
|
---|
94 | void t_bnscaster::open() {
|
---|
95 |
|
---|
96 | if (_mountpoint.isEmpty()) {
|
---|
97 | return;
|
---|
98 | }
|
---|
99 |
|
---|
100 | if (_outSocket != 0 &&
|
---|
101 | _outSocket->state() == QAbstractSocket::ConnectedState) {
|
---|
102 | return;
|
---|
103 | }
|
---|
104 |
|
---|
105 | delete _outSocket; _outSocket = 0;
|
---|
106 |
|
---|
107 | double minDt = pow(2.0,_sOpenTrial);
|
---|
108 | if (++_sOpenTrial > 4) {
|
---|
109 | _sOpenTrial = 4;
|
---|
110 | }
|
---|
111 | if (_outSocketOpenTime.isValid() &&
|
---|
112 | _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
|
---|
113 | return;
|
---|
114 | }
|
---|
115 | else {
|
---|
116 | _outSocketOpenTime = QDateTime::currentDateTime();
|
---|
117 | }
|
---|
118 |
|
---|
119 | bnsSettings settings;
|
---|
120 | _outSocket = new QTcpSocket();
|
---|
121 | QString password;
|
---|
122 | if (_ic == 1) {
|
---|
123 | _outSocket->connectToHost(settings.value("outHost1").toString(),
|
---|
124 | settings.value("outPort1").toInt());
|
---|
125 | password = settings.value("password1").toString();
|
---|
126 | }
|
---|
127 | if (_ic == 2) {
|
---|
128 | _outSocket->connectToHost(settings.value("outHost2").toString(),
|
---|
129 | settings.value("outPort2").toInt());
|
---|
130 | password = settings.value("password2").toString();
|
---|
131 | }
|
---|
132 | if (_ic == 3) {
|
---|
133 | _outSocket->connectToHost(settings.value("outHost3").toString(),
|
---|
134 | settings.value("outPort3").toInt());
|
---|
135 | password = settings.value("password3").toString();
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (_ic == 4) {
|
---|
139 | _outSocket->connectToHost(settings.value("outHost4").toString(),
|
---|
140 | settings.value("outPort4").toInt());
|
---|
141 | password = settings.value("password4").toString();
|
---|
142 | }
|
---|
143 |
|
---|
144 | if (_ic == 5) {
|
---|
145 | _outSocket->connectToHost(settings.value("outHost5").toString(),
|
---|
146 | settings.value("outPort5").toInt());
|
---|
147 | password = settings.value("password5").toString();
|
---|
148 | }
|
---|
149 |
|
---|
150 | if (_ic == 6) {
|
---|
151 | _outSocket->connectToHost(settings.value("outHost6").toString(),
|
---|
152 | settings.value("outPort6").toInt());
|
---|
153 | password = settings.value("password6").toString();
|
---|
154 | }
|
---|
155 |
|
---|
156 | if (_ic == 7) {
|
---|
157 | _outSocket->connectToHost(settings.value("outHost7").toString(),
|
---|
158 | settings.value("outPort7").toInt());
|
---|
159 | password = settings.value("password7").toString();
|
---|
160 | }
|
---|
161 |
|
---|
162 | if (_ic == 8) {
|
---|
163 | _outSocket->connectToHost(settings.value("outHost8").toString(),
|
---|
164 | settings.value("outPort8").toInt());
|
---|
165 | password = settings.value("password8").toString();
|
---|
166 | }
|
---|
167 |
|
---|
168 | if (_ic == 9) {
|
---|
169 | _outSocket->connectToHost(settings.value("outHost9").toString(),
|
---|
170 | settings.value("outPort9").toInt());
|
---|
171 | password = settings.value("password9").toString();
|
---|
172 | }
|
---|
173 |
|
---|
174 | if (_ic == 10) {
|
---|
175 | _outSocket->connectToHost(settings.value("outHost10").toString(),
|
---|
176 | settings.value("outPort10").toInt());
|
---|
177 | password = settings.value("password10").toString();
|
---|
178 | }
|
---|
179 |
|
---|
180 | if (_ic == 0) {
|
---|
181 | _outSocket->connectToHost(settings.value("outHostEph").toString(),
|
---|
182 | settings.value("outPortEph").toInt());
|
---|
183 | password = settings.value("passwordEph").toString();
|
---|
184 | }
|
---|
185 |
|
---|
186 | const int timeOut = 5000; // 5 seconds
|
---|
187 | if (!_outSocket->waitForConnected(timeOut)) {
|
---|
188 | delete _outSocket;
|
---|
189 | _outSocket = 0;
|
---|
190 | emit(newMessage("Broadcaster: Connect timeout"));
|
---|
191 | return;
|
---|
192 | }
|
---|
193 |
|
---|
194 | QByteArray msg = "SOURCE " + password.toAscii() + " /" +
|
---|
195 | _mountpoint.toAscii() + "\r\n" +
|
---|
196 | "Source-Agent: NTRIP BNS/1.1\r\n\r\n";
|
---|
197 |
|
---|
198 | _outSocket->write(msg);
|
---|
199 | _outSocket->waitForBytesWritten();
|
---|
200 |
|
---|
201 | _outSocket->waitForReadyRead();
|
---|
202 | QByteArray ans = _outSocket->readLine();
|
---|
203 |
|
---|
204 | if (ans.indexOf("OK") == -1) {
|
---|
205 | delete _outSocket;
|
---|
206 | _outSocket = 0;
|
---|
207 | emit(newMessage("Broadcaster: Connection broken"));
|
---|
208 | }
|
---|
209 | else {
|
---|
210 | emit(newMessage("Broadcaster: Connection opened"));
|
---|
211 | _sOpenTrial = 0;
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | // Write buffer
|
---|
216 | ////////////////////////////////////////////////////////////////////////////
|
---|
217 | void t_bnscaster::write(char* buffer, unsigned len) {
|
---|
218 | if (_outSocket) {
|
---|
219 | _outSocket->write(buffer, len);
|
---|
220 | _outSocket->flush();
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | // Print Ascii Output
|
---|
225 | ////////////////////////////////////////////////////////////////////////////
|
---|
226 | void t_bnscaster::printAscii(const QString& line) {
|
---|
227 | if (_outStream) {
|
---|
228 | *_outStream << line;
|
---|
229 | _outStream->flush();
|
---|
230 | }
|
---|
231 | }
|
---|