From 88a24f9b006a8fde4606956bf5beb9fb67468a51 Mon Sep 17 00:00:00 2001 From: eeff <33566201+eeff@users.noreply.github.com> Date: Mon, 6 Mar 2023 11:32:31 +0800 Subject: [PATCH 1/2] feat: json decoding for driver group resp --- src/parser/neu_json_global_config.c | 7 +- src/parser/neu_json_global_config.h | 4 +- src/parser/neu_json_group_config.c | 99 +++++++++++++++++++++++++++++ src/parser/neu_json_group_config.h | 6 ++ 4 files changed, 114 insertions(+), 2 deletions(-) diff --git a/src/parser/neu_json_global_config.c b/src/parser/neu_json_global_config.c index 0e87521a..a466b5d3 100644 --- a/src/parser/neu_json_global_config.c +++ b/src/parser/neu_json_global_config.c @@ -25,6 +25,7 @@ #include "json/json.h" #include "neu_json_global_config.h" +#include "parser/neu_json_group_config.h" int neu_json_decode_global_config_req(char * buf, neu_json_global_config_req_t **result) @@ -41,7 +42,10 @@ int neu_json_decode_global_config_req(char * buf, } int ret = 0; - if (0 == neu_json_decode_get_nodes_resp_json(json_obj, &req->nodes)) { + if (0 == neu_json_decode_get_nodes_resp_json(json_obj, &req->nodes) && + 0 == + neu_json_decode_get_driver_group_resp_json(json_obj, + &req->groups)) { *result = req; } else { ret = -1; @@ -56,6 +60,7 @@ void neu_json_decode_global_config_req_free(neu_json_global_config_req_t *req) { if (req) { neu_json_decode_get_nodes_resp_free(req->nodes); + neu_json_decode_get_driver_group_resp_free(req->groups); free(req); } } diff --git a/src/parser/neu_json_global_config.h b/src/parser/neu_json_global_config.h index f8055178..60dad074 100644 --- a/src/parser/neu_json_global_config.h +++ b/src/parser/neu_json_global_config.h @@ -22,6 +22,7 @@ #include "json/json.h" +#include "parser/neu_json_group_config.h" #include "parser/neu_json_node.h" #ifdef __cplusplus @@ -29,7 +30,8 @@ extern "C" { #endif typedef struct { - neu_json_get_nodes_resp_t *nodes; + neu_json_get_nodes_resp_t * nodes; + neu_json_get_driver_group_resp_t *groups; } neu_json_global_config_req_t; int neu_json_decode_global_config_req(char * buf, diff --git a/src/parser/neu_json_group_config.c b/src/parser/neu_json_group_config.c index 817cf25f..d5878a77 100644 --- a/src/parser/neu_json_group_config.c +++ b/src/parser/neu_json_group_config.c @@ -280,6 +280,105 @@ int neu_json_encode_get_driver_group_resp(void *json_object, void *param) return ret; } +int neu_json_decode_get_driver_group_resp( + char *buf, neu_json_get_driver_group_resp_t **result) +{ + json_t *json_obj = neu_json_decode_new(buf); + if (NULL == json_obj) { + return -1; + } + + int ret = neu_json_decode_get_driver_group_resp_json(json_obj, result); + neu_json_decode_free(json_obj); + return ret; +} + +int neu_json_decode_get_driver_group_resp_json( + void *json_obj, neu_json_get_driver_group_resp_t **result) +{ + int ret = 0; + json_t * grp_array = NULL; + neu_json_get_driver_group_resp_t *resp = NULL; + + grp_array = json_object_get(json_obj, "groups"); + if (!json_is_array(grp_array)) { + goto decode_fail; + } + + resp = calloc(1, sizeof(*resp)); + if (resp == NULL) { + goto decode_fail; + } + + resp->n_group = json_array_size(grp_array); + if (resp->n_group <= 0) { + goto decode_fail; + } + + resp->groups = calloc(resp->n_group, sizeof(*resp->groups)); + if (NULL == resp->groups) { + goto decode_fail; + } + + for (int i = 0; i < resp->n_group; i++) { + neu_json_elem_t group_elems[] = { + { + .name = "driver", + .t = NEU_JSON_STR, + }, + { + .name = "group", + .t = NEU_JSON_STR, + }, + { + .name = "interval", + .t = NEU_JSON_INT, + }, + { + .name = "tag_count", + .t = NEU_JSON_INT, + }, + }; + + ret = neu_json_decode_by_json(json_array_get(grp_array, i), + NEU_JSON_ELEM_SIZE(group_elems), + group_elems); + + // set the fields before check for easy clean up on error + resp->groups[i].driver = group_elems[0].v.val_str; + resp->groups[i].group = group_elems[1].v.val_str; + resp->groups[i].interval = group_elems[2].v.val_int; + resp->groups[i].tag_count = group_elems[3].v.val_int; + + if (ret != 0) { + goto decode_fail; + } + } + + *result = resp; + goto decode_exit; + +decode_fail: + neu_json_decode_get_driver_group_resp_free(resp); + ret = -1; + +decode_exit: + return ret; +} + +void neu_json_decode_get_driver_group_resp_free( + neu_json_get_driver_group_resp_t *resp) +{ + if (resp) { + for (int i = 0; i < resp->n_group; ++i) { + free(resp->groups[i].driver); + free(resp->groups[i].group); + } + free(resp->groups); + free(resp); + } +} + int neu_json_encode_get_subscribe_resp(void *object, void *param) { int ret = 0; diff --git a/src/parser/neu_json_group_config.h b/src/parser/neu_json_group_config.h index 55975a5f..26c66c72 100644 --- a/src/parser/neu_json_group_config.h +++ b/src/parser/neu_json_group_config.h @@ -80,6 +80,12 @@ typedef struct { } neu_json_get_driver_group_resp_t; int neu_json_encode_get_driver_group_resp(void *json_object, void *param); +int neu_json_decode_get_driver_group_resp( + char *buf, neu_json_get_driver_group_resp_t **result); +int neu_json_decode_get_driver_group_resp_json( + void *json_obj, neu_json_get_driver_group_resp_t **result); +void neu_json_decode_get_driver_group_resp_free( + neu_json_get_driver_group_resp_t *resp); typedef struct { char *group; From db4f80d66d258b2f881379a79603868683f435c0 Mon Sep 17 00:00:00 2001 From: eeff <33566201+eeff@users.noreply.github.com> Date: Mon, 6 Mar 2023 11:35:50 +0800 Subject: [PATCH 2/2] feat: put global config groups --- plugins/restful/global_config_handle.c | 47 ++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/plugins/restful/global_config_handle.c b/plugins/restful/global_config_handle.c index 61f3dc66..673784de 100644 --- a/plugins/restful/global_config_handle.c +++ b/plugins/restful/global_config_handle.c @@ -49,6 +49,7 @@ typedef enum { STATE_DEL_APP, STATE_DEL_DRIVER, STATE_ADD_NODE, + STATE_ADD_GROUP, STATE_END, } context_state_e; @@ -90,6 +91,8 @@ static int get_setting_resp(context_t * ctx, static int del_node(context_t *ctx, neu_resp_node_info_t *info); static int add_node(context_t *ctx, neu_json_get_nodes_resp_node_t *req); +static int add_group(context_t * ctx, + neu_json_get_driver_group_resp_group_t *data); static inline bool is_static_node(const char *name, const char *plugin) { @@ -334,6 +337,28 @@ static void put_context_next(context_t *ctx, neu_reqresp_type_e type, break; } + ctx->idx = 0; + ctx->state = STATE_ADD_GROUP; + } + // fall through + + case STATE_ADD_GROUP: { + neu_resp_error_t * err = data; + neu_json_get_driver_group_resp_t *groups = ctx->req->groups; + + if (ctx->idx > 0 && 0 != err->error) { + // received error response + ctx->error = err->error; + ctx->state = STATE_END; + break; + } + + if (ctx->idx < (size_t) groups->n_group) { + NEXT(ctx, add_group, &groups->groups[ctx->idx]); + ++ctx->idx; + break; + } + ctx->state = STATE_END; break; } @@ -751,6 +776,28 @@ static int add_node(context_t *ctx, neu_json_get_nodes_resp_node_t *req) return 0; } +static int add_group(context_t * ctx, + neu_json_get_driver_group_resp_group_t *data) +{ + neu_plugin_t *plugin = neu_rest_get_plugin(); + + neu_reqresp_head_t header = { + .type = NEU_REQ_ADD_GROUP, + .ctx = ctx->aio, + }; + + neu_req_add_group_t cmd = { 0 }; + strcpy(cmd.driver, data->driver); + strcpy(cmd.group, data->group); + cmd.interval = data->interval; + + 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);