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

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