From a088c116f0a520c2b6e1fea4bebc288a97359913 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Thu, 22 Nov 2018 09:56:30 +0200 Subject: [PATCH] node: added support for pause/resume and restart node instances --- include/villas/common.h | 3 +- include/villas/node.h | 18 ++++++++++++ include/villas/node_type.h | 24 +++++++++++++++ lib/node.c | 60 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 1 deletion(-) diff --git a/include/villas/common.h b/include/villas/common.h index 73ba53ba0..3aa4c2d21 100644 --- a/include/villas/common.h +++ b/include/villas/common.h @@ -40,7 +40,8 @@ enum state { STATE_UNLOADED = 5, /* alias for STATE_STARTED used by struct plugin */ STATE_CLOSED = 5, /* alias for STATE_STARTED used by struct io */ STATE_PENDING_CONNECT = 6, - STATE_CONNECTED = 7 + STATE_CONNECTED = 7, + STATE_PAUSED = 9 }; /** Callback to destroy list elements. diff --git a/include/villas/node.h b/include/villas/node.h index 27aace9fa..a0673f6af 100644 --- a/include/villas/node.h +++ b/include/villas/node.h @@ -125,6 +125,24 @@ int node_start(struct node *n); */ int node_stop(struct node *n); +/** Pauses operation of a node. + * + * @see node_type::close + */ +int node_pause(struct node *n); + +/** Resumes operation of a node. + * + * @see node_type::close + */ +int node_resume(struct node *n); + +/** Restarts operation of a node. + * + * @see node_type::close + */ +int node_restart(struct node *n); + /** Destroy node by freeing dynamically allocated memory. * * @see node_type::destroy diff --git a/include/villas/node_type.h b/include/villas/node_type.h index 82eaa743e..39b35ca3b 100644 --- a/include/villas/node_type.h +++ b/include/villas/node_type.h @@ -128,6 +128,14 @@ struct node_type { */ int (*start)(struct node *n); + /** Restart this node. + * + * @param n A pointer to the node object. + * @retval 0 Success. Everything went well. + * @retval <0 Error. Something went wrong. + */ + int (*restart)(struct node *n); + /** Stop this node. * * @param n A pointer to the node object. @@ -136,6 +144,22 @@ struct node_type { */ int (*stop)(struct node *n); + /** Pause this node. + * + * @param n A pointer to the node object. + * @retval 0 Success. Everything went well. + * @retval <0 Error. Something went wrong. + */ + int (*pause)(struct node *n); + + /** Resume this node. + * + * @param n A pointer to the node object. + * @retval 0 Success. Everything went well. + * @retval <0 Error. Something went wrong. + */ + int (*resume)(struct node *n); + /** Receive multiple messages at once. * * Messages are received with a single recvmsg() syscall by diff --git a/lib/node.c b/lib/node.c index fbbc8f653..6a219211a 100644 --- a/lib/node.c +++ b/lib/node.c @@ -380,6 +380,66 @@ int node_stop(struct node *n) return ret; } +int node_pause(struct node *n) +{ + int ret; + + if (n->state != STATE_STARTED) + return -1; + + info("Pausing node %s", node_name(n)); + + ret = node_type(n)->pause ? node_type(n)->pause(n) : 0; + + if (ret == 0) + n->state = STATE_PAUSED; + + return ret; +} + +int node_resume(struct node *n) +{ + int ret; + + if (n->state != STATE_PAUSED) + return -1; + + info("Resuming node %s", node_name(n)); + + ret = node_type(n)->resume ? node_type(n)->resume(n) : 0; + + if (ret == 0) + n->state = STATE_STARTED; + + return ret; +} + +int node_restart(struct node *n) +{ + int ret; + + if (n->state != STATE_STARTED) + return -1; + + info("Restarting node %s", node_name(n)); + + if (node_type(n)->restart) { + ret = node_type(n)->restart(n); + } + else { + ret = node_type(n)->stop ? node_type(n)->stop(n) : 0; + if (ret) + return ret; + + ret = node_type(n)->start ? node_type(n)->start(n) : 0; + if (ret) + return ret; + } + + return 0; +} + + int node_destroy(struct node *n) { int ret;