Remoto - VFS: VFS_udp_socket.cpp Source File
Remoto - VFS
VFS_udp_socket.cpp
Go to the documentation of this file.
1 
2 #include <QNetworkDatagram>
3 
4 #include "VFS_udp_socket.h"
5 
6 #include "VFS.h"
7 
22 VFS_udp_socket::VFS_udp_socket(QHostAddress multicast, quint16 port, QString mode)
23 : VFS_node()
24 , _address(multicast)
25 , _port(port)
26 , _socket(this)
27 , _mode(mode)
28 {
29  if (_mode == "w" || _mode == "rw")
30  {
31  //connect(this,SIGNAL(mounted()),this,SLOT(listen()));
32  connect(VFS::root(),SIGNAL(initialized()),this,SLOT(listen()));
33 
34  connect( &_socket, SIGNAL( error(QAbstractSocket::SocketError) ),
35  this, SLOT( socketError(QAbstractSocket::SocketError) ) );
36  }
37  else
38  VFS::ERROR( QString("%1 bad mode: %2").arg(className()).arg(_mode) );
39 }
40 
42 {
43  _socket.close();
44 }
45 
54 {
55  return false;
56 }
57 
66 {
67  if (_mode.contains("r"))
68  {
69  //bool l = _socket.bind( _address, _port);
70  //bool l = _socket.bind( _port, QUdpSocket::ShareAddress );
71 
72  bool l = _socket.bind(QHostAddress::AnyIPv4, _port, QUdpSocket::ShareAddress);
73  _socket.joinMulticastGroup(_address);
74 
75  if (!l)
76  VFS::WARN( QString("%1 could not bind on UDP %2:%3. Is it already in use?").arg(className()).arg(_address.toString()).arg(_port) );
77  else
78  {
79  if (_port == 0)
80  _port = _socket.localPort();
81 
82  //_address = _socket.localAddress();
83 
84  connect(&_socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
85  VFS::LOG( QString("%1 listening on UDP multicast group %2:%3 (insecure)").arg(className()).arg(_address.toString()).arg(_port), 2 );
86  }
87 
88  return l;
89  }
90  else
91  {
92  VFS::LOG( QString("%1 created on UDP multicast group %2:%3 (insecure)").arg(className()).arg(_address.toString()).arg(_port), 2 );
93  return true;
94  }
95 }
96 
97 
106 {
107  while (_socket.hasPendingDatagrams())
108  {
109  QNetworkDatagram datagram = _socket.receiveDatagram();
110  VFS::WARN( QString("Datagram: %1").arg( qUtf8Printable(datagram.data()) ) );
111  }
112 }
113 
121 void VFS_udp_socket::socketError(QAbstractSocket::SocketError error)
122 {
123  VFS::ERROR( QString("%1: (%2) %3").arg(className()).arg(error).arg(_socket.errorString()) );
124  VFS::WARN( QString("(%1) Seems like something should happen here.").arg(className()) );
125 }
126 
133 {
134  r->_success = true;
135 
136  QNetworkDatagram d(r->toJson(), _address, _port);
137 
138  qint64 l = _socket.writeDatagram(d);
139 
140  if (l > -1)
141  r->_success = true;
142  else
143  { r->_success = false;
144  r->_reason = "Could not send datagram";
145  }
146 }
147 
155 {
156  return QString("address: %1\nport: %2\nstate: %3").arg(_address.toString()).arg(_port).arg(_socket.isOpen() ? "open" : "closed");
157 }
VFS_node is the base class from which all other VFS_node classes derive.
Definition: VFS_node.h:143
QString className()
Return the class name of a node.
Definition: VFS_node.cpp:2039
The base class for all requests between nodes.
Definition: VFS_node.h:54
QString _reason
if something (probably bad) happened, this is the reason
Definition: VFS_node.h:108
bool _success
if the request was successfully completed
Definition: VFS_node.h:107
virtual QByteArray toJson(postID id=0, bool ignoreSuccess=false, bool includeInitialPath=false)
Serialize this request.
Definition: VFS_node.cpp:415
virtual void socketError(QAbstractSocket::SocketError error)
virtual bool isContainer()
A VFS_udp_socket cannot have children.
QString _mode
Either "rw" or "w".
virtual QString reportDetails()
Report details of this node.
quint16 _port
UDP port.
virtual ~VFS_udp_socket()
Q_INVOKABLE VFS_udp_socket(QHostAddress multicast, quint16 port, QString mode="rw")
Create a VFS_udp_socket.
QHostAddress _address
Address used to listen or speak.
QUdpSocket _socket
UDP socket.
virtual bool listen()
virtual void write(VFS_request *r)
Write to this node, which will emit UDP datagrams to the network.
static VFS * root()
Return the root node of the VFS filesystem.
Definition: VFS.cpp:399
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
setter error
Set the error value of this widget.