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 [new file with mode: 0644]
include/util/request-handler.h [deleted file]
include/util/request.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 81bcff5d3235419c2715c50f45ff82b1dbf2ba6f..4ea606a112d40dd0e9430dbdd160786e77e68e81 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 08e9e21a93c18f1ca23463248ee06ff0703da06e..38b4e6266a3ef5a36f2b0151e3783ac420d65183 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/monitor/request.h b/include/monitor/request.h
new file mode 100644 (file)
index 0000000..f33640b
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * 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_H__
+#define __REQUEST_H__
+
+#define GENERIC_BUFF_MAX               (1024)
+#define HUGE_BUFF_MAX                  (64 * 1024)
+/*
+ * request format
+ * REQUEST_TYPE:REQUEST_CONTENT(if required)
+ */
+
+enum {
+       REQUEST_CREATE_RESOURCE,
+       REQUEST_DELETE_RESOURCE,
+       REQUEST_UPDATE_RESOURCE,
+       REQUEST_UPDATE_RESOURCE_ALL,
+       REQUEST_GET_RESOURCE_COUNT,
+       REQUEST_SET_RESOURCE_CTRL,
+       REQUEST_SET_RESOURCE_ATTR,
+       REQUEST_UNSET_RESOURCE_ATTR,
+       REQUEST_IS_RESOURCE_ATTR_SUPPORTED,
+       REQUEST_GET_RESOURCE_JSON,
+       REQUEST_GET_VALUE_JSON,
+       REQUEST_GET_VALUE_INT,
+       REQUEST_GET_VALUE_INT64,
+       REQUEST_GET_VALUE_UINT,
+       REQUEST_GET_VALUE_UINT64,
+       REQUEST_GET_VALUE_DOUBLE,
+       REQUEST_GET_VALUE_STRING,
+       REQUEST_GET_VALUE_ARRAY,
+       REQUEST_GET_RESOURCE_TS,
+       REQUEST_GET_RESOURCE_LIST_JSON,
+       REQUEST_IS_RESOURCE_ATTR_SET,
+       REQUEST_MAX,
+};
+
+static const char request_name[][128] = {
+       "REQUEST_CREATE_RESOURCE",
+       "REQUEST_DELETE_RESOURCE",
+       "REQUEST_UPDATE_RESOURCE",
+       "REQUEST_UPDATE_RESOURCE_ALL",
+       "REQUEST_GET_RESOURCE_COUNT",
+       "REQUEST_SET_RESOURCE_CTRL",
+       "REQUEST_SET_RESOURCE_ATTR",
+       "REQUEST_UNSET_RESOURCE_ATTR",
+       "REQUEST_IS_RESOURCE_ATTR_SUPPORTED",
+       "REQUEST_GET_RESOURCE_JSON",
+       "REQUEST_GET_VALUE_JSON",
+       "REQUEST_GET_VALUE_INT",
+       "REQUEST_GET_VALUE_INT64",
+       "REQUEST_GET_VALUE_UINT",
+       "REQUEST_GET_VALUE_UINT64",
+       "REQUEST_GET_VALUE_DOUBLE",
+       "REQUEST_GET_VALUE_STRING",
+       "REQUEST_GET_VALUE_ARRAY",
+       "REQUEST_GET_RESOURCE_TS",
+       "REQUEST_GET_RESOURCE_LIST_JSON",
+       "REQUEST_MAX",
+};
+
+#endif /* __REQUEST_H__ */
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__ */
diff --git a/include/util/request.h b/include/util/request.h
deleted file mode 100644 (file)
index f33640b..0000000
+++ /dev/null
@@ -1,77 +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_H__
-#define __REQUEST_H__
-
-#define GENERIC_BUFF_MAX               (1024)
-#define HUGE_BUFF_MAX                  (64 * 1024)
-/*
- * request format
- * REQUEST_TYPE:REQUEST_CONTENT(if required)
- */
-
-enum {
-       REQUEST_CREATE_RESOURCE,
-       REQUEST_DELETE_RESOURCE,
-       REQUEST_UPDATE_RESOURCE,
-       REQUEST_UPDATE_RESOURCE_ALL,
-       REQUEST_GET_RESOURCE_COUNT,
-       REQUEST_SET_RESOURCE_CTRL,
-       REQUEST_SET_RESOURCE_ATTR,
-       REQUEST_UNSET_RESOURCE_ATTR,
-       REQUEST_IS_RESOURCE_ATTR_SUPPORTED,
-       REQUEST_GET_RESOURCE_JSON,
-       REQUEST_GET_VALUE_JSON,
-       REQUEST_GET_VALUE_INT,
-       REQUEST_GET_VALUE_INT64,
-       REQUEST_GET_VALUE_UINT,
-       REQUEST_GET_VALUE_UINT64,
-       REQUEST_GET_VALUE_DOUBLE,
-       REQUEST_GET_VALUE_STRING,
-       REQUEST_GET_VALUE_ARRAY,
-       REQUEST_GET_RESOURCE_TS,
-       REQUEST_GET_RESOURCE_LIST_JSON,
-       REQUEST_IS_RESOURCE_ATTR_SET,
-       REQUEST_MAX,
-};
-
-static const char request_name[][128] = {
-       "REQUEST_CREATE_RESOURCE",
-       "REQUEST_DELETE_RESOURCE",
-       "REQUEST_UPDATE_RESOURCE",
-       "REQUEST_UPDATE_RESOURCE_ALL",
-       "REQUEST_GET_RESOURCE_COUNT",
-       "REQUEST_SET_RESOURCE_CTRL",
-       "REQUEST_SET_RESOURCE_ATTR",
-       "REQUEST_UNSET_RESOURCE_ATTR",
-       "REQUEST_IS_RESOURCE_ATTR_SUPPORTED",
-       "REQUEST_GET_RESOURCE_JSON",
-       "REQUEST_GET_VALUE_JSON",
-       "REQUEST_GET_VALUE_INT",
-       "REQUEST_GET_VALUE_INT64",
-       "REQUEST_GET_VALUE_UINT",
-       "REQUEST_GET_VALUE_UINT64",
-       "REQUEST_GET_VALUE_DOUBLE",
-       "REQUEST_GET_VALUE_STRING",
-       "REQUEST_GET_VALUE_ARRAY",
-       "REQUEST_GET_RESOURCE_TS",
-       "REQUEST_GET_RESOURCE_LIST_JSON",
-       "REQUEST_MAX",
-};
-
-#endif /* __REQUEST_H__ */
index b36006af2ca838ce18e31acf6a6b244b357b078a..d22540e1111c2a179e78a7815f169e8ff1f4379a 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 50f602ceca186e5ad947e38523d5e0e3fefe3ea8..3e1787f0f86d98ee17f392d4b04c90091abc4f03 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 c81bce488820eacfc08f53dd47f824d8740da0c1..068d9508f04e8b5b39e1aec3e24dbf939f6a1bf0 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);