monitor: request-server, request-handler-thread: refactoring code sandbox/sebuns2/request_server
authorSung-hun Kim <sfoon.kim@samsung.com>
Wed, 23 Feb 2022 09:47:16 +0000 (18:47 +0900)
committerSung-hun Kim <sfoon.kim@samsung.com>
Wed, 23 Feb 2022 09:47:16 +0000 (18:47 +0900)
Change-Id: I1972fcc0a95f69abc8804257f91f68dcaeb2114f
Signed-off-by: Sung-hun Kim <sfoon.kim@samsung.com>
src/monitor/request-handler-thread.c
src/monitor/request-server.c

index 98bdaadc765ff54f4e3bede7e89ffdce8387c33a..734eab2984cab03191607ef7be0c371251fad7d5 100644 (file)
@@ -75,8 +75,10 @@ static void finalize_request_client(struct request_client *client)
        if (client->g_request_resource_head)
                release_resource_list(&client->g_request_resource_head);
 
-       for (i = 0; i < client->nr_resources; i++)
-               free(client->res[i]);
+       for (i = 0; i < client->nr_resources; i++) {
+               if (client->res[i])
+                       free(client->res[i]);
+       }
 
        client->nr_resources = 0;
        client->state = CLIENT_FINALIZED;
@@ -89,7 +91,6 @@ static void handle_request_in_stopped(struct request_client *client, char *buffe
        int nr_tokens;
        char *tokens[REQUEST_TOKEN_MAX];
        int request_type;
-       int i;
        int id;
 
        nr_tokens = buffer_tokenize(buffer, tokens);
@@ -139,7 +140,6 @@ static void handle_request_in_stopped(struct request_client *client, char *buffe
        }
 
        _I("send response \"%s\" to client-%d", response, client->id);
-
 }
 
 static struct resource *find_resource_by_type(struct request_client *client, int resource_type)
@@ -215,12 +215,10 @@ static void handle_request_get_value_int(struct request_client *client,
        }
 
        _I("send response \"%s\" to client-%d", response, client->id);
-
 }
 
 static void handle_request_stop(struct request_client *client)
 {
-       int i;
        char response[10];
        int response_len;
 
@@ -316,11 +314,8 @@ static void handle_request_in_thread(struct request_client *client, char *buffer
 
 static int request_handler_func(void *data, void **result)
 {
-       int recv_len;
-       fd_set read_fds;
        fd_set active_fds;
        char buffer[REQUEST_BUFFER_MAX];
-       struct timeval wait;
        struct request_client *client = (struct request_client *)data;
 
        FD_ZERO(&active_fds);
@@ -329,7 +324,11 @@ static int request_handler_func(void *data, void **result)
        _I("start worker thread for client-%d", client->id);
 
        while (client->worker_running) {
+               struct timeval wait;
+               fd_set read_fds;
+
                read_fds = active_fds;
+               /* below numbers are randomly picked */
                wait.tv_sec = 1;
                wait.tv_usec = 0;
 
@@ -340,7 +339,8 @@ static int request_handler_func(void *data, void **result)
 
                if (FD_ISSET(client->socket_fd, &read_fds)) {
                        /* client submits request */
-                       recv_len = recv(client->socket_fd, buffer, REQUEST_BUFFER_MAX, 0);
+                       int recv_len = recv(client->socket_fd, buffer, REQUEST_BUFFER_MAX, 0);
+
                        if (recv_len == 0) {
                                /* finalize client connection */
                                _I("client-%d state(%d) disconnected",
@@ -362,6 +362,9 @@ static int request_handler_func(void *data, void **result)
        return THREAD_RETURN_DONE;
 
 error_out:
+       if (client->state != CLIENT_FINALIZED) {
+               _E();
+       }
        client->state = CLIENT_FINALIZED;
        return THREAD_RETURN_ERROR;
 }
@@ -372,7 +375,6 @@ void request_handler_setup(struct request_client *client)
        GList *node;
        struct request_resource *resource;
 
-       /* */
        for (i = 0; i < RESOURCE_MAX; i++)
                client->res[i] = NULL;
 
index 4e56484201db78c637d2e28079c9ae05fe7267f4..2412ce45959fa217a216bd866b0511e951ed540a 100644 (file)
@@ -98,7 +98,7 @@ static void handle_request_in_connected(struct request_client *client, char *buf
        request_type = atoi(tokens[0]);
        id = atoi(tokens[1]);
 
-       /* expect start, set_attrs, exit calls */
+       /* expect start, set_attrs calls */
        if (request_type != REQUEST_START && request_type != REQUEST_SET_ATTRS) {
                _E("client-%d: state: %d Invalid request type: %s client-%d",
                                client->id, client->state,
@@ -121,9 +121,11 @@ static void handle_request_in_connected(struct request_client *client, char *buf
                 * Format of REQUEST_START:
                 *  - <REQUEST_TYPE:ID>
                 */
-               /* fork thread */
+               /* fork request-handler thread */
                client->state = CLIENT_RUNNING;
                request_handler_setup(client);
+
+               /* request-server no more recieve the request from this socket */
                FD_CLR(client->socket_fd, fds);
                break;
 
@@ -136,7 +138,8 @@ static void handle_request_in_connected(struct request_client *client, char *buf
                resource_type = atoi(tokens[2]);
                attr_mask = (u_int64_t) strtoul(tokens[3], NULL, 16);
                add_resource_to_client(client, resource_type, attr_mask);
-               _I("client-%d: resource %d - attr_mask %lx", client->id, resource_type, attr_mask);
+               _I("client-%d: resource %d - attr_mask %lx",
+                               client->id, resource_type, attr_mask);
                break;
        }
 
@@ -226,44 +229,6 @@ static void remove_client_from_list(struct request_client *client)
                        g_list_remove(g_request_client_head, (gpointer)client);
 }
 
-static void client_list_test()
-{
-       int i;
-       GList *node;
-       struct request_client *clients[100];
-       int sequence[100] = {30, 4, 92, 19, 45, 63, 21, 98, 72, 56, 52, 44, 73, 41,
-               60, 29, 7, 15, 86, 8, 75, 0, 17, 68, 66, 10, 47, 49, 91, 18, 89, 20, 40,
-               32, 36, 83, 59, 87, 71, 76, 13, 25, 64, 84, 24, 55, 43, 50, 79, 23, 11,
-               61, 9, 34, 5, 96, 39, 46, 80, 42, 38, 53, 90, 94, 74, 51, 82, 48, 1, 77,
-               88, 95, 26, 70, 81, 85, 35, 16, 22, 57, 14, 37, 3, 54, 58, 12, 67, 97, 93,
-               27, 99, 62, 78, 28, 6, 31, 33, 2, 65, 69};
-
-       for (i = 0; i < 100; i++) {
-               clients[i] = malloc(sizeof(struct request_client));
-               add_client_to_list(clients[i]);
-       }
-       node = g_request_client_head;
-       while (node != NULL) {
-               struct request_client *client = node->data;
-               GList *next = node->next;
-               remove_client_from_list(client);
-               free(client);
-               node = next;
-       }
-       _I("after sequential test, nr_nodes: %d", g_list_length(g_request_client_head));
-
-       for (i = 0; i < 100; i++) {
-               clients[i] = malloc(sizeof(struct request_client));
-               add_client_to_list(clients[i]);
-       }
-
-       for (i = 0; i < 100; i++) {
-               remove_client_from_list(clients[sequence[i]]);
-               free(clients[sequence[i]]);
-       }
-       _I("after random test, nr_nodes: %d", g_list_length(g_request_client_head));
-}
-
 static void create_request_client(int socket_fd)
 {
        struct request_client *client;
@@ -291,11 +256,7 @@ static int request_server_func(void *ctx, void **result)
        int opt = true;
        int server_socket;
        int addrlen;
-       int new_socket;
-       int read_len;
        struct sockaddr_in address;
-       struct timeval wait;
-       fd_set read_fds;
        fd_set active_fds;
 
        if (!request_server_run)
@@ -335,13 +296,13 @@ static int request_server_func(void *ctx, void **result)
 
        _I("start server");
 
-       client_list_test();
-
-       _I("after client list test");
-
        while (request_server_run) {
+               struct timeval wait;
+               fd_set read_fds;
+
                /* 1. (re)initialize client sockets */
                read_fds = active_fds;
+               /* below numberss are randomly picked */
                wait.tv_sec = 1;
                wait.tv_usec = 0;
 
@@ -352,8 +313,9 @@ static int request_server_func(void *ctx, void **result)
 
                /* 2. accept a new socket connection */
                if (FD_ISSET(server_socket, &read_fds)) {
-                       new_socket = accept(server_socket, (struct sockaddr *)&address,
+                       int new_socket = accept(server_socket, (struct sockaddr *)&address,
                                        (socklen_t*)&addrlen);
+
                        if (new_socket < 0) {
                                _E("accept error");
                                goto error_out;
@@ -374,7 +336,8 @@ static int request_server_func(void *ctx, void **result)
                        int sd = client->socket_fd;
 
                        if (FD_ISSET(sd, &read_fds)) {
-                               read_len = read(sd, buffer, REQUEST_BUFFER_MAX);
+                               int read_len = read(sd, buffer, REQUEST_BUFFER_MAX);
+
                                if (read_len == 0) {
                                        /* finalize client connection */
                                        getpeername(sd, (struct sockaddr *)&address,