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

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

* empty log message *

File size: 4.0 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 const QString& refSys, const int ic) {
27
28 _mountpoint = mountpoint;
29 _outSocket = 0;
30 _outSocketOpenTrial = 0;
31 _ic = ic;
32
33 bnsSettings settings;
34
35 QIODevice::OpenMode oMode;
36 if (Qt::CheckState(settings.value("fileAppend").toInt()) == Qt::Checked) {
37 oMode = QIODevice::WriteOnly | QIODevice::Unbuffered | QIODevice::Append;
38 }
39 else {
40 oMode = QIODevice::WriteOnly | QIODevice::Unbuffered;
41 }
42
43 if (outFileName.isEmpty()) {
44 _outFile = 0;
45 _outStream = 0;
46 }
47 else {
48 _outFile = new QFile(outFileName);
49 if (_outFile->open(oMode)) {
50 _outStream = new QTextStream(_outFile);
51 }
52 }
53
54 // Reference frame
55 // ---------------
56 _crdTrafo = false;
57 if (refSys == "ETRF2000") {
58 _crdTrafo = true;
59 }
60}
61
62// Destructor
63////////////////////////////////////////////////////////////////////////////
64t_bnscaster::~t_bnscaster() {
65 delete _outSocket;
66 delete _outStream;
67 delete _outFile;
68}
69
70// Start the Communication with NTRIP Caster
71////////////////////////////////////////////////////////////////////////////
72void t_bnscaster::open() {
73
74 if (_mountpoint.isEmpty()) {
75 return;
76 }
77
78 if (_outSocket != 0 &&
79 _outSocket->state() == QAbstractSocket::ConnectedState) {
80 return;
81 }
82
83 delete _outSocket; _outSocket = 0;
84
85 double minDt = pow(2.0,_outSocketOpenTrial);
86 if (++_outSocketOpenTrial > 8) {
87 _outSocketOpenTrial = 8;
88 }
89 if (_outSocketOpenTime.isValid() &&
90 _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
91 return;
92 }
93 else {
94 _outSocketOpenTime = QDateTime::currentDateTime();
95 }
96
97 bnsSettings settings;
98 _outSocket = new QTcpSocket();
99 QString password;
100 if (_ic == 1) {
101 _outSocket->connectToHost(settings.value("outHost1").toString(),
102 settings.value("outPort1").toInt());
103 password = settings.value("password1").toString();
104 }
105 if (_ic == 2) {
106 _outSocket->connectToHost(settings.value("outHost2").toString(),
107 settings.value("outPort2").toInt());
108 password = settings.value("password2").toString();
109 }
110
111 const int timeOut = 5000; // 5 seconds
112 if (!_outSocket->waitForConnected(timeOut)) {
113 delete _outSocket;
114 _outSocket = 0;
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.