#1043 add function to append client to json tree

og_json_client_append() adds a client objet to the json tree.
master
OpenGnSys Support Team 2021-04-27 23:34:31 +02:00
parent 8775c06d36
commit c05f1345e7
1 changed files with 28 additions and 18 deletions

View File

@ -366,14 +366,39 @@ static int og_json_dump_clients(const char *buffer, size_t size, void *data)
return 0; 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, static int og_cmd_get_clients(json_t *element, struct og_msg_params *params,
char *buffer_reply) char *buffer_reply)
{ {
json_t *root, *array, *addr, *state, *object;
struct og_client *client; struct og_client *client;
struct og_buffer og_buffer = { struct og_buffer og_buffer = {
.data = buffer_reply, .data = buffer_reply,
}; };
json_t *array, *root;
array = json_array(); array = json_array();
if (!array) if (!array)
@ -383,27 +408,12 @@ static int og_cmd_get_clients(json_t *element, struct og_msg_params *params,
if (!client->agent) if (!client->agent)
continue; continue;
object = json_object(); if (og_json_client_append(array, client) < 0) {
if (!object) {
json_decref(array); json_decref(array);
return -1; 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); root = json_pack("{s:o}", "clients", array);
if (!root) { if (!root) {
json_decref(array); json_decref(array);