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

222 lines
4.5 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 {
BUILTIN = (1 << 0), /**< Should we add this hook by default to every path?. */
PATH = (1 << 1), /**< This hook type is used by paths. */
NODE_READ = (1 << 2), /**< This hook type is used by nodes. */
NODE_WRITE = (1 << 3) /**< This hook type is used by nodes. */
};
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
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
/** Called whenever a hook is started; before threads are created. */
virtual void start()
{
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. */
virtual void stop()
{
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
}
virtual void check()
{
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-02-19 06:40:26 +01:00
virtual void prepare()
{ }
2019-03-26 15:33:47 +01:00
/** Called periodically. Period is set by global 'stats' option in the configuration file. */
virtual void periodic()
{
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. */
virtual void restart()
{
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. */
2019-06-23 16:13:23 +02:00
virtual Reason process(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-02-22 23:16:53 +01: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;
}
};
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
2020-08-25 21:00:52 +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;
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;
2020-08-25 21:00:52 +02:00
virtual Hook *make(struct vpath *p, struct vnode *n)
2019-06-23 16:13:23 +02:00
{
return new T(p, n, getFlags(), getPriority());
}
/// Get plugin name
virtual std::string
getName() const
{ return name; }
/// Get plugin description
virtual std::string
getDescription() const
{ return desc; }
/// Get hook flags
virtual int
getFlags() const
{ return flags; }
/// Get hook priority
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 */