2016-05-30 23:58:58 -04:00
|
|
|
#pragma once
|
2016-05-19 10:41:06 -04:00
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <map>
|
|
|
|
#include <thread>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "exception.hpp"
|
|
|
|
#include "services/builder.hpp"
|
|
|
|
#include "services/inotify.hpp"
|
2016-05-30 23:58:58 -04:00
|
|
|
#include "services/logger.hpp"
|
2016-06-20 22:21:29 -04:00
|
|
|
#include "services/event_throttler.hpp"
|
2016-05-19 10:41:06 -04:00
|
|
|
#include "utils/config.hpp"
|
|
|
|
#include "utils/string.hpp"
|
|
|
|
#include "utils/concurrency.hpp"
|
|
|
|
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2016-05-30 23:58:58 -04:00
|
|
|
#define Concat(one, two) one ## two
|
|
|
|
|
|
|
|
#define _Stringify(expr) #expr
|
|
|
|
#define Stringify(expr) _Stringify(expr)
|
2016-05-19 10:41:06 -04:00
|
|
|
|
|
|
|
#define DefineModule(ModuleName, ModuleType) struct ModuleName : public ModuleType<ModuleName>
|
2016-06-20 22:34:11 -04:00
|
|
|
#define DefineModule2(ModuleName, ModuleType, P2) struct ModuleName : public ModuleType<ModuleName, P2>
|
|
|
|
#define DefineModule3(ModuleName, ModuleType, P2, P3) struct ModuleName : public ModuleType<ModuleName, P2, P3>
|
2016-05-19 10:41:06 -04:00
|
|
|
#define CastModule(ModuleName) static_cast<ModuleName *>(this)
|
|
|
|
#define ConstCastModule(ModuleName) static_cast<ModuleName const &>(*this)
|
|
|
|
|
2016-05-30 23:58:58 -04:00
|
|
|
#define DEFAULT_FORMAT "format"
|
|
|
|
|
2016-05-19 10:41:06 -04:00
|
|
|
DefineBaseException(ModuleError);
|
|
|
|
DefineChildException(UndefinedFormat, ModuleError);
|
|
|
|
DefineChildException(UndefinedFormatTag, ModuleError);
|
|
|
|
|
|
|
|
class ModuleFormatter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct Format
|
|
|
|
{
|
|
|
|
std::string value;
|
|
|
|
std::vector<std::string> tags;
|
|
|
|
std::string fg, bg, ul, ol;
|
|
|
|
int spacing, padding, margin, offset;
|
|
|
|
|
2016-06-20 22:14:04 -04:00
|
|
|
std::string decorate(Builder *builder, std::string output);
|
2016-05-19 10:41:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
std::string module_name;
|
|
|
|
std::map<std::string, std::unique_ptr<Format>> formats;
|
|
|
|
|
|
|
|
public:
|
2016-06-20 21:59:43 -04:00
|
|
|
explicit ModuleFormatter(std::string module_name)
|
2016-05-19 10:41:06 -04:00
|
|
|
: module_name(module_name) {}
|
|
|
|
|
2016-06-20 22:14:04 -04:00
|
|
|
void add(std::string name, std::string fallback, std::vector<std::string>&& tags, std::vector<std::string>&& whitelist = {});
|
|
|
|
std::unique_ptr<Format>& get(std::string format_name);
|
|
|
|
bool has(std::string tag, std::string format_name);
|
|
|
|
bool has(std::string tag);
|
2016-05-19 10:41:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace modules
|
|
|
|
{
|
2016-06-20 21:59:43 -04:00
|
|
|
void broadcast_module_update(std::string module_name);
|
|
|
|
std::string get_tag_name(std::string tag);
|
2016-05-19 10:41:06 -04:00
|
|
|
|
|
|
|
struct ModuleInterface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~ModuleInterface(){}
|
|
|
|
|
|
|
|
virtual std::string name() const = 0;
|
|
|
|
|
|
|
|
virtual void start() = 0;
|
|
|
|
virtual void stop() = 0;
|
|
|
|
virtual void refresh() = 0;
|
|
|
|
|
|
|
|
virtual std::string operator()() = 0;
|
|
|
|
|
2016-06-20 21:59:43 -04:00
|
|
|
virtual bool handle_command(std::string cmd) = 0;
|
2016-05-19 10:41:06 -04:00
|
|
|
};
|
|
|
|
|
2016-06-20 22:34:11 -04:00
|
|
|
template<class ModuleImpl>
|
2016-05-19 10:41:06 -04:00
|
|
|
class Module : public ModuleInterface
|
|
|
|
{
|
|
|
|
concurrency::Atomic<bool> enabled_flag;
|
|
|
|
concurrency::Value<std::string> cache;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
concurrency::SpinLock output_lock;
|
|
|
|
concurrency::SpinLock broadcast_lock;
|
2016-06-20 22:26:42 -04:00
|
|
|
concurrency::SpinLock update_lock;
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-06-20 16:11:58 -04:00
|
|
|
std::mutex sleep_lock;
|
|
|
|
std::condition_variable sleep_handler;
|
|
|
|
|
2016-05-19 10:41:06 -04:00
|
|
|
std::string name_;
|
|
|
|
std::unique_ptr<Builder> builder;
|
2016-06-20 22:21:29 -04:00
|
|
|
std::unique_ptr<EventThrottler> broadcast_throttler;
|
2016-05-19 10:41:06 -04:00
|
|
|
std::unique_ptr<ModuleFormatter> formatter;
|
|
|
|
std::vector<std::thread> threads;
|
|
|
|
|
2016-06-20 23:01:45 -04:00
|
|
|
event_throttler::limit_t broadcast_throttler_limit() const {
|
|
|
|
return event_throttler::limit_t(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
event_throttler::timewindow_t broadcast_throttler_timewindow() const {
|
|
|
|
return event_throttler::timewindow_t(25);
|
|
|
|
}
|
|
|
|
|
2016-05-19 10:41:06 -04:00
|
|
|
public:
|
2016-06-20 22:21:29 -04:00
|
|
|
Module(std::string name, bool lazy_builder = true)
|
|
|
|
: name_("module/"+ name)
|
|
|
|
, builder(std::make_unique<Builder>(lazy_builder))
|
2016-06-20 23:01:45 -04:00
|
|
|
, broadcast_throttler(std::make_unique<EventThrottler>(ConstCastModule(ModuleImpl).broadcast_throttler_limit(), ConstCastModule(ModuleImpl).broadcast_throttler_timewindow()))
|
2016-05-19 10:41:06 -04:00
|
|
|
{
|
|
|
|
this->enable(false);
|
|
|
|
this->cache = "";
|
|
|
|
this->formatter = std::make_unique<ModuleFormatter>(ConstCastModule(ModuleImpl).name());
|
|
|
|
}
|
|
|
|
|
|
|
|
~Module()
|
|
|
|
{
|
|
|
|
if (this->enabled())
|
|
|
|
this->stop();
|
|
|
|
|
|
|
|
std::lock_guard<concurrency::SpinLock> lck(this->broadcast_lock);
|
2016-06-20 22:26:42 -04:00
|
|
|
{
|
|
|
|
std::lock_guard<concurrency::SpinLock> lck(this->update_lock);
|
|
|
|
{
|
|
|
|
std::lock_guard<concurrency::SpinLock> lck(this->output_lock);
|
|
|
|
{
|
|
|
|
for (auto &&t : this->threads) {
|
|
|
|
if (t.joinable())
|
|
|
|
t.join();
|
|
|
|
else
|
|
|
|
log_warning("["+ ConstCastModule(ModuleImpl).name() +"] Runner thread not joinable");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-19 10:41:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
log_trace(name());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string name() const {
|
|
|
|
return name_;
|
|
|
|
}
|
|
|
|
|
2016-06-20 16:11:58 -04:00
|
|
|
void stop()
|
|
|
|
{
|
2016-06-21 02:26:26 -04:00
|
|
|
std::lock_guard<concurrency::SpinLock> lck(this->broadcast_lock);
|
2016-05-19 10:41:06 -04:00
|
|
|
log_trace(name());
|
2016-06-20 16:11:58 -04:00
|
|
|
this->wakeup();
|
2016-05-19 10:41:06 -04:00
|
|
|
this->enable(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void refresh() {
|
|
|
|
this->cache = CastModule(ModuleImpl)->get_output();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string operator()() {
|
|
|
|
return this->cache();
|
|
|
|
}
|
|
|
|
|
2016-06-20 21:59:43 -04:00
|
|
|
bool handle_command(std::string cmd) {
|
2016-05-19 10:41:06 -04:00
|
|
|
return CastModule(ModuleImpl)->handle_command(cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool enabled() {
|
|
|
|
return this->enabled_flag();
|
|
|
|
}
|
|
|
|
|
|
|
|
void enable(bool state) {
|
|
|
|
this->enabled_flag = state;
|
|
|
|
}
|
|
|
|
|
2016-06-20 16:11:58 -04:00
|
|
|
void broadcast()
|
|
|
|
{
|
2016-05-19 10:41:06 -04:00
|
|
|
std::lock_guard<concurrency::SpinLock> lck(this->broadcast_lock);
|
2016-06-20 22:21:29 -04:00
|
|
|
if (!this->broadcast_throttler->passthrough()) {
|
|
|
|
log_trace("Throttled broadcast for: "+ this->name_);
|
|
|
|
return;
|
|
|
|
}
|
2016-05-19 10:41:06 -04:00
|
|
|
broadcast_module_update(ConstCastModule(ModuleImpl).name());
|
|
|
|
}
|
|
|
|
|
2016-06-20 16:11:58 -04:00
|
|
|
void sleep(std::chrono::duration<double> sleep_duration)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lck(this->sleep_lock);
|
2016-06-21 02:26:26 -04:00
|
|
|
this->sleep_handler.wait_for(lck, sleep_duration);
|
2016-06-20 16:11:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void wakeup()
|
|
|
|
{
|
|
|
|
log_trace("Releasing sleep lock for "+ this->name_);
|
2016-06-20 23:01:45 -04:00
|
|
|
this->sleep_handler.notify_all();
|
2016-06-20 16:11:58 -04:00
|
|
|
}
|
|
|
|
|
2016-05-19 10:41:06 -04:00
|
|
|
std::string get_format() {
|
|
|
|
return DEFAULT_FORMAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string get_output()
|
|
|
|
{
|
|
|
|
std::lock_guard<concurrency::SpinLock> lck(this->output_lock);
|
|
|
|
|
|
|
|
log_trace(ConstCastModule(ModuleImpl).name());
|
|
|
|
|
|
|
|
if (!this->enabled()) {
|
|
|
|
log_trace(ConstCastModule(ModuleImpl).name() +" is disabled");
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
auto format_name = CastModule(ModuleImpl)->get_format();
|
|
|
|
auto &&format = this->formatter->get(format_name);
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (auto tag : string::split(format->value, ' ')) {
|
|
|
|
if ((i > 0 && !tag.empty()) || tag.empty()) {
|
|
|
|
this->builder->space(format->spacing);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag[0] == '<' && tag[tag.length()-1] == '>') {
|
|
|
|
if (!CastModule(ModuleImpl)->build(this->builder.get(), tag)) {
|
|
|
|
this->builder->remove_trailing_space(format->spacing);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this->builder->node(tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return format->decorate(this->builder.get(), this->builder->flush());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-20 22:34:11 -04:00
|
|
|
template<class ModuleImpl>
|
2016-05-19 10:41:06 -04:00
|
|
|
class StaticModule : public Module<ModuleImpl>
|
|
|
|
{
|
2016-05-30 23:58:58 -04:00
|
|
|
using Module<ModuleImpl>::Module;
|
2016-05-19 10:41:06 -04:00
|
|
|
|
|
|
|
public:
|
|
|
|
void start()
|
|
|
|
{
|
|
|
|
this->enable(true);
|
|
|
|
this->threads.emplace_back(std::thread(&StaticModule::broadcast, this));
|
|
|
|
}
|
|
|
|
|
2016-06-20 21:59:43 -04:00
|
|
|
bool build(Builder *builder, std::string tag)
|
2016-05-19 10:41:06 -04:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-20 22:34:11 -04:00
|
|
|
template<class ModuleImpl>
|
2016-05-19 10:41:06 -04:00
|
|
|
class TimerModule : public Module<ModuleImpl>
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
std::chrono::duration<double> interval = 1s;
|
|
|
|
|
|
|
|
void runner()
|
|
|
|
{
|
|
|
|
while (this->enabled()) {
|
2016-06-20 16:11:58 -04:00
|
|
|
{ std::lock_guard<concurrency::SpinLock> lck(this->update_lock);
|
|
|
|
if (CastModule(ModuleImpl)->update())
|
|
|
|
CastModule(ModuleImpl)->broadcast();
|
|
|
|
}
|
|
|
|
this->sleep(this->interval);
|
2016-05-19 10:41:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
template<typename I>
|
2016-06-20 21:59:43 -04:00
|
|
|
TimerModule(std::string name, I const &interval)
|
2016-05-19 10:41:06 -04:00
|
|
|
: Module<ModuleImpl>(name), interval(interval) {}
|
|
|
|
|
|
|
|
void start()
|
|
|
|
{
|
|
|
|
this->enable(true);
|
|
|
|
this->threads.emplace_back(std::thread(&TimerModule::runner, this));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-20 22:34:11 -04:00
|
|
|
template<class ModuleImpl>
|
2016-05-19 10:41:06 -04:00
|
|
|
class EventModule : public Module<ModuleImpl>
|
|
|
|
{
|
|
|
|
using Module<ModuleImpl>::Module;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void runner()
|
|
|
|
{
|
|
|
|
// warmup
|
|
|
|
CastModule(ModuleImpl)->update();
|
|
|
|
CastModule(ModuleImpl)->broadcast();
|
|
|
|
|
|
|
|
while (this->enabled()) {
|
|
|
|
std::lock_guard<concurrency::SpinLock> lck(this->update_lock);
|
|
|
|
|
|
|
|
if (!CastModule(ModuleImpl)->has_event())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!CastModule(ModuleImpl)->update())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CastModule(ModuleImpl)->broadcast();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
void start()
|
|
|
|
{
|
|
|
|
this->enable(true);
|
|
|
|
this->threads.emplace_back(std::thread(&EventModule::runner, this));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-20 22:34:11 -04:00
|
|
|
template<class ModuleImpl, const int ThrottleLimit = 2, const int ThrottleWindowMs = 50>
|
2016-05-19 10:41:06 -04:00
|
|
|
class InotifyModule : public Module<ModuleImpl>
|
|
|
|
{
|
|
|
|
protected:
|
2016-06-20 22:34:11 -04:00
|
|
|
concurrency::SpinLock poll_lock;
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-06-20 22:34:11 -04:00
|
|
|
std::unique_ptr<EventThrottler> poll_throttler;
|
|
|
|
|
|
|
|
std::map<std::string, int> watch_list;
|
2016-05-19 10:41:06 -04:00
|
|
|
|
|
|
|
void runner()
|
|
|
|
{
|
|
|
|
// warmup
|
2016-06-20 22:34:11 -04:00
|
|
|
CastModule(ModuleImpl)->on_event(nullptr);
|
|
|
|
CastModule(ModuleImpl)->broadcast();
|
2016-05-19 10:41:06 -04:00
|
|
|
|
|
|
|
while (this->enabled()) {
|
|
|
|
try {
|
2016-06-20 22:34:11 -04:00
|
|
|
std::lock_guard<concurrency::SpinLock> lck(this->poll_lock);
|
|
|
|
if (this->poll_throttler->passthrough())
|
|
|
|
this->poll_events();
|
2016-05-19 10:41:06 -04:00
|
|
|
} catch (InotifyException &e) {
|
|
|
|
get_logger()->fatal(e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 22:34:11 -04:00
|
|
|
void idle() const {
|
|
|
|
// this->sleep(1s);
|
2016-05-30 23:58:58 -04:00
|
|
|
}
|
|
|
|
|
2016-05-19 10:41:06 -04:00
|
|
|
void poll_events()
|
|
|
|
{
|
|
|
|
std::vector<std::unique_ptr<InotifyWatch>> watches;
|
|
|
|
|
2016-06-14 00:26:46 -04:00
|
|
|
try {
|
|
|
|
for (auto &&w : this->watch_list)
|
|
|
|
watches.emplace_back(std::make_unique<InotifyWatch>(w.first, w.second));
|
|
|
|
} catch (InotifyException &e) {
|
|
|
|
watches.clear();
|
2016-06-20 22:34:11 -04:00
|
|
|
log_error(e.what());
|
|
|
|
this->sleep(0.1s);
|
2016-06-14 00:26:46 -04:00
|
|
|
return;
|
|
|
|
}
|
2016-05-19 10:41:06 -04:00
|
|
|
|
|
|
|
while (this->enabled()) {
|
2016-05-30 23:58:58 -04:00
|
|
|
ConstCastModule(ModuleImpl).idle();
|
|
|
|
|
2016-05-19 10:41:06 -04:00
|
|
|
for (auto &&w : watches) {
|
|
|
|
log_trace("Polling inotify event for watch at "+ (*w)());
|
2016-05-30 23:58:58 -04:00
|
|
|
|
2016-05-19 10:41:06 -04:00
|
|
|
if (w->has_event(500 / watches.size())) {
|
|
|
|
std::unique_ptr<InotifyEvent> event = w->get_event();
|
2016-05-30 23:58:58 -04:00
|
|
|
|
2016-05-19 10:41:06 -04:00
|
|
|
watches.clear();
|
2016-05-30 23:58:58 -04:00
|
|
|
|
2016-06-20 22:34:11 -04:00
|
|
|
std::lock_guard<concurrency::SpinLock> lck(this->update_lock);
|
|
|
|
|
|
|
|
if (!this->poll_throttler->passthrough())
|
|
|
|
return;
|
|
|
|
|
2016-05-19 10:41:06 -04:00
|
|
|
if (CastModule(ModuleImpl)->on_event(event.get()))
|
|
|
|
CastModule(ModuleImpl)->broadcast();
|
2016-05-30 23:58:58 -04:00
|
|
|
|
2016-05-19 10:41:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 22:34:11 -04:00
|
|
|
void watch(std::string path, int mask = InotifyEvent::ALL)
|
2016-05-19 10:41:06 -04:00
|
|
|
{
|
|
|
|
log_trace(path);
|
|
|
|
this->watch_list.insert(std::make_pair(path, mask));
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2016-06-20 23:01:45 -04:00
|
|
|
explicit InotifyModule(std::string name)
|
2016-06-20 22:34:11 -04:00
|
|
|
: Module<ModuleImpl>(name)
|
|
|
|
, poll_throttler(std::make_unique<EventThrottler>(event_throttler::limit_t(ThrottleLimit), event_throttler::timewindow_t(ThrottleWindowMs))) {}
|
|
|
|
|
|
|
|
InotifyModule(std::string name, std::string path, int mask = InotifyEvent::ALL)
|
|
|
|
: Module<ModuleImpl>(name)
|
|
|
|
, poll_throttler(std::make_unique<EventThrottler>(event_throttler::limit_t(ThrottleLimit), event_throttler::timewindow_t(ThrottleWindowMs))) {
|
2016-05-19 10:41:06 -04:00
|
|
|
this->watch(path, mask);
|
|
|
|
}
|
|
|
|
|
2016-06-20 22:34:11 -04:00
|
|
|
InotifyModule(std::string name, std::vector<std::string> paths, int mask = InotifyEvent::ALL)
|
|
|
|
: Module<ModuleImpl>(name)
|
|
|
|
, poll_throttler(std::make_unique<EventThrottler>(event_throttler::limit_t(ThrottleLimit), event_throttler::timewindow_t(ThrottleWindowMs)))
|
2016-05-19 10:41:06 -04:00
|
|
|
{
|
|
|
|
for (auto &&path : paths)
|
|
|
|
this->watch(path, mask);
|
|
|
|
}
|
|
|
|
|
2016-06-20 22:34:11 -04:00
|
|
|
~InotifyModule() {
|
|
|
|
std::lock_guard<concurrency::SpinLock> lck(this->poll_lock);
|
|
|
|
}
|
|
|
|
|
2016-05-19 10:41:06 -04:00
|
|
|
void start()
|
|
|
|
{
|
|
|
|
this->enable(true);
|
|
|
|
this->threads.emplace_back(std::thread(&InotifyModule::runner, this));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|