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

Last change on this file since 1747 was 1728, checked in by weber, 16 years ago

* empty log message *

File size: 4.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
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 if (settings.value(QString("refSys_%1").arg(_ic)).toString() == "ETRF2000") {
58 _crdTrafo = true;
59 }
60 else {
61 _crdTrafo = false;
62 }
63
64 if ( Qt::CheckState(settings.value(QString("beClocks%1").arg(_ic)).toInt())
65 == Qt::Checked ) {
66 _beClocks = true;
67 }
68 else {
69 _beClocks = false;
70 }
71}
72
73// Destructor
74////////////////////////////////////////////////////////////////////////////
75t_bnscaster::~t_bnscaster() {
76 delete _outSocket;
77 delete _outStream;
78 delete _outFile;
79}
80
81// Start the Communication with NTRIP Caster
82////////////////////////////////////////////////////////////////////////////
83void t_bnscaster::open() {
84
85 if (_mountpoint.isEmpty()) {
86 return;
87 }
88
89 if (_outSocket != 0 &&
90 _outSocket->state() == QAbstractSocket::ConnectedState) {
91 return;
92 }
93
94 delete _outSocket; _outSocket = 0;
95
96 double minDt = pow(2.0,_sOpenTrial);
97 if (++_sOpenTrial > 8) {
98 _sOpenTrial = 8;
99 }
100 if (_outSocketOpenTime.isValid() &&
101 _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
102 return;
103 }
104 else {
105 _outSocketOpenTime = QDateTime::currentDateTime();
106 }
107
108 bnsSettings settings;
109 _outSocket = new QTcpSocket();
110 QString password;
111 if (_ic == 1) {
112 _outSocket->connectToHost(settings.value("outHost1").toString(),
113 settings.value("outPort1").toInt());
114 password = settings.value("password1").toString();
115 }
116 if (_ic == 2) {
117 _outSocket->connectToHost(settings.value("outHost2").toString(),
118 settings.value("outPort2").toInt());
119 password = settings.value("password2").toString();
120 }
121 if (_ic == 3) {
122 _outSocket->connectToHost(settings.value("outHost3").toString(),
123 settings.value("outPort3").toInt());
124 password = settings.value("password3").toString();
125 }
126
127 const int timeOut = 5000; // 5 seconds
128 if (!_outSocket->waitForConnected(timeOut)) {
129 delete _outSocket;
130 _outSocket = 0;
131 emit(error("Broadcaster: Connect timeout"));
132 return;
133 }
134
135 QByteArray msg = "SOURCE " + password.toAscii() + " /" +
136 _mountpoint.toAscii() + "\r\n" +
137 "Source-Agent: NTRIP BNS/1.0\r\n\r\n";
138
139 _outSocket->write(msg);
140 _outSocket->waitForBytesWritten();
141
142 _outSocket->waitForReadyRead();
143 QByteArray ans = _outSocket->readLine();
144
145 if (ans.indexOf("OK") == -1) {
146 delete _outSocket;
147 _outSocket = 0;
148 emit(newMessage("Broadcaster: Connection broken"));
149 }
150 else {
151 emit(newMessage("Broadcaster: Connection opened"));
152 _sOpenTrial = 0;
153 }
154}
155
156// Write buffer
157////////////////////////////////////////////////////////////////////////////
158void t_bnscaster::write(char* buffer, unsigned len) {
159 if (_outSocket) {
160 _outSocket->write(buffer, len);
161 _outSocket->flush();
162 }
163}
164
165// Print Ascii Output
166////////////////////////////////////////////////////////////////////////////
167void t_bnscaster::printAscii(const QString& line) {
168 if (_outStream) {
169 *_outStream << line;
170 _outStream->flush();
171 }
172}
Note: See TracBrowser for help on using the repository browser.