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/lib/node_direction.cpp

232 lines
5.6 KiB
C++
Raw Permalink Normal View History

2019-02-24 11:07:07 +01:00
/** Node direction
*
* @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-02-24 11:07:07 +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/>.
*********************************************************************************/
2019-04-05 02:22:53 +02:00
#include <villas/config.h>
#include <villas/utils.hpp>
2019-06-23 16:13:23 +02:00
#include <villas/hook.hpp>
2019-04-23 13:12:04 +02:00
#include <villas/hook_list.hpp>
2019-02-24 11:07:07 +01:00
#include <villas/node.h>
#include <villas/utils.hpp>
2019-02-24 11:07:07 +01:00
#include <villas/node_direction.h>
#include <villas/exceptions.hpp>
2019-02-24 11:07:07 +01:00
using namespace villas;
2019-06-23 16:13:23 +02:00
using namespace villas::node;
2019-06-04 16:55:38 +02:00
using namespace villas::utils;
2020-08-25 21:00:52 +02:00
int node_direction_prepare(struct vnode_direction *nd, struct vnode *n)
2019-02-24 11:07:07 +01:00
{
2019-06-23 16:13:23 +02:00
assert(nd->state == State::CHECKED);
2019-02-24 11:07:07 +01:00
#ifdef WITH_HOOKS
2019-06-23 16:13:23 +02:00
int t = nd->direction == NodeDir::OUT ? (int) Hook::Flags::NODE_WRITE : (int) Hook::Flags::NODE_READ;
int m = nd->builtin ? t | (int) Hook::Flags::BUILTIN : 0;
2019-02-24 11:07:07 +01:00
hook_list_prepare(&nd->hooks, &nd->signals, m, nullptr, n);
2019-02-24 11:07:07 +01:00
#endif /* WITH_HOOKS */
2019-06-23 16:13:23 +02:00
nd->state = State::PREPARED;
2019-02-24 11:07:07 +01:00
return 0;
}
2020-08-25 21:00:52 +02:00
int node_direction_init(struct vnode_direction *nd, enum NodeDir dir, struct vnode *n)
2019-02-24 11:07:07 +01:00
{
int ret;
nd->direction = dir;
nd->enabled = 1;
nd->vectorize = 1;
nd->builtin = 1;
2021-05-10 00:12:30 +02:00
nd->path = nullptr;
2019-02-24 11:07:07 +01:00
#ifdef WITH_HOOKS
ret = hook_list_init(&nd->hooks);
if (ret)
return ret;
#endif /* WITH_HOOKS */
ret = signal_list_init(&nd->signals);
if (ret)
return ret;
2019-06-23 16:13:23 +02:00
nd->state = State::INITIALIZED;
2019-02-24 11:07:07 +01:00
return 0;
}
2020-08-25 21:00:52 +02:00
int node_direction_destroy(struct vnode_direction *nd, struct vnode *n)
2019-02-24 11:07:07 +01:00
{
int ret = 0;
2019-06-23 16:13:23 +02:00
assert(nd->state != State::DESTROYED && nd->state != State::STARTED);
2019-02-24 11:07:07 +01:00
#ifdef WITH_HOOKS
ret = hook_list_destroy(&nd->hooks);
if (ret)
return ret;
#endif /* WITH_HOOKS */
ret = signal_list_destroy(&nd->signals);
if (ret)
return ret;
2019-06-23 16:13:23 +02:00
nd->state = State::DESTROYED;
2019-02-24 11:07:07 +01:00
return 0;
}
2021-02-16 14:15:14 +01:00
int node_direction_parse(struct vnode_direction *nd, struct vnode *n, json_t *json)
2019-02-24 11:07:07 +01:00
{
int ret;
2019-06-23 16:13:23 +02:00
assert(nd->state == State::INITIALIZED);
2019-02-24 11:07:07 +01:00
json_error_t err;
2019-04-08 08:59:08 +02:00
json_t *json_hooks = nullptr;
json_t *json_signals = nullptr;
2019-02-24 11:07:07 +01:00
2021-02-16 14:15:14 +01:00
nd->config = json;
2019-02-24 11:07:07 +01:00
2021-02-16 14:15:14 +01:00
ret = json_unpack_ex(json, &err, 0, "{ s?: o, s?: o, s?: i, s?: b, s?: b }",
2019-02-24 11:07:07 +01:00
"hooks", &json_hooks,
"signals", &json_signals,
"vectorize", &nd->vectorize,
"builtin", &nd->builtin,
"enabled", &nd->enabled
);
if (ret)
2021-02-16 14:15:14 +01:00
throw ConfigError(json, err, "node-config-node-in");
2019-02-24 11:07:07 +01:00
if (node_type(n)->flags & (int) NodeFlags::PROVIDES_SIGNALS) {
/* Do nothing.. Node-type will provide signals */
2019-02-24 11:07:07 +01:00
}
else if (json_is_array(json_signals)) {
ret = signal_list_parse(&nd->signals, json_signals);
if (ret)
2021-02-16 14:15:14 +01:00
throw ConfigError(json_signals, "node-config-node-signals", "Failed to parse signal definition");
2019-02-24 11:07:07 +01:00
}
else if (json_is_string(json_signals)) {
const char *dt = json_string_value(json_signals);
ret = signal_list_generate2(&nd->signals, dt);
if (ret)
return ret;
}
2019-02-24 11:07:07 +01:00
else {
int count = DEFAULT_SAMPLE_LENGTH;
2019-02-24 11:07:07 +01:00
const char *type_str = "float";
if (json_is_object(json_signals)) {
ret = json_unpack_ex(json_signals, &err, 0, "{ s: i, s: s }",
2019-02-24 11:07:07 +01:00
"count", &count,
"type", &type_str
);
if (ret)
throw ConfigError(json_signals, "node-config-node-signals", "Invalid signal definition");
2019-02-24 11:07:07 +01:00
}
2019-06-23 16:13:23 +02:00
enum SignalType type = signal_type_from_str(type_str);
if (type == SignalType::INVALID)
2021-02-16 14:15:14 +01:00
throw ConfigError(json_signals, "node-config-node-signals", "Invalid signal type {}", type_str);
2019-02-24 11:07:07 +01:00
ret = signal_list_generate(&nd->signals, count, type);
if (ret)
return ret;
2019-02-24 11:07:07 +01:00
}
#ifdef WITH_HOOKS
if (json_hooks) {
2019-06-23 16:13:23 +02:00
int m = nd->direction == NodeDir::OUT ? (int) Hook::Flags::NODE_WRITE : (int) Hook::Flags::NODE_READ;
2019-02-24 11:07:07 +01:00
hook_list_parse(&nd->hooks, json_hooks, m, nullptr, n);
2019-02-24 11:07:07 +01:00
}
#endif /* WITH_HOOKS */
2019-06-23 16:13:23 +02:00
nd->state = State::PARSED;
2019-02-24 11:07:07 +01:00
return 0;
}
2021-07-06 18:33:33 +02:00
void node_direction_check(struct vnode_direction *nd, struct vnode *n)
2019-02-24 11:07:07 +01:00
{
assert(n->state != State::DESTROYED);
2019-02-24 11:07:07 +01:00
if (nd->vectorize <= 0)
2021-02-16 14:15:14 +01:00
throw RuntimeError("Invalid setting 'vectorize' with value {}. Must be natural number!", nd->vectorize);
2019-02-24 11:07:07 +01:00
2021-07-06 18:33:33 +02:00
#ifdef WITH_HOOKS
hook_list_check(&nd->hooks);
#endif /* WITH_HOOKS */
2019-02-24 11:07:07 +01:00
2021-07-06 18:33:33 +02:00
nd->state = State::CHECKED;
2019-02-24 11:07:07 +01:00
}
2020-08-25 21:00:52 +02:00
int node_direction_start(struct vnode_direction *nd, struct vnode *n)
2019-02-24 11:07:07 +01:00
{
2019-06-23 16:13:23 +02:00
assert(nd->state == State::PREPARED);
2019-02-24 11:07:07 +01:00
#ifdef WITH_HOOKS
hook_list_start(&nd->hooks);
2019-02-24 11:07:07 +01:00
#endif /* WITH_HOOKS */
2019-06-23 16:13:23 +02:00
nd->state = State::STARTED;
2019-02-24 11:07:07 +01:00
return 0;
}
2020-08-25 21:00:52 +02:00
int node_direction_stop(struct vnode_direction *nd, struct vnode *n)
2019-02-24 11:07:07 +01:00
{
2019-06-23 16:13:23 +02:00
assert(nd->state == State::STARTED);
2019-02-24 11:07:07 +01:00
#ifdef WITH_HOOKS
hook_list_stop(&nd->hooks);
2019-02-24 11:07:07 +01:00
#endif /* WITH_HOOKS */
2019-06-23 16:13:23 +02:00
nd->state = State::STOPPED;
2019-02-24 11:07:07 +01:00
return 0;
}
2020-08-25 21:00:52 +02:00
struct vlist * node_direction_get_signals(struct vnode_direction *nd)
2019-02-24 11:07:07 +01:00
{
2019-06-23 16:13:23 +02:00
assert(nd->state == State::PREPARED);
2019-02-24 11:07:07 +01:00
2019-04-02 09:17:59 -04:00
#ifdef WITH_HOOKS
if (vlist_length(&nd->hooks) > 0)
return hook_list_get_signals(&nd->hooks);
2019-04-05 02:22:53 +02:00
#endif /* WITH_HOOKS */
2019-04-02 09:17:59 -04:00
return &nd->signals;
2019-02-24 11:07:07 +01:00
}
unsigned node_direction_get_signals_max_cnt(struct vnode_direction *nd)
{
#ifdef WITH_HOOKS
if (vlist_length(&nd->hooks) > 0)
return MAX(vlist_length(&nd->signals), hook_list_get_signals_max_cnt(&nd->hooks));
#endif /* WITH_HOOKS */
return vlist_length(&nd->signals);
}