Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions include/neuron/msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ typedef enum neu_reqresp_type {
NEU_REQ_SUBSCRIBE_GROUPS,
NEU_REQ_GET_SUBSCRIBE_GROUP,
NEU_RESP_GET_SUBSCRIBE_GROUP,
NEU_REQ_GET_DRIVER_SUBSCRIBE_GROUP,
NEU_RESP_GET_DRIVER_SUBSCRIBE_GROUP,
NEU_REQ_GET_SUB_DRIVER_TAGS,
NEU_RESP_GET_SUB_DRIVER_TAGS,

Expand Down Expand Up @@ -188,14 +190,17 @@ static const char *neu_reqresp_type_string_t[] = {
[NEU_RESP_WRITE_TAGS] = "NEU_RESP_WRITE_TAGS",
[NEU_REQ_WRITE_GTAGS] = "NEU_REQ_WRITE_GTAGS",

[NEU_REQ_SUBSCRIBE_GROUP] = "NEU_REQ_SUBSCRIBE_GROUP",
[NEU_REQ_UNSUBSCRIBE_GROUP] = "NEU_REQ_UNSUBSCRIBE_GROUP",
[NEU_REQ_UPDATE_SUBSCRIBE_GROUP] = "NEU_REQ_UPDATE_SUBSCRIBE_GROUP",
[NEU_REQ_SUBSCRIBE_GROUPS] = "NEU_REQ_SUBSCRIBE_GROUPS",
[NEU_REQ_GET_SUBSCRIBE_GROUP] = "NEU_REQ_GET_SUBSCRIBE_GROUP",
[NEU_RESP_GET_SUBSCRIBE_GROUP] = "NEU_RESP_GET_SUBSCRIBE_GROUP",
[NEU_REQ_GET_SUB_DRIVER_TAGS] = "NEU_REQ_GET_SUB_DRIVER_TAGS",
[NEU_RESP_GET_SUB_DRIVER_TAGS] = "NEU_RESP_GET_SUB_DRIVER_TAGS",
[NEU_REQ_SUBSCRIBE_GROUP] = "NEU_REQ_SUBSCRIBE_GROUP",
[NEU_REQ_UNSUBSCRIBE_GROUP] = "NEU_REQ_UNSUBSCRIBE_GROUP",
[NEU_REQ_UPDATE_SUBSCRIBE_GROUP] = "NEU_REQ_UPDATE_SUBSCRIBE_GROUP",
[NEU_REQ_SUBSCRIBE_GROUPS] = "NEU_REQ_SUBSCRIBE_GROUPS",
[NEU_REQ_GET_SUBSCRIBE_GROUP] = "NEU_REQ_GET_SUBSCRIBE_GROUP",
[NEU_RESP_GET_SUBSCRIBE_GROUP] = "NEU_RESP_GET_SUBSCRIBE_GROUP",
[NEU_REQ_GET_DRIVER_SUBSCRIBE_GROUP] = "NEU_REQ_GET_DRIVER_SUBSCRIBE_GROUP",
[NEU_RESP_GET_DRIVER_SUBSCRIBE_GROUP] =
"NEU_RESP_GET_DRIVER_SUBSCRIBE_GROUP",
[NEU_REQ_GET_SUB_DRIVER_TAGS] = "NEU_REQ_GET_SUB_DRIVER_TAGS",
[NEU_RESP_GET_SUB_DRIVER_TAGS] = "NEU_RESP_GET_SUB_DRIVER_TAGS",

[NEU_REQ_GET_DATALAYERS_GROUPS] = "NEU_REQ_GET_DATALAYERS_GROUPS",
[NEU_RESP_GET_DATALAYERS_GROUPS] = "NEU_RESP_GET_DATALAYERS_GROUPS",
Expand Down Expand Up @@ -699,6 +704,11 @@ typedef struct neu_req_get_subscribe_group {
char group[NEU_GROUP_NAME_LEN];
} neu_req_get_subscribe_group_t;

typedef struct neu_req_get_driver_subscribe_group {
char app[NEU_NODE_NAME_LEN];
char name[NEU_NODE_NAME_LEN];
} neu_req_get_driver_subscribe_group_t;

typedef struct {
char app[NEU_NODE_NAME_LEN];
char driver[NEU_NODE_NAME_LEN];
Expand All @@ -714,6 +724,13 @@ typedef struct neu_resp_subscribe_info {
char *static_tags;
} neu_resp_subscribe_info_t;

typedef struct neu_resp_driver_subscribe_info {
char driver[NEU_NODE_NAME_LEN];
char app[NEU_NODE_NAME_LEN];
char group[NEU_GROUP_NAME_LEN];
bool subscribed;
} neu_resp_driver_subscribe_info_t;

static inline void neu_resp_subscribe_info_fini(neu_resp_subscribe_info_t *info)
{
free(info->params);
Expand All @@ -737,7 +754,8 @@ typedef struct {
} neu_resp_get_sub_driver_tags_t;

typedef struct neu_resp_get_subscribe_group {
UT_array *groups; // array neu_resp_subscribe_info_t
UT_array *groups; // array neu_resp_subscribe_info_t or
// neu_resp_driver_subscribe_info_t
} neu_resp_get_subscribe_group_t;

typedef struct neu_req_node_setting {
Expand Down
107 changes: 107 additions & 0 deletions plugins/restful/group_config_handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,4 +488,111 @@ void handle_grp_get_subscribe_resp(nng_aio * aio,
free(result);
free(sub_grp_configs.groups);
utarray_free(groups->groups);
}

void handle_grp_get_subscribes(nng_aio *aio)
{
int ret = 0;
neu_plugin_t * plugin = neu_rest_get_plugin();
neu_req_get_driver_subscribe_group_t cmd = { 0 };
neu_reqresp_head_t header = {
.ctx = aio,
.type = NEU_REQ_GET_DRIVER_SUBSCRIBE_GROUP,
.otel_trace_type = NEU_OTEL_TRACE_TYPE_REST_COMM,
};

NEU_VALIDATE_JWT(aio);

// required parameter
ret = neu_http_get_param_str(aio, "app", cmd.app, sizeof(cmd.app));
if (ret <= 0) {
NEU_JSON_RESPONSE_ERROR(NEU_ERR_PARAM_IS_WRONG, {
neu_http_response(aio, error_code.error, result_error);
})
return;
}

// optional parameter
ret = neu_http_get_param_str(aio, "name", cmd.name, sizeof(cmd.name));
if (-1 == ret || (size_t) ret == sizeof(cmd.name)) {
NEU_JSON_RESPONSE_ERROR(NEU_ERR_PARAM_IS_WRONG, {
neu_http_response(aio, error_code.error, result_error);
})
return;
}

ret = neu_plugin_op(plugin, header, &cmd);
if (ret != 0) {
NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, {
neu_http_response(aio, NEU_ERR_IS_BUSY, result_error);
});
}
}

void handle_grp_get_subscribes_resp(nng_aio * aio,
neu_resp_get_subscribe_group_t *drivers)
{
char * result = NULL;
neu_json_get_driver_subscribe_resp_t drivers_resp = { 0 };

utarray_foreach(drivers->groups, neu_resp_driver_subscribe_info_t *, group)
{
// int index = utarray_eltidx(drivers->groups, group);
bool found = false;

for (int k = 0; k < drivers_resp.n_driver; k++) {
if (strcmp(drivers_resp.drivers[k].driver, group->driver) == 0) {
found = true;
drivers_resp.drivers[k].n_group++;
drivers_resp.drivers[k].groups = realloc(
drivers_resp.drivers[k].groups,
drivers_resp.drivers[k].n_group *
sizeof(neu_json_get_driver_subscribe_resp_group_t));
drivers_resp.drivers[k]
.groups[drivers_resp.drivers[k].n_group - 1]
.group = strdup(group->group);
drivers_resp.drivers[k]
.groups[drivers_resp.drivers[k].n_group - 1]
.subscribed = group->subscribed;
break;
}
}

if (!found) {
drivers_resp.n_driver++;
drivers_resp.drivers = realloc(
drivers_resp.drivers,
drivers_resp.n_driver *
sizeof(neu_json_get_driver_subscribe_resp_driver_t));

drivers_resp.drivers[drivers_resp.n_driver - 1].driver =
strdup(group->driver);
drivers_resp.drivers[drivers_resp.n_driver - 1].n_group = 1;
drivers_resp.drivers[drivers_resp.n_driver - 1].groups =
calloc(1, sizeof(neu_json_get_driver_subscribe_resp_group_t));

drivers_resp.drivers[drivers_resp.n_driver - 1].groups[0].group =
strdup(group->group);
drivers_resp.drivers[drivers_resp.n_driver - 1]
.groups[0]
.subscribed = group->subscribed;
}
}

neu_json_encode_by_fn(&drivers_resp,
neu_json_encode_get_driver_subscribe_resp, &result);

neu_http_ok(aio, result);
free(result);

for (int i = 0; i < drivers_resp.n_driver; i++) {
free(drivers_resp.drivers[i].driver);
for (int j = 0; j < drivers_resp.drivers[i].n_group; j++) {
free(drivers_resp.drivers[i].groups[j].group);
}
free(drivers_resp.drivers[i].groups);
}
free(drivers_resp.drivers);

utarray_free(drivers->groups);
}
4 changes: 4 additions & 0 deletions plugins/restful/group_config_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ void handle_grp_get_subscribe(nng_aio *aio);
void handle_grp_get_subscribe_resp(nng_aio * aio,
neu_resp_get_subscribe_group_t *groups);

void handle_grp_get_subscribes(nng_aio *aio);
void handle_grp_get_subscribes_resp(nng_aio * aio,
neu_resp_get_subscribe_group_t *groups);

#endif
6 changes: 6 additions & 0 deletions plugins/restful/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,12 @@ static struct neu_http_handler rest_handlers[] = {
.url = "/api/v2/subscribe",
.value.handler = handle_grp_get_subscribe,
},
{
.method = NEU_HTTP_METHOD_GET,
.type = NEU_HTTP_HANDLER_FUNCTION,
.url = "/api/v2/subscribes",
.value.handler = handle_grp_get_subscribes,
},
{
.method = NEU_HTTP_METHOD_POST,
.type = NEU_HTTP_HANDLER_FUNCTION,
Expand Down
7 changes: 7 additions & 0 deletions plugins/restful/rest.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,13 @@ static int dashb_plugin_request(neu_plugin_t * plugin,
neu_otel_scope_set_status_code2(scope, NEU_OTEL_STATUS_OK, 0);
}
break;
case NEU_RESP_GET_DRIVER_SUBSCRIBE_GROUP:
handle_grp_get_subscribes_resp(header->ctx,
(neu_resp_get_subscribe_group_t *) data);
if (neu_otel_control_is_started() && trace) {
neu_otel_scope_set_status_code2(scope, NEU_OTEL_STATUS_OK, 0);
}
break;
case NEU_RESP_GET_NODE_SETTING:
handle_get_node_setting_resp(header->ctx,
(neu_resp_get_node_setting_t *) data);
Expand Down
1 change: 1 addition & 0 deletions src/adapter/adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ static int adapter_loop(enum neu_event_io_type type, int fd, void *usr_data)
case NEU_RESP_GET_NODE_SETTING:
case NEU_REQ_UPDATE_GROUP:
case NEU_RESP_GET_SUBSCRIBE_GROUP:
case NEU_RESP_GET_DRIVER_SUBSCRIBE_GROUP:
case NEU_RESP_GET_DATALAYERS_GROUPS:
case NEU_RESP_GET_DATALAYERS_TAGS:
case NEU_RESP_GET_DATALAYERS_TAG:
Expand Down
Loading