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

Last change on this file since 3081 was 2645, checked in by mervart, 14 years ago
File size: 6.3 KB
RevLine 
[1060]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"
[1668]19#include "bnssettings.h"
[2509]20#include "bnsversion.h"
[1060]21
22using namespace std;
23
24// Constructor
25////////////////////////////////////////////////////////////////////////////
[1067]26t_bnscaster::t_bnscaster(const QString& mountpoint, const QString& outFileName,
[1698]27 int ic) {
[1067]28
[1668]29 bnsSettings settings;
[1065]30
[1698]31 _mountpoint = mountpoint;
32 _ic = ic;
33 _outSocket = 0;
34 _sOpenTrial = 0;
[1065]35
36 if (outFileName.isEmpty()) {
37 _outFile = 0;
38 _outStream = 0;
39 }
40 else {
41 _outFile = new QFile(outFileName);
[1698]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
[1065]51 if (_outFile->open(oMode)) {
52 _outStream = new QTextStream(_outFile);
53 }
54 }
[1066]55
56 // Reference frame
57 // ---------------
[1772]58 _crdTrafo = settings.value(QString("refSys_%1").arg(_ic)).toString();
[1698]59
[2046]60 if ( Qt::CheckState(settings.value(QString("CoM_%1").arg(_ic)).toInt())
[1698]61 == Qt::Checked ) {
[2046]62 _CoM = true;
[1698]63 }
64 else {
[2046]65 _CoM = false;
[1698]66 }
[1060]67}
68
[1796]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 = "";
[2046]82 _CoM = false;
[1796]83}
84
[1060]85// Destructor
86////////////////////////////////////////////////////////////////////////////
87t_bnscaster::~t_bnscaster() {
88 delete _outSocket;
[1065]89 delete _outStream;
90 delete _outFile;
[1060]91}
92
93// Start the Communication with NTRIP Caster
94////////////////////////////////////////////////////////////////////////////
[2645]95void t_bnscaster::open() {
[1060]96
[1123]97 if (_mountpoint.isEmpty()) {
98 return;
99 }
100
[2645]101 if (_outSocket != 0 &&
102 _outSocket->state() == QAbstractSocket::ConnectedState) {
103 return;
[1060]104 }
105
106 delete _outSocket; _outSocket = 0;
107
[1698]108 double minDt = pow(2.0,_sOpenTrial);
[2331]109 if (++_sOpenTrial > 4) {
110 _sOpenTrial = 4;
[1060]111 }
[2645]112 if (_outSocketOpenTime.isValid() &&
[1060]113 _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
114 return;
115 }
116 else {
117 _outSocketOpenTime = QDateTime::currentDateTime();
118 }
119
[1668]120 bnsSettings settings;
[1060]121 _outSocket = new QTcpSocket();
[1249]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();
[1253]127 }
[1249]128 if (_ic == 2) {
129 _outSocket->connectToHost(settings.value("outHost2").toString(),
130 settings.value("outPort2").toInt());
131 password = settings.value("password2").toString();
132 }
[1728]133 if (_ic == 3) {
134 _outSocket->connectToHost(settings.value("outHost3").toString(),
135 settings.value("outPort3").toInt());
136 password = settings.value("password3").toString();
137 }
[2328]138
139 if (_ic == 4) {
140 _outSocket->connectToHost(settings.value("outHost4").toString(),
141 settings.value("outPort4").toInt());
[2329]142 password = settings.value("password4").toString();
[2328]143 }
144
145 if (_ic == 5) {
146 _outSocket->connectToHost(settings.value("outHost5").toString(),
147 settings.value("outPort5").toInt());
[2329]148 password = settings.value("password5").toString();
[2328]149 }
150
151 if (_ic == 6) {
152 _outSocket->connectToHost(settings.value("outHost6").toString(),
153 settings.value("outPort6").toInt());
[2329]154 password = settings.value("password6").toString();
[2328]155 }
156
[2374]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
[1797]181 if (_ic == 0) {
182 _outSocket->connectToHost(settings.value("outHostEph").toString(),
183 settings.value("outPortEph").toInt());
184 password = settings.value("passwordEph").toString();
185 }
[1060]186
[1124]187 const int timeOut = 5000; // 5 seconds
[1060]188 if (!_outSocket->waitForConnected(timeOut)) {
189 delete _outSocket;
190 _outSocket = 0;
[1945]191 emit(newMessage("Broadcaster: Connect timeout"));
[1060]192 return;
193 }
194
195 QByteArray msg = "SOURCE " + password.toAscii() + " /" +
196 _mountpoint.toAscii() + "\r\n" +
[2509]197 "Source-Agent: NTRIP BNS/" BNSVERSION "\r\n\r\n";
[1060]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;
[1698]208 emit(newMessage("Broadcaster: Connection broken"));
[1060]209 }
210 else {
[1698]211 emit(newMessage("Broadcaster: Connection opened"));
212 _sOpenTrial = 0;
[1060]213 }
214}
215
216// Write buffer
217////////////////////////////////////////////////////////////////////////////
218void t_bnscaster::write(char* buffer, unsigned len) {
[2645]219 if (_outSocket) {
220 _outSocket->write(buffer, len);
221 _outSocket->flush();
[1060]222 }
223}
[1065]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.