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