Use tts_ipc function as same way regardless of ipc methods. 30/256630/1
authorSuyeon Hwang <stom.hwang@samsung.com>
Thu, 8 Apr 2021 07:23:25 +0000 (16:23 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Thu, 8 Apr 2021 07:39:20 +0000 (16:39 +0900)
tts_ipc layer is interface layer.
Thus, developer should not have no interest below the layer.

However, in current code, tts_ipc function is used as different way by the ipc method.
Calling interface as same way is better to read, and if there is difference, the difference should
be covered on implementation layer.

This patch unify the usages of functions in tts_ipc. To unify the usage, this patch also fix the
implementation of each method dbus and tidl.

Change-Id: I557fed82f11cace93f52eb13a9ba37bb9287a38d
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
client/tts.c
client/tts_dbus.c
client/tts_dbus.h
client/tts_ipc.c
client/tts_tidl.c

index e65d0cd..2bb25d8 100644 (file)
@@ -280,18 +280,14 @@ int tts_create(tts_h* tts)
                int pid = getpid();
                char* appid = NULL;
                int ret = app_manager_get_app_id(pid, &appid);
-               if (0 != ret) {
-                       SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get appid, ret(%d), pid(%d), appid(%s)", ret, pid, appid);
+               if (0 != ret || NULL == appid) {
+                       SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get appid, ret(%d), pid(%d)", ret, pid);
                        tts_ipc_set_method(TTS_IPC_METHOD_DBUS);
                } else {
-                       if(NULL == appid ) {
-                               SLOG(LOG_INFO, TAG_TTSC, "[INFO] tts_create : client appid(null) => This is daemon process");
-                               tts_ipc_set_method(TTS_IPC_METHOD_DBUS);
-                       } else {
-                               SLOG(LOG_INFO, TAG_TTSC, "[INFO] ]tts_create : client appid(%s)", appid);
-                               tts_ipc_set_method(TTS_IPC_METHOD_TIDL);
-                       }
+                       SLOG(LOG_INFO, TAG_TTSC, "[INFO] tts_create : client appid(%s), pid(%d)", appid, pid);
+                       tts_ipc_set_method(TTS_IPC_METHOD_TIDL);
                }
+
                if (NULL != appid) {
                        free(appid);
                        appid = NULL;
@@ -410,16 +406,6 @@ int tts_destroy(tts_h tts)
                        }
                }
 
-               if (tts->connected) {
-                       if (1 == tts_client_get_size()) {
-                               SLOG(LOG_ERROR, TAG_TTSC, "[INFO] all clients are destroied");
-                               if (0 != tts_ipc_close_connection(client->uid)) {
-                                       SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to close connection");
-                                       return TTS_ERROR_OPERATION_FAILED;
-                               }
-                       }
-               }
-
                tts_client_set_current_state(client, TTS_STATE_CREATED);
 
        case TTS_STATE_CREATED:
@@ -439,6 +425,11 @@ int tts_destroy(tts_h tts)
                        thread_count = ecore_thread_active_get();
                }
 
+               if (0 != tts_ipc_close_connection(client->uid)) {
+                       SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to close connection");
+                       return TTS_ERROR_OPERATION_FAILED;
+               }
+
                /* Free resources */
                tts_client_destroy(tts);
 
@@ -449,17 +440,12 @@ int tts_destroy(tts_h tts)
        }
 
        int num_of_client = tts_client_get_size();
-       if (0 == num_of_client) {
-               SLOG(LOG_ERROR, TAG_TTSC, "[INFO] all clients are destroied");
-               if (0 != tts_ipc_close_connection(client->uid)) {
-                       SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to close connection");
-               }
+       SLOG(LOG_ERROR, TAG_TTSC, "[INFO] num_of_client(%d)", num_of_client);
 
+       if (0 == num_of_client) {
                if (0 != tts_core_deinitialize()) {
                        SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to deinitialize core");
                }
-       } else {
-               SLOG(LOG_ERROR, TAG_TTSC, "[INFO] num_of_client(%d)", num_of_client);
        }
 
        if (NULL != g_language) {
index b54cded..01b1ff3 100644 (file)
@@ -32,6 +32,7 @@ static DBusConnection* g_conn_listener = NULL;
 
 static Ecore_Fd_Handler* g_dbus_fd_handler = NULL;
 
+static volatile int g_connected_client = 0;
 
 static int __tts_cb_error(int uid, tts_error_e reason, int utt_id, char* err_msg)
 {
@@ -379,16 +380,10 @@ static void __tts_dbus_connection_free()
        }
 }
 
-int tts_dbus_open_connection()
+static int __dbus_open_connection()
 {
-       if (NULL != g_conn_sender && NULL != g_conn_listener) {
-               SLOG(LOG_WARN, TAG_TTSC, "already existed connection ");
-               return 0;
-       }
-
-       DBusError err;
-
        /* initialize the error value */
+       DBusError err;
        dbus_error_init(&err);
 
        /* connect to the DBUS system bus, and check for errors */
@@ -452,14 +447,28 @@ int tts_dbus_open_connection()
                return TTS_ERROR_OPERATION_FAILED;
        }
 
-       return 0;
+       return TTS_ERROR_NONE;
 }
 
-int tts_dbus_close_connection()
+int tts_dbus_open_connection(int uid)
 {
-       DBusError err;
-       dbus_error_init(&err);
+       if (NULL != g_conn_sender && NULL != g_conn_listener) {
+               SLOG(LOG_WARN, TAG_TTSC, "already existed connection ");
+               g_connected_client++;
+
+               return TTS_ERROR_NONE;
+       }
+
+       int ret = __dbus_open_connection();
+       if (TTS_ERROR_NONE == ret) {
+               g_connected_client++;
+       }
+
+       return ret;
+}
 
+static int __dbus_close_connection()
+{
        if (NULL != g_dbus_fd_handler) {
                ecore_main_fd_handler_del(g_dbus_fd_handler);
                g_dbus_fd_handler = NULL;
@@ -467,7 +476,18 @@ int tts_dbus_close_connection()
 
        __tts_dbus_connection_free();
 
-       return 0;
+       return TTS_ERROR_NONE;
+}
+
+int tts_dbus_close_connection(int uid)
+{
+       g_connected_client--;
+
+       if (0 == g_connected_client) {
+               return __dbus_close_connection();
+       }
+
+       return TTS_ERROR_NONE;
 }
 
 int tts_dbus_stop_listening(int uid)
@@ -484,9 +504,9 @@ int tts_dbus_stop_listening(int uid)
 int tts_dbus_reconnect()
 {
        if (!g_conn_sender || !g_conn_listener) {
-               tts_dbus_close_connection();
+               __dbus_close_connection();
 
-               if (0 != tts_dbus_open_connection()) {
+               if (0 != __dbus_open_connection()) {
                        SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to reconnect");
                        return -1;
                }
@@ -501,9 +521,9 @@ int tts_dbus_reconnect()
                 sender_connected ? "Connected" : "Not connected", listener_connected ? "Connected" : "Not connected");
 
        if (false == sender_connected || false == listener_connected) {
-               tts_dbus_close_connection();
+               __dbus_close_connection();
 
-               if (0 != tts_dbus_open_connection()) {
+               if (0 != __dbus_open_connection()) {
                        SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to reconnect");
                        return -1;
                }
index 371a866..6842fc3 100644 (file)
@@ -21,9 +21,9 @@
 extern "C" {
 #endif
 
-int tts_dbus_open_connection();
+int tts_dbus_open_connection(int uid);
 
-int tts_dbus_close_connection();
+int tts_dbus_close_connection(int uid);
 
 int tts_dbus_stop_listening(int uid);
 
index 1d416bc..297495e 100644 (file)
@@ -55,8 +55,22 @@ int tts_ipc_set_method(tts_ipc_method_e method) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] method is not valid");
                return TTS_ERROR_INVALID_PARAMETER;
        }
+
        g_ipc_method = method;
 
+       /* choose ipc */
+       switch (g_ipc_method) {
+       case TTS_IPC_METHOD_DBUS:
+               g_vtable = ttsc_dbus_vtable;
+               break;
+       case TTS_IPC_METHOD_TIDL:
+               g_vtable = ttsc_tidl_vtable;
+               break;
+       default:
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] IPC method is not valid");
+               g_vtable = NULL;
+       }
+
        return TTS_ERROR_NONE;
 }
 
@@ -89,14 +103,16 @@ int tts_ipc_open_connection(int uid)
        switch (g_ipc_method) {
        case TTS_IPC_METHOD_DBUS:
                g_vtable = ttsc_dbus_vtable;
-               return g_vtable[OPEN_CONNECTION]();
+               break;
        case TTS_IPC_METHOD_TIDL:
                g_vtable = ttsc_tidl_vtable;
-               return g_vtable[OPEN_CONNECTION](uid);
+               break;
        default:
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] IPC method is not set");
                return TTS_ERROR_OPERATION_FAILED;
        }
+
+       return g_vtable[OPEN_CONNECTION](uid);
 }
 
 int tts_ipc_close_connection(int uid)
@@ -114,15 +130,7 @@ int tts_ipc_close_connection(int uid)
                return TTS_ERROR_OPERATION_FAILED;
        }
 
-       switch (g_ipc_method) {
-       case TTS_IPC_METHOD_DBUS:
-               return g_vtable[CLOSE_CONNECTION]();
-       case TTS_IPC_METHOD_TIDL:
-               return g_vtable[CLOSE_CONNECTION](uid);
-       default:
-               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] IPC method is not set");
-               return TTS_ERROR_OPERATION_FAILED;
-       }
+       return g_vtable[CLOSE_CONNECTION](uid);
 }
 
 int tts_ipc_stop_listening(int uid)
index a1d8785..4c216ce 100644 (file)
@@ -221,7 +221,7 @@ static void __on_connected(rpc_port_proxy_tts_h h, void *user_data)
                return;
        }
 
-       tts->connected = TRUE;
+       tts->connected = true;
        if (0 != rpc_port_proxy_tts_invoke_set_mode(tts->rpc_h, client->mode)) {
                SLOG(LOG_ERROR, TAG_TTSC, "Failed to set mode");
                return;
@@ -246,8 +246,7 @@ static void __on_disconnected(rpc_port_proxy_tts_h h, void *user_data)
                return;
        }
 
-       tts->connected = FALSE;
-
+       tts->connected = false;
        if (tts_client_is_listening_started(uid)) {
                __tts_cb_error(-1, TTS_ERROR_SERVICE_RESET, -1, "Daemon Reset");
                SLOG(LOG_DEBUG, TAG_TTSC, "Disconnected from server");
@@ -359,6 +358,7 @@ int tts_tidl_close_connection(int uid)
        }
 
        tts->rpc_h = NULL;
+       tts->connected = false;
        return TTS_ERROR_NONE;
 }