Remoto - VFS: VFS_websocket_VFSclient.cpp Source File
Remoto - VFS
VFS_websocket_VFSclient.cpp
Go to the documentation of this file.
1 
2 #include "VFS.h"
4 
21 VFS_websocket_VFSclient::VFS_websocket_VFSclient(QString wsurl, QString username, QString password, quint16 interval)
22 : VFS_websocket_client(wsurl,interval)
23 , _username(username)
24 , _password(password)
25 {
26 }
27 
29 {
30 }
31 
37 {
39 
40  //printf("CONNECTED! SEND AUTH!\n");
41  QJsonObject o;
42  o["username"] = _username;
43  o["password"] = _password;
44  o["type"] = "vfsclient";
45 
46  sendText("identify",o);
47 }
48 
55 void VFS_websocket_VFSclient::sendText(QString command, QJsonObject attributes)
56 {
57  QJsonObject p;
58 
59  p["command"] = command;
60  p["attributes"] = attributes;
61 
62  QString c = QJsonDocument(p).toJson();
63 
64  writeText( c );
65 }
66 
73 void VFS_websocket_VFSclient::sendBinary(QString command, QJsonObject attributes)
74 {
75  QJsonObject p;
76 
77  p["command"] = command;
78  p["attributes"] = attributes;
79 
80  QByteArray c = QJsonDocument(p).toJson();
81 
82  writeBinary( c );
83 }
84 
95 QJsonDocument VFS_websocket_VFSclient::receive(QByteArray message)
96 {
97  QJsonDocument m = VFS_websocket_client::receive(message);
98  if (m.isNull())
99  return m;
100 
101  QJsonObject o = m.object();
102  QString command = o["command"].toString();
103  QJsonObject attrs = o["attributes"].toObject();
104 
105  if (command == "error")
106  {
107  VFS::ERROR( attrs["message"].toString() );
108  m = QJsonDocument(); //make a null document
109  }
110  else if (command == "authorization")
111  {
112  if (attrs["accepted"].toBool())
113  {
114  _authenticated = true;
115  emit authorized();
116  }
117  else
118  {
119  close();
120  killTimer(_connectID); //auth has failed... don't attempt to reconnect with the same credentials
121 
122  //printf("received:\n%s\n",qUtf8Printable(message));
123 
124  VFS::ERROR( QString("%1 authorization was denied... use different credentials?").arg(className()) );
125  emit authDenied();
126  }
127  }
128  else if (command == "ping" || command == "pong")
129  {
130  //do nothing
131  }
132  else if (command == "code")
133  {
134  VFS::WARN( QString("%1 should never receive code.").arg(className()) );
135  }
136  else
137  {
138  //VFS::WARN( QString("%1 unknown command: %2").arg(className()).arg(command) );
139  emit commandReceived(command,attrs);
140  }
141 
142  /*
143  A subclass will probably want to implement:
144  diff
145  read
146  subscribe
147  metdata
148  ls
149  write
150  */
151 
152  return m;
153 }
QString className()
Return the class name of a node.
Definition: VFS_node.cpp:2039
virtual QJsonDocument receive(QByteArray message)
Receive a message from the server.
QString _password
password used when authenticating
void authDenied()
A connection was attempted, but authorization was denied.
void sendText(QString command, QJsonObject attributes)
Send a VFS command as a text message.
void sendBinary(QString command, QJsonObject attributes)
Send a VFS command as a text message.
void commandReceived(QString command, QJsonObject attributes)
A command was received, but it needs to be processed by a listener.
QString _username
username used when authenticating
void authorized()
A connection has been made and authorization is complete.
virtual void socket_connected()
Override the default VFS_websocket_client::socket_connected() command to include an authorization ste...
VFS_websocket_VFSclient(QString wsurl, QString username, QString password="", quint16 interval=3000)
Create a websocket client that will observe VFS connection rules like auth and reconnect.
A VFS_node that manages a QWebSocket connection.
virtual void socket_connected()
Slot called when the client is connected.
int _connectID
For client connections, the reconnect timerID.
bool _authenticated
if auth has taken place and succeeded
virtual void close()
Close the socket if it is connected.
virtual bool writeBinary(QByteArray data)
Write binary data over the websocket.
virtual bool writeText(QString data)
Write text data over the websocket.
virtual QJsonDocument receive(QByteArray message)
VFS_websocket_VFSclient::receive.
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.