From c05f1345e76e26eadc798425d820665b9fb8a819 Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Tue, 27 Apr 2021 23:34:31 +0200 Subject: #1043 add function to append client to json tree og_json_client_append() adds a client objet to the json tree. --- src/rest.c | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/rest.c b/src/rest.c index 00f40fb..f78483e 100644 --- a/src/rest.c +++ b/src/rest.c @@ -366,14 +366,39 @@ static int og_json_dump_clients(const char *buffer, size_t size, void *data) return 0; } +static int og_json_client_append(json_t *array, struct og_client *client) +{ + json_t *addr, *state, *object; + + object = json_object(); + if (!object) + return -1; + + addr = json_string(inet_ntoa(client->addr.sin_addr)); + if (!addr) { + json_decref(object); + return -1; + } + json_object_set_new(object, "addr", addr); + state = json_string(og_client_status(client)); + if (!state) { + json_decref(object); + return -1; + } + json_object_set_new(object, "state", state); + json_array_append_new(array, object); + + return 0; +} + static int og_cmd_get_clients(json_t *element, struct og_msg_params *params, char *buffer_reply) { - json_t *root, *array, *addr, *state, *object; struct og_client *client; struct og_buffer og_buffer = { .data = buffer_reply, }; + json_t *array, *root; array = json_array(); if (!array) @@ -383,27 +408,12 @@ static int og_cmd_get_clients(json_t *element, struct og_msg_params *params, if (!client->agent) continue; - object = json_object(); - if (!object) { + if (og_json_client_append(array, client) < 0) { json_decref(array); return -1; } - addr = json_string(inet_ntoa(client->addr.sin_addr)); - if (!addr) { - json_decref(object); - json_decref(array); - return -1; - } - json_object_set_new(object, "addr", addr); - state = json_string(og_client_status(client)); - if (!state) { - json_decref(object); - json_decref(array); - return -1; - } - json_object_set_new(object, "state", state); - json_array_append_new(array, object); } + root = json_pack("{s:o}", "clients", array); if (!root) { json_decref(array); -- cgit v1.2.3-18-g5258