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

Last change on this file since 2094 was 2046, checked in by mervart, 15 years ago

* empty log message *

File size: 4.8 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 if (_ic == 0) {
138 _outSocket->connectToHost(settings.value("outHostEph").toString(),
139 settings.value("outPortEph").toInt());
140 password = settings.value("passwordEph").toString();
141 }
142
143 const int timeOut = 5000; // 5 seconds
144 if (!_outSocket->waitForConnected(timeOut)) {
145 delete _outSocket;
146 _outSocket = 0;
147 emit(newMessage("Broadcaster: Connect timeout"));
148 return;
149 }
150
151 QByteArray msg = "SOURCE " + password.toAscii() + " /" +
152 _mountpoint.toAscii() + "\r\n" +
153 "Source-Agent: NTRIP BNS/1.0\r\n\r\n";
154
155 _outSocket->write(msg);
156 _outSocket->waitForBytesWritten();
157
158 _outSocket->waitForReadyRead();
159 QByteArray ans = _outSocket->readLine();
160
161 if (ans.indexOf("OK") == -1) {
162 delete _outSocket;
163 _outSocket = 0;
164 emit(newMessage("Broadcaster: Connection broken"));
165 }
166 else {
167 emit(newMessage("Broadcaster: Connection opened"));
168 _sOpenTrial = 0;
169 }
170}
171
172// Write buffer
173////////////////////////////////////////////////////////////////////////////
174void t_bnscaster::write(char* buffer, unsigned len) {
175 if (_outSocket) {
176 _outSocket->write(buffer, len);
177 _outSocket->flush();
178 }
179}
180
181// Print Ascii Output
182////////////////////////////////////////////////////////////////////////////
183void t_bnscaster::printAscii(const QString& line) {
184 if (_outStream) {
185 *_outStream << line;
186 _outStream->flush();
187 }
188}
Note: See TracBrowser for help on using the repository browser.