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

Last change on this file since 2328 was 2328, checked in by weber, 14 years ago

* empty log message *

File size: 5.4 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
21using namespace std;
22
23// Constructor
24////////////////////////////////////////////////////////////////////////////
25t_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////////////////////////////////////////////////////////////////////////////
70t_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////////////////////////////////////////////////////////////////////////////
86t_bnscaster::~t_bnscaster() {
87 delete _outSocket;
88 delete _outStream;
89 delete _outFile;
90}
91
92// Start the Communication with NTRIP Caster
93////////////////////////////////////////////////////////////////////////////
94void 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("password3").toString();
142 }
143
144 if (_ic == 5) {
145 _outSocket->connectToHost(settings.value("outHost5").toString(),
146 settings.value("outPort5").toInt());
147 password = settings.value("password3").toString();
148 }
149
150 if (_ic == 6) {
151 _outSocket->connectToHost(settings.value("outHost6").toString(),
152 settings.value("outPort6").toInt());
153 password = settings.value("password3").toString();
154 }
155
156 if (_ic == 0) {
157 _outSocket->connectToHost(settings.value("outHostEph").toString(),
158 settings.value("outPortEph").toInt());
159 password = settings.value("passwordEph").toString();
160 }
161
162 const int timeOut = 5000; // 5 seconds
163 if (!_outSocket->waitForConnected(timeOut)) {
164 delete _outSocket;
165 _outSocket = 0;
166 emit(newMessage("Broadcaster: Connect timeout"));
167 return;
168 }
169
170 QByteArray msg = "SOURCE " + password.toAscii() + " /" +
171 _mountpoint.toAscii() + "\r\n" +
172 "Source-Agent: NTRIP BNS/1.1\r\n\r\n";
173
174 _outSocket->write(msg);
175 _outSocket->waitForBytesWritten();
176
177 _outSocket->waitForReadyRead();
178 QByteArray ans = _outSocket->readLine();
179
180 if (ans.indexOf("OK") == -1) {
181 delete _outSocket;
182 _outSocket = 0;
183 emit(newMessage("Broadcaster: Connection broken"));
184 }
185 else {
186 emit(newMessage("Broadcaster: Connection opened"));
187 _sOpenTrial = 0;
188 }
189}
190
191// Write buffer
192////////////////////////////////////////////////////////////////////////////
193void t_bnscaster::write(char* buffer, unsigned len) {
194 if (_outSocket) {
195 _outSocket->write(buffer, len);
196 _outSocket->flush();
197 }
198}
199
200// Print Ascii Output
201////////////////////////////////////////////////////////////////////////////
202void t_bnscaster::printAscii(const QString& line) {
203 if (_outStream) {
204 *_outStream << line;
205 _outStream->flush();
206 }
207}
Note: See TracBrowser for help on using the repository browser.