monitor: request-server, request-handler-thread: refactoring
authorSung-hun Kim <sfoon.kim@samsung.com>
Tue, 22 Feb 2022 07:27:13 +0000 (16:27 +0900)
committerSung-hun Kim <sfoon.kim@samsung.com>
Tue, 22 Feb 2022 07:27:13 +0000 (16:27 +0900)
Change-Id: I5f410aa11f79aee29adca5e15755e8cb339e9c45
Signed-off-by: Sung-hun Kim <sfoon.kim@samsung.com>
src/monitor/request-handler-thread.c
src/monitor/request-server.c

index a375cf5a84d8b3eb6b9f095da14d1967827ff8b4..98bdaadc765ff54f4e3bede7e89ffdce8387c33a 100644 (file)
@@ -65,6 +65,23 @@ static void release_resource_list(GList **resource_head)
        }
 }
 
+static void finalize_request_client(struct request_client *client)
+{
+       int i;
+
+       if (!client)
+               return;
+
+       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]);
+
+       client->nr_resources = 0;
+       client->state = CLIENT_FINALIZED;
+}
+
 static void handle_request_in_stopped(struct request_client *client, char *buffer)
 {
        char response[10];
@@ -84,9 +101,8 @@ static void handle_request_in_stopped(struct request_client *client, char *buffe
        request_type = atoi(tokens[0]);
        id = atoi(tokens[1]);
 
-       /* expect start, set_attrs, exit calls */
-       if (request_type != REQUEST_START && request_type != REQUEST_SET_ATTRS
-                       && request_type != REQUEST_EXIT) {
+       /* expect set_attrs, exit calls */
+       if (request_type != REQUEST_EXIT) {
                _E("client-%d: state: %d Invalid request type: %s client-%d",
                                client->id, client->state,
                                request_type_str[request_type], client->id);
@@ -108,16 +124,7 @@ static void handle_request_in_stopped(struct request_client *client, char *buffe
                 * Format of REQUEST_EXIT:
                 *  - <REQUEST_TYPE:ID>
                 */
-               if (client->g_request_resource_head) {
-                       _I("client-%d: before release resource list", client->id);
-                       release_resource_list(&client->g_request_resource_head);
-                       _I("client-%d: release resource list", client->id);
-               }
-
-               for (i = 0; i < client->nr_resources; i++)
-                       free(client->res[i]);
-
-               client->state = CLIENT_FINALIZED;
+               finalize_request_client(client);
                break;
        }
 
@@ -196,9 +203,10 @@ static void handle_request_get_value_int(struct request_client *client,
        }
 
        get_resource_attr_integer(resource, attr, &val);
+
        /**
         * Format of response
-        *  - <REQUEST_TYPE:ID>
+        *  - <REQUEST_TYPE:ID:VAL>
         */
        response_len = sprintf(response, "%d:%d:%d", REQUEST_GET_VALUE_INT, client->id, val);
        if (send(client->socket_fd, response, response_len, 0) < 0) {
@@ -217,6 +225,7 @@ static void handle_request_stop(struct request_client *client)
        int response_len;
 
        client->state = CLIENT_STOPPED;
+
        /**
         * Format of response
         *  - <REQUEST_TYPE:ID>
@@ -336,8 +345,11 @@ static int request_handler_func(void *data, void **result)
                                /* finalize client connection */
                                _I("client-%d state(%d) disconnected",
                                                client->id, client->state);
+                               if (client->state != CLIENT_FINALIZED) {
+                                       _E("client-%d is abnormally disconnected", client->id);
+                                       finalize_request_client(client);
+                               }
                                close(client->socket_fd);
-                               client->state = CLIENT_FINALIZED;
                                break;
                        } else {
                                buffer[recv_len] = '\0';
index c13af7db16f471c18ab24c95817b8209bcaab85f..4e56484201db78c637d2e28079c9ae05fe7267f4 100644 (file)
@@ -264,6 +264,27 @@ static void client_list_test()
        _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;
+
+       client = malloc(sizeof(struct request_client));
+       if (!client) {
+               _E("malloc failed for creating request_client");
+               return;
+       }
+
+       client->state = CLIENT_DISCONNECTED;
+       client->socket_fd = socket_fd;
+       client->id = client_id++;
+       client->nr_resources = 0;
+       client->worker_running = 0;
+       client->g_request_resource_head = NULL;
+
+       add_client_to_list(client);
+       _I("adding to list of sockets: client-%d\n", client->id);
+}
+
 static int request_server_func(void *ctx, void **result)
 {
        char buffer[REQUEST_BUFFER_MAX];
@@ -274,7 +295,6 @@ static int request_server_func(void *ctx, void **result)
        int read_len;
        struct sockaddr_in address;
        struct timeval wait;
-       struct request_client *client;
        fd_set read_fds;
        fd_set active_fds;
 
@@ -343,25 +363,16 @@ static int request_server_func(void *ctx, void **result)
                                        ntohs(address.sin_port));
                        FD_SET(new_socket, &active_fds);
 
-                       client = malloc(sizeof(struct request_client));
-                       client->state = CLIENT_DISCONNECTED;
-                       client->socket_fd = new_socket;
-                       client->id = client_id++;
-                       client->nr_resources = 0;
-                       client->worker_running = 0;
-                       client->g_request_resource_head = NULL;
-
-                       add_client_to_list(client);
-
-                       _I("adding to list of sockets: client-%d\n", client->id);
+                       create_request_client(new_socket);
                }
 
                /* 3. receive requests */
                GList *node = g_request_client_head;
                while (node != NULL) {
                        GList *next = node->next;
-                       client = node->data;
+                       struct request_client *client = node->data;
                        int sd = client->socket_fd;
+
                        if (FD_ISSET(sd, &read_fds)) {
                                read_len = read(sd, buffer, REQUEST_BUFFER_MAX);
                                if (read_len == 0) {
@@ -386,13 +397,12 @@ static int request_server_func(void *ctx, void **result)
                        }
 
                        if (client->state == CLIENT_FINALIZED) {
-                               /* TODO remove client */
                                void *dummy;
+
                                client->worker_running = 0;
                                wait_for_completion(client->worker, &dummy);
-                               _I("worker for client-%d is completed", client->id);
                                remove_client_from_list(client);
-                               _I("clean up for finalized client-%d", client->id);
+                               _I("clean up finalized client-%d", client->id);
                                free(client);
                        }
                        node = next;