Add to reconnect dbus when connection is missed 96/133196/2
authorWonnam Jang <wn.jang@samsung.com>
Fri, 9 Jun 2017 06:48:31 +0000 (15:48 +0900)
committerWonnam Jang <wn.jang@samsung.com>
Wed, 28 Jun 2017 05:19:53 +0000 (14:19 +0900)
Change-Id: Ib1769b34ee7962f4f75340b7cf85b09730311efa
Signed-off-by: Wonnam Jang <wn.jang@samsung.com>
client/stt_dbus.c

index f6ddbf0..e5da100 100644 (file)
@@ -648,6 +648,20 @@ static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handle
        return ECORE_CALLBACK_RENEW;
 }
 
+static void __stt_dbus_connection_free()
+{
+       if (NULL != g_conn_listener) {
+               dbus_connection_close(g_conn_listener);
+               dbus_connection_unref(g_conn_listener);
+               g_conn_listener = NULL;
+       }
+       if (NULL != g_conn_sender) {
+               dbus_connection_close(g_conn_sender);
+               dbus_connection_unref(g_conn_sender);
+               g_conn_sender = NULL;
+       }
+}
+
 int stt_dbus_open_connection()
 {
        if (NULL != g_conn_sender && NULL != g_conn_listener) {
@@ -687,6 +701,7 @@ int stt_dbus_open_connection()
 
        if (NULL == g_conn_listener) {
                SLOG(LOG_ERROR, TAG_STTC, "Fail to get dbus connection");
+               __stt_dbus_connection_free();
                return STT_ERROR_OPERATION_FAILED;
        }
 
@@ -716,12 +731,14 @@ int stt_dbus_open_connection()
        if (dbus_error_is_set(&err)) {
                SLOG(LOG_ERROR, TAG_STTC, "Match Error (%s)", err.message);
                dbus_error_free(&err);
+               __stt_dbus_connection_free();
                return STT_ERROR_OPERATION_FAILED;
        }
 
        int fd = 0;
        if (true != dbus_connection_get_unix_fd(g_conn_listener, &fd)) {
                SLOG(LOG_ERROR, TAG_STTC, "Fail to get fd from dbus");
+               __stt_dbus_connection_free();
                return STT_ERROR_OPERATION_FAILED;
        } else {
                SLOG(LOG_DEBUG, TAG_STTC, "Get fd from dbus : %d", fd);
@@ -730,6 +747,7 @@ int stt_dbus_open_connection()
        g_fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)listener_event_callback, g_conn_listener, NULL, NULL);
        if (NULL == g_fd_handler) {
                SLOG(LOG_ERROR, TAG_STTC, "fail to get fd handler from ecore");
+               __stt_dbus_connection_free();
                return STT_ERROR_OPERATION_FAILED;
        }
 
@@ -758,15 +776,7 @@ int stt_dbus_close_connection()
                dbus_error_free(&err);
        }
 
-       dbus_connection_close(g_conn_sender);
-       dbus_connection_close(g_conn_listener);
-
-       dbus_connection_unref(g_conn_sender);
-       dbus_connection_unref(g_conn_listener);
-
-       g_conn_sender = NULL;
-       g_conn_listener = NULL;
-
+       __stt_dbus_connection_free();
        __stt_dbus_service_free();
 
        return 0;
@@ -774,6 +784,18 @@ int stt_dbus_close_connection()
 
 int stt_dbus_reconnect()
 {
+       if (!g_conn_sender || !g_conn_listener) {
+               stt_dbus_close_connection();
+
+               if (0 != stt_dbus_open_connection()) {
+                       SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to reconnect");
+                       return -1;
+               }
+
+               SLOG(LOG_DEBUG, TAG_STTC, "[DBUS] Reconnect");
+               return 0;
+       }
+
        bool sender_connected = dbus_connection_get_is_connected(g_conn_sender);
        bool listener_connected = dbus_connection_get_is_connected(g_conn_listener);
        SLOG(LOG_DEBUG, TAG_STTC, "[DBUS] Sender(%s) Listener(%s)",