daemon: Check number of clients to exit the service itself 64/323564/2 tizen
authorSangchul Lee <sc11.lee@samsung.com>
Wed, 30 Apr 2025 05:45:58 +0000 (14:45 +0900)
committerSangchul Lee <sc11.lee@samsung.com>
Thu, 8 May 2025 05:57:27 +0000 (14:57 +0900)
The current checking interval is 10 seconds.

[Version] 0.3.22

Change-Id: I19322054f5e7c48b93c8e2a85534e5b5ba2a513a
Signed-off-by: Sangchul Lee <sc11.lee@samsung.com>
packaging/espp-service.spec
src/daemon/espp_service.c
src/daemon/espp_service_priv.h
src/daemon/espp_service_socket.c

index d6b8c84a6b649e03e873d3841b7b278193ee1a91..6413036e76c62217becf20e315421c8258087b54 100644 (file)
@@ -5,7 +5,7 @@
 
 Name:       espp-service
 Summary:    ESPP service package which contains client lib. and daemon binary
-Version:    0.3.21
+Version:    0.3.22
 Release:    0
 Group:      Multimedia/Libraries
 License:    Apache-2.0
index a0e8c4abb754f9aa1106b6c66903f2ea989ec3ac..e6b58fefb8e6b7bd38a0aebb878c994a7c794caa 100644 (file)
@@ -21,9 +21,9 @@
 #include <sys/stat.h>
 #ifdef USE_SERVICE_APP
 #include <service_app.h>
-static espp_service_s g_svc = { -1, 0, NULL };
+static espp_service_s g_svc = { NULL, NULL, -1, 0, NULL };
 #else
-static espp_service_s g_svc = { false, NULL, -1, 0, NULL };
+static espp_service_s g_svc = { NULL, false, -1, 0, NULL };
 static struct sigaction g_int_old_action;
 static struct sigaction g_abrt_old_action;
 static struct sigaction g_segv_old_action;
@@ -166,6 +166,46 @@ static void run(espp_service_s *svc)
        LOG_DEBUG_LEAVE();
 }
 #else
+#define NUM_OF_CLIENTS_CHECK_TIMER_SEC    10
+
+static gboolean __timer_cb(gpointer data)
+{
+       espp_service_s *svc = (espp_service_s *)data;
+       guint num_of_clients;
+
+       ASSERT(svc);
+       ASSERT(svc->fd_table);
+
+       if ((num_of_clients = g_hash_table_size(svc->fd_table)) == 0) {
+               LOG_WARNING("num of clients is 0, quit main loop to exit service...");
+               g_main_loop_quit(svc->mainloop);
+               return G_SOURCE_REMOVE;
+       }
+
+       LOG_DEBUG("num of clients[%d]", num_of_clients);
+       return G_SOURCE_CONTINUE;
+}
+
+static gpointer __loop_thread_func(gpointer data)
+{
+       espp_service_s *svc = (espp_service_s *)data;
+
+       ASSERT(svc);
+
+       g_timeout_add_seconds(NUM_OF_CLIENTS_CHECK_TIMER_SEC, __timer_cb, svc);
+
+       LOG_DEBUG("loop[%p] runs", svc->mainloop);
+       g_main_loop_run(svc->mainloop);
+
+       g_main_loop_unref(svc->mainloop);
+       svc->mainloop = NULL;
+
+       service_app_exit();
+       LOG_DEBUG("request to exit service");
+
+       return NULL;
+}
+
 static bool svc_app_create_cb(void *user_data)
 {
        espp_service_s *svc = (espp_service_s *)user_data;
@@ -178,6 +218,9 @@ static bool svc_app_create_cb(void *user_data)
 
        svc->fd_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, espp_service_handle_destroy_cb);
 
+       svc->mainloop = g_main_loop_new(NULL, FALSE);
+       svc->thread = g_thread_new("loop-thread-for-timer", __loop_thread_func, svc);
+
        return true;
 }
 
@@ -191,6 +234,10 @@ static void svc_app_terminate_cb(void *user_data)
        g_hash_table_destroy(svc->fd_table);
        espp_service_deinit_socket(svc);
 
+       g_thread_join(svc->thread);
+       LOG_DEBUG("thread[%p] joined", svc->thread);
+       svc->thread = NULL;
+
        LOG_WARNING("exit");
 }
 
index d633563206374e37e04cda0017dd8170df87c754..a9c88e15a7bdb9fb38f51c27a60b25cb7be6388a 100644 (file)
@@ -52,9 +52,11 @@ do { \
 } while (0) \
 
 typedef struct {
-#ifndef USE_SERVICE_APP
-       bool start_service;
        GMainLoop *mainloop;
+#ifdef USE_SERVICE_APP
+       GThread *thread;
+#else
+       bool start_service;
 #endif
        int fd;
        pthread_t thread_id;
index 7fc03866b25213983496eb2ffd7cc79367a461b0..68218957b191e3e1796282f3b06765d84008d5d9 100644 (file)
@@ -160,8 +160,8 @@ exit:
        g_free(hdata);
 #ifdef USE_SERVICE_APP
        if (num_of_clients == 0) {
-               LOG_ERROR("no more clients, service exits now...");
-               service_app_exit();
+               LOG_ERROR("no more clients, quit main loop to exit service...");
+               g_main_loop_quit(svc->mainloop);
        }
 #endif
        pthread_exit(NULL);