From a070b0de9f9e9cbca150eea4eda74757ca588bed Mon Sep 17 00:00:00 2001 From: Jordan Augé Date: Wed, 23 Sep 2020 17:50:52 +0200 Subject: [HICN-645] Control plane (WIP) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I4be6a40b690b62f22f57de6d8c10b01a1be42a6d Signed-off-by: Jordan Augé Signed-off-by: Enrico Loparco (eloparco) Signed-off-by: Mauro Sardara --- hicn-light/src/hicn/config/command.c | 61 ++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 hicn-light/src/hicn/config/command.c (limited to 'hicn-light/src/hicn/config/command.c') diff --git a/hicn-light/src/hicn/config/command.c b/hicn-light/src/hicn/config/command.c new file mode 100644 index 000000000..4c89d6680 --- /dev/null +++ b/hicn-light/src/hicn/config/command.c @@ -0,0 +1,61 @@ + +/** + * @file command.c + * @brief Implementation of commands. + */ + +#include /* tfind, tdestroy, twalk */ +#include + +#include "command.h" + +/* Commands are registered in the following tree. */ +static void * commands_root = NULL; /**< Tree ordered by name */ + +static void nothing_to_free() {} + +#ifdef __linux__ +__attribute__((destructor)) +static +void +command_clear() +{ + tdestroy(commands_root, nothing_to_free); +} +#endif /* __linux__ */ + +static +int +_command_compare(const command_parser_t * c1, + const command_parser_t * c2) +{ + if (c1->action != c2->action) + return c2->action - c1->action; + if (c1->object != c2->object) + return c2->object - c1->object; + if (c1->nparams != c2->nparams) + return c2->nparams - c1->nparams; + return 0; +} + +#define command_compare (int (*)(const void *, const void *))(_command_compare) + +void +command_register(const command_parser_t * command) +{ + // Insert the command in the tree if the keys does not exist yet + tsearch(command, &commands_root, command_compare); +} + +const command_parser_t * +command_search(hc_action_t action, hc_object_type_t object, unsigned nparams) +{ + command_parser_t ** command, search; + + search.action = action; + search.object = object; + search.nparams = nparams; + command = tfind(&search, &commands_root, command_compare); + + return command ? *command : NULL; +} -- cgit 1.2.3-korg