-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathhook_list.cpp
More file actions
194 lines (152 loc) · 4.32 KB
/
hook_list.cpp
File metadata and controls
194 lines (152 loc) · 4.32 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* Hook-releated functions.
*
* 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/hook.hpp>
#include <villas/hook_list.hpp>
#include <villas/list.hpp>
#include <villas/plugin.hpp>
#include <villas/sample.hpp>
#include <villas/utils.hpp>
using namespace villas;
using namespace villas::node;
void HookList::parse(json_t *json, int mask, Path *o, Node *n) {
if (!json_is_array(json))
throw ConfigError(json, "node-config-hook",
"Hooks must be configured as a list of hook objects");
size_t i;
json_t *json_hook;
json_array_foreach (json, i, json_hook) {
int ret;
const char *type;
Hook::Ptr h;
json_error_t err;
json_t *json_config;
switch (json_typeof(json_hook)) {
case JSON_STRING:
type = json_string_value(json_hook);
json_config = json_object();
break;
case JSON_OBJECT:
ret = json_unpack_ex(json_hook, &err, 0, "{ s: s }", "type", &type);
if (ret)
throw ConfigError(json_hook, err, "node-config-hook",
"Failed to parse hook");
json_config = json_hook;
break;
default:
throw ConfigError(json_hook, "node-config-hook",
"Hook must be configured by simple string or object");
}
auto hf = plugin::registry->lookup<HookFactory>(type);
if (!hf)
throw ConfigError(json_hook, "node-config-hook", "Unknown hook type '{}'",
type);
if (!(hf->getFlags() & mask))
throw ConfigError(json_hook, "node-config-hook",
"Hook '{}' not allowed here", type);
h = hf->make(o, n);
h->parse(json_config);
push_back(h);
}
}
void HookList::check() {
for (auto h : *this)
h->check();
}
void HookList::prepare(SignalList::Ptr signals, int m, Path *p, Node *n) {
if (!m)
goto skip_add;
// Add internal hooks if they are not already in the list
for (auto f : plugin::registry->lookup<HookFactory>()) {
if ((f->getFlags() & m) == m) {
auto h = f->make(p, n);
push_back(h);
}
}
skip_add:
// Remove filters which are not enabled
remove_if([](Hook::Ptr h) { return !h->isEnabled(); });
// We sort the hooks according to their priority
sort([](const value_type &a, const value_type b) {
return a->getPriority() < b->getPriority();
});
unsigned i = 0;
auto sigs = signals;
for (auto h : *this) {
h->prepare(sigs);
sigs = h->getSignals();
auto logger = h->getLogger();
logger->debug("Signal list after hook #{}:", i++);
if (logger->level() <= spdlog::level::debug)
sigs->dump(logger);
}
}
int HookList::process(struct Sample *smps[], unsigned cnt) {
unsigned current, processed = 0;
if (size() == 0)
return cnt;
for (current = 0; current < cnt; current++) {
struct Sample *smp = smps[current];
for (auto h : *this) {
auto ret = h->process(smp);
smp->signals = h->getSignals();
switch (ret) {
case Hook::Reason::ERROR:
return -1;
case Hook::Reason::OK:
continue;
case Hook::Reason::SKIP_SAMPLE:
goto skip;
case Hook::Reason::STOP_PROCESSING:
goto stop;
}
}
stop:
SWAP(smps[processed], smps[current]);
processed++;
skip: {}
}
return processed;
}
void HookList::periodic() {
for (auto h : *this)
h->periodic();
}
void HookList::start() {
for (auto h : *this)
h->start();
}
void HookList::stop() {
for (auto h : *this)
h->stop();
}
SignalList::Ptr HookList::getSignals() const {
auto h = back();
if (!h)
return nullptr;
return h->getSignals();
}
unsigned HookList::getSignalsMaxCount() const {
unsigned max_cnt = 0;
for (auto h : *this) {
unsigned sigs_cnt = h->getSignals()->size();
if (sigs_cnt > max_cnt)
max_cnt = sigs_cnt;
}
return max_cnt;
}
json_t *HookList::toJson() const {
json_t *json_hooks = json_array();
for (auto h : *this)
json_array_append(json_hooks, h->getConfig());
return json_hooks;
}
void HookList::dump(Logger logger, std::string subject) const {
logger->debug("Hooks of {}:", subject);
unsigned i = 0;
for (auto h : *this)
logger->debug(" {}: {}", i++, h->getFactory()->getName());
}