Merge pull request #1090 from eeff/eeff/dev

feat: put global config groups
This commit is contained in:
eeff
2023-03-06 16:09:57 +08:00
committed by GitHub
5 changed files with 161 additions and 2 deletions
+47
View File
@@ -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);
+6 -1
View File
@@ -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);
}
}
+3 -1
View File
@@ -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,
+99
View File
@@ -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;
+6
View File
@@ -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;