From db914268befd8e3973fa8d2d352d7bca8e416f15 Mon Sep 17 00:00:00 2001 From: fengzero Date: Tue, 18 Jan 2022 07:56:40 +0000 Subject: [PATCH] the basic info(plugin name, type, kind...) of the plugin is read from the dynamic library add dynamic library search path configuration, install to /etc/ld.so.conf.d/ dynamic library search path priority: 1. driver.path in neuron.yaml 2. os path --- CMakeLists.txt | 4 +- docs/json-api/neu_json_plugin.json | 9 ---- include/json/neu_json_plugin.h | 16 +----- include/neu_adapter.h | 5 +- include/neu_errcodes.h | 43 ++++++++-------- include/neu_plugin.h | 8 +-- neuron.conf | 2 + neuron.yaml | 3 +- plugins/modbus/modbus_tcp.c | 4 +- plugins/mqtt/command/command.c | 9 ---- plugins/mqtt/command/plugin.c | 18 ++----- plugins/mqtt/command/plugin.h | 2 - plugins/mqtt/plugin_mqtt.c | 4 +- plugins/restful/handle.c | 6 --- plugins/restful/http.c | 3 ++ plugins/restful/plugin_handle.c | 26 ++-------- plugins/restful/rest.c | 16 +++--- plugins/sample/plugin_sample_app.c | 9 ++-- plugins/sample/plugin_sample_drv.c | 4 +- src/adapter/adapter.c | 30 +++++------ src/base/neu_plugin_common.c | 9 +--- src/config.c | 16 ++++-- src/config.h | 5 +- src/core/neu_manager.c | 39 ++++++++++---- src/core/plugin_manager.c | 36 ++++++++++--- src/main.c | 27 +++++++--- src/parser/neu_json_plugin.c | 82 +++--------------------------- tests/config_test.cc | 25 +++------ tests/json_plugin_test.cc | 26 +--------- tests/jwt_test.cc | 19 ++++--- 30 files changed, 200 insertions(+), 305 deletions(-) create mode 100644 neuron.conf diff --git a/CMakeLists.txt b/CMakeLists.txt index b226dde8..38d83d67 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -182,6 +182,8 @@ if(NOT DISABLE_UT) add_subdirectory(tests) endif() + +install(FILES "${CMAKE_SOURCE_DIR}/neuron.conf" DESTINATION /etc/ld.so.conf.d) install(DIRECTORY "${CMAKE_SOURCE_DIR}/include/" DESTINATION include/neuron FILES_MATCHING @@ -195,4 +197,4 @@ install(FILES "${CMAKE_SOURCE_DIR}/cmake/neuron-config.cmake" DESTINATION lib/cmake/neuron) install(TARGETS neuron-base neuron LIBRARY DESTINATION lib - RUNTIME DESTINATION bin) + RUNTIME DESTINATION bin) \ No newline at end of file diff --git a/docs/json-api/neu_json_plugin.json b/docs/json-api/neu_json_plugin.json index 99f3fb95..5364dea3 100644 --- a/docs/json-api/neu_json_plugin.json +++ b/docs/json-api/neu_json_plugin.json @@ -1,19 +1,10 @@ { "add_plugin_req": { - "kind": 1, - "node_type": 1, - "name": "plugin_name", "lib_name": "plugin_name.so" }, "del_plugin_req": { "id": 1 }, - "update_plugin_req": { - "kind": 1, - "node_type": 1, - "name": "plugin_name", - "lib_name": "plugin_name.so" - }, "get_plugin_req": { "id": 1 }, diff --git a/include/json/neu_json_plugin.h b/include/json/neu_json_plugin.h index 0d408e16..1a8f1902 100644 --- a/include/json/neu_json_plugin.h +++ b/include/json/neu_json_plugin.h @@ -32,10 +32,7 @@ extern "C" { #endif typedef struct { - char * name; - int64_t kind; - char * lib_name; - int64_t node_type; + char *lib_name; } neu_json_add_plugin_req_t; int neu_json_decode_add_plugin_req(char * buf, @@ -73,17 +70,6 @@ typedef struct { int neu_json_encode_get_plugin_resp(void *json_object, void *param); -typedef struct { - char * name; - int64_t kind; - char * lib_name; - int64_t node_type; -} neu_json_update_plugin_req_t; - -int neu_json_decode_update_plugin_req(char * buf, - neu_json_update_plugin_req_t **result); -void neu_json_decode_update_plugin_req_free(neu_json_update_plugin_req_t *req); - #ifdef __cplusplus } #endif diff --git a/include/neu_adapter.h b/include/neu_adapter.h index 37fdc265..6f501e6d 100644 --- a/include/neu_adapter.h +++ b/include/neu_adapter.h @@ -221,10 +221,7 @@ typedef struct plugin_lib_info { /* NEU_REQRESP_ADD_PLUGIN_LIB */ typedef struct neu_cmd_add_plugin_lib_t { - plugin_kind_e plugin_kind; - neu_node_type_e node_type; - const char * plugin_name; - const char * plugin_lib_name; + const char *plugin_lib_name; } neu_cmd_add_plugin_lib_t; /* NEU_REQRESP_DEL_PLUGIN_LIB */ diff --git a/include/neu_errcodes.h b/include/neu_errcodes.h index 3909eb08..2eb5a668 100644 --- a/include/neu_errcodes.h +++ b/include/neu_errcodes.h @@ -70,26 +70,29 @@ typedef enum { NEU_ERR_INVALID_TOKEN = 1008, NEU_ERR_INVALID_USER_OR_PASSWORD = 1009, - NEU_ERR_PLUGIN_NAME_NOT_FOUND = 2000, - NEU_ERR_NODE_TYPE_INVALID = 2001, - NEU_ERR_NODE_EXIST = 2002, - NEU_ERR_NODE_NOT_EXIST = 2003, - NEU_ERR_GRP_CONFIG_NOT_EXIST = 2004, - NEU_ERR_TAG_NOT_EXIST = 2005, - NEU_ERR_TAG_ATTRIBUTE_NOT_SUPPORT = 2006, - NEU_ERR_NODE_SETTING_INVALID = 2007, - NEU_ERR_NODE_SETTING_NOT_FOUND = 2008, - NEU_ERR_GRP_CONFIG_IN_USE = 2009, - NEU_ERR_NODE_NOT_READY = 2010, - NEU_ERR_NODE_IS_RUNNING = 2011, - NEU_ERR_NODE_NOT_RUNNING = 2012, - NEU_ERR_NODE_IS_STOPED = 2013, - NEU_ERR_GRP_NOT_SUBSCRIBE = 2014, - NEU_ERR_TAG_TYPE_NOT_SUPPORT = 2015, - NEU_ERR_GRP_CONFIG_EXIST = 2016, - NEU_ERR_TAG_NAME_EXIST = 2017, - NEU_ERR_TAG_LIST_NOT_EXIST = 2018, - NEU_ERR_TAG_ADDRESS_FORMAT_INVALID = 2019, + NEU_ERR_PLUGIN_NAME_NOT_FOUND = 2000, + NEU_ERR_NODE_TYPE_INVALID = 2001, + NEU_ERR_NODE_EXIST = 2002, + NEU_ERR_NODE_NOT_EXIST = 2003, + NEU_ERR_GRP_CONFIG_NOT_EXIST = 2004, + NEU_ERR_TAG_NOT_EXIST = 2005, + NEU_ERR_TAG_ATTRIBUTE_NOT_SUPPORT = 2006, + NEU_ERR_NODE_SETTING_INVALID = 2007, + NEU_ERR_NODE_SETTING_NOT_FOUND = 2008, + NEU_ERR_GRP_CONFIG_IN_USE = 2009, + NEU_ERR_NODE_NOT_READY = 2010, + NEU_ERR_NODE_IS_RUNNING = 2011, + NEU_ERR_NODE_NOT_RUNNING = 2012, + NEU_ERR_NODE_IS_STOPED = 2013, + NEU_ERR_GRP_NOT_SUBSCRIBE = 2014, + NEU_ERR_TAG_TYPE_NOT_SUPPORT = 2015, + NEU_ERR_GRP_CONFIG_EXIST = 2016, + NEU_ERR_TAG_NAME_EXIST = 2017, + NEU_ERR_TAG_LIST_NOT_EXIST = 2018, + NEU_ERR_TAG_ADDRESS_FORMAT_INVALID = 2019, + NEU_ERR_PLUGIN_LIBRARY_NOT_FOUND = 2020, + NEU_ERR_PLUGIN_LIBRARY_INFO_INVALID = 2021, + NEU_ERR_PLUGIN_LIBRARY_NAME_CONFLICT = 2022, NEU_ERR_DEVICE_CONNECTION_FAILURE = 3000, NEU_ERR_DEVICE_READ_FAILURE = 3001, diff --git a/include/neu_plugin.h b/include/neu_plugin.h index f3af1bad..a6879009 100644 --- a/include/neu_plugin.h +++ b/include/neu_plugin.h @@ -58,6 +58,8 @@ typedef struct neu_plugin_module { const char * module_name; const char * module_descr; const neu_plugin_intf_funs_t *intf_funs; + neu_node_type_e type; + plugin_kind_e kind; } neu_plugin_module_t; inline static neu_plugin_common_t * @@ -118,10 +120,8 @@ void neu_plugin_response_trans_data(neu_plugin_t * plugin, neu_node_id_t neu_plugin_self_node_id(neu_plugin_t *plugin); const char * neu_plugin_self_node_name(neu_plugin_t *plugin); -intptr_t neu_system_add_plugin(neu_plugin_t *plugin, plugin_kind_e kind, - neu_node_type_e node_type, - const char * plugin_name, - const char * plugin_lib_name); +intptr_t neu_system_add_plugin(neu_plugin_t *plugin, + const char * plugin_lib_name); intptr_t neu_system_del_plugin(neu_plugin_t *plugin, plugin_id_t plugin_id); intptr_t neu_system_update_plugin(neu_plugin_t *plugin, plugin_kind_e kind, neu_node_type_e node_type, diff --git a/neuron.conf b/neuron.conf new file mode 100644 index 00000000..1d267f6d --- /dev/null +++ b/neuron.conf @@ -0,0 +1,2 @@ +# neuron driver default configuration +/usr/local/lib/neuron \ No newline at end of file diff --git a/neuron.yaml b/neuron.yaml index b6968de6..c24f4392 100644 --- a/neuron.yaml +++ b/neuron.yaml @@ -1,4 +1,5 @@ -lib_path: ./ +driver: + path: ./ web: host: 0.0.0.0 port: 7000 diff --git a/plugins/modbus/modbus_tcp.c b/plugins/modbus/modbus_tcp.c index e875d636..1942e352 100644 --- a/plugins/modbus/modbus_tcp.c +++ b/plugins/modbus/modbus_tcp.c @@ -736,7 +736,9 @@ static const neu_plugin_intf_funs_t plugin_intf_funs = { const neu_plugin_module_t neu_plugin_module = { .version = NEURON_PLUGIN_VER_1_0, - .module_name = "neuron-modbus-tcp-plugin", + .module_name = "modbus-tcp-plugin", .module_descr = "modbus tcp driver plugin", .intf_funs = &plugin_intf_funs, + .kind = PLUGIN_KIND_SYSTEM, + .type = NEU_NODE_TYPE_DRIVER, }; diff --git a/plugins/mqtt/command/command.c b/plugins/mqtt/command/command.c index 6a0cf076..f4e57806 100644 --- a/plugins/mqtt/command/command.c +++ b/plugins/mqtt/command/command.c @@ -241,15 +241,6 @@ void command_response_handle(mqtt_response_t *response) } break; } - case NEU_MQTT_OP_UPDATE_PLUGIN: { - neu_json_update_plugin_req_t *req = NULL; - rc = neu_json_decode_update_plugin_req(json_str, &req); - if (0 == rc) { - ret_str = command_plugin_update(plugin, mqtt, req); - neu_json_decode_update_plugin_req_free(req); - } - break; - } case NEU_MQTT_OP_DELETE_PLUGIN: { neu_json_del_plugin_req_t *req = NULL; rc = neu_json_decode_del_plugin_req(json_str, &req); diff --git a/plugins/mqtt/command/plugin.c b/plugins/mqtt/command/plugin.c index f021eb36..f83bb556 100644 --- a/plugins/mqtt/command/plugin.c +++ b/plugins/mqtt/command/plugin.c @@ -65,23 +65,11 @@ char *command_plugin_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, char *command_plugin_add(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, neu_json_add_plugin_req_t *req) { - log_info("Add plugin uuid:%s, node name:%s", mqtt->uuid, req->name); + log_info("Add plugin uuid:%s, node name:%s", mqtt->uuid, req->lib_name); neu_json_error_resp_t error = { 0 }; - error.error = neu_system_add_plugin(plugin, req->kind, req->node_type, - req->name, req->lib_name); - char *result = NULL; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} -char *command_plugin_update(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_update_plugin_req_t *req) -{ - log_info("Update plugin uuid:%s, node name:%s", mqtt->uuid, req->name); - neu_json_error_resp_t error = { 0 }; - error.error = neu_system_update_plugin(plugin, req->kind, req->node_type, - req->name, req->lib_name); + error.error = neu_system_add_plugin(plugin, req->lib_name); + char *result = NULL; neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, neu_json_encode_mqtt_resp, &result); diff --git a/plugins/mqtt/command/plugin.h b/plugins/mqtt/command/plugin.h index c98835c2..a1a15b88 100644 --- a/plugins/mqtt/command/plugin.h +++ b/plugins/mqtt/command/plugin.h @@ -32,8 +32,6 @@ char *command_plugin_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, neu_json_get_plugin_req_t *req); char *command_plugin_add(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, neu_json_add_plugin_req_t *req); -char *command_plugin_update(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_update_plugin_req_t *req); char *command_plugin_delete(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, neu_json_del_plugin_req_t *req); diff --git a/plugins/mqtt/plugin_mqtt.c b/plugins/mqtt/plugin_mqtt.c index 2f3233ae..74aaca35 100644 --- a/plugins/mqtt/plugin_mqtt.c +++ b/plugins/mqtt/plugin_mqtt.c @@ -450,5 +450,7 @@ const neu_plugin_module_t neu_plugin_module = { .version = NEURON_PLUGIN_VER_1_0, .module_name = "neuron-mqtt-plugin", .module_descr = "Neuron northbound MQTT communication plugin", - .intf_funs = &plugin_intf_funs + .intf_funs = &plugin_intf_funs, + .kind = PLUGIN_KIND_SYSTEM, + .type = NEU_NODE_TYPE_MQTT }; diff --git a/plugins/restful/handle.c b/plugins/restful/handle.c index f32758db..578403d3 100644 --- a/plugins/restful/handle.c +++ b/plugins/restful/handle.c @@ -208,12 +208,6 @@ struct neu_rest_handler api_handlers[] = { .url = "/api/v2/plugin", .value.handler = handle_get_plugin, }, - { - .method = NEU_REST_METHOD_PUT, - .type = NEU_REST_HANDLER_FUNCTION, - .url = "/api/v2/plugin", - .value.handler = handle_update_plugin, - }, { .method = NEU_REST_METHOD_DELETE, .type = NEU_REST_HANDLER_FUNCTION, diff --git a/plugins/restful/http.c b/plugins/restful/http.c index d63c0e61..ff67837c 100644 --- a/plugins/restful/http.c +++ b/plugins/restful/http.c @@ -263,6 +263,7 @@ int http_response(nng_aio *aio, neu_err_code_e code, char *content) case NEU_ERR_DEVICE_TAG_NOT_EXIST: case NEU_ERR_DEVICE_TAG_TYPE_NOT_MATCH: case NEU_ERR_NODE_SETTING_INVALID: + case NEU_ERR_PLUGIN_LIBRARY_INFO_INVALID: status = NNG_HTTP_STATUS_BAD_REQUEST; break; case NEU_ERR_PLUGIN_NAME_NOT_FOUND: @@ -270,6 +271,7 @@ int http_response(nng_aio *aio, neu_err_code_e code, char *content) case NEU_ERR_GRP_CONFIG_NOT_EXIST: case NEU_ERR_TAG_NOT_EXIST: case NEU_ERR_GRP_NOT_SUBSCRIBE: + case NEU_ERR_PLUGIN_LIBRARY_NOT_FOUND: status = NNG_HTTP_STATUS_NOT_FOUND; break; case NEU_ERR_NODE_EXIST: @@ -280,6 +282,7 @@ int http_response(nng_aio *aio, neu_err_code_e code, char *content) case NEU_ERR_TAG_NAME_EXIST: case NEU_ERR_TAG_LIST_NOT_EXIST: case NEU_ERR_GRP_CONFIG_EXIST: + case NEU_ERR_PLUGIN_LIBRARY_NAME_CONFLICT: status = NNG_HTTP_STATUS_CONFLICT; break; case NEU_ERR_TAG_ATTRIBUTE_NOT_SUPPORT: diff --git a/plugins/restful/plugin_handle.c b/plugins/restful/plugin_handle.c index df12e7d1..957873b7 100644 --- a/plugins/restful/plugin_handle.c +++ b/plugins/restful/plugin_handle.c @@ -36,13 +36,9 @@ void handle_add_plugin(nng_aio *aio) REST_PROCESS_HTTP_REQUEST_VALIDATE_JWT( aio, neu_json_add_plugin_req_t, neu_json_decode_add_plugin_req, { - intptr_t err = neu_system_add_plugin( - plugin, req->kind, req->node_type, req->name, req->lib_name); - if (err != 0) { - http_bad_request(aio, "{\"error\": 1}"); - } else { - http_ok(aio, "{\"error\": 0}"); - } + NEU_JSON_RESPONSE_ERROR( + neu_system_add_plugin(plugin, req->lib_name), + { http_response(aio, error_code.error, result_error); }); }) } @@ -63,22 +59,6 @@ void handle_del_plugin(nng_aio *aio) }) } -void handle_update_plugin(nng_aio *aio) -{ - neu_plugin_t *plugin = neu_rest_get_plugin(); - - REST_PROCESS_HTTP_REQUEST_VALIDATE_JWT( - aio, neu_json_update_plugin_req_t, neu_json_decode_update_plugin_req, { - intptr_t err = neu_system_update_plugin( - plugin, req->kind, req->node_type, req->name, req->lib_name); - if (err != 0) { - http_bad_request(aio, "{\"error\": 1}"); - } else { - http_ok(aio, "{\"error\": 0}"); - } - }) -} - void handle_get_plugin(nng_aio *aio) { neu_plugin_t * plugin = neu_rest_get_plugin(); diff --git a/plugins/restful/rest.c b/plugins/restful/rest.c index 61871938..025cd879 100644 --- a/plugins/restful/rest.c +++ b/plugins/restful/rest.c @@ -105,8 +105,8 @@ static nng_http_server *server_init(char *type) char host_port[128] = { 0 }; nng_http_server *server; - host = neu_config_get_value("./neuron.yaml", 2, type, "host"); - port = neu_config_get_value("./neuron.yaml", 2, type, "port"); + host = neu_config_get_value(2, type, "host"); + port = neu_config_get_value(2, type, "port"); snprintf(host_port, sizeof(host_port), "http://%s:%s", host, port); log_info("%s bind url: %s", type, host_port); @@ -229,10 +229,10 @@ static int dashb_plugin_init(neu_plugin_t *plugin) (void) plugin; - char *private_key_path = neu_config_get_value( - (char *) "./neuron.yaml", 2, (char *) "api", (char *) "private_key"); - char *public_key_path = neu_config_get_value( - (char *) "./neuron.yaml", 2, (char *) "api", (char *) "public_key"); + char *private_key_path = + neu_config_get_value(2, (char *) "api", (char *) "private_key"); + char *public_key_path = + neu_config_get_value(2, (char *) "api", (char *) "public_key"); rv = neu_jwt_init(public_key_path, private_key_path); free(private_key_path); @@ -342,5 +342,7 @@ const neu_plugin_module_t default_dashboard_plugin_module = { .version = NEURON_PLUGIN_VER_1_0, .module_name = "neuron-default-dashboard", .module_descr = DEFAULT_DASHBOARD_PLUGIN_DESCR, - .intf_funs = &plugin_intf_funs + .intf_funs = &plugin_intf_funs, + .kind = PLUGIN_KIND_SYSTEM, + .type = NEU_NODE_TYPE_WEBSERVER }; diff --git a/plugins/sample/plugin_sample_app.c b/plugins/sample/plugin_sample_app.c index 8bbca6f8..cb3ee750 100644 --- a/plugins/sample/plugin_sample_app.c +++ b/plugins/sample/plugin_sample_app.c @@ -104,9 +104,6 @@ static void *sample_app_work_loop(void *arg) neu_response_t * result = NULL; neu_cmd_add_plugin_lib_t plugin_add_cmd; - plugin_add_cmd.plugin_kind = PLUGIN_KIND_SYSTEM; - plugin_add_cmd.node_type = NEU_NODE_TYPE_DRIVER; - plugin_add_cmd.plugin_name = "sample-drv-plugin"; plugin_add_cmd.plugin_lib_name = "libplugin-sample-drv.so"; sync_cmd.req_type = NEU_REQRESP_ADD_PLUGIN_LIB; @@ -117,7 +114,7 @@ static void *sample_app_work_loop(void *arg) rv = adapter_callbacks->command(plugin->common.adapter, &sync_cmd, &result); if (rv < 0) { log_error("Failed to add plugin lib with name: %s", - plugin_add_cmd.plugin_name); + plugin_add_cmd.plugin_lib_name); return NULL; } log_info("Result of add command is %d", (intptr_t) result->buf); @@ -571,5 +568,7 @@ const neu_plugin_module_t neu_plugin_module = { .version = NEURON_PLUGIN_VER_1_0, .module_name = "neuron-sample-app-plugin", .module_descr = SAMPLE_APP_PLUGIN_DESCR, - .intf_funs = &plugin_intf_funs + .intf_funs = &plugin_intf_funs, + .kind = PLUGIN_KIND_SYSTEM, + .type = NEU_NODE_TYPE_APP, }; diff --git a/plugins/sample/plugin_sample_drv.c b/plugins/sample/plugin_sample_drv.c index 4d7c856c..25c59a9a 100644 --- a/plugins/sample/plugin_sample_drv.c +++ b/plugins/sample/plugin_sample_drv.c @@ -332,5 +332,7 @@ const neu_plugin_module_t neu_plugin_module = { .version = NEURON_PLUGIN_VER_1_0, .module_name = "neuron-sample-drv-plugin", .module_descr = SAMPLE_DRV_PLUGIN_DESCR, - .intf_funs = &plugin_intf_funs + .intf_funs = &plugin_intf_funs, + .kind = PLUGIN_KIND_SYSTEM, + .type = NEU_NODE_TYPE_DRIVER }; diff --git a/src/adapter/adapter.c b/src/adapter/adapter.c index c0352044..0c172c1b 100644 --- a/src/adapter/adapter.c +++ b/src/adapter/adapter.c @@ -174,8 +174,8 @@ static neu_persister_t *g_persister_singleton = NULL; static void persister_singleton_init() { - const char *persistence_dir = neu_config_get_value( - (char *) "./neuron.yaml", 2, (char *) "persistence", (char *) "dir"); + const char *persistence_dir = + neu_config_get_value(2, (char *) "persistence", (char *) "dir"); if (NULL == persistence_dir) { log_error("can't get persistence dir from config"); exit(EXIT_FAILURE); @@ -214,9 +214,6 @@ static int persister_singleton_load_plugins(neu_adapter_t *adapter) neu_persist_plugin_info_t *plugin_info = iterator_get(&iter); plugin_id_t plugin_id = {}; neu_cmd_add_plugin_lib_t add_plugin_cmd = { - .node_type = plugin_info->adapter_type, - .plugin_kind = plugin_info->kind, - .plugin_name = plugin_info->name, .plugin_lib_name = plugin_info->plugin_lib_name, }; @@ -1068,19 +1065,16 @@ static int adapter_command(neu_adapter_t *adapter, neu_request_t *cmd, } case NEU_REQRESP_ADD_PLUGIN_LIB: { - ADAPTER_RESP_CODE( - adapter, cmd, intptr_t, neu_cmd_add_plugin_lib_t, rv, - NEU_REQRESP_ERR_CODE, p_result, { - plugin_id_t plugin_id; - ret = NEU_ERR_SUCCESS; - if (neu_manager_add_plugin_lib(adapter->manager, req_cmd, - &plugin_id) != 0) { - ret = NEU_ERR_FAILURE; - } else { - ADAPTER_SEND_PLUGIN_EVENT(adapter, rv, - MSG_EVENT_ADD_PLUGIN); - } - }); + ADAPTER_RESP_CODE(adapter, cmd, intptr_t, neu_cmd_add_plugin_lib_t, rv, + NEU_REQRESP_ERR_CODE, p_result, { + plugin_id_t plugin_id; + ret = neu_manager_add_plugin_lib( + adapter->manager, req_cmd, &plugin_id); + if (ret == NEU_ERR_SUCCESS) { + ADAPTER_SEND_PLUGIN_EVENT( + adapter, rv, MSG_EVENT_ADD_PLUGIN); + } + }); break; } diff --git a/src/base/neu_plugin_common.c b/src/base/neu_plugin_common.c index 4cedeb86..afd20b61 100644 --- a/src/base/neu_plugin_common.c +++ b/src/base/neu_plugin_common.c @@ -335,17 +335,12 @@ const char *neu_plugin_self_node_name(neu_plugin_t *plugin) return node_name; } -intptr_t neu_system_add_plugin(neu_plugin_t *plugin, plugin_kind_e kind, - neu_node_type_e node_type, - const char * plugin_name, - const char * plugin_lib_name) +intptr_t neu_system_add_plugin(neu_plugin_t *plugin, + const char * plugin_lib_name) { intptr_t errorcode = -1; neu_cmd_add_plugin_lib_t add_plugin_cmd = { 0 }; - add_plugin_cmd.plugin_kind = kind; - add_plugin_cmd.node_type = node_type; - add_plugin_cmd.plugin_name = plugin_name; add_plugin_cmd.plugin_lib_name = plugin_lib_name; PLUGIN_CALL_CMD(plugin, NEU_REQRESP_ADD_PLUGIN_LIB, add_plugin_cmd, diff --git a/src/config.c b/src/config.c index 253dface..480e7c45 100644 --- a/src/config.c +++ b/src/config.c @@ -27,7 +27,9 @@ #include "config.h" -int neu_config_init(char *config_path) +static char *config = NULL; + +int neu_config_init(const char *config_path) { FILE *file = fopen(config_path, "r"); @@ -37,9 +39,15 @@ int neu_config_init(char *config_path) } fclose(file); + config = strdup(config_path); return 0; } +void neu_config_uninit() +{ + free(config); +} + static int find_first_key(yaml_parser_t *parser, char *key) { yaml_token_t token = { 0 }; @@ -75,16 +83,16 @@ static int find_next_key(yaml_parser_t *parser, char *key) return find_first_key(parser, key); } -char *neu_config_get_value(char *config_path, int n_key, char *key, ...) +char *neu_config_get_value(int n_key, char *key, ...) { yaml_token_t token = { 0 }; yaml_token_t token_v = { 0 }; yaml_parser_t parser = { 0 }; char * result = NULL; - FILE * file = fopen(config_path, "r"); + FILE * file = fopen(config, "r"); if (file == NULL) { - log_error("open config %s fail, error: %d", config_path, errno); + log_error("open config %s fail, error: %d", config, errno); return NULL; } diff --git a/src/config.h b/src/config.h index 3bf47049..ee116ad1 100644 --- a/src/config.h +++ b/src/config.h @@ -21,9 +21,10 @@ extern "C" { #endif -int neu_config_init(char *config_file); +int neu_config_init(const char *config_file); +void neu_config_uninit(); // need free value -char *neu_config_get_value(char *config_file, int n_key, char *key, ...); +char *neu_config_get_value(int n_key, char *key, ...); #ifdef __cplusplus } diff --git a/src/core/neu_manager.c b/src/core/neu_manager.c index 43d6400d..3b903afa 100644 --- a/src/core/neu_manager.c +++ b/src/core/neu_manager.c @@ -729,7 +729,6 @@ static int register_default_plugins(neu_manager_t *manager) uint32_t i, j; plugin_id_t plugin_id; plugin_reg_param_t reg_param; - int path_len = 64; for (i = 0; i < DEFAULT_PLUGIN_COUNT; i++) { plugin_id.id_val = 0; @@ -741,14 +740,10 @@ static int register_default_plugins(neu_manager_t *manager) } if (j < DEFAULT_PLUGIN_INFO_SIZE) { - reg_param = default_plugin_infos[j]; - reg_param.plugin_lib_name = calloc(1, path_len); + reg_param = default_plugin_infos[j]; - snprintf(reg_param.plugin_lib_name, path_len, "./%s", - default_plugin_infos[j].plugin_lib_name); rv = plugin_manager_reg_plugin(manager->plugin_manager, ®_param, &plugin_id); - free(reg_param.plugin_lib_name); } } @@ -1987,13 +1982,35 @@ int neu_manager_add_plugin_lib(neu_manager_t * manager, neu_cmd_add_plugin_lib_t *cmd, plugin_id_t * p_plugin_id) { - int rv; - plugin_reg_param_t reg_param; + int rv = 0; + plugin_reg_param_t reg_param; + void * handle = NULL; + neu_plugin_module_t *module_info = NULL; - reg_param.plugin_kind = cmd->plugin_kind; - reg_param.adapter_type = adapter_type_from_node_type(cmd->node_type); - reg_param.plugin_name = cmd->plugin_name; + handle = load_plugin_library((char *) cmd->plugin_lib_name, + PLUGIN_KIND_SYSTEM, &module_info); + if (handle == NULL) { + return NEU_ERR_PLUGIN_LIBRARY_NOT_FOUND; + } + + reg_param.plugin_kind = module_info->kind; + reg_param.adapter_type = adapter_type_from_node_type(module_info->type); reg_param.plugin_lib_name = (char *) cmd->plugin_lib_name; + reg_param.plugin_name = module_info->module_name; + + unload_plugin_library(handle, PLUGIN_KIND_SYSTEM); + + if (reg_param.plugin_kind != PLUGIN_KIND_STATIC && + reg_param.plugin_kind != PLUGIN_KIND_SYSTEM && + reg_param.plugin_kind != PLUGIN_KIND_CUSTOM) { + return NEU_ERR_PLUGIN_LIBRARY_INFO_INVALID; + } + + if (reg_param.adapter_type <= ADAPTER_TYPE_UNKNOW || + reg_param.adapter_type >= ADAPTER_TYPE_MAX) { + return NEU_ERR_PLUGIN_LIBRARY_INFO_INVALID; + } + rv = plugin_manager_reg_plugin(manager->plugin_manager, ®_param, p_plugin_id); return rv; diff --git a/src/core/plugin_manager.c b/src/core/plugin_manager.c index a0981d60..d866b647 100644 --- a/src/core/plugin_manager.c +++ b/src/core/plugin_manager.c @@ -29,6 +29,7 @@ #include #include "adapter/adapter_internal.h" +#include "config.h" #include "dummy/dummy.h" #include "neu_panic.h" #include "neu_vector.h" @@ -325,7 +326,7 @@ int plugin_manager_reg_plugin(plugin_manager_t * plugin_mng, if (p_plugin_id->id_val == 0) { // There has same registered plugin log_warn("A plugin with same name has already been registered"); - return -1; + return NEU_ERR_PLUGIN_LIBRARY_NAME_CONFLICT; } reg_entity_init(®_entity, *p_plugin_id, param); @@ -587,12 +588,20 @@ void plugin_manager_dump(plugin_manager_t *plugin_mng) * load plugin and unload plugin */ -static void *load_dyn_plugin_library(char * plugin_lib_name, +static void *load_dyn_plugin_library(char *driver_path, char *plugin_lib_name, neu_plugin_module_t **p_plugin_module) { void *lib_handle; - lib_handle = dlopen(plugin_lib_name, RTLD_NOW | RTLD_NODELETE); + if (driver_path == NULL) { + lib_handle = dlopen(plugin_lib_name, RTLD_NOW | RTLD_NODELETE); + } else { + char lib_path[128] = { 0 }; + snprintf(lib_path, sizeof(lib_path) - 1, "%s%s", driver_path, + plugin_lib_name); + lib_handle = dlopen(lib_path, RTLD_NOW | RTLD_NODELETE); + } + if (lib_handle == NULL) { log_error("Failed to open dynamic library %s: %s", plugin_lib_name, dlerror()); @@ -668,11 +677,24 @@ static int unload_static_plugin(void *lib_handle) void *load_plugin_library(char *plugin_lib_name, plugin_kind_e plugin_kind, neu_plugin_module_t **p_plugin_module) { - if (plugin_kind == PLUGIN_KIND_STATIC) { - return load_static_plugin(plugin_lib_name, p_plugin_module); - } else { - return load_dyn_plugin_library(plugin_lib_name, p_plugin_module); + char *path = NULL; + void *result = NULL; + + path = neu_config_get_value(2, "driver", "path"); + + switch (plugin_kind) { + case PLUGIN_KIND_STATIC: + result = load_static_plugin(plugin_lib_name, p_plugin_module); + break; + case PLUGIN_KIND_SYSTEM: + case PLUGIN_KIND_CUSTOM: + result = + load_dyn_plugin_library(path, plugin_lib_name, p_plugin_module); + free(path); + break; } + + return result; } int unload_plugin_library(void *lib_handle, plugin_kind_e plugin_kind) diff --git a/src/main.c b/src/main.c index ed5eb220..823a341f 100644 --- a/src/main.c +++ b/src/main.c @@ -76,12 +76,12 @@ static void uninit() static void usage() { - fprintf(stderr, "neuron [--help] [--daemon]\n"); + fprintf(stderr, "neuron [--help] [--config] [--daemon]\n"); } -static int read_neuron_config() +static int read_neuron_config(const char *config) { - return neu_config_init("./neuron.yaml"); + return neu_config_init(config); } static void sig_handler(int sig) @@ -141,14 +141,16 @@ static void unbind_main_adapter(nng_pipe p, nng_pipe_ev ev, void *arg) int main(int argc, char *argv[]) { - int rv = 0; - bool is_daemon = false; + int rv = 0; + bool is_daemon = false; + char *config_file = NULL; init(); - char * opts = ":hd:W;"; + char * opts = "c::hd:W;"; struct option long_options[] = { { "help", no_argument, NULL, 1 }, + { "config", optional_argument, NULL, 'c' }, { "daemon", required_argument, NULL, 1 }, { NULL, 0, NULL, 0 }, }; @@ -168,6 +170,11 @@ int main(int argc, char *argv[]) optopt); usage(); exit(0); + case 'c': + if (argc == 3) { + config_file = strdup(argv[2]); + } + break; case '?': default: fprintf(stderr, "%s: option '-%c' is invalid: ignored\n", argv[0], @@ -177,7 +184,12 @@ int main(int argc, char *argv[]) } } - if ((rv = read_neuron_config()) < 0) { + if (config_file == NULL) { + config_file = strdup("./neuron.yaml"); + } + + rv = read_neuron_config(config_file); + if (rv < 0) { log_error("Failed to get neuron configuration."); goto main_end; } @@ -221,5 +233,6 @@ int main(int argc, char *argv[]) main_end: uninit(); + free(config_file); return rv; } diff --git a/src/parser/neu_json_plugin.c b/src/parser/neu_json_plugin.c index 37e7e18c..81ca946d 100644 --- a/src/parser/neu_json_plugin.c +++ b/src/parser/neu_json_plugin.c @@ -36,31 +36,18 @@ int neu_json_decode_add_plugin_req(char * buf, neu_json_add_plugin_req_t *req = calloc(1, sizeof(neu_json_add_plugin_req_t)); - neu_json_elem_t req_elems[] = { { - .name = "node_type", - .t = NEU_JSON_INT, - }, - { - .name = "name", - .t = NEU_JSON_STR, - }, - { - .name = "lib_name", - .t = NEU_JSON_STR, - }, - { - .name = "kind", - .t = NEU_JSON_INT, - } }; + neu_json_elem_t req_elems[] = { + { + .name = "lib_name", + .t = NEU_JSON_STR, + }, + }; ret = neu_json_decode(buf, NEU_JSON_ELEM_SIZE(req_elems), req_elems); if (ret != 0) { goto decode_fail; } - req->node_type = req_elems[0].v.val_int; - req->name = req_elems[1].v.val_str; - req->lib_name = req_elems[2].v.val_str; - req->kind = req_elems[3].v.val_int; + req->lib_name = req_elems[0].v.val_str; *result = req; return ret; @@ -75,7 +62,6 @@ decode_fail: void neu_json_decode_add_plugin_req_free(neu_json_add_plugin_req_t *req) { - free(req->name); free(req->lib_name); free(req); @@ -199,56 +185,4 @@ int neu_json_encode_get_plugin_resp(void *json_object, void *param) NEU_JSON_ELEM_SIZE(resp_elems)); return ret; -} - -int neu_json_decode_update_plugin_req(char * buf, - neu_json_update_plugin_req_t **result) -{ - int ret = 0; - - neu_json_update_plugin_req_t *req = - calloc(1, sizeof(neu_json_update_plugin_req_t)); - neu_json_elem_t req_elems[] = { { - .name = "node_type", - .t = NEU_JSON_INT, - }, - { - .name = "name", - .t = NEU_JSON_STR, - }, - { - .name = "lib_name", - .t = NEU_JSON_STR, - }, - { - .name = "kind", - .t = NEU_JSON_INT, - } }; - ret = neu_json_decode(buf, NEU_JSON_ELEM_SIZE(req_elems), req_elems); - if (ret != 0) { - goto decode_fail; - } - - req->node_type = req_elems[0].v.val_int; - req->name = req_elems[1].v.val_str; - req->lib_name = req_elems[2].v.val_str; - req->kind = req_elems[3].v.val_int; - - *result = req; - return ret; - -decode_fail: - if (req != NULL) { - free(req); - } - return -1; -} - -void neu_json_decode_update_plugin_req_free(neu_json_update_plugin_req_t *req) -{ - - free(req->name); - free(req->lib_name); - - free(req); -} +} \ No newline at end of file diff --git a/tests/config_test.cc b/tests/config_test.cc index b3af2f5b..5b133b2c 100644 --- a/tests/config_test.cc +++ b/tests/config_test.cc @@ -2,42 +2,31 @@ #include "config.h" -TEST(JsonTest, DecodeField) +TEST(ConfigTest, ReadConfig) { neu_config_init((char *) "./neuron.yaml"); - char *result = - neu_config_get_value((char *) "./neuron.yaml", 1, (char *) "lib_path"); - EXPECT_STREQ("./", result); - free(result); - - result = neu_config_get_value((char *) "./neuron.yaml", 2, (char *) "web", - (char *) "host"); + char *result = neu_config_get_value(2, (char *) "web", (char *) "host"); EXPECT_STREQ("0.0.0.0", result); free(result); - result = neu_config_get_value((char *) "./neuron.yaml", 2, (char *) "web", - (char *) "port"); + result = neu_config_get_value(2, (char *) "web", (char *) "port"); EXPECT_STREQ("7000", result); free(result); - result = neu_config_get_value((char *) "./neuron.yaml", 2, (char *) "api", - (char *) "host"); + result = neu_config_get_value(2, (char *) "api", (char *) "host"); EXPECT_STREQ("0.0.0.0", result); free(result); - result = neu_config_get_value((char *) "./neuron.yaml", 2, (char *) "api", - (char *) "port"); + result = neu_config_get_value(2, (char *) "api", (char *) "port"); EXPECT_STREQ("7001", result); free(result); - result = neu_config_get_value((char *) "./neuron.yaml", 2, (char *) "api", - (char *) "public_key"); + result = neu_config_get_value(2, (char *) "api", (char *) "public_key"); EXPECT_STREQ("./public_test.pem", result); free(result); - result = neu_config_get_value((char *) "./neuron.yaml", 2, (char *) "api", - (char *) "private_key"); + result = neu_config_get_value(2, (char *) "api", (char *) "private_key"); EXPECT_STREQ("./private_test.key", result); free(result); } diff --git a/tests/json_plugin_test.cc b/tests/json_plugin_test.cc index b36952e5..83c9700b 100644 --- a/tests/json_plugin_test.cc +++ b/tests/json_plugin_test.cc @@ -9,17 +9,11 @@ TEST(JsonPluginTest, AddPluginDecode) { - char *buf = (char *) "{\"kind\": 1, " - "\"node_type\": 1, " - "\"name\": \"plugin1\", " - "\"lib_name\": \"lib1\" }"; + char * buf = (char *) "{\"lib_name\": \"lib1\" }"; neu_json_add_plugin_req_t *req = NULL; EXPECT_EQ(0, neu_json_decode_add_plugin_req(buf, &req)); - EXPECT_EQ(1, req->kind); - EXPECT_EQ(1, req->node_type); - EXPECT_STREQ("plugin1", req->name); EXPECT_STREQ("lib1", req->lib_name); neu_json_decode_add_plugin_req_free(req); @@ -37,24 +31,6 @@ TEST(JsonPluginTest, DeletePluginDecode) neu_json_decode_del_plugin_req_free(req); } -TEST(JsonPluginTest, UpdatePluginDecode) -{ - char *buf = (char *) "{\"kind\": 2, " - "\"node_type\": 2, " - "\"name\": \"plugin1\", " - "\"lib_name\": \"lib1\" }"; - neu_json_update_plugin_req_t *req = NULL; - - EXPECT_EQ(0, neu_json_decode_update_plugin_req(buf, &req)); - - EXPECT_EQ(2, req->kind); - EXPECT_EQ(2, req->node_type); - EXPECT_STREQ("plugin1", req->name); - EXPECT_STREQ("lib1", req->lib_name); - - neu_json_decode_update_plugin_req_free(req); -} - TEST(JsonPluginTest, GetPluginEncode) { char *buf = diff --git a/tests/jwt_test.cc b/tests/jwt_test.cc index 74d56cfe..fe66d6f5 100644 --- a/tests/jwt_test.cc +++ b/tests/jwt_test.cc @@ -13,10 +13,12 @@ TEST(JsonTest, SetJwt) unsigned char public_key[4096] = { 0 }; size_t pub_key_len = 0; - char *public_key_path = neu_config_get_value( - (char *) "./neuron.yaml", 2, (char *) "api", (char *) "public_key"); - char *private_key_path = neu_config_get_value( - (char *) "./neuron.yaml", 2, (char *) "api", (char *) "private_key"); + neu_config_init((char *) "./neuron.yaml"); + + char *public_key_path = + neu_config_get_value(2, (char *) "api", (char *) "public_key"); + char *private_key_path = + neu_config_get_value(2, (char *) "api", (char *) "private_key"); EXPECT_EQ(0, neu_jwt_init(public_key_path, private_key_path)); @@ -36,10 +38,11 @@ TEST(JsonTest, JwtValidate) size_t pub_key_len = 0; struct timeval expire_tv = { 0 }; - char *private_key_path = neu_config_get_value( - (char *) "./neuron.yaml", 2, (char *) "api", (char *) "private_key"); - char *public_key_path = neu_config_get_value( - (char *) "./neuron.yaml", 2, (char *) "api", (char *) "public_key"); + neu_config_init((char *) "./neuron.yaml"); + char *private_key_path = + neu_config_get_value(2, (char *) "api", (char *) "private_key"); + char *public_key_path = + neu_config_get_value(2, (char *) "api", (char *) "public_key"); EXPECT_EQ(0, neu_jwt_init(public_key_path, private_key_path)); EXPECT_EQ(0, neu_jwt_new(&token));