Code clean up regarding getting latest server 07/199607/4
authorhj kim <backto.kim@samsung.com>
Wed, 13 Feb 2019 07:43:43 +0000 (16:43 +0900)
committerhj kim <backto.kim@samsung.com>
Thu, 14 Feb 2019 02:44:32 +0000 (11:44 +0900)
Change-Id: I4f5bfd5d39a655d187eb0dc32eddf2ab39bdb588

svc/media_controller_db_util.c [changed mode: 0644->0755]
svc/media_controller_svc.c [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index 9b25533..735fe4b
@@ -178,52 +178,67 @@ static int __mc_create_playlist_table(sqlite3 *handle)
        return ret;
 }
 
-static int __mc_update_dead_server(void *handle, const char *server_name)
+static char * __mc_db_get_latest_server_name(void *handle)
 {
        int ret = MEDIA_CONTROLLER_ERROR_NONE;
        sqlite3 *db_handle = (sqlite3 *)handle;
        char *sql_str = NULL;
        sqlite3_stmt *stmt = NULL;
-       gboolean is_latest_server = FALSE;
-       const char *latest_server_name;
+       char *server_name = NULL;
 
-       mc_retvm_if(handle == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Handle is NULL");
-       mc_retvm_if(!MC_STRING_VALID(server_name), MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "server_name is invalid");
+       mc_retvm_if(handle == NULL, NULL, "Handle is NULL");
 
-       /* Get latest server */
        sql_str = sqlite3_mprintf("SELECT server_name FROM '%q';", MC_DB_TABLE_LATEST_SERVER);
-       mc_retvm_if(!MC_STRING_VALID(sql_str), MEDIA_CONTROLLER_ERROR_INVALID_OPERATION, "SQL string is null");
+       mc_retvm_if(!MC_STRING_VALID(sql_str), NULL, "SQL string is null");
 
        ret = sqlite3_prepare_v2(db_handle, sql_str, strlen(sql_str), &stmt, NULL);
        if (SQLITE_OK != ret) {
-               mc_secure_error("prepare error [%s]", sqlite3_errmsg(db_handle));
+               mc_error("prepare error [%s]", sqlite3_errmsg(db_handle));
                SQLITE3_SAFE_FREE(sql_str);
-               return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION;
+               return NULL;
        }
 
        ret = sqlite3_step(stmt);
-       if (SQLITE_ROW == ret) {
-               /* Get latest server name */
-               latest_server_name = (const char *)sqlite3_column_text(stmt, 0);
-               if (MC_STRING_VALID(latest_server_name)) {
-                       mc_secure_debug("latest_server_name: %s", latest_server_name);
-                       if (strcmp(server_name, latest_server_name) == 0)
-                               is_latest_server = TRUE;
-               } else {
-                       mc_secure_warning("invalid latest_server_name: %s", latest_server_name);
-               }
+       if (SQLITE_ROW != ret) {
+               mc_error("[No-Error] No latest server [%s]", sqlite3_errmsg(db_handle));
+               SQLITE3_FINALIZE(stmt);
+               SQLITE3_SAFE_FREE(sql_str);
+               return NULL; /*There is no latest server yet. */
+       }
+
+       if (MC_STRING_VALID((char *)sqlite3_column_text(stmt, 0))) {
+               server_name = g_strdup((char *)sqlite3_column_text(stmt, 0));
+               mc_secure_debug("latest_server_name : [%s]", server_name);
+       } else {
+               mc_error("Fail to get latest_server_name");
        }
 
        SQLITE3_FINALIZE(stmt);
        SQLITE3_SAFE_FREE(sql_str);
 
+       return server_name;
+}
+
+static int __mc_update_dead_server(void *handle, const char *server_name)
+{
+       int ret = MEDIA_CONTROLLER_ERROR_NONE;
+       char *sql_str = NULL;
+       char *latest_server_name = NULL;
+
+       mc_retvm_if(handle == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Handle is NULL");
+       mc_retvm_if(!MC_STRING_VALID(server_name), MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "server_name is invalid");
+
+       latest_server_name = __mc_db_get_latest_server_name(handle);
+
        /* Update server_state to deactivated, if the server is latest.
         * Drop the table of server_name, if it is not latest.
         */
-       if (is_latest_server)
+       if (g_strcmp0(server_name, latest_server_name) == 0)
                sql_str = sqlite3_mprintf(DB_UPDATE_STATE_PLAYBACK, server_name, MC_SERVER_STATE_DEACTIVATE, MC_PLAYBACK_STATE_STOPPED);
        else
                sql_str = sqlite3_mprintf(DB_DROP_TABLE, server_name);
+
+       MC_SAFE_FREE(latest_server_name);
        mc_retvm_if(!MC_STRING_VALID(sql_str), MEDIA_CONTROLLER_ERROR_INVALID_OPERATION, "SQL string is null");
 
        ret = mc_db_util_update_db(handle, sql_str);
@@ -559,41 +574,14 @@ int mc_db_util_delete_whole_server_tables(void *handle)
 int mc_db_util_init_latest_server_table(void *handle)
 {
        int ret = MEDIA_CONTROLLER_ERROR_NONE;
-       sqlite3 *db_handle = (sqlite3 *)handle;
        char *sql_str = NULL;
-       sqlite3_stmt *stmt = NULL;
        char *latest_server_name = NULL;
 
        mc_retvm_if(handle == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Handle is NULL");
 
        /* Get latest server name */
-       sql_str = sqlite3_mprintf("SELECT server_name FROM '%q';", MC_DB_TABLE_LATEST_SERVER);
-       mc_retvm_if(!MC_STRING_VALID(sql_str), MEDIA_CONTROLLER_ERROR_INVALID_OPERATION, "SQL string is null");
-
-       ret = sqlite3_prepare_v2(db_handle, sql_str, strlen(sql_str), &stmt, NULL);
-       if (SQLITE_OK != ret) {
-               mc_error("prepare error [%s]", sqlite3_errmsg(db_handle));
-               SQLITE3_SAFE_FREE(sql_str);
-               return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION;
-       }
-
-       ret = sqlite3_step(stmt);
-       if (SQLITE_ROW != ret) {
-               mc_debug("[no record] latest_server does not exist");
-               SQLITE3_FINALIZE(stmt);
-               SQLITE3_SAFE_FREE(sql_str);
-               return MEDIA_CONTROLLER_ERROR_NONE;
-       }
-
-       if (MC_STRING_VALID((const char *)sqlite3_column_text(stmt, 0))) {
-               latest_server_name = strdup((const char *)sqlite3_column_text(stmt, 0));
-               mc_secure_debug("latest_server_name: %s", latest_server_name);
-       }
-
-       SQLITE3_FINALIZE(stmt);
-       SQLITE3_SAFE_FREE(sql_str);
-
-       mc_retvm_if(latest_server_name == NULL, MEDIA_CONTROLLER_ERROR_OUT_OF_MEMORY, "latest_server is null");
+       latest_server_name = __mc_db_get_latest_server_name(handle);
+       mc_retvm_if(latest_server_name == NULL, MEDIA_CONTROLLER_ERROR_NONE, "[No-Error] latest_server does not exist");
 
        /* Update server_state and playback_state to 0 */
        sql_str = sqlite3_mprintf(DB_UPDATE_STATE_PLAYBACK, latest_server_name, MC_SERVER_STATE_DEACTIVATE, MC_PLAYBACK_STATE_STOPPED);
@@ -601,11 +589,6 @@ int mc_db_util_init_latest_server_table(void *handle)
        mc_retvm_if(!MC_STRING_VALID(sql_str), MEDIA_CONTROLLER_ERROR_INVALID_OPERATION, "SQL string is null");
 
        ret = mc_db_util_update_db(handle, sql_str);
-       if (ret != MEDIA_CONTROLLER_ERROR_NONE) {
-               mc_error("Error mc_db_util_update_db %d", ret);
-               SQLITE3_SAFE_FREE(sql_str);
-               return ret;
-       }
 
        SQLITE3_SAFE_FREE(sql_str);
 
old mode 100644 (file)
new mode 100755 (executable)
index 10738b2..9aac1ef
@@ -307,10 +307,7 @@ static int _mc_service_reset_db(uid_t uid)
 
        /* Connect media controller DB*/
        res = mc_db_util_connect(&db_handle, uid);
-       if (res != MEDIA_CONTROLLER_ERROR_NONE) {
-               mc_error("Failed to connect DB");
-               return res;
-       }
+       mc_retvm_if(res != MEDIA_CONTROLLER_ERROR_NONE, res, "Failed to connect DB");
 
        /* Destroy tables */
        res = mc_db_util_delete_whole_server_tables(db_handle);
@@ -319,15 +316,13 @@ static int _mc_service_reset_db(uid_t uid)
 
        /* Create tables */
        res = mc_db_util_create_tables(db_handle);
-       if (res != MEDIA_CONTROLLER_ERROR_NONE) {
+       if (res != MEDIA_CONTROLLER_ERROR_NONE)
                mc_error("mc_db_util_create_tables failed [%d]", res);
-               return res;
-       }
 
        /* Initialize latest server table */
        res = mc_db_util_init_latest_server_table(db_handle);
        if (res != MEDIA_CONTROLLER_ERROR_NONE)
-               mc_error("mc_db_util_create_tables failed [%d]", res);
+               mc_error("mc_db_util_init_latest_server_table failed [%d]", res);
 
        /* Disconnect media controller DB*/
        res = mc_db_util_disconnect(db_handle);