source: ntrip/trunk/BNC/upload/bncuploadcaster.cpp@ 3172

Last change on this file since 3172 was 3172, checked in by mervart, 13 years ago
File size: 3.8 KB
Line 
1/* -------------------------------------------------------------------------
2 * BKG NTRIP Server
3 * -------------------------------------------------------------------------
4 *
5 * Class: bncUploadCaster
6 *
7 * Purpose: Connection to NTRIP Caster
8 *
9 * Author: L. Mervart
10 *
11 * Created: 29-Mar-2011
12 *
13 * Changes:
14 *
15 * -----------------------------------------------------------------------*/
16
17#include <math.h>
18#include "bncuploadcaster.h"
19#include "bncsettings.h"
20#include "bncversion.h"
21#include "bncapp.h"
22
23using namespace std;
24
25// Constructor
26////////////////////////////////////////////////////////////////////////////
27bncUploadCaster::bncUploadCaster(const QString& mountpoint,
28 const QString& outHost, int outPort,
29 const QString& password,
30 const QString& outFileName) {
31
32 bncSettings settings;
33
34 _mountpoint = mountpoint;
35 _outHost = outHost;
36 _outPort = outPort;
37 _password = password;
38
39 _outSocket = 0;
40 _sOpenTrial = 0;
41
42 if (outFileName.isEmpty()) {
43 _outFile = 0;
44 _outStream = 0;
45 }
46 else {
47 _outFile = new QFile(outFileName);
48 QIODevice::OpenMode oMode;
49 if (Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
50 oMode = QIODevice::WriteOnly | QIODevice::Unbuffered | QIODevice::Append;
51 }
52 else {
53 oMode = QIODevice::WriteOnly | QIODevice::Unbuffered;
54 }
55
56 if (_outFile->open(oMode)) {
57 _outStream = new QTextStream(_outFile);
58 }
59 }
60 connect(this, SIGNAL(newMessage(QByteArray,bool)),
61 ((bncApp*)qApp), SLOT(slotMessage(const QByteArray,bool)));
62}
63
64// Destructor
65////////////////////////////////////////////////////////////////////////////
66bncUploadCaster::~bncUploadCaster() {
67 delete _outSocket;
68 delete _outStream;
69 delete _outFile;
70}
71
72// Start the Communication with NTRIP Caster
73////////////////////////////////////////////////////////////////////////////
74void bncUploadCaster::open() {
75
76 if (_mountpoint.isEmpty()) {
77 return;
78 }
79
80 if (_outSocket != 0 &&
81 _outSocket->state() == QAbstractSocket::ConnectedState) {
82 return;
83 }
84
85 delete _outSocket; _outSocket = 0;
86
87 double minDt = pow(2.0,_sOpenTrial);
88 if (++_sOpenTrial > 4) {
89 _sOpenTrial = 4;
90 }
91 if (_outSocketOpenTime.isValid() &&
92 _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
93 return;
94 }
95 else {
96 _outSocketOpenTime = QDateTime::currentDateTime();
97 }
98
99 bncSettings settings;
100 _outSocket = new QTcpSocket();
101 _outSocket->connectToHost(_outHost, _outPort);
102
103 const int timeOut = 5000; // 5 seconds
104 if (!_outSocket->waitForConnected(timeOut)) {
105 delete _outSocket;
106 _outSocket = 0;
107 emit(newMessage("Broadcaster: Connect timeout"));
108 return;
109 }
110
111 QByteArray msg = "SOURCE " + _password.toAscii() + " /" +
112 _mountpoint.toAscii() + "\r\n" +
113 "Source-Agent: NTRIP BNC/" BNCVERSION "\r\n\r\n";
114
115 _outSocket->write(msg);
116 _outSocket->waitForBytesWritten();
117
118 _outSocket->waitForReadyRead();
119 QByteArray ans = _outSocket->readLine();
120
121 if (ans.indexOf("OK") == -1) {
122 delete _outSocket;
123 _outSocket = 0;
124 emit(newMessage("Broadcaster: Connection broken"));
125 }
126 else {
127 emit(newMessage("Broadcaster: Connection opened"));
128 _sOpenTrial = 0;
129 }
130}
131
132// Write buffer
133////////////////////////////////////////////////////////////////////////////
134void bncUploadCaster::write(char* buffer, unsigned len) {
135 if (_outSocket) {
136 _outSocket->write(buffer, len);
137 _outSocket->flush();
138 }
139}
140
141// Print Ascii Output
142////////////////////////////////////////////////////////////////////////////
143void bncUploadCaster::printAscii(const QString& line) {
144 if (_outStream) {
145 *_outStream << line;
146 _outStream->flush();
147 }
148}
Note: See TracBrowser for help on using the repository browser.