1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/include/villas/hook.hpp

249 lines
4.7 KiB
C++
Raw Permalink Normal View History

2019-03-26 15:33:47 +01:00
/** Hook functions
*
* Every node or path can register hook functions which is called for every
* processed sample. This can be used to debug the data flow, get statistics
* or alter the sample contents.
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2020-01-20 17:17:00 +01:00
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
2019-03-26 15:33:47 +01:00
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#pragma once
#include <villas/list.h>
#include <villas/signal.h>
#include <villas/log.hpp>
#include <villas/plugin.hpp>
#include <villas/exceptions.hpp>
/* Forward declarations */
2020-06-08 02:25:07 +02:00
struct vpath;
2020-08-25 21:00:52 +02:00
struct vnode;
2019-03-26 15:33:47 +01:00
struct sample;
namespace villas {
namespace node {
class Hook {
2019-06-23 16:13:23 +02:00
public:
enum class Flags {
2021-06-18 21:00:10 -04:00
BUILTIN = (1 << 0), /**< Should we add this hook by default to every path?. */
READ_ONLY = (1 << 1), /**< The does not modify the sample contents */
PATH = (1 << 8), /**< This hook type is used by paths. */
NODE_READ = (1 << 9), /**< This hook type is used by nodes. */
NODE_WRITE = (1 << 10) /**< This hook type is used by nodes. */
2019-06-23 16:13:23 +02:00
};
enum class Reason {
2021-02-22 23:16:53 +01:00
OK = 0,
2019-06-23 16:13:23 +02:00
ERROR,
SKIP_SAMPLE,
STOP_PROCESSING
};
2019-03-26 15:33:47 +01:00
protected:
Logger logger;
2019-06-23 16:13:23 +02:00
enum State state;
2019-03-26 15:33:47 +01:00
int flags;
2019-06-23 16:13:23 +02:00
int priority; /**< A priority to change the order of execution within one type of hook. */
int enabled; /**< Is this hook active? */
2019-03-26 15:33:47 +01:00
2020-06-08 02:25:07 +02:00
struct vpath *path;
2020-08-25 21:00:52 +02:00
struct vnode *node;
2019-03-26 15:33:47 +01:00
2020-12-07 21:30:22 +01:00
struct vlist signals;
2019-03-26 15:33:47 +01:00
2021-02-16 14:15:14 +01:00
json_t *config; /**< A JSON object containing the configuration of the hook. */
2019-03-26 15:33:47 +01:00
public:
2020-08-25 21:00:52 +02:00
Hook(struct vpath *p, struct vnode *n, int fl, int prio, bool en = true);
2019-03-26 15:33:47 +01:00
2021-06-18 14:28:31 -04:00
virtual
~Hook();
virtual
void parse(json_t *c);
2021-02-19 06:40:26 +01:00
void prepare(struct vlist *sigs);
2019-03-26 15:33:47 +01:00
2021-06-18 14:24:28 -04:00
void setLogger(Logger log)
{ logger = log; }
2019-03-26 15:33:47 +01:00
/** Called whenever a hook is started; before threads are created. */
2021-06-18 14:28:31 -04:00
virtual
void start()
2019-03-26 15:33:47 +01:00
{
2019-06-23 16:13:23 +02:00
assert(state == State::PREPARED);
2019-03-26 15:33:47 +01:00
2019-06-23 16:13:23 +02:00
state = State::STARTED;
2019-03-26 15:33:47 +01:00
}
/** Called whenever a hook is stopped; after threads are destoyed. */
2021-06-18 14:28:31 -04:00
virtual
void stop()
2019-03-26 15:33:47 +01:00
{
2019-06-23 16:13:23 +02:00
assert(state == State::STARTED);
2019-03-26 15:33:47 +01:00
2019-06-23 16:13:23 +02:00
state = State::STOPPED;
2019-03-26 15:33:47 +01:00
}
2021-06-18 14:28:31 -04:00
virtual
void check()
2019-03-26 15:33:47 +01:00
{
2019-06-23 16:13:23 +02:00
assert(state == State::PARSED);
2019-03-26 15:33:47 +01:00
2019-06-23 16:13:23 +02:00
state = State::CHECKED;
2019-03-26 15:33:47 +01:00
}
2021-06-18 14:28:31 -04:00
virtual
void prepare()
2021-02-19 06:40:26 +01:00
{ }
2019-03-26 15:33:47 +01:00
/** Called periodically. Period is set by global 'stats' option in the configuration file. */
2021-06-18 14:28:31 -04:00
virtual
void periodic()
2019-03-26 15:33:47 +01:00
{
2019-06-23 16:13:23 +02:00
assert(state == State::STARTED);
2019-03-26 15:33:47 +01:00
}
/** Called whenever a new simulation case is started. This is detected by a sequence no equal to zero. */
2021-06-18 14:28:31 -04:00
virtual
void restart()
2019-03-26 15:33:47 +01:00
{
2019-06-23 16:13:23 +02:00
assert(state == State::STARTED);
2019-03-26 15:33:47 +01:00
}
/** Called whenever a sample is processed. */
2021-06-18 14:28:31 -04:00
virtual
Reason process(struct sample *smp)
2019-03-26 15:33:47 +01:00
{
2019-06-23 16:13:23 +02:00
return Reason::OK;
2019-03-26 15:33:47 +01:00
};
int getPriority() const
{
return priority;
}
int getFlags() const
{
return flags;
}
2021-06-18 14:28:31 -04:00
virtual
struct vlist * getSignals()
2019-03-26 15:33:47 +01:00
{
return &signals;
}
2020-08-17 17:06:58 +02:00
json_t * getConfig() const
{
2021-02-16 14:15:14 +01:00
return config;
2020-08-17 17:06:58 +02:00
}
2019-03-26 15:33:47 +01:00
bool isEnabled() const
{
return enabled;
}
2021-06-18 21:00:10 -04:00
bool isReadOnly() const
{
2021-06-18 22:08:16 -04:00
return flags & (int) Flags::READ_ONLY;
2021-06-18 21:00:10 -04:00
}
2019-03-26 15:33:47 +01:00
};
class LimitHook : public Hook {
public:
using Hook::Hook;
virtual void setRate(double rate, double maxRate = -1) = 0;
2019-04-15 12:27:41 +02:00
void parse()
{
2019-06-23 16:13:23 +02:00
assert(state == State::INITIALIZED);
2019-04-15 12:27:41 +02:00
2019-06-23 16:13:23 +02:00
state = State::PARSED;
2019-04-15 12:27:41 +02:00
}
void init()
{
parse();
check();
prepare();
start();
}
};
2019-03-26 15:33:47 +01:00
class HookFactory : public plugin::Plugin {
public:
using plugin::Plugin::Plugin;
2019-03-26 15:33:47 +01:00
2021-05-10 00:12:30 +02:00
virtual Hook * make(struct vpath *p, struct vnode *n) = 0;
2019-03-26 15:33:47 +01:00
virtual int getFlags() const = 0;
virtual int getPriority() const = 0;
2021-06-18 14:24:28 -04:00
virtual
std::string
getType() const
{ return "hook"; }
2019-03-26 15:33:47 +01:00
};
template <typename T, const char *name, const char *desc, int flags = 0, int prio = 99>
2019-03-26 15:33:47 +01:00
class HookPlugin : public HookFactory {
public:
using HookFactory::HookFactory;
2021-05-10 00:12:30 +02:00
virtual Hook * make(struct vpath *p, struct vnode *n)
2019-06-23 16:13:23 +02:00
{
2021-06-18 14:24:28 -04:00
auto *h = new T(p, n, getFlags(), getPriority());
h->setLogger(getLogger());
return h;
}
virtual std::string
getName() const
{ return name; }
virtual std::string
getDescription() const
{ return desc; }
virtual int
getFlags() const
{ return flags; }
virtual int
getPriority() const
{ return prio; }
2019-03-26 15:33:47 +01:00
};
2020-06-15 22:16:38 +02:00
} /* namespace node */
} /* namespace villas */