source: ntrip/trunk/BNS/bnscaster.cpp@ 4041

Last change on this file since 4041 was 2645, checked in by mervart, 14 years ago
File size: 6.3 KB
Line 
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
22using namespace std;
23
24// Constructor
25////////////////////////////////////////////////////////////////////////////
26t_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////////////////////////////////////////////////////////////////////////////
71t_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////////////////////////////////////////////////////////////////////////////
87t_bnscaster::~t_bnscaster() {
88 delete _outSocket;
89 delete _outStream;
90 delete _outFile;
91}
92
93// Start the Communication with NTRIP Caster
94////////////////////////////////////////////////////////////////////////////
95void t_bnscaster::open() {
96
97 if (_mountpoint.isEmpty()) {
98 return;
99 }
100
101 if (_outSocket != 0 &&
102 _outSocket->state() == QAbstractSocket::ConnectedState) {
103 return;
104 }
105
106 delete _outSocket; _outSocket = 0;
107
108 double minDt = pow(2.0,_sOpenTrial);
109 if (++_sOpenTrial > 4) {
110 _sOpenTrial = 4;
111 }
112 if (_outSocketOpenTime.isValid() &&
113 _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
114 return;
115 }
116 else {
117 _outSocketOpenTime = QDateTime::currentDateTime();
118 }
119
120 bnsSettings settings;
121 _outSocket = new QTcpSocket();
122 QString password;
123 if (_ic == 1) {
124 _outSocket->connectToHost(settings.value("outHost1").toString(),
125 settings.value("outPort1").toInt());
126 password = settings.value("password1").toString();
127 }
128 if (_ic == 2) {
129 _outSocket->connectToHost(settings.value("outHost2").toString(),
130 settings.value("outPort2").toInt());
131 password = settings.value("password2").toString();
132 }
133 if (_ic == 3) {
134 _outSocket->connectToHost(settings.value("outHost3").toString(),
135 settings.value("outPort3").toInt());
136 password = settings.value("password3").toString();
137 }
138
139 if (_ic == 4) {
140 _outSocket->connectToHost(settings.value("outHost4").toString(),
141 settings.value("outPort4").toInt());
142 password = settings.value("password4").toString();
143 }
144
145 if (_ic == 5) {
146 _outSocket->connectToHost(settings.value("outHost5").toString(),
147 settings.value("outPort5").toInt());
148 password = settings.value("password5").toString();
149 }
150
151 if (_ic == 6) {
152 _outSocket->connectToHost(settings.value("outHost6").toString(),
153 settings.value("outPort6").toInt());
154 password = settings.value("password6").toString();
155 }
156
157 if (_ic == 7) {
158 _outSocket->connectToHost(settings.value("outHost7").toString(),
159 settings.value("outPort7").toInt());
160 password = settings.value("password7").toString();
161 }
162
163 if (_ic == 8) {
164 _outSocket->connectToHost(settings.value("outHost8").toString(),
165 settings.value("outPort8").toInt());
166 password = settings.value("password8").toString();
167 }
168
169 if (_ic == 9) {
170 _outSocket->connectToHost(settings.value("outHost9").toString(),
171 settings.value("outPort9").toInt());
172 password = settings.value("password9").toString();
173 }
174
175 if (_ic == 10) {
176 _outSocket->connectToHost(settings.value("outHost10").toString(),
177 settings.value("outPort10").toInt());
178 password = settings.value("password10").toString();
179 }
180
181 if (_ic == 0) {
182 _outSocket->connectToHost(settings.value("outHostEph").toString(),
183 settings.value("outPortEph").toInt());
184 password = settings.value("passwordEph").toString();
185 }
186
187 const int timeOut = 5000; // 5 seconds
188 if (!_outSocket->waitForConnected(timeOut)) {
189 delete _outSocket;
190 _outSocket = 0;
191 emit(newMessage("Broadcaster: Connect timeout"));
192 return;
193 }
194
195 QByteArray msg = "SOURCE " + password.toAscii() + " /" +
196 _mountpoint.toAscii() + "\r\n" +
197 "Source-Agent: NTRIP BNS/" BNSVERSION "\r\n\r\n";
198
199 _outSocket->write(msg);
200 _outSocket->waitForBytesWritten();
201
202 _outSocket->waitForReadyRead();
203 QByteArray ans = _outSocket->readLine();
204
205 if (ans.indexOf("OK") == -1) {
206 delete _outSocket;
207 _outSocket = 0;
208 emit(newMessage("Broadcaster: Connection broken"));
209 }
210 else {
211 emit(newMessage("Broadcaster: Connection opened"));
212 _sOpenTrial = 0;
213 }
214}
215
216// Write buffer
217////////////////////////////////////////////////////////////////////////////
218void t_bnscaster::write(char* buffer, unsigned len) {
219 if (_outSocket) {
220 _outSocket->write(buffer, len);
221 _outSocket->flush();
222 }
223}
224
225// Print Ascii Output
226////////////////////////////////////////////////////////////////////////////
227void t_bnscaster::printAscii(const QString& line) {
228 if (_outStream) {
229 *_outStream << line;
230 _outStream->flush();
231 }
232}
Note: See TracBrowser for help on using the repository browser.