Split off the Events subsystem from the server class and set it up for easy expansion later.

This commit is contained in:
Ken Moore
2016-01-15 15:36:04 -05:00
parent ec7f25d804
commit 82b549ead3
10 changed files with 214 additions and 127 deletions

View File

@@ -0,0 +1,60 @@
// ===============================
// PC-BSD REST API Server
// Available under the 3-clause BSD License
// Written by: Ken Moore <ken@pcbsd.org> 2015-2016
// =================================
#include "EventWatcher.h"
// === PUBLIC ===
EventWatcher::EventWatcher(){
watcher = new QFileSystemWatcher(this);
connect(watcher, SIGNAL(fileChanged(const QString&)), this, SLOT(WatcherUpdate(QString)) );
connect(watcher, SIGNAL(directoryChanged(const QString&)), this, SLOT(WatcherUpdate(QString)) );
}
EventWatcher::~EventWatcher(){
}
void EventWatcher::start(){
// - DISPATCH Events
if(!QFile::exists(DISPATCHWORKING)){ QProcess::execute("touch "+DISPATCHWORKING); }
qDebug() << " Dispatcher Events:" << DISPATCHWORKING;
watcher->addPath(DISPATCHWORKING);
WatcherUpdate(DISPATCHWORKING); //load it initially
}
QString EventWatcher::lastEvent(EVENT_TYPE type){
if(HASH.contains(type)){ return HASH.value(type); }
else{ return ""; }
}
// === PRIVATE ===
QString EventWatcher::readFile(QString path){
QFile file(path);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){ return ""; }
QTextStream in(&file);
QString contents = in.readAll();
file.close();
if(contents.endsWith("\n")){ contents.chop(1); }
return contents;
}
// === PRIVATE SLOTS ===
void EventWatcher::WatcherUpdate(QString path){
if(path==DISPATCHWORKING){
//Read the file contents
QString stat = readFile(DISPATCHWORKING);
if(stat.simplified().isEmpty()){ stat = "idle"; }
qDebug() << "Dispatcher Update:" << stat;
HASH.insert(DISPATCHER,stat); //save for later
//Forward those contents on to the currently-open sockets
emit NewEvent(DISPATCHER, stat);
}
//Make sure this file/dir is not removed from the watcher
if(!watcher->files().contains(path) && !watcher->directories().contains(path)){
watcher->addPath(path); //re-add it to the watcher. This happens when the file is removed/re-created instead of just overwritten
}
}