diff --git a/include/json/neu_json_group_config.h b/include/json/neu_json_group_config.h index 5912ebca..897cfc7b 100644 --- a/include/json/neu_json_group_config.h +++ b/include/json/neu_json_group_config.h @@ -16,97 +16,106 @@ * 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_JSON_API_GROUP_CONFIG_H_ -#define _NEU_JSON_API_GROUP_CONFIG_H_ + +/* + * DO NOT EDIT THIS FILE MANUALLY! + * It was automatically generated by `json-autotype`. + */ + +#ifndef _NEU_JSON_API_NEU_GROUP_CONFIG_H_ #ifdef __cplusplus extern "C" { #endif -#include +typedef struct { + char * name; + int64_t node_id; + int64_t interval; +} neu_json_add_group_config_req_t; + +int neu_json_decode_add_group_config_req( + char *buf, neu_json_add_group_config_req_t **result); +void neu_json_decode_add_group_config_req_free( + neu_json_add_group_config_req_t *req); typedef struct { - char *name; - int node_id; -} neu_parse_get_group_config_req_t; + char * name; + int64_t node_id; +} neu_json_del_group_config_req_t; + +int neu_json_decode_del_group_config_req( + char *buf, neu_json_del_group_config_req_t **result); +void neu_json_decode_del_group_config_req_free( + neu_json_del_group_config_req_t *req); typedef struct { - char * name; - uint32_t interval; - int pipe_count; - int tag_count; -} neu_parse_get_group_config_res_row_t; + int64_t node_id; +} neu_json_get_group_config_req_t; + +int neu_json_decode_get_group_config_req( + char *buf, neu_json_get_group_config_req_t **result); +void neu_json_decode_get_group_config_req_free( + neu_json_get_group_config_req_t *req); typedef struct { - int n_config; - neu_parse_get_group_config_res_row_t *rows; -} neu_parse_get_group_config_res_t; - -int neu_parse_decode_get_group_config( - char *buf, neu_parse_get_group_config_req_t **result); -void neu_parse_decode_get_group_config_free( - neu_parse_get_group_config_req_t *req); -int neu_parse_encode_get_group_config(void *json_object, void *param); + char * name; + int64_t pipe_count; + int64_t interval; + int64_t tag_count; +} neu_json_get_group_config_resp_group_config_t; typedef struct { - char * name; - uint32_t node_id; - uint32_t interval; -} neu_parse_add_group_config_req_t; + int n_group_config; + neu_json_get_group_config_resp_group_config_t *group_configs; +} neu_json_get_group_config_resp_t; -int neu_parse_decode_add_group_config( - char *buf, neu_parse_add_group_config_req_t **result); -void neu_parse_decode_add_group_config_free( - neu_parse_add_group_config_req_t *req); +int neu_json_encode_get_group_config_resp(void *json_object, void *param); typedef struct { - char * name; - uint32_t node_id; - uint32_t interval; -} neu_parse_update_group_config_req_t; - -int neu_parse_decode_update_group_config( - char *buf, neu_parse_update_group_config_req_t **result); -void neu_parse_decode_update_group_config_free( - neu_parse_update_group_config_req_t *req); + char * group_config_name; + int64_t node_id; +} neu_json_get_subscribe_resp_group_t; typedef struct { - char * name; - uint32_t node_id; -} neu_parse_del_group_config_req_t; + int n_group; + neu_json_get_subscribe_resp_group_t *groups; +} neu_json_get_subscribe_resp_t; -int neu_parse_decode_del_group_config( - char *buf, neu_parse_del_group_config_req_t **result); -void neu_parse_decode_del_group_config_free( - neu_parse_del_group_config_req_t *req); +int neu_json_encode_get_subscribe_resp(void *json_object, void *param); typedef struct { - uint32_t dst_node_id; - uint32_t src_node_id; - char * name; -} neu_parse_subscribe_req_t, neu_parse_unsubscribe_req_t; + char * name; + int64_t src_node_id; + int64_t dst_node_id; +} neu_json_subscribe_req_t; -int neu_parse_decode_subscribe(char *buf, neu_parse_subscribe_req_t **result); -void neu_parse_decode_subscribe_free(neu_parse_subscribe_req_t *req); - -int neu_parse_decode_unsubscribe(char * buf, - neu_parse_unsubscribe_req_t **result); -void neu_parse_decode_unsubscribe_free(neu_parse_unsubscribe_req_t *req); +int neu_json_decode_subscribe_req(char *buf, neu_json_subscribe_req_t **result); +void neu_json_decode_subscribe_req_free(neu_json_subscribe_req_t *req); typedef struct { - uint32_t node_id; - char * group_config_name; -} neu_parse_subscribe_res_grp_t; + char * name; + int64_t src_node_id; + int64_t dst_node_id; +} neu_json_unsubscribe_req_t; + +int neu_json_decode_unsubscribe_req(char * buf, + neu_json_unsubscribe_req_t **result); +void neu_json_decode_unsubscribe_req_free(neu_json_unsubscribe_req_t *req); typedef struct { - int n_config; - neu_parse_subscribe_res_grp_t *group_configs; -} neu_parse_get_subscribe_res_t; + char * name; + int64_t node_id; + int64_t interval; +} neu_json_update_group_config_req_t; -int neu_parse_encode_get_subscribe(void *json_object, void *param); +int neu_json_decode_update_group_config_req( + char *buf, neu_json_update_group_config_req_t **result); +void neu_json_decode_update_group_config_req_free( + neu_json_update_group_config_req_t *req); #ifdef __cplusplus } #endif -#endif \ No newline at end of file +#endif diff --git a/jsons/neu_json_group_config.json b/jsons/neu_json_group_config.json new file mode 100644 index 00000000..0d253b47 --- /dev/null +++ b/jsons/neu_json_group_config.json @@ -0,0 +1,57 @@ +{ + "add_group_config_req": { + "name": "modbus-tcp-config1", + "node_id": 1, + "interval": 10000 + }, + "del_group_config_req": { + "node_id": 1, + "name": "gconfig1" + }, + "update_group_config_req": { + "name": "modbus-tcp-config1", + "interval": 20000, + "node_id": 4 + }, + "get_group_config_req": { + "node_id": 1 + }, + "get_group_config_resp": { + "group_configs": [ + { + "name": "config_modbus_tcp_sample_2", + "interval": 2000, + "pipe_count": 1, + "tag_count": 0 + }, + { + "name": "gconfig1", + "interval": 10000, + "pipe_count": 0, + "tag_count": 0 + } + ] + }, + "subscribe_req": { + "src_node_id": 4, + "dst_node_id": 5, + "name": "gconfig1" + }, + "unsubscribe_req": { + "src_node_id": 4, + "dst_node_id": 5, + "name": "gconfig1" + }, + "get_subscribe_resp": { + "groups": [ + { + "node_id": 1, + "group_config_name": "g1name" + }, + { + "node_id": 2, + "group_config_name": "g2name" + } + ] + } +} \ No newline at end of file diff --git a/plugins/mqtt/command/command.c b/plugins/mqtt/command/command.c index 2299000a..f32e778d 100644 --- a/plugins/mqtt/command/command.c +++ b/plugins/mqtt/command/command.c @@ -30,38 +30,38 @@ void command_response_handle(mqtt_response_t *response) switch (mqtt->function) { case NEU_MQTT_OP_GET_GROUP_CONFIG: { - neu_parse_get_group_config_req_t *req = NULL; - rc = neu_parse_decode_get_group_config(json_str, &req); + neu_json_get_group_config_req_t *req = NULL; + rc = neu_json_decode_get_group_config_req(json_str, &req); if (0 == rc) { ret_str = command_get_group_configs(plugin, mqtt, req); - neu_parse_decode_get_group_config_free(req); + neu_json_decode_get_group_config_req_free(req); } break; } case NEU_MQTT_OP_ADD_GROUP_CONFIG: { - neu_parse_add_group_config_req_t *req = NULL; - rc = neu_parse_decode_add_group_config(json_str, &req); + neu_json_add_group_config_req_t *req = NULL; + rc = neu_json_decode_add_group_config_req(json_str, &req); if (0 == rc) { ret_str = command_add_group_config(plugin, mqtt, req); - neu_parse_decode_add_group_config_free(req); + neu_json_decode_add_group_config_req_free(req); } break; } case NEU_MQTT_OP_UPDATE_GROUP_CONFIG: { - neu_parse_update_group_config_req_t *req = NULL; - rc = neu_parse_decode_update_group_config(json_str, &req); + neu_json_update_group_config_req_t *req = NULL; + rc = neu_json_decode_update_group_config_req(json_str, &req); if (0 == rc) { ret_str = command_update_group_config(plugin, mqtt, req); - neu_parse_decode_update_group_config_free(req); + neu_json_decode_update_group_config_req_free(req); } break; } case NEU_MQTT_OP_DELETE_GROUP_CONFIG: { - neu_parse_del_group_config_req_t *req = NULL; - rc = neu_parse_decode_del_group_config(json_str, &req); + neu_json_del_group_config_req_t *req = NULL; + rc = neu_json_decode_del_group_config_req(json_str, &req); if (0 == rc) { ret_str = command_delete_group_config(plugin, mqtt, req); - neu_parse_decode_del_group_config_free(req); + neu_json_decode_del_group_config_req_free(req); } break; } diff --git a/plugins/mqtt/command/common.h b/plugins/mqtt/command/common.h index 1a2a7bf9..bfaf47fa 100644 --- a/plugins/mqtt/command/common.h +++ b/plugins/mqtt/command/common.h @@ -30,7 +30,6 @@ extern "C" { #include "json/neu_json_fn.h" #include "json/neu_json_get_tag.h" #include "json/neu_json_group_config.h" -// #include "json/neu_group_config.h" #include "json/neu_json_mqtt.h" #include "json/neu_json_node.h" #include "json/neu_json_rw.h" diff --git a/plugins/mqtt/command/group_config.c b/plugins/mqtt/command/group_config.c index faa8fc5a..7ec61367 100644 --- a/plugins/mqtt/command/group_config.c +++ b/plugins/mqtt/command/group_config.c @@ -22,7 +22,7 @@ #include "group_config.h" static void get_all_group_configs(neu_plugin_t *plugin, const uint32_t node_id, - neu_parse_get_group_config_res_t *res) + neu_json_get_group_config_resp_t *res) { vector_t nodes = neu_system_get_nodes(plugin, NEU_NODE_TYPE_DRIVER); int rc = common_node_id_exist(&nodes, node_id); @@ -37,11 +37,14 @@ static void get_all_group_configs(neu_plugin_t *plugin, const uint32_t node_id, return; } - res->n_config = configs.size; - res->rows = (neu_parse_get_group_config_res_row_t *) malloc( - sizeof(neu_parse_get_group_config_res_row_t) * configs.size); - memset(res->rows, 0, - sizeof(neu_parse_get_group_config_res_row_t) * configs.size); + res->n_group_config = configs.size; + res->group_configs = + (neu_json_get_group_config_resp_group_config_t *) malloc( + sizeof(neu_json_get_group_config_resp_group_config_t) * + configs.size); + memset(res->group_configs, 0, + sizeof(neu_json_get_group_config_resp_group_config_t) * + configs.size); int count = 0; neu_taggrp_config_t *config; @@ -52,18 +55,19 @@ static void get_all_group_configs(neu_plugin_t *plugin, const uint32_t node_id, continue; } - res->rows[count].name = + res->group_configs[count].name = strdup((char *) neu_taggrp_cfg_get_name(config)); - res->rows[count].interval = neu_taggrp_cfg_get_interval(config); + res->group_configs[count].interval = + neu_taggrp_cfg_get_interval(config); vector_t *pipes = neu_taggrp_cfg_get_subpipes(config); if (NULL != pipes) { - res->rows[count].pipe_count = pipes->size; + res->group_configs[count].pipe_count = pipes->size; } vector_t *ids = neu_taggrp_cfg_get_datatag_ids(config); if (NULL != ids) { - res->rows[count].tag_count = ids->size; + res->group_configs[count].tag_count = ids->size; } count++; @@ -72,34 +76,34 @@ static void get_all_group_configs(neu_plugin_t *plugin, const uint32_t node_id, } static void -free_parse_get_group_config_res(neu_parse_get_group_config_res_t *res) +free_parse_get_group_config_res(neu_json_get_group_config_resp_t *res) { if (NULL == res) { return; } - if (NULL == res->rows) { + if (NULL == res->group_configs) { return; } int i; - for (i = 0; i < res->n_config; i++) { - if (NULL == res->rows[i].name) { - free(res->rows[i].name); + for (i = 0; i < res->n_group_config; i++) { + if (NULL == res->group_configs[i].name) { + free(res->group_configs[i].name); } } - free(res->rows); + free(res->group_configs); } char *command_get_group_configs(neu_plugin_t *plugin, neu_parse_mqtt_t *mqtt, - neu_parse_get_group_config_req_t *req) + neu_json_get_group_config_req_t *req) { log_debug("uuid:%s, node id:%d", mqtt->uuid, req->node_id); char * json_str = NULL; - neu_parse_get_group_config_res_t res = { 0 }; + neu_json_get_group_config_resp_t res = { 0 }; get_all_group_configs(plugin, req->node_id, &res); - neu_json_encode_with_mqtt(&res, neu_parse_encode_get_group_config, mqtt, + neu_json_encode_with_mqtt(&res, neu_json_encode_get_group_config_resp, mqtt, neu_parse_encode_mqtt_param, &json_str); free_parse_get_group_config_res(&res); @@ -137,7 +141,7 @@ static int add_group_config(neu_plugin_t *plugin, const char *config_name, } char *command_add_group_config(neu_plugin_t *plugin, neu_parse_mqtt_t *mqtt, - neu_parse_add_group_config_req_t *req) + neu_json_add_group_config_req_t *req) { log_debug("uuid:%s, config:%s, node id:%d, " "read interval:%d", @@ -190,7 +194,7 @@ static int update_group_config(neu_plugin_t *plugin, const char *config_name, } char *command_update_group_config(neu_plugin_t *plugin, neu_parse_mqtt_t *mqtt, - neu_parse_update_group_config_req_t *req) + neu_json_update_group_config_req_t *req) { log_debug("uuid:%s, config:%s, src node id:%d," "read interval:%d", @@ -238,7 +242,7 @@ static int delete_group_config(neu_plugin_t *plugin, const char *config_name, } char *command_delete_group_config(neu_plugin_t *plugin, neu_parse_mqtt_t *mqtt, - neu_parse_del_group_config_req_t *req) + neu_json_del_group_config_req_t *req) { log_debug("uuid:%s, config:%s, src node id:%d", mqtt->uuid, req->name, req->node_id); diff --git a/plugins/mqtt/command/group_config.h b/plugins/mqtt/command/group_config.h index 41aac602..0ce74844 100644 --- a/plugins/mqtt/command/group_config.h +++ b/plugins/mqtt/command/group_config.h @@ -29,13 +29,13 @@ extern "C" { #include "common.h" char *command_get_group_configs(neu_plugin_t *plugin, neu_parse_mqtt_t *mqtt, - neu_parse_get_group_config_req_t *req); + neu_json_get_group_config_req_t *req); char *command_add_group_config(neu_plugin_t *plugin, neu_parse_mqtt_t *mqtt, - neu_parse_add_group_config_req_t *req); + neu_json_add_group_config_req_t *req); char *command_update_group_config(neu_plugin_t *plugin, neu_parse_mqtt_t *mqtt, - neu_parse_update_group_config_req_t *req); + neu_json_update_group_config_req_t *req); char *command_delete_group_config(neu_plugin_t *plugin, neu_parse_mqtt_t *mqtt, - neu_parse_del_group_config_req_t *req); + neu_json_del_group_config_req_t *req); #ifdef __cplusplus } diff --git a/plugins/restful/group_config_handle.c b/plugins/restful/group_config_handle.c index 651b1c90..d452ec0e 100644 --- a/plugins/restful/group_config_handle.c +++ b/plugins/restful/group_config_handle.c @@ -24,7 +24,6 @@ #include "neu_log.h" #include "neu_plugin.h" #include "neu_subscribe.h" -// #include "json/neu_group_config.h" #include "json/neu_json_fn.h" #include "json/neu_json_group_config.h" @@ -38,8 +37,8 @@ void handle_add_group_config(nng_aio *aio) neu_plugin_t *plugin = neu_rest_get_plugin(); REST_PROCESS_HTTP_REQUEST_WITH_ERROR( - aio, neu_parse_add_group_config_req_t, - neu_parse_decode_add_group_config, { + aio, neu_json_add_group_config_req_t, + neu_json_decode_add_group_config_req, { neu_taggrp_config_t *gconfig = neu_taggrp_cfg_new(req->name); neu_taggrp_cfg_set_interval(gconfig, req->interval); error.error = @@ -61,8 +60,8 @@ void handle_del_group_config(nng_aio *aio) neu_plugin_t *plugin = neu_rest_get_plugin(); REST_PROCESS_HTTP_REQUEST_WITH_ERROR( - aio, neu_parse_del_group_config_req_t, - neu_parse_decode_del_group_config, { + aio, neu_json_del_group_config_req_t, + neu_json_decode_del_group_config_req, { neu_taggrp_config_t *config = neu_system_find_group_config(plugin, req->node_id, req->name); neu_datatag_table_t *table = @@ -101,8 +100,8 @@ void handle_update_group_config(nng_aio *aio) neu_plugin_t *plugin = neu_rest_get_plugin(); REST_PROCESS_HTTP_REQUEST_WITH_ERROR( - aio, neu_parse_update_group_config_req_t, - neu_parse_decode_update_group_config, { + aio, neu_json_update_group_config_req_t, + neu_json_decode_update_group_config_req, { neu_taggrp_config_t *config = neu_system_find_group_config(plugin, req->node_id, req->name); @@ -141,37 +140,40 @@ void handle_get_group_config(nng_aio *aio) } neu_node_id_t node_id = (neu_node_id_t) atoi(s_node_id); - neu_parse_get_group_config_res_t gconfig_res = { 0 }; + neu_json_get_group_config_resp_t gconfig_res = { 0 }; int index = 0; vector_t gconfigs = neu_system_get_group_configs(plugin, node_id); - gconfig_res.n_config = gconfigs.size; - gconfig_res.rows = calloc(gconfig_res.n_config, - sizeof(neu_parse_get_group_config_res_row_t)); + gconfig_res.n_group_config = gconfigs.size; + gconfig_res.group_configs = + calloc(gconfig_res.n_group_config, + sizeof(neu_json_get_group_config_resp_group_config_t)); VECTOR_FOR_EACH(&gconfigs, iter) { neu_taggrp_config_t *config = *(neu_taggrp_config_t **) iterator_get(&iter); - gconfig_res.rows[index].name = (char *) neu_taggrp_cfg_get_name(config); - gconfig_res.rows[index].interval = neu_taggrp_cfg_get_interval(config); - gconfig_res.rows[index].tag_count = + gconfig_res.group_configs[index].name = + (char *) neu_taggrp_cfg_get_name(config); + gconfig_res.group_configs[index].interval = + neu_taggrp_cfg_get_interval(config); + gconfig_res.group_configs[index].tag_count = neu_taggrp_cfg_get_datatag_ids(config)->size; - gconfig_res.rows[index].pipe_count = + gconfig_res.group_configs[index].pipe_count = neu_taggrp_cfg_get_subpipes(config)->size; index += 1; } - neu_json_encode_by_fn(&gconfig_res, neu_parse_encode_get_group_config, + neu_json_encode_by_fn(&gconfig_res, neu_json_encode_get_group_config_resp, &result); http_ok(aio, result); vector_uninit(&gconfigs); free(result); - free(gconfig_res.rows); + free(gconfig_res.group_configs); } void handle_grp_subscribe(nng_aio *aio) @@ -179,7 +181,7 @@ void handle_grp_subscribe(nng_aio *aio) neu_plugin_t *plugin = neu_rest_get_plugin(); REST_PROCESS_HTTP_REQUEST( - aio, neu_parse_subscribe_req_t, neu_parse_decode_subscribe, { + aio, neu_json_subscribe_req_t, neu_json_decode_subscribe_req, { neu_taggrp_config_t *config = neu_system_find_group_config( plugin, req->src_node_id, req->name); @@ -198,7 +200,7 @@ void handle_grp_unsubscribe(nng_aio *aio) neu_plugin_t *plugin = neu_rest_get_plugin(); REST_PROCESS_HTTP_REQUEST( - aio, neu_parse_unsubscribe_req_t, neu_parse_decode_unsubscribe, { + aio, neu_json_unsubscribe_req_t, neu_json_decode_unsubscribe_req, { neu_taggrp_config_t *config = neu_system_find_group_config( plugin, req->src_node_id, req->name); @@ -218,7 +220,7 @@ void handle_grp_get_subscribe(nng_aio *aio) char * result = NULL; char * s_node_id = http_get_param(aio, "node_id"); neu_node_id_t node_id = 0; - neu_parse_get_subscribe_res_t sub_grp_configs = { 0 }; + neu_json_get_subscribe_resp_t sub_grp_configs = { 0 }; int index = 0; if (s_node_id == NULL) { @@ -229,27 +231,27 @@ void handle_grp_get_subscribe(nng_aio *aio) node_id = (neu_node_id_t) atoi(s_node_id); vector_t *gconfigs = neu_system_get_sub_group_configs(plugin, node_id); - sub_grp_configs.n_config = gconfigs->size; - sub_grp_configs.group_configs = - calloc(gconfigs->size, sizeof(neu_parse_subscribe_res_grp_t)); + sub_grp_configs.n_group = gconfigs->size; + sub_grp_configs.groups = + calloc(gconfigs->size, sizeof(neu_json_get_subscribe_resp_group_t)); VECTOR_FOR_EACH(gconfigs, iter) { neu_sub_grp_config_t *sgc = (neu_sub_grp_config_t *) iterator_get(&iter); - sub_grp_configs.group_configs[index].node_id = sgc->node_id; - sub_grp_configs.group_configs[index].group_config_name = + sub_grp_configs.groups[index].node_id = sgc->node_id; + sub_grp_configs.groups[index].group_config_name = sgc->group_config_name; index += 1; } - neu_json_encode_by_fn(&sub_grp_configs, neu_parse_encode_get_subscribe, + neu_json_encode_by_fn(&sub_grp_configs, neu_json_encode_get_subscribe_resp, &result); http_ok(aio, result); vector_free(gconfigs); free(result); - free(sub_grp_configs.group_configs); + free(sub_grp_configs.groups); } \ No newline at end of file diff --git a/src/parser/neu_json_group_config.c b/src/parser/neu_json_group_config.c index ddd38009..4bba6828 100644 --- a/src/parser/neu_json_group_config.c +++ b/src/parser/neu_json_group_config.c @@ -17,6 +17,11 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. **/ +/* + * DO NOT EDIT THIS FILE MANUALLY! + * It was automatically generated by `json-autotype`. + */ + #include #include @@ -24,309 +29,348 @@ #include "json/neu_json_group_config.h" -int neu_parse_decode_add_group_config(char * buf, - neu_parse_add_group_config_req_t **result) +int neu_json_decode_add_group_config_req( + char *buf, neu_json_add_group_config_req_t **result) { - neu_parse_add_group_config_req_t *req = - malloc(sizeof(neu_parse_add_group_config_req_t)); - memset(req, 0, sizeof(neu_parse_add_group_config_req_t)); + int ret = 0; - neu_json_elem_t elem[] = { - { - .name = "node_id", - .t = NEU_JSON_INT, - }, - { - .name = "interval", - .t = NEU_JSON_INT, - }, - { - .name = "name", - .t = NEU_JSON_STR, - }, - }; - - int ret = neu_json_decode(buf, NEU_JSON_ELEM_SIZE(elem), elem); - if (ret != 0) { - free(req); - return -1; - } - req->node_id = elem[0].v.val_int; - req->interval = elem[1].v.val_int; - req->name = elem[2].v.val_str; - - *result = req; - return 0; -} - -void neu_parse_decode_add_group_config_free( - neu_parse_add_group_config_req_t *req) -{ - free(req->name); - free(req); -} - -int neu_parse_decode_update_group_config( - char *buf, neu_parse_update_group_config_req_t **result) -{ - neu_parse_update_group_config_req_t *req = - malloc(sizeof(neu_parse_update_group_config_req_t)); - memset(req, 0, sizeof(neu_parse_update_group_config_req_t)); - - neu_json_elem_t elem[] = { { - .name = "name", - .t = NEU_JSON_STR, - }, - { - .name = "node_id", - .t = NEU_JSON_INT, - }, - { - .name = "interval", - .t = NEU_JSON_INT, - } }; - - int ret = neu_json_decode(buf, NEU_JSON_ELEM_SIZE(elem), elem); - if (ret != 0) { - free(req); - return -1; - } - req->name = elem[0].v.val_str; - req->node_id = elem[1].v.val_int; - req->interval = elem[2].v.val_int; - - *result = req; - return 0; -} - -void neu_parse_decode_update_group_config_free( - neu_parse_update_group_config_req_t *req) -{ - free(req->name); - free(req); -} - -int neu_parse_decode_del_group_config(char * buf, - neu_parse_del_group_config_req_t **result) -{ - neu_parse_del_group_config_req_t *req = - malloc(sizeof(neu_parse_del_group_config_req_t)); - memset(req, 0, sizeof(neu_parse_del_group_config_req_t)); - - neu_json_elem_t elem[] = { { - .name = "name", - .t = NEU_JSON_STR, - }, - { - .name = "node_id", - .t = NEU_JSON_INT, - } - - }; - - int ret = neu_json_decode(buf, NEU_JSON_ELEM_SIZE(elem), elem); - if (ret != 0) { - free(req); - return -1; - } - req->name = elem[0].v.val_str; - req->node_id = elem[1].v.val_int; - - *result = req; - return 0; -} - -void neu_parse_decode_del_group_config_free( - neu_parse_del_group_config_req_t *req) -{ - free(req->name); - free(req); -} - -int neu_parse_decode_get_group_config(char * buf, - neu_parse_get_group_config_req_t **result) -{ - neu_parse_get_group_config_req_t *req = - malloc(sizeof(neu_parse_get_group_config_req_t)); - memset(req, 0, sizeof(neu_parse_get_group_config_req_t)); - - neu_json_elem_t elem[] = { - { - .name = "node_id", - .t = NEU_JSON_INT, - }, - { - .name = "name", - .t = NEU_JSON_STR, - }, - }; - - int ret = neu_json_decode(buf, NEU_JSON_ELEM_SIZE(elem), elem); - if (ret != 0) { - free(req); - return -1; - } - - req->node_id = elem[0].v.val_int; - req->name = elem[1].v.val_str; - - *result = req; - return 0; -} - -void neu_parse_decode_get_group_config_free( - neu_parse_get_group_config_req_t *req) -{ - free(req->name); - free(req); -} - -int neu_parse_encode_get_group_config(void *json_object, void *param) -{ - neu_parse_get_group_config_res_t *res = - (neu_parse_get_group_config_res_t *) param; - void *array = NULL; - - for (int i = 0; i < res->n_config; i++) { - neu_json_elem_t group[] = { { - .name = "name", - .t = NEU_JSON_STR, - .v.val_str = res->rows[i].name, + neu_json_add_group_config_req_t *req = + calloc(1, sizeof(neu_json_add_group_config_req_t)); + neu_json_elem_t req_elems[] = { { + .name = "node_id", + .t = NEU_JSON_INT, }, { - .name = "interval", - .t = NEU_JSON_INT, - .v.val_int = res->rows[i].interval, + .name = "name", + .t = NEU_JSON_STR, }, { - .name = "pipe_count", - .t = NEU_JSON_INT, - .v.val_int = res->rows[i].pipe_count, - }, - { - .name = "tag_count", - .t = NEU_JSON_INT, - .v.val_int = res->rows[i].tag_count, + .name = "interval", + .t = NEU_JSON_INT, } }; - array = neu_json_encode_array(array, group, 4); + ret = neu_json_decode(buf, NEU_JSON_ELEM_SIZE(req_elems), req_elems); + if (ret != 0) { + goto decode_fail; } - neu_json_elem_t elems[] = { { + + req->node_id = req_elems[0].v.val_int; + req->name = req_elems[1].v.val_str; + req->interval = req_elems[2].v.val_int; + + *result = req; + return ret; + +decode_fail: + if (req != NULL) { + free(req); + } + return -1; +} + +void neu_json_decode_add_group_config_req_free( + neu_json_add_group_config_req_t *req) +{ + + free(req->name); + + free(req); +} + +int neu_json_decode_del_group_config_req( + char *buf, neu_json_del_group_config_req_t **result) +{ + int ret = 0; + + neu_json_del_group_config_req_t *req = + calloc(1, sizeof(neu_json_del_group_config_req_t)); + neu_json_elem_t req_elems[] = { { + .name = "node_id", + .t = NEU_JSON_INT, + }, + { + .name = "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_id = req_elems[0].v.val_int; + req->name = req_elems[1].v.val_str; + + *result = req; + return ret; + +decode_fail: + if (req != NULL) { + free(req); + } + return -1; +} + +void neu_json_decode_del_group_config_req_free( + neu_json_del_group_config_req_t *req) +{ + + free(req->name); + + free(req); +} + +int neu_json_decode_get_group_config_req( + char *buf, neu_json_get_group_config_req_t **result) +{ + int ret = 0; + + neu_json_get_group_config_req_t *req = + calloc(1, sizeof(neu_json_get_group_config_req_t)); + neu_json_elem_t req_elems[] = { { + .name = "node_id", + .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_id = req_elems[0].v.val_int; + + *result = req; + return ret; + +decode_fail: + if (req != NULL) { + free(req); + } + return -1; +} + +void neu_json_decode_get_group_config_req_free( + neu_json_get_group_config_req_t *req) +{ + + free(req); +} + +int neu_json_encode_get_group_config_resp(void *json_object, void *param) +{ + int ret = 0; + neu_json_get_group_config_resp_t *resp = + (neu_json_get_group_config_resp_t *) param; + + void * group_config_array = NULL; + neu_json_get_group_config_resp_group_config_t *p_group_config = + resp->group_configs; + for (int i = 0; i < resp->n_group_config; i++) { + neu_json_elem_t group_config_elems[] = { + { + .name = "tag_count", + .t = NEU_JSON_INT, + .v.val_int = p_group_config->tag_count, + }, + { + .name = "pipe_count", + .t = NEU_JSON_INT, + .v.val_int = p_group_config->pipe_count, + }, + { + .name = "name", + .t = NEU_JSON_STR, + .v.val_str = p_group_config->name, + }, + { + .name = "interval", + .t = NEU_JSON_INT, + .v.val_int = p_group_config->interval, + } + }; + group_config_array = + neu_json_encode_array(group_config_array, group_config_elems, + NEU_JSON_ELEM_SIZE(group_config_elems)); + p_group_config++; + } + + neu_json_elem_t resp_elems[] = { { .name = "group_configs", .t = NEU_JSON_OBJECT, - .v.val_object = array, + .v.val_object = group_config_array, } }; + ret = neu_json_encode_field(json_object, resp_elems, + NEU_JSON_ELEM_SIZE(resp_elems)); - return neu_json_encode_field(json_object, elems, 1); + return ret; } -int neu_parse_decode_subscribe(char *buf, neu_parse_subscribe_req_t **result) +int neu_json_encode_get_subscribe_resp(void *json_object, void *param) { - neu_parse_subscribe_req_t *req = malloc(sizeof(neu_parse_subscribe_req_t)); - memset(req, 0, sizeof(neu_parse_subscribe_req_t)); + int ret = 0; + neu_json_get_subscribe_resp_t *resp = + (neu_json_get_subscribe_resp_t *) param; - neu_json_elem_t elem[] = { - { - .name = "name", - .t = NEU_JSON_STR, - }, - { - .name = "dst_node_id", - .t = NEU_JSON_INT, - }, - { - .name = "src_node_id", - .t = NEU_JSON_INT, - }, - }; - - int ret = neu_json_decode(buf, NEU_JSON_ELEM_SIZE(elem), elem); - if (ret != 0) { - free(req); - return -1; - } - req->name = elem[0].v.val_str; - req->dst_node_id = elem[1].v.val_int; - req->src_node_id = elem[2].v.val_int; - - *result = req; - return 0; -} - -void neu_parse_decode_subscribe_free(neu_parse_subscribe_req_t *req) -{ - free(req->name); - free(req); -} - -int neu_parse_decode_unsubscribe(char * buf, - neu_parse_unsubscribe_req_t **result) -{ - neu_parse_unsubscribe_req_t *req = - malloc(sizeof(neu_parse_unsubscribe_req_t)); - memset(req, 0, sizeof(neu_parse_unsubscribe_req_t)); - - neu_json_elem_t elem[] = { - { - .name = "name", - .t = NEU_JSON_STR, - }, - { - .name = "dst_node_id", - .t = NEU_JSON_INT, - }, - { - .name = "src_node_id", - .t = NEU_JSON_INT, - }, - }; - - int ret = neu_json_decode(buf, NEU_JSON_ELEM_SIZE(elem), elem); - if (ret != 0) { - free(req); - return -1; - } - req->name = elem[0].v.val_str; - req->dst_node_id = elem[1].v.val_int; - req->src_node_id = elem[2].v.val_int; - - *result = req; - return 0; -} - -void neu_parse_decode_unsubscribe_free(neu_parse_unsubscribe_req_t *req) -{ - free(req->name); - free(req); -} - -int neu_parse_encode_get_subscribe(void *json_object, void *param) -{ - neu_parse_get_subscribe_res_t *res = - (neu_parse_get_subscribe_res_t *) param; - void *array = NULL; - - for (int i = 0; i < res->n_config; i++) { - neu_json_elem_t grp[] = { - { - .name = "group_config_name", - .t = NEU_JSON_STR, - .v.val_str = res->group_configs[i].group_config_name, - }, - { - .name = "node_id", - .t = NEU_JSON_INT, - .v.val_int = res->group_configs[i].node_id, - }, - }; - - array = neu_json_encode_array(array, grp, 2); + void * group_array = NULL; + neu_json_get_subscribe_resp_group_t *p_group = resp->groups; + for (int i = 0; i < resp->n_group; i++) { + neu_json_elem_t group_elems[] = { { + .name = "node_id", + .t = NEU_JSON_INT, + .v.val_int = p_group->node_id, + }, + { + .name = "group_config_name", + .t = NEU_JSON_STR, + .v.val_str = + p_group->group_config_name, + } }; + group_array = neu_json_encode_array(group_array, group_elems, + NEU_JSON_ELEM_SIZE(group_elems)); + p_group++; } - neu_json_elem_t elems[] = { { + neu_json_elem_t resp_elems[] = { { .name = "groups", .t = NEU_JSON_OBJECT, - .v.val_object = array, + .v.val_object = group_array, } }; + ret = neu_json_encode_field(json_object, resp_elems, + NEU_JSON_ELEM_SIZE(resp_elems)); - return neu_json_encode_field(json_object, elems, 1); -} \ No newline at end of file + return ret; +} + +int neu_json_decode_subscribe_req(char *buf, neu_json_subscribe_req_t **result) +{ + int ret = 0; + + neu_json_subscribe_req_t *req = calloc(1, sizeof(neu_json_subscribe_req_t)); + neu_json_elem_t req_elems[] = { { + .name = "src_node_id", + .t = NEU_JSON_INT, + }, + { + .name = "name", + .t = NEU_JSON_STR, + }, + { + .name = "dst_node_id", + .t = NEU_JSON_INT, + } }; + ret = neu_json_decode(buf, NEU_JSON_ELEM_SIZE(req_elems), req_elems); + if (ret != 0) { + goto decode_fail; + } + + req->src_node_id = req_elems[0].v.val_int; + req->name = req_elems[1].v.val_str; + req->dst_node_id = req_elems[2].v.val_int; + + *result = req; + return ret; + +decode_fail: + if (req != NULL) { + free(req); + } + return -1; +} + +void neu_json_decode_subscribe_req_free(neu_json_subscribe_req_t *req) +{ + + free(req->name); + + free(req); +} + +int neu_json_decode_unsubscribe_req(char * buf, + neu_json_unsubscribe_req_t **result) +{ + int ret = 0; + + neu_json_unsubscribe_req_t *req = + calloc(1, sizeof(neu_json_unsubscribe_req_t)); + neu_json_elem_t req_elems[] = { { + .name = "src_node_id", + .t = NEU_JSON_INT, + }, + { + .name = "name", + .t = NEU_JSON_STR, + }, + { + .name = "dst_node_id", + .t = NEU_JSON_INT, + } }; + ret = neu_json_decode(buf, NEU_JSON_ELEM_SIZE(req_elems), req_elems); + if (ret != 0) { + goto decode_fail; + } + + req->src_node_id = req_elems[0].v.val_int; + req->name = req_elems[1].v.val_str; + req->dst_node_id = req_elems[2].v.val_int; + + *result = req; + return ret; + +decode_fail: + if (req != NULL) { + free(req); + } + return -1; +} + +void neu_json_decode_unsubscribe_req_free(neu_json_unsubscribe_req_t *req) +{ + + free(req->name); + + free(req); +} + +int neu_json_decode_update_group_config_req( + char *buf, neu_json_update_group_config_req_t **result) +{ + int ret = 0; + + neu_json_update_group_config_req_t *req = + calloc(1, sizeof(neu_json_update_group_config_req_t)); + neu_json_elem_t req_elems[] = { { + .name = "node_id", + .t = NEU_JSON_INT, + }, + { + .name = "name", + .t = NEU_JSON_STR, + }, + { + .name = "interval", + .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_id = req_elems[0].v.val_int; + req->name = req_elems[1].v.val_str; + req->interval = req_elems[2].v.val_int; + + *result = req; + return ret; + +decode_fail: + if (req != NULL) { + free(req); + } + return -1; +} + +void neu_json_decode_update_group_config_req_free( + neu_json_update_group_config_req_t *req) +{ + + free(req->name); + + free(req); +} diff --git a/src/parser/neu_json_rw.c b/src/parser/neu_json_rw.c index 34eb75c4..62d8a65d 100644 --- a/src/parser/neu_json_rw.c +++ b/src/parser/neu_json_rw.c @@ -160,5 +160,3 @@ void neu_parse_decode_write_free(neu_parse_write_req_t *req) free(req->tags); free(req); } - - diff --git a/tests/json_group_config_test.cc b/tests/json_group_config_test.cc index 0508f3f6..257cd69c 100644 --- a/tests/json_group_config_test.cc +++ b/tests/json_group_config_test.cc @@ -1,54 +1,52 @@ #include #include -// #include "json/neu_group_config.h" #include "json/neu_json_fn.h" #include "json/neu_json_group_config.h" TEST(TagGroupConfigTest, neu_parse_decode_get_group_config_req) { - char *json = (char *) "{\"node_id\": 123456, " - "\"name\": \"Config0\" }"; + char *json = (char *) "{\"node_id\": 123456 }"; - neu_parse_get_group_config_req_t *req = NULL; + neu_json_get_group_config_req_t *req = NULL; - EXPECT_EQ(0, neu_parse_decode_get_group_config(json, &req)); + EXPECT_EQ(0, neu_json_decode_get_group_config_req(json, &req)); EXPECT_EQ(123456, req->node_id); - EXPECT_STREQ("Config0", req->name); - neu_parse_decode_get_group_config_free(req); + neu_json_decode_get_group_config_req_free(req); } TEST(TagGroupConfigTest, neu_parse_encode_get_group_config_res) { char *buf = - (char *) "{\"group_configs\": [{\"name\": \"config0\", \"interval\": " - "0, \"pipe_count\": 0, \"tag_count\": 0}, {\"name\": " - "\"config1\", \"interval\": 0, \"pipe_count\": 0, " - "\"tag_count\": 0}, {\"name\": \"config2\", \"interval\": 0, " - "\"pipe_count\": 0, \"tag_count\": 0}]}"; + (char *) "{\"group_configs\": [{\"tag_count\": 0, \"pipe_count\": 0, " + "\"name\": \"config0\", \"interval\": 0}, {\"tag_count\": 0, " + "\"pipe_count\": 0, \"name\": \"config1\", \"interval\": 0}, " + "{\"tag_count\": 0, \"pipe_count\": 0, \"name\": \"config2\", " + "\"interval\": 0}]}"; char * result = NULL; - neu_parse_get_group_config_res_t res = { - .n_config = 3, + neu_json_get_group_config_resp_t res = { + .n_group_config = 3, }; - res.rows = (neu_parse_get_group_config_res_row_t *) calloc( - 3, sizeof(neu_parse_get_group_config_res_row_t)); + res.group_configs = + (neu_json_get_group_config_resp_group_config_t *) calloc( + 3, sizeof(neu_json_get_group_config_resp_group_config_t)); - res.rows[0].name = strdup((char *) "config0"); - res.rows[1].name = strdup((char *) "config1"); - res.rows[2].name = strdup((char *) "config2"); + res.group_configs[0].name = strdup((char *) "config0"); + res.group_configs[1].name = strdup((char *) "config1"); + res.group_configs[2].name = strdup((char *) "config2"); EXPECT_EQ(0, - neu_json_encode_by_fn(&res, neu_parse_encode_get_group_config, + neu_json_encode_by_fn(&res, neu_json_encode_get_group_config_resp, &result)); EXPECT_STREQ(buf, result); - free(res.rows[0].name); - free(res.rows[1].name); - free(res.rows[2].name); - free(res.rows); + free(res.group_configs[0].name); + free(res.group_configs[1].name); + free(res.group_configs[2].name); + free(res.group_configs); free(result); } @@ -59,15 +57,15 @@ TEST(TagGroupConfigTest, neu_parse_decode_add_group_config_req) "\"interval\": 2000 " " }"; - neu_parse_add_group_config_req_t *req = NULL; + neu_json_add_group_config_req_t *req = NULL; - EXPECT_EQ(0, neu_parse_decode_add_group_config(buf, &req)); + EXPECT_EQ(0, neu_json_decode_add_group_config_req(buf, &req)); EXPECT_STREQ("Config0", req->name); EXPECT_EQ(123456, req->node_id); EXPECT_EQ(2000, req->interval); - neu_parse_decode_add_group_config_free(req); + neu_json_decode_add_group_config_req_free(req); } TEST(TagGroupConfigTest, neu_parse_decode_update_group_config_req) @@ -77,15 +75,15 @@ TEST(TagGroupConfigTest, neu_parse_decode_update_group_config_req) "\"interval\": 2000 " " }"; - neu_parse_update_group_config_req_t *req = NULL; + neu_json_update_group_config_req_t *req = NULL; - EXPECT_EQ(0, neu_parse_decode_update_group_config(buf, &req)); + EXPECT_EQ(0, neu_json_decode_update_group_config_req(buf, &req)); EXPECT_STREQ("Config0", req->name); EXPECT_EQ(123456, req->node_id); EXPECT_EQ(2000, req->interval); - neu_parse_decode_update_group_config_free(req); + neu_json_decode_update_group_config_req_free(req); } TEST(TagGroupConfigTest, neu_parse_decode_delete_group_config_req) @@ -94,12 +92,12 @@ TEST(TagGroupConfigTest, neu_parse_decode_delete_group_config_req) "\"node_id\":123456 " "}"; - neu_parse_del_group_config_req_t *req = NULL; + neu_json_del_group_config_req_t *req = NULL; - EXPECT_EQ(0, neu_parse_decode_del_group_config(buf, &req)); + EXPECT_EQ(0, neu_json_decode_del_group_config_req(buf, &req)); EXPECT_STREQ("Config0", req->name); EXPECT_EQ(123456, req->node_id); - neu_parse_decode_del_group_config_free(req); + neu_json_decode_del_group_config_req_free(req); }