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

Last change on this file since 1065 was 1065, checked in by mervart, 16 years ago

* empty log message *

File size: 3.4 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) {
25 _mountpoint = mountpoint;
26 _outSocket = 0;
27 _outSocketOpenTrial = 0;
28
29 QSettings settings;
30
31 QIODevice::OpenMode oMode;
32 if (Qt::CheckState(settings.value("fileAppend").toInt()) == Qt::Checked) {
33 oMode = QIODevice::WriteOnly | QIODevice::Unbuffered | QIODevice::Append;
34 }
35 else {
36 oMode = QIODevice::WriteOnly | QIODevice::Unbuffered;
37 }
38
39 QString outFileName = settings.value("outFile").toString();
40 if (outFileName.isEmpty()) {
41 _outFile = 0;
42 _outStream = 0;
43 }
44 else {
45 _outFile = new QFile(outFileName);
46 if (_outFile->open(oMode)) {
47 _outStream = new QTextStream(_outFile);
48 }
49 }
50}
51
52// Destructor
53////////////////////////////////////////////////////////////////////////////
54t_bnscaster::~t_bnscaster() {
55 delete _outSocket;
56 delete _outStream;
57 delete _outFile;
58}
59
60// Start the Communication with NTRIP Caster
61////////////////////////////////////////////////////////////////////////////
62void t_bnscaster::open() {
63
64 if (_outSocket != 0 &&
65 _outSocket->state() == QAbstractSocket::ConnectedState) {
66 return;
67 }
68
69 delete _outSocket; _outSocket = 0;
70
71 double minDt = exp2(_outSocketOpenTrial);
72 if (++_outSocketOpenTrial > 8) {
73 _outSocketOpenTrial = 8;
74 }
75 if (_outSocketOpenTime.isValid() &&
76 _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
77 return;
78 }
79 else {
80 _outSocketOpenTime = QDateTime::currentDateTime();
81 }
82
83 QSettings settings;
84 _outSocket = new QTcpSocket();
85 _outSocket->connectToHost(settings.value("outHost").toString(),
86 settings.value("outPort").toInt());
87
88 const int timeOut = 100; // 0.1 seconds
89 if (!_outSocket->waitForConnected(timeOut)) {
90 delete _outSocket;
91 _outSocket = 0;
92 emit(error("bns::openCaster Connect Timeout"));
93 return;
94 }
95
96 QString password = settings.value("password").toString();
97
98 QByteArray msg = "SOURCE " + password.toAscii() + " /" +
99 _mountpoint.toAscii() + "\r\n" +
100 "Source-Agent: NTRIP BNS/1.0\r\n\r\n";
101
102 _outSocket->write(msg);
103 _outSocket->waitForBytesWritten();
104
105 _outSocket->waitForReadyRead();
106 QByteArray ans = _outSocket->readLine();
107
108 if (ans.indexOf("OK") == -1) {
109 delete _outSocket;
110 _outSocket = 0;
111 emit(newMessage("bns::openCaster socket deleted"));
112 }
113 else {
114 emit(newMessage("bns::openCaster socket OK"));
115 _outSocketOpenTrial = 0;
116 }
117}
118
119// Write buffer
120////////////////////////////////////////////////////////////////////////////
121void t_bnscaster::write(char* buffer, unsigned len) {
122 if (_outSocket) {
123 _outSocket->write(buffer, len);
124 _outSocket->flush();
125 }
126}
127
128// Print Ascii Output
129////////////////////////////////////////////////////////////////////////////
130void t_bnscaster::printAscii(const QString& line) {
131 if (_outStream) {
132 *_outStream << line;
133 _outStream->flush();
134 }
135}
Note: See TracBrowser for help on using the repository browser.