mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
improve parsing of booleans from JSON
This commit is contained in:
parent
ba72f5cc27
commit
3f456f922b
8 changed files with 61 additions and 34 deletions
|
@ -80,12 +80,12 @@ struct vpath {
|
|||
struct Task timeout;
|
||||
|
||||
double rate; /**< A timeout for */
|
||||
int enabled; /**< Is this path enabled? */
|
||||
int muxed; /**< Is this path muxed? */
|
||||
bool enabled; /**< Is this path enabled? */
|
||||
bool muxed; /**< Is this path muxed? */
|
||||
int affinity; /**< Thread affinity. */
|
||||
int poll; /**< Weather or not to use poll(2). */
|
||||
int reverse; /**< This path has a matching reverse path. */
|
||||
int builtin; /**< This path should use built-in hooks by default. */
|
||||
bool reverse; /**< This path has a matching reverse path. */
|
||||
bool builtin_hooks; /**< This path should use built-in hooks by default. */
|
||||
int original_sequence_no; /**< Use original source sequence number when multiplexing */
|
||||
unsigned queuelen; /**< The queue length for each path_destination::queue */
|
||||
|
||||
|
|
|
@ -56,7 +56,7 @@ class SuperNode {
|
|||
protected:
|
||||
enum State state;
|
||||
|
||||
int idleStop;
|
||||
bool idleStop;
|
||||
|
||||
Logger logger;
|
||||
|
||||
|
|
|
@ -81,18 +81,21 @@ void Hook::prepare(struct vlist *sigs)
|
|||
|
||||
void Hook::parse(json_t *json)
|
||||
{
|
||||
int ret;
|
||||
int ret, en = -1;
|
||||
json_error_t err;
|
||||
|
||||
assert(state != State::STARTED);
|
||||
|
||||
ret = json_unpack_ex(json, &err, 0, "{ s?: i, s?: b }",
|
||||
"priority", &priority,
|
||||
"enabled", &enabled
|
||||
"enabled", &en
|
||||
);
|
||||
if (ret)
|
||||
throw ConfigError(json, err, "node-config-hook");
|
||||
|
||||
if (en >= 0)
|
||||
enabled = en != 0;
|
||||
|
||||
config = json;
|
||||
|
||||
state = State::PARSED;
|
||||
|
|
14
lib/node.cpp
14
lib/node.cpp
|
@ -138,7 +138,7 @@ int node_prepare(struct vnode *n)
|
|||
|
||||
int node_parse(struct vnode *n, json_t *json, const uuid_t sn_uuid)
|
||||
{
|
||||
int ret, enabled = n->enabled;
|
||||
int ret, en = -1;
|
||||
|
||||
json_error_t err;
|
||||
json_t *json_netem = nullptr;
|
||||
|
@ -149,7 +149,7 @@ int node_parse(struct vnode *n, json_t *json, const uuid_t sn_uuid)
|
|||
ret = json_unpack_ex(json, &err, 0, "{ s?: s, s?: s, s?: b }",
|
||||
"name", &name_str,
|
||||
"uuid", &uuid_str,
|
||||
"enabled", &enabled
|
||||
"enabled", &en
|
||||
);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
@ -167,7 +167,8 @@ int node_parse(struct vnode *n, json_t *json, const uuid_t sn_uuid)
|
|||
return ret;
|
||||
#endif /* __linux__ */
|
||||
|
||||
n->enabled = enabled;
|
||||
if (en >= 0)
|
||||
n->enabled = en != 0;
|
||||
|
||||
if (name_str)
|
||||
n->name = strdup(name_str);
|
||||
|
@ -185,13 +186,14 @@ int node_parse(struct vnode *n, json_t *json, const uuid_t sn_uuid)
|
|||
|
||||
if (json_netem) {
|
||||
#ifdef WITH_NETEM
|
||||
int enabled = 1;
|
||||
/* Enabled by default */
|
||||
int en = 1;
|
||||
|
||||
ret = json_unpack_ex(json_netem, &err, 0, "{ s?: b }", "enabled", &enabled);
|
||||
ret = json_unpack_ex(json_netem, &err, 0, "{ s?: b }", "enabled", &en);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
if (enabled)
|
||||
if (en)
|
||||
kernel::tc::netem_parse(&n->tc_qdisc, json_netem);
|
||||
else
|
||||
n->tc_qdisc = nullptr;
|
||||
|
|
|
@ -39,7 +39,7 @@ int node_direction_prepare(struct vnode_direction *nd, struct vnode *n)
|
|||
|
||||
#ifdef WITH_HOOKS
|
||||
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;
|
||||
int m = nd->builtin_hooks ? t | (int) Hook::Flags::BUILTIN : 0;
|
||||
|
||||
hook_list_prepare(&nd->hooks, &nd->signals, m, nullptr, n);
|
||||
|
||||
|
@ -56,11 +56,11 @@ int node_direction_init(struct vnode_direction *nd, enum NodeDir dir, struct vno
|
|||
int ret;
|
||||
|
||||
nd->direction = dir;
|
||||
nd->enabled = 1;
|
||||
nd->enabled = true;
|
||||
nd->vectorize = 1;
|
||||
nd->builtin = 1;
|
||||
nd->path = nullptr;
|
||||
nd->read_only_hooks = false;
|
||||
nd->builtin_hooks = true;
|
||||
|
||||
#ifdef WITH_HOOKS
|
||||
ret = hook_list_init(&nd->hooks);
|
||||
|
@ -100,7 +100,7 @@ int node_direction_destroy(struct vnode_direction *nd, struct vnode *n)
|
|||
|
||||
int node_direction_parse(struct vnode_direction *nd, struct vnode *n, json_t *json)
|
||||
{
|
||||
int ret;
|
||||
int ret, builtin_hooks = -1, enabled = -1;
|
||||
|
||||
assert(nd->state == State::INITIALIZED);
|
||||
|
||||
|
@ -114,12 +114,18 @@ int node_direction_parse(struct vnode_direction *nd, struct vnode *n, json_t *js
|
|||
"hooks", &json_hooks,
|
||||
"signals", &json_signals,
|
||||
"vectorize", &nd->vectorize,
|
||||
"builtin", &nd->builtin,
|
||||
"enabled", &nd->enabled
|
||||
"builtin", &builtin_hooks,
|
||||
"enabled", &enabled
|
||||
);
|
||||
if (ret)
|
||||
throw ConfigError(json, err, "node-config-node-in");
|
||||
|
||||
if (builtin_hooks >= 0)
|
||||
nd->builtin_hooks = builtin_hooks != 0;
|
||||
|
||||
if (enabled >= 0)
|
||||
nd->enabled = enabled != 0;
|
||||
|
||||
if (n->_vt->flags & (int) NodeFlags::PROVIDES_SIGNALS) {
|
||||
/* Do nothing.. Node-type will provide signals */
|
||||
}
|
||||
|
|
27
lib/path.cpp
27
lib/path.cpp
|
@ -173,9 +173,9 @@ int path_init(struct vpath *p)
|
|||
p->mode = PathMode::ANY;
|
||||
p->rate = 0; /* Disabled */
|
||||
|
||||
p->builtin = 1;
|
||||
p->reverse = 0;
|
||||
p->enabled = 1;
|
||||
p->builtin_hooks = true;
|
||||
p->reverse = false;
|
||||
p->enabled = true;
|
||||
p->poll = -1;
|
||||
p->queuelen = DEFAULT_QUEUE_LENGTH;
|
||||
p->original_sequence_no = -1;
|
||||
|
@ -368,7 +368,7 @@ int path_prepare(struct vpath *p, NodeList &nodes)
|
|||
|
||||
#ifdef WITH_HOOKS
|
||||
/* Prepare path hooks */
|
||||
int m = p->builtin ? (int) Hook::Flags::PATH | (int) Hook::Flags::BUILTIN : 0;
|
||||
int m = p->builtin_hooks ? (int) Hook::Flags::PATH | (int) Hook::Flags::BUILTIN : 0;
|
||||
|
||||
/* Add internal hooks if they are not already in the list */
|
||||
hook_list_prepare(&p->hooks, &p->signals, m, p, nullptr);
|
||||
|
@ -401,6 +401,8 @@ int path_parse(struct vpath *p, json_t *json, NodeList &nodes, const uuid_t sn_u
|
|||
const char *mode = nullptr;
|
||||
const char *uuid_str = nullptr;
|
||||
|
||||
int enabled = -1, reverse = -1, builtin_hooks = -1, original_sequence_no = -1, poll = -1;
|
||||
|
||||
struct vlist destinations;
|
||||
|
||||
ret = vlist_init(&destinations);
|
||||
|
@ -411,9 +413,9 @@ int path_parse(struct vpath *p, json_t *json, NodeList &nodes, const uuid_t sn_u
|
|||
"in", &json_in,
|
||||
"out", &json_out,
|
||||
"hooks", &json_hooks,
|
||||
"reverse", &p->reverse,
|
||||
"enabled", &p->enabled,
|
||||
"builtin", &p->builtin,
|
||||
"reverse", &reverse,
|
||||
"enabled", &enabled,
|
||||
"builtin", &builtin_hooks,
|
||||
"queuelen", &p->queuelen,
|
||||
"mode", &mode,
|
||||
"poll", &p->poll,
|
||||
|
@ -426,6 +428,15 @@ int path_parse(struct vpath *p, json_t *json, NodeList &nodes, const uuid_t sn_u
|
|||
if (ret)
|
||||
throw ConfigError(json, err, "node-config-path", "Failed to parse path configuration");
|
||||
|
||||
if (enabled >= 0)
|
||||
p->enabled = enabled;
|
||||
|
||||
if (reverse >= 0)
|
||||
p->reverse = reverse;
|
||||
|
||||
if (builtin_hooks >= 0)
|
||||
p->builtin_hooks = builtin_hooks;
|
||||
|
||||
/* Optional settings */
|
||||
if (mode) {
|
||||
if (!strcmp(mode, "any"))
|
||||
|
@ -851,7 +862,7 @@ json_t * path_to_json(struct vpath *p)
|
|||
"state", state_print(p->state),
|
||||
"mode", p->mode == PathMode::ANY ? "any" : "all",
|
||||
"enabled", p->enabled,
|
||||
"builtin", p->builtin,
|
||||
"builtin", p->builtin_hooks,
|
||||
"reverse", p->reverse,
|
||||
"original_sequence_no", p->original_sequence_no,
|
||||
"last_sequence", p->last_sequence,
|
||||
|
|
|
@ -195,13 +195,14 @@ int signal_parse(struct signal *s, json_t *json)
|
|||
const char *name = nullptr;
|
||||
const char *unit = nullptr;
|
||||
const char *type = "float";
|
||||
int en = -1;
|
||||
|
||||
ret = json_unpack_ex(json, &err, 0, "{ s?: s, s?: s, s?: s, s?: o, s?: b }",
|
||||
"name", &name,
|
||||
"unit", &unit,
|
||||
"type", &type,
|
||||
"init", &json_init,
|
||||
"enabled", &s->enabled
|
||||
"enabled", &en
|
||||
);
|
||||
if (ret)
|
||||
return -1;
|
||||
|
@ -226,6 +227,9 @@ int signal_parse(struct signal *s, json_t *json)
|
|||
else
|
||||
signal_data_set(&s->init, s->type, 0);
|
||||
|
||||
if (en >= 0)
|
||||
s->enabled == en != 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -52,7 +52,7 @@ typedef char uuid_string_t[37];
|
|||
|
||||
SuperNode::SuperNode() :
|
||||
state(State::INITIALIZED),
|
||||
idleStop(-1),
|
||||
idleStop(true),
|
||||
#ifdef WITH_API
|
||||
api(this),
|
||||
#endif
|
||||
|
@ -96,7 +96,7 @@ void SuperNode::parse(const std::string &u)
|
|||
|
||||
void SuperNode::parse(json_t *root)
|
||||
{
|
||||
int ret;
|
||||
int ret, is = -1;
|
||||
|
||||
assert(state != State::STARTED);
|
||||
|
||||
|
@ -109,8 +109,6 @@ void SuperNode::parse(json_t *root)
|
|||
|
||||
json_error_t err;
|
||||
|
||||
idleStop = 1;
|
||||
|
||||
ret = json_unpack_ex(root, &err, 0, "{ s?: F, s?: o, s?: o, s?: o, s?: o, s?: i, s?: i, s?: i, s?: b, s?: s }",
|
||||
"stats", &statsRate,
|
||||
"http", &json_http,
|
||||
|
@ -120,7 +118,7 @@ void SuperNode::parse(json_t *root)
|
|||
"hugepages", &hugepages,
|
||||
"affinity", &affinity,
|
||||
"priority", &priority,
|
||||
"idle_stop", &idleStop,
|
||||
"idle_stop", &is,
|
||||
"uuid", &uuid_str
|
||||
);
|
||||
if (ret)
|
||||
|
@ -132,6 +130,9 @@ void SuperNode::parse(json_t *root)
|
|||
throw ConfigError(root, "node-config-uuid", "Failed to parse UUID: {}", uuid_str);
|
||||
}
|
||||
|
||||
if (is >= 0)
|
||||
idleStop = is != 0;
|
||||
|
||||
#ifdef WITH_WEB
|
||||
if (json_http)
|
||||
web.parse(json_http);
|
||||
|
@ -501,7 +502,7 @@ int SuperNode::periodic()
|
|||
}
|
||||
}
|
||||
|
||||
if (idleStop > 0 && state == State::STARTED && started == 0) {
|
||||
if (idleStop && state == State::STARTED && started == 0) {
|
||||
logger->info("No more active paths. Stopping super-node");
|
||||
|
||||
return -1;
|
||||
|
|
Loading…
Add table
Reference in a new issue