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

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

* empty log message *

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