From 05033aa5bfd134851c55c3b0a1f6e6a50c2f9995 Mon Sep 17 00:00:00 2001 From: Suyeon Hwang Date: Wed, 23 Feb 2022 19:04:25 +0900 Subject: [PATCH] Refactor retry IPC code to enhance readability The code for retry IPC is too complex to understand. To simplify the code, this patch refactors the code for retry IPC. By this patch, the name of function and its logic become more clear to understand. Change-Id: I9b7cdc4265ce639f3ebc5d71230af14c684adc95 Signed-off-by: Suyeon Hwang --- client/tts_client.c | 18 ++++++++++++ client/tts_client.h | 4 +++ client/tts_core.c | 79 +++++++++++++++++++++++++++-------------------------- 3 files changed, 62 insertions(+), 39 deletions(-) diff --git a/client/tts_client.c b/client/tts_client.c index 8d189d3..9ad47e3 100644 --- a/client/tts_client.c +++ b/client/tts_client.c @@ -429,6 +429,24 @@ bool tts_client_is_listening_started(unsigned int uid) return client->start_listening; } +void tts_client_set_reprepared(tts_client_s* client, bool is_reprepared) +{ + if (false == tts_client_is_valid_client(client)) { + return; + } + + client->reprepared = is_reprepared; +} + +bool tts_client_is_reprepared(tts_client_s* client) +{ + if (false == tts_client_is_valid_client(client)) { + return INVALID_HANDLE; + } + + return client->reprepared; +} + void tts_client_set_mode(tts_client_s* client, tts_mode_e mode) { if (false == tts_client_is_valid_client(client)) { diff --git a/client/tts_client.h b/client/tts_client.h index c241100..3a41d58 100644 --- a/client/tts_client.h +++ b/client/tts_client.h @@ -70,6 +70,7 @@ typedef struct { int prepare_count; Ecore_Timer* hello_timer; bool start_listening; + bool reprepared; /* options */ char* credential; @@ -116,6 +117,9 @@ tts_state_e tts_client_get_current_state(tts_client_s* client); void tts_client_set_start_listening(unsigned int uid, bool is_listening_started); bool tts_client_is_listening_started(unsigned int uid); +void tts_client_set_reprepared(tts_client_s* client, bool is_reprepared); +bool tts_client_is_reprepared(tts_client_s* client); + void tts_client_set_mode(tts_client_s* client, tts_mode_e mode); tts_mode_e tts_client_get_mode(tts_client_s* client); diff --git a/client/tts_core.c b/client/tts_core.c index 0fed71b..231cd8f 100644 --- a/client/tts_core.c +++ b/client/tts_core.c @@ -608,26 +608,30 @@ static bool __is_screen_reader_turned_on() return (bool)screen_reader; } -static inline bool __handle_dbus_request_result(tts_client_s* client, tts_error_e ret, bool* is_prepared) +static inline bool __is_ipc_retry_needed(tts_client_s* client, tts_error_e ret) { if (TTS_ERROR_NONE == ret) { - SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success the dbus request"); + SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success the IPC request"); return false; - } else if (TTS_ERROR_INVALID_PARAMETER == ret && false == *is_prepared) { - tts_client_set_current_state(client, TTS_STATE_CREATED); - if (0 == tts_core_prepare_sync(client)) { - *is_prepared = true; - SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync"); - } - } else if (TTS_ERROR_TIMED_OUT != ret) { - SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", tts_core_covert_error_code(ret)); - return false; - } else { - SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry play : %s", tts_core_covert_error_code(ret)); + } + + if (TTS_ERROR_TIMED_OUT == ret) { + SLOG(LOG_WARN, TAG_TTSC, "[WARNING] IPC is failed. Try again. ret(%d/%s)", ret, get_error_message(ret)); usleep(10000); + return true; } - return true; + if (TTS_ERROR_INVALID_PARAMETER == ret && false == tts_client_is_reprepared(client)) { + SLOG(LOG_WARN, TAG_TTSC, "[WARNING] IPC is failed by unregistered client. ret(%d/%s)", ret, get_error_message(ret)); + SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Try to prepare again"); + tts_client_set_current_state(client, TTS_STATE_CREATED); + tts_core_prepare_sync(client); + tts_client_set_reprepared(client, true); + return true; + } + + SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] IPC request is failed. ret(%d/%s)", ret, get_error_message(ret)); + return false; } static inline int __request_add_text(tts_client_s* client, const char* text, const char* language, int voice_type, int speed, int* utt_id) @@ -642,10 +646,10 @@ static inline int __request_add_text(tts_client_s* client, const char* text, con // TODO: If use cpp, remove dupliceated code using command class pattern unsigned int uid = tts_client_get_uid(client); int ret = -1; - bool is_prepared = false; + tts_client_set_reprepared(client, false); for (int count = 0; count < TTS_RETRY_COUNT; count++) { ret = tts_ipc_request_add_text(uid, text, convert_language, voice_type, speed, new_utt_id, client->credential); - if (false == __handle_dbus_request_result(client, ret, &is_prepared)) { + if (false == __is_ipc_retry_needed(client, ret)) { break; } } @@ -664,10 +668,10 @@ static inline int __request_play(tts_client_s* client) { unsigned int uid = tts_client_get_uid(client); int ret = -1; - bool is_prepared = false; + tts_client_set_reprepared(client, false); for (int count = 0; count < TTS_RETRY_COUNT; count++) { ret = tts_ipc_request_play(uid, client->credential); - if (false == __handle_dbus_request_result(client, ret, &is_prepared)) { + if (false == __is_ipc_retry_needed(client, ret)) { break; } } @@ -1142,10 +1146,10 @@ int tts_core_unprepare(tts_client_s* client) } else { SLOG(LOG_INFO, TAG_TTSC, "[INFO] Request finalize"); - bool is_prepared = false; + tts_client_set_reprepared(client, false); for (int count = 0; count < TTS_RETRY_COUNT; count++) { ret = tts_ipc_request_finalize(uid); - if (false == __handle_dbus_request_result(client, ret, &is_prepared)) { + if (false == __is_ipc_retry_needed(client, ret)) { break; } } @@ -1280,16 +1284,13 @@ int tts_core_stop(tts_client_s* client) } unsigned int uid = tts_client_get_uid(client); - int ret = 0; - int count = 0; - bool is_prepared = false; - while (TTS_RETRY_COUNT > count) { + int ret = -1; + tts_client_set_reprepared(client, false); + for (int count = 0; count < TTS_RETRY_COUNT; count++) { ret = tts_ipc_request_stop(uid); - if (false == __handle_dbus_request_result(client, ret, &is_prepared)) { + if (false == __is_ipc_retry_needed(client, ret)) { break; } - - count++; } if (TTS_ERROR_NONE != ret) { @@ -1309,10 +1310,10 @@ int tts_core_pause(tts_client_s* client) unsigned int uid = tts_client_get_uid(client); int ret = -1; - bool is_prepared = false; + tts_client_set_reprepared(client, false); for (int count = 0; count < TTS_RETRY_COUNT; count++) { ret = tts_ipc_request_pause(uid); - if (false == __handle_dbus_request_result(client, ret, &is_prepared)) { + if (false == __is_ipc_retry_needed(client, ret)) { break; } } @@ -1374,10 +1375,10 @@ int tts_core_add_pcm(tts_client_s* client, int event, const void* data, unsigned unsigned int uid = tts_client_get_uid(client); int ret = -1; - bool is_prepared = false; + tts_client_set_reprepared(client, false); for (int count = 0; count < TTS_RETRY_COUNT; count++) { ret = tts_ipc_request_add_pcm(uid, event, data, data_size, audio_type, rate); - if (false == __handle_dbus_request_result(client, ret, &is_prepared)) { + if (false == __is_ipc_retry_needed(client, ret)) { break; } } @@ -1400,10 +1401,10 @@ int tts_core_play_pcm(tts_client_s* client) unsigned int uid = tts_client_get_uid(client); int ret = -1; - bool is_prepared = false; + tts_client_set_reprepared(client, false); for (int count = 0; count < TTS_RETRY_COUNT; count++) { ret = tts_ipc_request_play_pcm(uid); - if (false == __handle_dbus_request_result(client, ret, &is_prepared)) { + if (false == __is_ipc_retry_needed(client, ret)) { break; } } @@ -1426,10 +1427,10 @@ int tts_core_stop_pcm(tts_client_s* client) unsigned int uid = tts_client_get_uid(client); int ret = -1; - bool is_prepared = false; + tts_client_set_reprepared(client, false); for (int count = 0; count < TTS_RETRY_COUNT; count++) { ret = tts_ipc_request_stop_pcm(uid); - if (false == __handle_dbus_request_result(client, ret, &is_prepared)) { + if (false == __is_ipc_retry_needed(client, ret)) { break; } } @@ -1452,10 +1453,10 @@ int tts_core_set_private_data(tts_client_s* client, const char* key, const char* unsigned int uid = tts_client_get_uid(client); int ret = -1; - bool is_prepared = false; + tts_client_set_reprepared(client, false); for (int count = 0; count < TTS_RETRY_COUNT; count++) { ret = tts_ipc_request_set_private_data(uid, key, data); - if (false == __handle_dbus_request_result(client, ret, &is_prepared)) { + if (false == __is_ipc_retry_needed(client, ret)) { break; } } @@ -1478,10 +1479,10 @@ int tts_core_get_private_data(tts_client_s* client, const char* key, char** data unsigned int uid = tts_client_get_uid(client); int ret = -1; - bool is_prepared = false; + tts_client_set_reprepared(client, false); for (int count = 0; count < TTS_RETRY_COUNT; count++) { ret = tts_ipc_request_get_private_data(uid, key, data); - if (false == __handle_dbus_request_result(client, ret, &is_prepared)) { + if (false == __is_ipc_retry_needed(client, ret)) { break; } } -- 2.7.4