monitor: Restructure request server 79/279979/3
authorDongwoo Lee <dwoo08.lee@samsung.com>
Thu, 18 Aug 2022 11:09:54 +0000 (20:09 +0900)
committerDongwoo Lee <dwoo08.lee@samsung.com>
Mon, 22 Aug 2022 09:58:47 +0000 (18:58 +0900)
In order to make all initialization about resource monitor handled
in monitor module, convert request-server and request-handler from
the distinguish module to a part of monitor module.

Change-Id: I820147add2b44c40fb0ce1d8e9223c708037adc1
Signed-off-by: Dongwoo Lee <dwoo08.lee@samsung.com>
CMakeLists.txt
include/monitor/monitor.h
include/monitor/request.h [moved from include/util/request.h with 100% similarity]
include/util/request-handler.h [deleted file]
lib/resource-monitor/resource-monitor.c
src/monitor/monitor.c
src/monitor/request-handler.c
src/monitor/request-server.c [deleted file]

index 81bcff5..4ea606a 100644 (file)
@@ -54,7 +54,6 @@ SET(SRCS
        src/monitor/monitor.c
        src/monitor/monitor-thread.c
        src/monitor/monitor-command.c
-       src/monitor/request-server.c
        src/monitor/request-handler.c
 )
 
index 08e9e21..38b4e62 100644 (file)
@@ -52,4 +52,7 @@ void monitor_command_unbind_resource(struct monitor_command *cmd);
 int monitor_command_init(struct monitor_command **cmd);
 void monitor_command_exit(struct monitor_command *cmd);
 
+int request_server_init(void);
+void request_server_exit(void);
+
 #endif
diff --git a/include/util/request-handler.h b/include/util/request-handler.h
deleted file mode 100644 (file)
index 68355ce..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * PASS (Power Aware System Service)
- *
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#ifndef __REQUEST_HANDLER_H__
-#define __REQUEST_HANDLER_H__
-
-#include <glib.h>
-#include <string.h>
-
-#include <util/resource.h>
-
-struct request_client {
-       int socket_fd;
-       int nr_resources;
-       struct thread *worker;
-       GHashTable *resource_table;
-};
-
-int create_request_client(int socket_fd);
-
-#endif /* __REQUEST_HANDLER_H__ */
index b36006a..d22540e 100644 (file)
@@ -21,7 +21,7 @@
 #define _GNU_SOURCE
 #include <errno.h>
 #include <util/log.h>
-#include <util/request.h>
+#include <monitor/request.h>
 #include <util/resource.h>
 
 #include <stdio.h>
index 50f602c..3e1787f 100644 (file)
@@ -34,9 +34,16 @@ static int monitor_setup(void *data, void *user_data)
 {
        int ret;
 
+       ret = request_server_init();
+       if (ret < 0) {
+               _E("failed to initialize request server\n");
+               return ret;
+       }
+
        ret = monitor_thread_init(&g_monitor);
        if (ret < 0) {
                _E("failed to initialize monitor thread\n");
+               request_server_exit();
                return ret;
        }
 
@@ -51,6 +58,7 @@ static void monitor_init(void *data)
 static void monitor_exit(void *data)
 {
        monitor_thread_exit(&g_monitor);
+       request_server_exit();
        unregister_notifier(DEVICE_NOTIFIER_INIT_DONE, monitor_setup, NULL);
 }
 
index c81bce4..068d950 100644 (file)
@@ -17,7 +17,7 @@
  */
 
 /**
- * @file       request-handler-thread.c
+ * @file       request-handler.c
  * @brief      TBD
  * @ingroup    TBD
  */
 #include <util/log.h>
 #include <util/resource.h>
 #include <util/thread.h>
-#include <util/device-notifier.h>
-#include <util/devices.h>
-#include <util/request.h>
-#include <util/request-handler.h>
+#include <monitor/request.h>
+#include <monitor/monitor.h>
 
 #include <string.h>
 #include <arpa/inet.h>
 #include <sys/time.h>
 #include <assert.h>
 
+#define PENDING_MAX 3
+#define REQUEST_SERVER_PORT 10001
+
+struct request_client {
+       int socket_fd;
+       int nr_resources;
+       struct thread *worker;
+       GHashTable *resource_table;
+};
+
+static bool g_request_server_run;
+static struct thread *g_server_thread;
+
 static void update_resource(gpointer key, gpointer value, gpointer user_data)
 {
        struct resource *res = value;
@@ -1003,7 +1014,7 @@ static int request_handler_func(void *data, void **result)
        return THREAD_RETURN_DONE;
 }
 
-int create_request_client(int socket_fd)
+static int create_request_client(int socket_fd)
 {
        struct request_client *client;
 
@@ -1024,3 +1035,98 @@ int create_request_client(int socket_fd)
        return 0;
 }
 
+static int request_server_func(void *ctx, void **result)
+{
+       struct sockaddr_in address;
+       struct timeval wait;
+       int opt = true;
+       int server_socket;
+       int addrlen;
+       int ret;
+       fd_set fds;
+
+       if (!g_request_server_run)
+               return THREAD_RETURN_DONE;
+
+       init_resource_id();
+
+       /* 0. initialize server socket */
+       server_socket = socket(AF_INET, SOCK_STREAM, 0);
+       if (server_socket < 0) {
+               _E("Failed to initialize socket");
+               goto error_out;
+       }
+
+       if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR,
+                       (char *)&opt, sizeof(opt)) < 0) {
+               _E("Failed to setsockopt");
+               goto error_out_close;
+       }
+
+       address.sin_family = AF_INET;
+       address.sin_addr.s_addr = INADDR_ANY;
+       address.sin_port = htons(REQUEST_SERVER_PORT);
+
+       if (bind(server_socket, (struct sockaddr *)&address, sizeof(address)) < 0) {
+               _E("Failed to bind");
+               goto error_out_close;
+       }
+
+       if (listen(server_socket, PENDING_MAX) < 0) {
+               _E("Failed to begin listenning");
+               goto error_out_close;
+       }
+
+       addrlen = sizeof(address);
+
+       while (g_request_server_run) {
+               FD_ZERO(&fds);
+               FD_SET(server_socket, &fds);
+
+               wait.tv_sec = 1;
+               wait.tv_usec = 0;
+
+               ret = select(FD_SETSIZE, &fds, NULL, NULL, &wait);
+               if (ret < 0) {
+                       _E("failed to select");
+                       goto error_out_close;
+               }
+
+               if (FD_ISSET(server_socket, &fds)) {
+                       int new_socket = accept(server_socket, (struct sockaddr *)&address,
+                                       (socklen_t *)&addrlen);
+
+                       if (new_socket < 0) {
+                               _E("Failed to accept");
+                               goto error_out_close;
+                       }
+
+                       create_request_client(new_socket);
+               }
+       }
+
+       close(server_socket);
+
+       return THREAD_RETURN_DONE;
+
+error_out_close:
+       close(server_socket);
+error_out:
+
+       return THREAD_RETURN_ERROR;
+}
+
+int request_server_init(void)
+{
+       g_request_server_run = true;
+       if (create_daemon_thread(&g_server_thread, request_server_func, NULL))
+               _E("Failed to create request_server_thread ");
+
+       return 0;
+}
+
+void request_server_exit(void)
+{
+       g_request_server_run = false;
+       destroy_thread(g_server_thread);
+}
diff --git a/src/monitor/request-server.c b/src/monitor/request-server.c
deleted file mode 100644 (file)
index 8a221c5..0000000
+++ /dev/null
@@ -1,174 +0,0 @@
-/*
- * PASS (Power Aware System Service)
- *
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/**
- * @file       request-server.c
- * @brief      TBD
- * @ingroup    TBD
- */
-
-#include <glib.h>
-
-#include <util/common.h>
-#include <util/log.h>
-#include <util/resource.h>
-#include <util/thread.h>
-#include <util/device-notifier.h>
-#include <util/devices.h>
-#include <util/request.h>
-#include <util/request-handler.h>
-
-#include <string.h>
-#include <arpa/inet.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <sys/time.h>
-#include <assert.h>
-
-#define PENDING_MAX 3
-#define REQUEST_SERVER_PORT 10001
-
-static bool g_request_server_run;
-
-static int request_server_func(void *ctx, void **result)
-{
-       struct sockaddr_in address;
-       struct timeval wait;
-       int opt = true;
-       int server_socket;
-       int addrlen;
-       int ret;
-       fd_set fds;
-
-       if (!g_request_server_run)
-               return THREAD_RETURN_DONE;
-
-       init_resource_id();
-
-       /* 0. initialize server socket */
-       server_socket = socket(AF_INET, SOCK_STREAM, 0);
-       if (server_socket < 0) {
-               _E("Failed to initialize socket");
-               goto error_out;
-       }
-
-       if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR,
-                       (char *)&opt, sizeof(opt)) < 0) {
-               _E("Failed to setsockopt");
-               goto error_out_close;
-       }
-
-       address.sin_family = AF_INET;
-       address.sin_addr.s_addr = INADDR_ANY;
-       address.sin_port = htons(REQUEST_SERVER_PORT);
-
-       if (bind(server_socket, (struct sockaddr *)&address, sizeof(address)) < 0) {
-               _E("Failed to bind");
-               goto error_out_close;
-       }
-
-       if (listen(server_socket, PENDING_MAX) < 0) {
-               _E("Failed to begin listenning");
-               goto error_out_close;
-       }
-
-       addrlen = sizeof(address);
-
-       while (g_request_server_run) {
-               FD_ZERO(&fds);
-               FD_SET(server_socket, &fds);
-
-               wait.tv_sec = 1;
-               wait.tv_usec = 0;
-
-               ret = select(FD_SETSIZE, &fds, NULL, NULL, &wait);
-               if (ret < 0) {
-                       _E("failed to select");
-                       goto error_out_close;
-               }
-
-               if (FD_ISSET(server_socket, &fds)) {
-                       int new_socket = accept(server_socket, (struct sockaddr *)&address,
-                                       (socklen_t *)&addrlen);
-
-                       if (new_socket < 0) {
-                               _E("Failed to accept");
-                               goto error_out_close;
-                       }
-
-                       create_request_client(new_socket);
-               }
-       }
-
-       close(server_socket);
-
-       return THREAD_RETURN_DONE;
-
-error_out_close:
-       close(server_socket);
-error_out:
-
-       return THREAD_RETURN_ERROR;
-}
-
-static struct thread *server_thread;
-
-static int request_server_init_done(void *data, void *user_data)
-{
-       g_request_server_run = true;
-       if (create_daemon_thread(&server_thread, request_server_func, NULL))
-               _E("Failed to create request_server_thread ");
-
-       return 0;
-}
-
-static void request_server_init(void *data)
-{
-       /* nothing to do */
-}
-
-static void request_server_exit(void *data)
-{
-       unregister_notifier(DEVICE_NOTIFIER_INIT_DONE, request_server_init_done, NULL);
-       g_request_server_run = false;
-       destroy_thread(server_thread);
-}
-
-static int request_server_probe(void *data)
-{
-       int ret = 0;
-
-       ret = register_notifier(DEVICE_NOTIFIER_INIT_DONE,
-                       request_server_init_done, NULL);
-       if (ret < 0) {
-               _E("Failed to register a callback function");
-               return ret;
-       }
-
-       return 0;
-}
-
-static const struct device_ops request_server_device_ops = {
-       .name = "request-server",
-       .probe = request_server_probe,
-       .init = request_server_init,
-       .exit = request_server_exit,
-};
-
-DEVICE_OPS_REGISTER(&request_server_device_ops);