From 0a82c55dab09b57a2dc7a19989548ca9b20c19ac Mon Sep 17 00:00:00 2001 From: Parker Liu Date: Wed, 18 Aug 2021 17:44:42 +0800 Subject: [PATCH] Implement a static plugin of default dashboard in rest.c --- plugins/restful/handle.c | 6 +- plugins/restful/handle.h | 6 +- plugins/restful/rest.c | 147 +++++++++++++++++++++++++++++++------- plugins/restful/rest.h | 5 -- src/adapter/adapter.c | 9 --- src/core/neu_manager.c | 36 ++++++---- src/core/plugin_manager.c | 5 +- src/core/plugin_manager.h | 3 + src/main.c | 2 - 9 files changed, 156 insertions(+), 63 deletions(-) diff --git a/plugins/restful/handle.c b/plugins/restful/handle.c index 83e81273..a68781fd 100644 --- a/plugins/restful/handle.c +++ b/plugins/restful/handle.c @@ -75,8 +75,8 @@ struct neu_rest_handler rest_handlers[] = { } }; -void neu_rest_all_handler(const struct neu_rest_handler **handlers, - uint32_t * size) +void neu_rest_init_all_handler(const struct neu_rest_handler **handlers, + uint32_t * size) { *handlers = rest_handlers; *size = sizeof(rest_handlers) / sizeof(struct neu_rest_handler); @@ -174,4 +174,4 @@ static void bad_request(nng_aio *aio, char *error) nng_aio_set_output(aio, 0, res); nng_aio_finish(aio, 0); -} \ No newline at end of file +} diff --git a/plugins/restful/handle.h b/plugins/restful/handle.h index 774d3c2e..c899cdcf 100644 --- a/plugins/restful/handle.h +++ b/plugins/restful/handle.h @@ -42,7 +42,7 @@ struct neu_rest_handler { } value; }; -void neu_rest_all_handler(const struct neu_rest_handler **handlers, - uint32_t * size); +void neu_rest_init_all_handler(const struct neu_rest_handler **handlers, + uint32_t * size); -#endif \ No newline at end of file +#endif diff --git a/plugins/restful/rest.c b/plugins/restful/rest.c index a32d4469..50aeeb6e 100644 --- a/plugins/restful/rest.c +++ b/plugins/restful/rest.c @@ -28,10 +28,29 @@ #include "neu_log.h" #include "rest.h" -struct neu_rest_context { - nng_http_server *server; +#define neu_plugin_module default_dashboard_plugin_module + +struct neu_plugin { + neu_plugin_common_t common; + nng_http_server * server; + uint32_t new_event_id; }; +/* +static uint32_t plugin_get_event_id(neu_plugin_t *plugin) +{ + uint32_t req_id; + + plugin->new_event_id++; + if (plugin->new_event_id == 0) { + plugin->new_event_id = 1; + } + + req_id = plugin->new_event_id; + return req_id; +} +*/ + static int rest_add_handler(nng_http_server * server, const struct neu_rest_handler *rest_handler) { @@ -88,20 +107,30 @@ static int rest_add_handler(nng_http_server * server, return ret; } -void neu_rest_stop(neu_rest_context_t *ctx) -{ - nng_http_server_stop(ctx->server); - nng_http_server_release(ctx->server); - free(ctx); -} - -neu_rest_context_t *neu_rest_start(void) +static neu_plugin_t *dashb_plugin_open(neu_adapter_t * adapter, + const adapter_callbacks_t *callbacks) { + neu_plugin_t * plugin; nng_http_server * server; nng_url * url; uint32_t n_handler = 0; const struct neu_rest_handler *rest_handlers = NULL; - neu_rest_context_t * context = NULL; + + if (adapter == NULL || callbacks == NULL) { + log_error("Open plugin with NULL adapter or callbacks"); + return NULL; + } + + plugin = (neu_plugin_t *) malloc(sizeof(neu_plugin_t)); + if (plugin == NULL) { + log_error("Failed to allocate plugin %s", + neu_plugin_module.module_name); + return NULL; + } + + plugin->common.adapter = adapter; + plugin->common.adapter_callbacks = callbacks; + plugin->new_event_id = 1; int ret = nng_url_parse(&url, "http://0.0.0.0:7000"); if (ret != 0) { @@ -113,7 +142,7 @@ neu_rest_context_t *neu_rest_start(void) return NULL; } - neu_rest_all_handler(&rest_handlers, &n_handler); + neu_rest_init_all_handler(&rest_handlers, &n_handler); for (uint32_t i = 0; i < n_handler; i++) { rest_add_handler(server, &rest_handlers[i]); } @@ -123,24 +152,94 @@ neu_rest_context_t *neu_rest_start(void) return NULL; } + plugin->server = server; nng_url_free(url); + log_info("Success to create plugin: %s", neu_plugin_module.module_name); + return plugin; +} - context = calloc(1, sizeof(neu_rest_context_t)); - context->server = server; +static int dashb_plugin_close(neu_plugin_t *plugin) +{ + int rv = 0; - return context; + nng_http_server_stop(plugin->server); + nng_http_server_release(plugin->server); + free(plugin); + log_info("Success to free plugin: %s", neu_plugin_module.module_name); + return rv; +} + +static int dashb_plugin_init(neu_plugin_t *plugin) +{ + int rv = 0; + + (void) plugin; + + log_info("Initialize plugin: %s", neu_plugin_module.module_name); + return rv; +} + +static int dashb_plugin_uninit(neu_plugin_t *plugin) +{ + int rv = 0; + + (void) plugin; + + log_info("Uninitialize plugin: %s", neu_plugin_module.module_name); + return rv; +} + +static int dashb_plugin_config(neu_plugin_t *plugin, neu_config_t *configs) +{ + int rv = 0; + + (void) plugin; + (void) configs; + + log_info("config plugin: %s", neu_plugin_module.module_name); + return rv; +} + +static int dashb_plugin_request(neu_plugin_t *plugin, neu_request_t *req) +{ + int rv = 0; + + if (plugin == NULL || req == NULL) { + log_warn("The plugin pointer or request is NULL"); + return (-1); + } + + log_info("send request to plugin: %s", neu_plugin_module.module_name); + const adapter_callbacks_t *adapter_callbacks; + adapter_callbacks = plugin->common.adapter_callbacks; + + switch (req->req_type) { + default: + break; + } + return rv; +} + +static int dashb_plugin_event_reply(neu_plugin_t * plugin, + neu_event_reply_t *reply) +{ + int rv = 0; + + (void) plugin; + (void) reply; + + log_info("reply event to plugin: %s", neu_plugin_module.module_name); + return rv; } static const neu_plugin_intf_funs_t plugin_intf_funs = { - /* - .open = default_dashb_plugin_open, - .close = default_dashb_plugin_close, - .init = default_dashb_plugin_init, - .uninit = default_dashb_plugin_uninit, - .config = default_dashb_plugin_config, - .request = default_dashb_plugin_request, - .event_reply = default_dashb_plugin_event_reply - */ + .open = dashb_plugin_open, + .close = dashb_plugin_close, + .init = dashb_plugin_init, + .uninit = dashb_plugin_uninit, + .config = dashb_plugin_config, + .request = dashb_plugin_request, + .event_reply = dashb_plugin_event_reply }; #define DEFAULT_DASHBOARD_PLUGIN_DESCR \ diff --git a/plugins/restful/rest.h b/plugins/restful/rest.h index 90c00485..956d044d 100644 --- a/plugins/restful/rest.h +++ b/plugins/restful/rest.h @@ -24,9 +24,4 @@ const neu_plugin_module_t default_dashboard_plugin_module; -typedef struct neu_rest_context neu_rest_context_t; - -neu_rest_context_t *neu_rest_start(void); -void neu_rest_stop(neu_rest_context_t *ctx); - #endif diff --git a/src/adapter/adapter.c b/src/adapter/adapter.c index 7f8812ee..025a3530 100644 --- a/src/adapter/adapter.c +++ b/src/adapter/adapter.c @@ -486,14 +486,6 @@ neu_adapter_t *neu_adapter_create(neu_adapter_info_t *info) adapter->new_req_id = 1; adapter->plugin_id = info->plugin_id; adapter->plugin_kind = info->plugin_kind; - if (info->plugin_lib_name == NULL) { - adapter->plugin_lib_name = NULL; - adapter->plugin_lib = NULL; - adapter->plugin_module = NULL; - adapter->plugin = NULL; - log_info("Create a adapter without plugin"); - goto open_pipe; - } adapter->plugin_lib_name = strdup(info->plugin_lib_name); if (adapter->name == NULL || adapter->plugin_lib_name == NULL) { @@ -527,7 +519,6 @@ neu_adapter_t *neu_adapter_create(neu_adapter_info_t *info) } adapter->plugin = plugin; -open_pipe: rv = nng_pair1_open(&adapter->sock); if (rv != 0) { neu_panic("The adapter(%s) can't open pipe", adapter->name); diff --git a/src/core/neu_manager.c b/src/core/neu_manager.c index 815df514..509b21a8 100644 --- a/src/core/neu_manager.c +++ b/src/core/neu_manager.c @@ -110,6 +110,8 @@ static const char *const manager_url = "inproc://neu_manager"; #define SAMPLE_DRV_ADAPTER_NAME "sample-driver-adapter" #define SAMPLE_APP_ADAPTER_NAME "sample-app-adapter" #endif +#define DEFAULT_DASHBOARD_ADAPTER_NAME "default-dashboard-adapter" +#define WEBSERVER_ADAPTER_NAME "webserver-adapter" #define MQTT_ADAPTER_NAME "mqtt-adapter" #define ADAPTER_NAME_MAX_LEN 50 @@ -145,16 +147,16 @@ static const adapter_reg_param_t default_adapter_reg_params[] = { .plugin_name = MQTT_PLUGIN_NAME, .plugin_id = { 0 } // The plugin_id is nothing }, - /* { - .adapter_type = ADAPTER_TYPE_WEBSERVER, - .adapter_name = "builtin-webserver", - .plugin_name = NULL, // The builtin webserver has'nt plugin - .plugin_id = { 0 } // The plugin_id is nothing + .adapter_type = ADAPTER_TYPE_WEBSERVER, + .adapter_name = DEFAULT_DASHBOARD_ADAPTER_NAME, + .plugin_name = DEFAULT_DASHBOARD_PLUGIN_NAME, + .plugin_id = { 0 } // The plugin_id is nothing }, + /* { // for remote customize webserver by user, the plugin is a proxy .adapter_type = ADAPTER_TYPE_WEBSERVER, - .adapter_name = "webserver-adapter", + .adapter_name = WEBSERVER_ADAPTER_NAME, .plugin_name = WEBSERVER_PLUGIN_NAME, .plugin_id = { 0 } // The plugin_id is nothing }, @@ -184,7 +186,7 @@ static const char *default_plugin_lib_names[] = { (sizeof(default_plugin_lib_names) / sizeof(default_plugin_lib_names[0])) // clang-format off -static const plugin_reg_param_t system_plugin_infos[] = { +static const plugin_reg_param_t default_plugin_infos[] = { #ifdef NEU_HAS_SAMPLE_ADAPTER { .plugin_kind = PLUGIN_KIND_SYSTEM, @@ -206,6 +208,12 @@ static const plugin_reg_param_t system_plugin_infos[] = { .plugin_name = MQTT_PLUGIN_NAME, .plugin_lib_name = MQTT_PLUGIN_LIB_NAME }, + { + .plugin_kind = PLUGIN_KIND_STATIC, + .adapter_type = ADAPTER_TYPE_WEBSERVER, + .plugin_name = DEFAULT_DASHBOARD_PLUGIN_NAME, + .plugin_lib_name = DEFAULT_DASHBOARD_PLUGIN_LIB_NAME + }, /* { .plugin_kind = PLUGIN_KIND_SYSTEM, @@ -222,8 +230,8 @@ static const plugin_reg_param_t system_plugin_infos[] = { */ }; // clang-format on -#define SYSTEM_PLUGIN_INFO_SIZE \ - (sizeof(system_plugin_infos) / sizeof(system_plugin_infos[0])) +#define DEFAULT_PLUGIN_INFO_SIZE \ + (sizeof(default_plugin_infos) / sizeof(default_plugin_infos[0])) typedef struct config_add_param { char * config_name; @@ -584,19 +592,19 @@ static int register_default_plugins(neu_manager_t *manager) for (i = 0; i < DEFAULT_PLUGIN_COUNT; i++) { plugin_id.id_val = 0; - for (j = 0; j < SYSTEM_PLUGIN_INFO_SIZE; j++) { - if (strcmp(system_plugin_infos[j].plugin_lib_name, + for (j = 0; j < DEFAULT_PLUGIN_INFO_SIZE; j++) { + if (strcmp(default_plugin_infos[j].plugin_lib_name, default_plugin_lib_names[i]) == 0) { break; } } - if (j < SYSTEM_PLUGIN_INFO_SIZE) { - reg_param = system_plugin_infos[j]; + if (j < DEFAULT_PLUGIN_INFO_SIZE) { + reg_param = default_plugin_infos[j]; reg_param.plugin_lib_name = calloc(1, path_len); snprintf(reg_param.plugin_lib_name, path_len, "./%s", - system_plugin_infos[j].plugin_lib_name); + default_plugin_infos[j].plugin_lib_name); plugin_id = plugin_manager_reg_plugin(manager->plugin_manager, ®_param); if (plugin_id.id_val == 0) { diff --git a/src/core/plugin_manager.c b/src/core/plugin_manager.c index 0ecc1f0e..991fdd19 100644 --- a/src/core/plugin_manager.c +++ b/src/core/plugin_manager.c @@ -49,7 +49,6 @@ struct plugin_manager { vector_t custom_plugins; }; -#define DEFAULT_DASHBOARD_PLUGIN_LIB_NAME "libplugin_default_dashboard" /* * manage builtin static plugins */ @@ -58,7 +57,7 @@ static const plugin_reg_entity_t builtin_static_plugins[] = { .plugin_id = { 1 }, .plugin_kind = PLUGIN_KIND_STATIC, .adapter_type = ADAPTER_TYPE_WEBSERVER, - .plugin_name = "default_dashboard_plugin", + .plugin_name = DEFAULT_DASHBOARD_PLUGIN_NAME, .plugin_lib_name = DEFAULT_DASHBOARD_PLUGIN_LIB_NAME, }, }; @@ -626,7 +625,7 @@ static void *load_static_plugin(char * plugin_lib_name, *p_plugin_module = (neu_plugin_module_t *) plugin_module_maps[i].plugin_module; } - return (void *) i; + return (void *) (i + 1); // the pointer must not be NULL } } return NULL; diff --git a/src/core/plugin_manager.h b/src/core/plugin_manager.h index 6bbe51ea..c62ac593 100644 --- a/src/core/plugin_manager.h +++ b/src/core/plugin_manager.h @@ -26,6 +26,9 @@ #include "core/plugin_info.h" #include "neu_plugin.h" +#define DEFAULT_DASHBOARD_PLUGIN_NAME "default-dashboard-plugin" +#define DEFAULT_DASHBOARD_PLUGIN_LIB_NAME "libplugin_default_dashboard" + typedef struct plugin_reg_param { plugin_kind_e plugin_kind; adapter_type_e adapter_type; diff --git a/src/main.c b/src/main.c index f0e70498..dd68b61e 100644 --- a/src/main.c +++ b/src/main.c @@ -115,8 +115,6 @@ int main(int argc, char *argv[]) goto main_end; } - neu_rest_start(); - neu_manager_t *manager; log_info("running neuron main process, daemon: %d", is_daemon); manager = neu_manager_create();