Remoto - VFS: VFS_pulse.cpp Source File
Remoto - VFS
VFS_pulse.cpp
Go to the documentation of this file.
1 
2 #include <QTime>
3 #include <QJsonObject>
4 
5 #include "VFS.h"
6 #include "VFS_pulse.h"
7 
30 VFS_pulse::VFS_pulse(int interval)
31 : VFS_node()
32 , _interval(interval)
33 , _timerID(-1)
34 {
35  connect( this, SIGNAL(mounted()), this, SLOT(startPulse()) );
36  connect( this, SIGNAL(unmounted(VFS_node*)), this, SLOT(stopPulse()) );
37  connect( VFS::root(), SIGNAL(initialized()), this, SLOT(startPulse()) );
38 }
39 
41 {
42  stopPulse();
43 }
44 
51 {
52  return false;
53 }
54 
60 {
61  if (_timerID == -1)
62  {
63  _timerID = startTimer(_interval);
64  //emit timerEvent(0); //pulse when starting, rather than waiting for a full cycle.
65 
66  VFS::LOG( QString("%1 started pulse timer at an interval of %2ms").arg(className()).arg(_interval) );
67  }
68 }
69 
75 {
76  if (_timerID != -1)
77  {
78  killTimer(_timerID);
79  _timerID = -1;
80 
81  VFS::LOG( QString("%1 stopped pulse timer").arg(className()) );
82  }
83 }
84 
91 {
92  QString now = QTime::currentTime().toString();
93 
94  QJsonObject o;
95  o["data"] = now;
96 
97  r->_data.setObject(o);
98 
99  r->_success = true;
100 }
101 
106 void VFS_pulse::timerEvent(QTimerEvent *event)
107 {
108  if (event->timerId() == _timerID)
109  {
110  //QByteArray now = (QString("%1").arg((quintptr)this)+" "+QTime::currentTime().toString()).toAscii();
111  QString now = QTime::currentTime().toString();
112 
113  VFS::LOG( QString("Pulse: %1").arg(now), 9, className() );
114 
115  QJsonObject o;
116  o["data"] = now;
117  QJsonDocument d(o);
118 
119  VFS_request *r = createRequest(VFS_request::diff,"","pulse",d);
120 
121  emit diff(this,r);
122 
123  delete r; //Don't leak memory
124  }
125  else
126  VFS::ERROR("Received timer event from bad _timerID",0,className());
127 }
VFS_node is the base class from which all other VFS_node classes derive.
Definition: VFS_node.h:143
virtual VFS_request * createRequest(VFS_request::requestType type, QString path, QString user="unknown", QJsonDocument data=QJsonDocument(), QJsonObject metadata=QJsonObject(), bool dontDelete=false)
Create a new VFS_request with this object as _origin.
Definition: VFS_node.cpp:1913
void mounted()
Emitted when a node is mount()ed.
void unmounted(VFS_node *self)
Emitted when a node is unmount()ed.
void diff(VFS_node *origin, VFS_request *t)
Emit a diff, which will trigger notifySubscribers() for a mounted node.
QString className()
Return the class name of a node.
Definition: VFS_node.cpp:2039
virtual void read(VFS_request *r)
Read the current time.
Definition: VFS_pulse.cpp:90
void startPulse()
Start generating pulses.
Definition: VFS_pulse.cpp:59
Q_INVOKABLE VFS_pulse(int interval=1000)
Definition: VFS_pulse.cpp:30
virtual ~VFS_pulse()
Definition: VFS_pulse.cpp:40
virtual bool isContainer()
A VFS_pulse node cannot contain children.
Definition: VFS_pulse.cpp:50
virtual void timerEvent(QTimerEvent *event)
When this timer event occurs, generate a VFS_request and emit it.
Definition: VFS_pulse.cpp:106
int _interval
The frequency of the pulse, in milliseconds.
Definition: VFS_pulse.h:17
void stopPulse()
Stop pulsing.
Definition: VFS_pulse.cpp:74
int _timerID
The timerID returned by QObject::startTimer()
Definition: VFS_pulse.h:18
The base class for all requests between nodes.
Definition: VFS_node.h:54
@ diff
send a diff (7)
Definition: VFS_node.h:71
bool _success
if the request was successfully completed
Definition: VFS_node.h:107
QJsonDocument _data
the request payload
Definition: VFS_node.h:102
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