From f520a57f58bc450e3a7c7b3e61bf2dd60a17bf33 Mon Sep 17 00:00:00 2001 From: Roberto Hueso Gómez Date: Wed, 1 Jul 2020 12:15:37 +0200 Subject: #980 Refactor GET /scopes backend This improves style and fixes some memleaks. Co-authored-by: OpenGnSys Support Team --- src/rest.c | 132 +++++++++++++++++++++++++++++++------------------------------ 1 file changed, 68 insertions(+), 64 deletions(-) (limited to 'src') diff --git a/src/rest.c b/src/rest.c index 8c5c6da..606d1c2 100644 --- a/src/rest.c +++ b/src/rest.c @@ -2304,16 +2304,19 @@ static int og_cmd_task_post(json_t *element, struct og_msg_params *params) NULL); } -static int og_dbi_scope_get_center(struct og_dbi *dbi, json_t *array) +static int og_dbi_scope_get_computer(struct og_dbi *dbi, json_t *array, + uint32_t room_id) { - char center_name[OG_DB_CENTER_NAME_MAXLEN + 1] = {}; + char computer_name[OG_DB_COMPUTER_NAME_MAXLEN + 1] = {}; + uint32_t computer_id; const char *msglog; - uint32_t center_id; dbi_result result; - json_t *center; + json_t *computer; result = dbi_conn_queryf(dbi->conn, - "SELECT nombrecentro, idcentro FROM centros"); + "SELECT idordenador, nombreordenador, ip " + "FROM ordenadores WHERE idaula=%d", + room_id); if (!result) { dbi_conn_error(dbi->conn, &msglog); syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", @@ -2322,23 +2325,23 @@ static int og_dbi_scope_get_center(struct og_dbi *dbi, json_t *array) } while (dbi_result_next_row(result)) { - center_id = dbi_result_get_uint(result, "idcentro"); - strncpy(center_name, - dbi_result_get_string(result, "nombrecentro"), + computer_id = dbi_result_get_uint(result, "idordenador"); + strncpy(computer_name, + dbi_result_get_string(result, "nombreordenador"), OG_DB_CENTER_NAME_MAXLEN); - center = json_object(); - if (!center) { + computer = json_object(); + if (!computer) { dbi_result_free(result); return -1; } - json_object_set_new(center, "name", json_string(center_name)); - json_object_set_new(center, "type", json_string("center")); - json_object_set_new(center, "id", json_integer(center_id)); - json_object_set_new(center, "scope", json_array()); - json_array_append(array, center); - json_decref(center); + json_object_set_new(computer, "name", json_string(computer_name)); + json_object_set_new(computer, "type", json_string("computer")); + json_object_set_new(computer, "id", json_integer(computer_id)); + json_object_set_new(computer, "scope", json_array()); + json_array_append(array, computer); + json_decref(computer); } dbi_result_free(result); @@ -2349,10 +2352,10 @@ static int og_dbi_scope_get_room(struct og_dbi *dbi, json_t *array, uint32_t center_id) { char room_name[OG_DB_ROOM_NAME_MAXLEN + 1] = {}; + json_t *room, *room_array; const char *msglog; dbi_result result; uint32_t room_id; - json_t *room; result = dbi_conn_queryf(dbi->conn, "SELECT idaula, nombreaula FROM aulas WHERE " @@ -2383,25 +2386,33 @@ static int og_dbi_scope_get_room(struct og_dbi *dbi, json_t *array, json_object_set_new(room, "scope", json_array()); json_array_append(array, room); json_decref(room); + + room_array = json_object_get(room, "scope"); + if (!room_array) { + dbi_result_free(result); + return -1; + } + + if (og_dbi_scope_get_computer(dbi, room_array, room_id)) { + dbi_result_free(result); + return -1; + } } dbi_result_free(result); return 0; } -static int og_dbi_scope_get_computer(struct og_dbi *dbi, json_t *array, - uint32_t room_id) +static int og_dbi_scope_get(struct og_dbi *dbi, json_t *array) { - char computer_name[OG_DB_COMPUTER_NAME_MAXLEN + 1] = {}; - uint32_t computer_id; + char center_name[OG_DB_CENTER_NAME_MAXLEN + 1] = {}; + json_t *center, *array_room; const char *msglog; + uint32_t center_id; dbi_result result; - json_t *computer; result = dbi_conn_queryf(dbi->conn, - "SELECT idordenador, nombreordenador, ip " - "FROM ordenadores WHERE idaula=%d", - room_id); + "SELECT nombrecentro, idcentro FROM centros"); if (!result) { dbi_conn_error(dbi->conn, &msglog); syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", @@ -2410,24 +2421,37 @@ static int og_dbi_scope_get_computer(struct og_dbi *dbi, json_t *array, } while (dbi_result_next_row(result)) { - computer_id = dbi_result_get_uint(result, "idordenador"); - strncpy(computer_name, - dbi_result_get_string(result, "nombreordenador"), + center_id = dbi_result_get_uint(result, "idcentro"); + strncpy(center_name, + dbi_result_get_string(result, "nombrecentro"), OG_DB_CENTER_NAME_MAXLEN); - computer = json_object(); - if (!computer) { + center = json_object(); + if (!center) { dbi_result_free(result); return -1; } - json_object_set_new(computer, "name", json_string(computer_name)); - json_object_set_new(computer, "type", json_string("computer")); - json_object_set_new(computer, "id", json_integer(computer_id)); - json_object_set_new(computer, "scope", json_array()); - json_array_append(array, computer); - json_decref(computer); + array_room = json_array(); + if (!array_room) { + dbi_result_free(result); + json_decref(center); + return -1; + } + + json_object_set_new(center, "name", json_string(center_name)); + json_object_set_new(center, "type", json_string("center")); + json_object_set_new(center, "id", json_integer(center_id)); + json_object_set_new(center, "scope", array_room); + json_array_append(array, center); + json_decref(center); + + if (og_dbi_scope_get_room(dbi, array_room, center_id)) { + dbi_result_free(result); + return -1; + } } + dbi_result_free(result); return 0; @@ -2436,57 +2460,37 @@ static int og_dbi_scope_get_computer(struct og_dbi *dbi, json_t *array, static int og_cmd_scope_get(json_t *element, struct og_msg_params *params, char *buffer_reply) { - json_t *root, *children_root, *children_center, *children_room, - *center_value, *room_value; - uint32_t center_id, room_id, index1, index2; - struct og_dbi *dbi; - struct og_buffer og_buffer = { .data = buffer_reply }; + json_t *root, *array; + struct og_dbi *dbi; root = json_object(); if (!root) return -1; - children_root = json_array(); - if (!children_root) { + array = json_array(); + if (!array) { json_decref(root); return -1; } - - json_object_set(root, "scope", children_root); + json_object_set_new(root, "scope", array); dbi = og_dbi_open(&dbi_config); if (!dbi) { syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", __func__, __LINE__); + json_decref(root); return -1; } - if (og_dbi_scope_get_center(dbi, children_root)) { + if (og_dbi_scope_get(dbi, array)) { og_dbi_close(dbi); + json_decref(root); return -1; } - json_array_foreach(children_root, index1, center_value) { - center_id = json_integer_value(json_object_get(center_value,"id")); - children_center = json_object_get(center_value, "scope"); - if (og_dbi_scope_get_room(dbi, children_center, center_id)) { - og_dbi_close(dbi); - return -1; - } - - json_array_foreach(children_center, index2, room_value) { - room_id = json_integer_value(json_object_get(room_value, "id")); - children_room = json_object_get(room_value, "scope"); - if (og_dbi_scope_get_computer(dbi, children_room, room_id)) { - og_dbi_close(dbi); - return -1; - } - } - } - og_dbi_close(dbi); json_dump_callback(root, og_json_dump_clients, &og_buffer, 0); -- cgit v1.2.3-18-g5258