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

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

* empty log message *

File size: 4.1 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
20using namespace std;
21
22// Constructor
23////////////////////////////////////////////////////////////////////////////
24t_bnscaster::t_bnscaster(const QString& mountpoint, const QString& outFileName,
25 const QString& refSys, const int ic) {
26
27 _mountpoint = mountpoint;
28 _outSocket = 0;
29 _outSocketOpenTrial = 0;
30 _ic = ic;
31
32 QSettings settings;
33
34 QIODevice::OpenMode oMode;
35 if (Qt::CheckState(settings.value("fileAppend").toInt()) == Qt::Checked) {
36 oMode = QIODevice::WriteOnly | QIODevice::Unbuffered | QIODevice::Append;
37 }
38 else {
39 oMode = QIODevice::WriteOnly | QIODevice::Unbuffered;
40 }
41
42 if (outFileName.isEmpty()) {
43 _outFile = 0;
44 _outStream = 0;
45 }
46 else {
47 _outFile = new QFile(outFileName);
48 if (_outFile->open(oMode)) {
49 _outStream = new QTextStream(_outFile);
50 }
51 }
52
53 // Reference frame
54 // ---------------
55 _crdTrafo = false;
56 if (refSys == "ETRF2000") {
57 _crdTrafo = true;
58 }
59}
60
61// Destructor
62////////////////////////////////////////////////////////////////////////////
63t_bnscaster::~t_bnscaster() {
64 delete _outSocket;
65 delete _outStream;
66 delete _outFile;
67}
68
69// Start the Communication with NTRIP Caster
70////////////////////////////////////////////////////////////////////////////
71void t_bnscaster::open() {
72
73 if (_mountpoint.isEmpty()) {
74 return;
75 }
76
77 if (_outSocket != 0 &&
78 _outSocket->state() == QAbstractSocket::ConnectedState) {
79 return;
80 }
81
82 delete _outSocket; _outSocket = 0;
83
84 double minDt = exp2(_outSocketOpenTrial);
85 if (++_outSocketOpenTrial > 8) {
86 _outSocketOpenTrial = 8;
87 }
88 if (_outSocketOpenTime.isValid() &&
89 _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
90 return;
91 }
92 else {
93 _outSocketOpenTime = QDateTime::currentDateTime();
94 }
95
96 QSettings settings;
97 _outSocket = new QTcpSocket();
98 QString password;
99 if (_ic == 1) {
100 _outSocket->connectToHost(settings.value("outHost1").toString(),
101 settings.value("outPort1").toInt());
102 password = settings.value("password1").toString();
103 }
104 if (_ic == 2) {
105 _outSocket->connectToHost(settings.value("outHost2").toString(),
106 settings.value("outPort2").toInt());
107 password = settings.value("password2").toString();
108 }
109
110 const int timeOut = 5000; // 5 seconds
111 if (!_outSocket->waitForConnected(timeOut)) {
112 delete _outSocket;
113 _outSocket = 0;
114 //emit(error("t_bnscaster::open Connect Timeout"));
115 emit(error("Broadcaster; Connect Timeout"));
116 return;
117 }
118
119 QByteArray msg = "SOURCE " + password.toAscii() + " /" +
120 _mountpoint.toAscii() + "\r\n" +
121 "Source-Agent: NTRIP BNS/1.0\r\n\r\n";
122
123 _outSocket->write(msg);
124 _outSocket->waitForBytesWritten();
125
126 _outSocket->waitForReadyRead();
127 QByteArray ans = _outSocket->readLine();
128
129 if (ans.indexOf("OK") == -1) {
130 delete _outSocket;
131 _outSocket = 0;
132// emit(newMessage("t_bnscaster::open socket deleted"));
133 emit(newMessage("Broadcaster: Connection broken")); // weber
134 }
135 else {
136// emit(newMessage("t_bnscaster::open socket OK"));
137 emit(newMessage("Broadcaster: Connection opened")); // weber
138 _outSocketOpenTrial = 0;
139 }
140}
141
142// Write buffer
143////////////////////////////////////////////////////////////////////////////
144void t_bnscaster::write(char* buffer, unsigned len) {
145 if (_outSocket) {
146 _outSocket->write(buffer, len);
147 _outSocket->flush();
148 }
149}
150
151// Print Ascii Output
152////////////////////////////////////////////////////////////////////////////
153void t_bnscaster::printAscii(const QString& line) {
154 if (_outStream) {
155 *_outStream << line;
156 _outStream->flush();
157 }
158}
Note: See TracBrowser for help on using the repository browser.