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

Last change on this file since 2382 was 2374, checked in by weber, 15 years ago

* empty log message *

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