-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathnode_direction.cpp
More file actions
128 lines (104 loc) · 3.22 KB
/
node_direction.cpp
File metadata and controls
128 lines (104 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* Node direction.
*
* Author: Steffen Vogel <post@steffenvogel.de>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/
#include <villas/config.hpp>
#include <villas/exceptions.hpp>
#include <villas/hook.hpp>
#include <villas/hook_list.hpp>
#include <villas/node.hpp>
#include <villas/node_compat_type.hpp>
#include <villas/node_direction.hpp>
#include <villas/utils.hpp>
using namespace villas;
using namespace villas::node;
using namespace villas::utils;
NodeDirection::NodeDirection(enum NodeDirection::Direction dir, Node *n)
: direction(dir), path(nullptr), node(n), enabled(1), builtin(1),
vectorize(1), config(nullptr) {}
int NodeDirection::parse(json_t *json,
std::function<Signal::Ptr(json_t *)> parse_signal) {
int ret;
json_error_t err;
json_t *json_hooks = nullptr;
json_t *json_signals = nullptr;
config = json;
ret = json_unpack_ex(json, &err, 0, "{ s?: o, s?: o, s?: i, s?: b, s?: b }",
"hooks", &json_hooks, "signals", &json_signals,
"vectorize", &vectorize, "builtin", &builtin, "enabled",
&enabled);
if (ret)
throw ConfigError(json, err, "node-config-node-in");
if (node->getFactory()->getFlags() &
(int)NodeFactory::Flags::PROVIDES_SIGNALS) {
// Do nothing.. Node-type will provide signals
signals = std::make_shared<SignalList>();
if (!signals)
throw MemoryAllocationError();
} else if (json_signals) {
signals = std::make_shared<SignalList>(json_signals, parse_signal);
if (!signals)
throw MemoryAllocationError();
} else {
signals =
std::make_shared<SignalList>(DEFAULT_SAMPLE_LENGTH, SignalType::FLOAT);
if (!signals)
return -1;
}
#ifdef WITH_HOOKS
if (json_hooks) {
int m = direction == NodeDirection::Direction::OUT
? (int)Hook::Flags::NODE_WRITE
: (int)Hook::Flags::NODE_READ;
hooks.parse(json_hooks, m, nullptr, node);
}
#endif // WITH_HOOKS
return 0;
}
void NodeDirection::check() {
if (vectorize <= 0)
throw RuntimeError(
"Invalid setting 'vectorize' with value {}. Must be natural number!",
vectorize);
#ifdef WITH_HOOKS
hooks.check();
#endif // WITH_HOOKS
}
int NodeDirection::prepare() {
#ifdef WITH_HOOKS
int t = direction == NodeDirection::Direction::OUT
? (int)Hook::Flags::NODE_WRITE
: (int)Hook::Flags::NODE_READ;
int m = builtin ? t | (int)Hook::Flags::BUILTIN : 0;
hooks.prepare(signals, m, nullptr, node);
#endif // WITH_HOOKS
return 0;
}
int NodeDirection::start() {
#ifdef WITH_HOOKS
hooks.start();
#endif // WITH_HOOKS
return 0;
}
int NodeDirection::stop() {
#ifdef WITH_HOOKS
hooks.stop();
#endif // WITH_HOOKS
return 0;
}
SignalList::Ptr NodeDirection::getSignals(int after_hooks) const {
#ifdef WITH_HOOKS
if (after_hooks && hooks.size() > 0)
return hooks.getSignals();
#endif // WITH_HOOKS
return signals;
}
unsigned NodeDirection::getSignalsMaxCount() const {
#ifdef WITH_HOOKS
if (hooks.size() > 0)
return MAX(signals->size(), hooks.getSignalsMaxCount());
#endif // WITH_HOOKS
return signals->size();
}