From bfa0d300344415115b4e93d67f91f1e23d0e6c78 Mon Sep 17 00:00:00 2001 From: eeff <33566201+eeff@users.noreply.github.com> Date: Tue, 7 Mar 2023 14:32:38 +0800 Subject: [PATCH 1/2] feat: json decoding for global config node setting --- src/parser/neu_json_global_config.c | 83 ++++++++++++++++++++++++++++- src/parser/neu_json_global_config.h | 11 ++++ 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/src/parser/neu_json_global_config.c b/src/parser/neu_json_global_config.c index d9463a43..f9e4e543 100644 --- a/src/parser/neu_json_global_config.c +++ b/src/parser/neu_json_global_config.c @@ -354,6 +354,83 @@ void neu_json_decode_global_config_req_subscriptions_free( free(req_sub); } +static inline void fini_node_setting(neu_json_node_setting_req_t *req) +{ + free(req->node); + free(req->setting); +} + +int neu_json_decode_global_config_req_settings( + void *json_obj, neu_json_global_config_req_settings_t **result) +{ + json_t *settings_array = json_object_get(json_obj, "settings"); + if (!json_is_array(settings_array)) { + nlog_error("no `settings` array field"); + return -1; + } + + neu_json_global_config_req_settings_t *req = calloc(1, sizeof(*req)); + if (NULL == req) { + nlog_error("calloc fail"); + return -1; + } + + req->n_setting = json_array_size(settings_array); + req->settings = calloc(req->n_setting, sizeof(*req->settings)); + if (NULL == req->settings) { + free(req); + nlog_error("calloc fail"); + return -1; + } + + neu_json_node_setting_req_t *p = req->settings; + for (int i = 0; i < req->n_setting; ++i) { + json_t *setting_obj = json_array_get(settings_array, i); + json_t *node_obj = json_object_get(setting_obj, "node"); + + if (NULL == node_obj) { + nlog_error("no settings `node` field"); + goto error; + } + + p->node = strdup(json_string_value(node_obj)); + if (NULL == p->node) { + nlog_error("strdup fail"); + goto error; + } + + if (0 != neu_json_dump_key(setting_obj, "params", &p->setting, true)) { + free(p->node); + nlog_error("no settings `params` field"); + goto error; + } + ++p; + } + + *result = req; + return 0; + +error: + while (--p >= req->settings) { + fini_node_setting(p); + } + free(req->settings); + free(req); + return -1; +} + +void neu_json_decode_global_config_req_settings_free( + neu_json_global_config_req_settings_t *req_settings) +{ + if (req_settings) { + for (int i = 0; i < req_settings->n_setting; ++i) { + fini_node_setting(&req_settings->settings[i]); + } + free(req_settings->settings); + free(req_settings); + } +} + int neu_json_decode_global_config_req(char * buf, neu_json_global_config_req_t **result) { @@ -376,7 +453,10 @@ int neu_json_decode_global_config_req(char * buf, 0 == neu_json_decode_global_config_req_tags(json_obj, &req->tags) && 0 == neu_json_decode_global_config_req_subscriptions( - json_obj, &req->subscriptions)) { + json_obj, &req->subscriptions) && + 0 == + neu_json_decode_global_config_req_settings(json_obj, + &req->settings)) { *result = req; } else { ret = -1; @@ -395,6 +475,7 @@ void neu_json_decode_global_config_req_free(neu_json_global_config_req_t *req) neu_json_decode_global_config_req_tags_free(req->tags); neu_json_decode_global_config_req_subscriptions_free( req->subscriptions); + neu_json_decode_global_config_req_settings_free(req->settings); free(req); } } diff --git a/src/parser/neu_json_global_config.h b/src/parser/neu_json_global_config.h index 253432a6..f1f1dc37 100644 --- a/src/parser/neu_json_global_config.h +++ b/src/parser/neu_json_global_config.h @@ -50,11 +50,22 @@ int neu_json_decode_global_config_req_subscriptions( void neu_json_decode_global_config_req_subscriptions_free( neu_json_global_config_req_subscriptions_t *req_sub); +typedef struct { + int n_setting; + neu_json_node_setting_req_t *settings; +} neu_json_global_config_req_settings_t; + +int neu_json_decode_global_config_req_settings( + void *json_obj, neu_json_global_config_req_settings_t **result); +void neu_json_decode_global_config_req_settings_free( + neu_json_global_config_req_settings_t *req_settings); + typedef struct { neu_json_get_nodes_resp_t * nodes; neu_json_get_driver_group_resp_t * groups; neu_json_global_config_req_tags_t * tags; neu_json_global_config_req_subscriptions_t *subscriptions; + neu_json_global_config_req_settings_t * settings; } neu_json_global_config_req_t; int neu_json_decode_global_config_req(char * buf, From bde5060b09774ac525b9c25339f60e7c4698efc1 Mon Sep 17 00:00:00 2001 From: eeff <33566201+eeff@users.noreply.github.com> Date: Tue, 7 Mar 2023 14:35:07 +0800 Subject: [PATCH 2/2] feat(restful): put global config node setting --- plugins/restful/global_config_handle.c | 45 ++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/plugins/restful/global_config_handle.c b/plugins/restful/global_config_handle.c index 523d7af2..61b01966 100644 --- a/plugins/restful/global_config_handle.c +++ b/plugins/restful/global_config_handle.c @@ -53,6 +53,7 @@ typedef enum { STATE_ADD_GROUP, STATE_ADD_TAG, STATE_ADD_SUBSCRIPTION, + STATE_ADD_SETTING, STATE_END, } context_state_e; @@ -98,6 +99,7 @@ static int add_group(context_t * ctx, neu_json_get_driver_group_resp_group_t *data); static int add_tag(context_t *ctx, neu_json_add_tags_req_t *data); static int add_subscription(context_t *ctx, neu_json_subscribe_req_t *data); +static int add_setting(context_t *ctx, neu_json_node_setting_req_t *data); static inline bool is_static_node(const char *name, const char *plugin) { @@ -409,6 +411,28 @@ static void put_context_next(context_t *ctx, neu_reqresp_type_e type, break; } + ctx->idx = 0; + ctx->state = STATE_ADD_SETTING; + } + // fall through + + case STATE_ADD_SETTING: { + neu_resp_error_t * err = data; + neu_json_global_config_req_settings_t *settings = ctx->req->settings; + + if (ctx->idx > 0 && 0 != err->error) { + // received error response + ctx->error = err->error; + ctx->state = STATE_END; + break; + } + + if (ctx->idx < (size_t) settings->n_setting) { + NEXT(ctx, add_setting, &settings->settings[ctx->idx]); + ++ctx->idx; + break; + } + ctx->state = STATE_END; break; } @@ -928,6 +952,27 @@ static int add_subscription(context_t *ctx, neu_json_subscribe_req_t *data) return 0; } +static int add_setting(context_t *ctx, neu_json_node_setting_req_t *data) +{ + neu_plugin_t *plugin = neu_rest_get_plugin(); + + neu_reqresp_head_t header = { + .ctx = ctx->aio, + .type = NEU_REQ_NODE_SETTING, + }; + + neu_req_node_setting_t cmd = { 0 }; + strcpy(cmd.node, data->node); + cmd.setting = data->setting; // ownership moved + data->setting = NULL; + + if (0 != neu_plugin_op(plugin, header, &cmd)) { + return NEU_ERR_IS_BUSY; + } + + return 0; +} + void handle_get_global_config(nng_aio *aio) { NEU_VALIDATE_JWT(aio);