Remoto - VFS: VFS_tcp_export.cpp Source File
Remoto - VFS
VFS_tcp_export.cpp
Go to the documentation of this file.
1 
2 #include <QJsonDocument>
3 #include <QJsonObject>
4 
5 #include "VFS_tcp_export.h"
7 #include "VFS_base/VFS_icons.h"
8 #include "utilities/rutils.h"
9 
10 #include "VFS.h"
11 
51 VFS_tcp_export::VFS_tcp_export(QString exportRoot, quint16 port, QHostAddress address, bool ssl, QString sslCertPath, QString sslKeyPath, QByteArray icon, QString broadcastName, QString broadcastAddress, QString multicastAddress, quint16 multicastPort, QJsonArray tags)
52 : VFS_tcp_server(port,address,ssl,sslCertPath,sslKeyPath)
53 , _exportRoot(exportRoot)
54 , _broadcastid(-1)
55 , _broadcastName(broadcastName)
56 , _broadcastAddress(broadcastAddress)
57 , _multicastPort(multicastPort)
58 , _multicastAddress(multicastAddress)
59 , _tags(tags)
60 , _icon(icon)
61 {
63 
64  if (_multicastPort)
65  {
66  if (_broadcastAddress == "")
67  {
68  VFS::ERROR("A multicast port was specified, but a broadcastAddress was not. Will not broadcast.",0,className());
69  _multicastPort = 0;
70  }
71  else
72  _broadcastSocket.bind(QHostAddress(QHostAddress::AnyIPv4), 0);
73  }
74 }
75 
77 {
78 }
79 
88 {
89  return VFS_icons::get("unknown");
90 }
91 
92 
100 {
101  return "export: "+_exportRoot+"\n"+VFS_tcp_server::reportDetails();
102 }
103 
104 
113 VFS_tcp_client *VFS_tcp_export::newClient(QTcpSocket *socket, bool ssl, QSslConfiguration sslConfiguration)
114 {
115  VFS_tcp_client *client = new VFS_tcp_export_client(socket, _exportRoot, ssl, sslConfiguration, _icon );
116 
117  if (_broadcastid != -1)
118  {
119  killTimer(_broadcastid);
120  _broadcastid = -1;
121  VFS::LOG( QString("Mount received. Stopping broadcast on '%1'...").arg(_exportRoot), 9, className() );
122 
123  connect(client,SIGNAL(finished()),this,SLOT(startBroadcast()));
124  }
125 
126  return client;
127 }
128 
129 
138 {
139  Q_UNUSED(message)
140 
141  //VFS::WARN("clientMessage() should be implemented in VFS_tcp_export subclasses");
142 }
143 
144 
153 {
154  bool l = VFS_tcp_server::listen();
155 
156  if (l) startBroadcast();
157 
158  return l;
159 }
160 
161 
167 void VFS_tcp_export::timerEvent(QTimerEvent *event)
168 {
169  if (event->timerId() == _broadcastid)
170  sendBroadcast();
171  else
172  VFS::WARN("Unknown timer event.",0,className());
173 }
174 
175 
181 {
182  if (_multicastPort && _broadcastid==-1)
183  {
184  //https://docs.oracle.com/cd/E23824_01/html/821-1602/sockets-137.html
185  _broadcastSocket.setSocketOption(QAbstractSocket::MulticastTtlOption, 35);
186 
187  _broadcastid = startTimer(3000);
188  sendBroadcast();
189  }
190 }
191 
192 
198 {
199  VFS::LOG( QString("Broadcasting availability of '%1' on UDP %2:%3...").arg(_exportRoot).arg(_multicastAddress.toString()).arg(_multicastPort), 9, className() );
200 
201  QJsonObject b;
202  b["name"] = _broadcastName;
203  b["address"] = _broadcastAddress;
204  b["port"] = _port;
205  b["ssl"] = _ssl;
206  b["tags"] = _tags;
207 
208  QByteArray datagram = QJsonDocument(b).toJson();
209  _broadcastSocket.writeDatagram(datagram, _multicastAddress, _multicastPort);
210 }
static char * get(QString which="")
Fetch an icon from the library.
Definition: VFS_icons.cpp:34
QString className()
Return the class name of a node.
Definition: VFS_node.cpp:2039
void finished(bool andDelete=false)
Emitted if a thread fails to create its node, or a node is rm()'d, or any other reason a node has com...
The socket client created by VFS_tcp_export.
virtual ~VFS_tcp_export()
Q_INVOKABLE VFS_tcp_export(QString exportRoot, quint16 port, QHostAddress address=QHostAddress::Any, bool ssl=false, QString sslCertPath="", QString sslKeyPath="", QByteArray icon="", QString broadcastName="", QString broadcastAddress="", QString multicastAddress="", quint16 multicastPort=0, QJsonArray tags=QJsonArray())
VFS_tcp_export constructor.
virtual void timerEvent(QTimerEvent *event)
A timer event has fired, probably for sendBroadcast.
quint16 _multicastPort
QString _broadcastAddress
QString _broadcastName
virtual VFS_tcp_client * newClient(QTcpSocket *socket, bool ssl=false, QSslConfiguration sslConfiguration=QSslConfiguration())
Create a new VFS_tcp_export_client with the new socket.
QByteArray _icon
The optional override icon.
virtual QByteArray icon()
VFS_tcp_mount::icon.
virtual void startBroadcast()
Start broadcasting this mount.
QJsonArray _tags
QHostAddress _multicastAddress
virtual QString reportDetails()
Report details of this node.
QString _exportRoot
The VFS path to export.
virtual bool listen()
Open the server socket and listen for new connections.
QUdpSocket _broadcastSocket
virtual void sendBroadcast()
Broadcast information about this mount to the network.
virtual void clientMessage(QByteArray message)
A client message has been received.
Open a listening TCP port for other clients to connect to.
virtual QString reportDetails()
Report details of this node.
virtual bool listen()
Open the server socket and listen for new connections.
bool _ssl
Use ssl?
quint16 _port
TCP port to listen on, or 0 to find an available one.
static void LOG(QString message, int level=0, QString user="server")
Send a message to the VFS::_messages VFS_stream.
Definition: VFS.cpp:209
static void ERROR(QString message, int level=0, QString user="server")
Send a message to the VFS::_errors VFS_stream.
Definition: VFS.cpp:307
static void WARN(QString message, int level=0, QString user="server")
Send a message to the VFS::_warnings VFS_stream.
Definition: VFS.cpp:258
message(m)
Change the message of an existing arrowMessage.
QString cleanPath(QString path)
Clean and normalize a VFS path.
Definition: rutils.cpp:32