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

Last change on this file since 1698 was 1698, checked in by mervart, 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#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
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.