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
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 // ---------------
[1698]57 if (settings.value(QString("refSys_%1").arg(_ic)).toString() == "ETRF2000") {
[1066]58 _crdTrafo = true;
59 }
[1698]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 }
[1060]71}
72
73// Destructor
74////////////////////////////////////////////////////////////////////////////
75t_bnscaster::~t_bnscaster() {
76 delete _outSocket;
[1065]77 delete _outStream;
78 delete _outFile;
[1060]79}
80
81// Start the Communication with NTRIP Caster
82////////////////////////////////////////////////////////////////////////////
83void t_bnscaster::open() {
84
[1123]85 if (_mountpoint.isEmpty()) {
86 return;
87 }
88
[1060]89 if (_outSocket != 0 &&
90 _outSocket->state() == QAbstractSocket::ConnectedState) {
91 return;
92 }
93
94 delete _outSocket; _outSocket = 0;
95
[1698]96 double minDt = pow(2.0,_sOpenTrial);
97 if (++_sOpenTrial > 8) {
98 _sOpenTrial = 8;
[1060]99 }
100 if (_outSocketOpenTime.isValid() &&
101 _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
102 return;
103 }
104 else {
105 _outSocketOpenTime = QDateTime::currentDateTime();
106 }
107
[1668]108 bnsSettings settings;
[1060]109 _outSocket = new QTcpSocket();
[1249]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();
[1253]115 }
[1249]116 if (_ic == 2) {
117 _outSocket->connectToHost(settings.value("outHost2").toString(),
118 settings.value("outPort2").toInt());
119 password = settings.value("password2").toString();
120 }
[1728]121 if (_ic == 3) {
122 _outSocket->connectToHost(settings.value("outHost3").toString(),
123 settings.value("outPort3").toInt());
124 password = settings.value("password3").toString();
125 }
[1060]126
[1124]127 const int timeOut = 5000; // 5 seconds
[1060]128 if (!_outSocket->waitForConnected(timeOut)) {
129 delete _outSocket;
130 _outSocket = 0;
[1253]131 emit(error("Broadcaster: Connect timeout"));
[1060]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;
[1698]148 emit(newMessage("Broadcaster: Connection broken"));
[1060]149 }
150 else {
[1698]151 emit(newMessage("Broadcaster: Connection opened"));
152 _sOpenTrial = 0;
[1060]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}
[1065]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.