From 145b9b8aabf85e87d62d56814036fa54f9968b03 Mon Sep 17 00:00:00 2001 From: eeff <33566201+eeff@users.noreply.github.com> Date: Tue, 14 Mar 2023 10:50:36 +0800 Subject: [PATCH] feat(core): add template manager --- CMakeLists.txt | 1 + src/core/template_manager.c | 94 +++++++++++++++++++++++++++++++++++++ src/core/template_manager.h | 36 ++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 src/core/template_manager.c create mode 100644 src/core/template_manager.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c793326..be842918 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -104,6 +104,7 @@ set(NEURON_SOURCES src/core/subscribe.c src/core/plugin_manager.c src/core/node_manager.c + src/core/template_manager.c src/core/storage.c src/adapter/storage.c src/adapter/adapter.c diff --git a/src/core/template_manager.c b/src/core/template_manager.c new file mode 100644 index 00000000..2842f5ce --- /dev/null +++ b/src/core/template_manager.c @@ -0,0 +1,94 @@ +/** + * NEURON IIoT System for Industry 4.0 + * Copyright (C) 2020-2023 EMQ Technologies Co., Ltd All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + **/ + +#include "base/template.h" +#include "utils/uthash.h" + +#include "template_manager.h" + +typedef struct { + neu_template_t *tmpl; + UT_hash_handle hh; +} name_entry_t; + +static inline void template_entry_free(name_entry_t *ent) +{ + neu_template_free(ent->tmpl); + free(ent); +} + +struct neu_template_manager_s { + name_entry_t *name_map; +}; + +neu_template_manager_t *neu_template_manager_create() +{ + neu_template_manager_t *mgr = calloc(1, sizeof(*mgr)); + return mgr; +} + +void neu_template_manager_destroy(neu_template_manager_t *mgr) +{ + if (NULL == mgr) { + return; + } + + name_entry_t *ent = NULL, *tmp = NULL; + HASH_ITER(hh, mgr->name_map, ent, tmp) + { + HASH_DEL(mgr->name_map, ent); + template_entry_free(ent); + } + + free(mgr); +} + +int neu_template_manager_add(neu_template_manager_t *mgr, neu_template_t *tmpl) +{ + name_entry_t *ent = calloc(1, sizeof(*ent)); + if (NULL == ent) { + return -1; + } + + ent->tmpl = tmpl; + const char *name = neu_template_name(tmpl); + HASH_ADD_KEYPTR(hh, mgr->name_map, name, strlen(name), ent); + return 0; +} + +int neu_template_manager_del(neu_template_manager_t *mgr, const char *name) +{ + name_entry_t *ent = NULL; + HASH_FIND(hh, mgr->name_map, name, strlen(name), ent); + if (NULL == ent) { + return -1; + } + + HASH_DEL(mgr->name_map, ent); + template_entry_free(ent); + return 0; +} + +const neu_template_t * +neu_template_manager_find(const neu_template_manager_t *mgr, const char *name) +{ + name_entry_t *ent = NULL; + HASH_FIND(hh, mgr->name_map, name, strlen(name), ent); + return ent ? ent->tmpl : NULL; +} diff --git a/src/core/template_manager.h b/src/core/template_manager.h new file mode 100644 index 00000000..7ff33814 --- /dev/null +++ b/src/core/template_manager.h @@ -0,0 +1,36 @@ +/** + * NEURON IIoT System for Industry 4.0 + * Copyright (C) 2021-2023 EMQ Technologies Co., Ltd All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + **/ + +#ifndef _NEU_TEMPLATE_MANAGER_H_ +#define _NEU_TEMPLATE_MANAGER_H_ + +#include "base/template.h" + +typedef struct neu_template_manager_s neu_template_manager_t; + +neu_template_manager_t *neu_template_manager_create(); +void neu_template_manager_destroy(neu_template_manager_t *mgr); + +int neu_template_manager_add(neu_template_manager_t *mgr, neu_template_t *tmpl); +int neu_template_manager_del(neu_template_manager_t *mgr, const char *name); + +const neu_template_t * +neu_template_manager_find(const neu_template_manager_t *mgr, const char *name); + +#endif