# HG changeset patch
# User renatofilho
# Date 1183494015 -3600
# Node ID 1603bc7804dac210b7ba43687389d91323c1f7a1
# Parent  b405295259f31e408cfe2a9ee9f2e8cbbde2f6e2
[svn r775] create function in backend info for otimize socket usage

diff -r b405295259f3 -r 1603bc7804da gmyth/src/gmyth_backendinfo.c
--- a/gmyth/src/gmyth_backendinfo.c	Tue Jul 03 13:53:26 2007 +0100
+++ b/gmyth/src/gmyth_backendinfo.c	Tue Jul 03 21:20:15 2007 +0100
@@ -73,6 +73,7 @@
     g_free(backend_info->username);
     g_free(backend_info->password);
     g_free(backend_info->db_name);
+    g_object_unref (backend_info->sock);
 
     backend_info->hostname = NULL;
     backend_info->username = NULL;
@@ -342,3 +343,22 @@
     return
         gmyth_uri_is_local_file(gmyth_backend_info_get_uri(backend_info));
 }
+
+GMythSocket*
+gmyth_backend_info_get_connected_socket (GMythBackendInfo *backend_info)
+{
+    if (backend_info->sock != NULL) {
+        gboolean res;
+        backend_info->sock = gmyth_socket_new ();
+        res =  gmyth_socket_connect_to_backend (socket,
+                                                backend_info->hostname,
+                                                backend_info->port,
+                                                TRUE);
+        if (res == FALSE) {
+            g_object_unref (backend_info->sock);
+            backend_info->sock = NULL;
+        }
+    }
+
+    return g_object_ref (backend_info->sock);
+}
diff -r b405295259f3 -r 1603bc7804da gmyth/src/gmyth_backendinfo.h
--- a/gmyth/src/gmyth_backendinfo.h	Tue Jul 03 13:53:26 2007 +0100
+++ b/gmyth/src/gmyth_backendinfo.h	Tue Jul 03 21:20:15 2007 +0100
@@ -32,6 +32,7 @@
 #include <glib-object.h>
 
 #include "gmyth_uri.h"
+#include "gmyth_socket.h"
 
 G_BEGIN_DECLS
 #define GMYTH_BACKEND_INFO_TYPE \
@@ -75,6 +76,9 @@
     gint            port;
     /** The backend status port for http connection */
     gint            status_port;
+
+    /* Private */
+    GMythSocket *sock;
 };
 
 
@@ -105,6 +109,9 @@
 gint                gmyth_backend_info_get_port         (GMythBackendInfo   *backend_info);
 GMythURI*           gmyth_backend_info_get_uri          (GMythBackendInfo   *backend_info);
 gboolean            gmyth_backend_info_is_local_file    (GMythBackendInfo   *backend_info);
+GMythSocket*        gmyth_backend_info_get_connected_socket
+                                                        (GMythBackendInfo   *backend_info);
+                                                           
 
 G_END_DECLS
 #endif                          /* __GMYTH_BACKEND_INFO_H__ */
diff -r b405295259f3 -r 1603bc7804da gmyth/src/gmyth_scheduler.c
--- a/gmyth/src/gmyth_scheduler.c	Tue Jul 03 13:53:26 2007 +0100
+++ b/gmyth/src/gmyth_scheduler.c	Tue Jul 03 21:20:15 2007 +0100
@@ -351,7 +351,7 @@
 gmyth_scheduler_get_recorded_info (GMythScheduler *scheduler,
                                    gint record_id)
 {
-    RecordedInfo   *record;
+    RecordedInfo   *record = NULL;
     MYSQL_RES      *msql_res;
     GString        *query_str = g_string_new("");
 
@@ -366,7 +366,7 @@
     if (scheduler->msqlquery == NULL) {
         g_warning("[%s] Scheduler db connection not initialized", 
                         __FUNCTION__);
-        return -1;
+        return NULL;
     }
 
     msql_res =
@@ -375,7 +375,7 @@
 
     if (msql_res == NULL) {
         g_warning("DB retrieval of recording list failed");
-        return -1;
+        return NULL;
     } else {
         MYSQL_ROW       row;
         row = mysql_fetch_row(msql_res);
@@ -734,7 +734,7 @@
  * @param scheduler The GMythScheduler instance.
  * @return gint record_id if success, -1 otherwise 
  */
-static          gint
+static  gint
 get_record_id_from_database(GMythScheduler * scheduler)
 {
     gint            record_id;
@@ -756,12 +756,10 @@
  * 
  * @param record_id the id of the modified recording.
  */
-static          gboolean
-update_backend(GMythScheduler * scheduler, gint record_id)  // fixme: put
-                                                            // void and
-                                                            // discovery
-                                                            // record_id
-                                                            // inside
+// fixme: put void and discovery record_id inside
+static gboolean
+update_backend (GMythScheduler * scheduler, 
+                gint record_id)  
 {
     GMythSocket    *socket;
     GMythStringList *strlist = gmyth_string_list_new();
@@ -771,18 +769,16 @@
     g_string_append_printf(datastr, "%d", record_id);
     gmyth_string_list_append_string(strlist, datastr);
 
-    socket = gmyth_socket_new();
-    if (gmyth_socket_connect_to_backend
-        (socket, scheduler->backend_info->hostname,
-         scheduler->backend_info->port, TRUE)) {
+    socket = gmyth_backend_info_get_connected_socket (scheduler->backend_info);
+    if (socket != NULL) { 
         ret = (gmyth_socket_sendreceive_stringlist(socket, strlist) > 0);
+        g_object_unref (socket);
     } else {
         g_warning("[%s] Connection to backend failed!", __FUNCTION__);
     }
 
     g_string_free(datastr, TRUE);
     g_object_unref(strlist);
-
     return ret;
 }
 
diff -r b405295259f3 -r 1603bc7804da gmyth/src/gmyth_util.c
--- a/gmyth/src/gmyth_util.c	Tue Jul 03 13:53:26 2007 +0100
+++ b/gmyth/src/gmyth_util.c	Tue Jul 03 21:20:15 2007 +0100
@@ -430,42 +430,46 @@
                        const gchar * filename)
 {
     GMythSocket    *socket;
-    gboolean        res;
+    gboolean        res = FALSE;
 
     gmyth_debug("Check if file %s exists", filename);
 
     g_return_val_if_fail(backend_info != NULL, FALSE);
     g_return_val_if_fail(filename != NULL, FALSE);
 
-    socket = gmyth_socket_new();
-    res = gmyth_socket_connect_to_backend(socket, backend_info->hostname,
-                                          backend_info->port, TRUE);
-
-    if (res == TRUE) {
-        GMythStringList *slist;
-        GMythProgramInfo *program = NULL;
-
-        program = gmyth_program_info_new();
-        program->pathname = g_string_new(filename);
-
-        slist = gmyth_string_list_new();
-        gmyth_string_list_append_char_array(slist, "QUERY_CHECKFILE");
-
-        gmyth_program_info_to_string_list(program, slist);
-
-        gmyth_socket_sendreceive_stringlist(socket, slist);
-
-        res = (gmyth_string_list_get_int(slist, 0) == 1);
-
-        g_object_unref(program);
-        g_object_unref(slist);
-
-        gmyth_socket_close_connection(socket);
+    socket = gmyth_backend_info_get_connected_socket (backend_info);
+    if (socket != NULL) {
+        res = gmyth_util_file_exists_from_socket (socket, filename);
     }
     g_object_unref(socket);
     return res;
 }
 
+gboolean
+gmyth_util_file_exists_from_socket (GMythSocket *sock, 
+                                    const gchar *filename)
+{
+    gboolean res;
+    GMythStringList *slist;
+    GMythProgramInfo *program = NULL;
+
+    program = gmyth_program_info_new();
+    program->pathname = g_string_new(filename);
+
+    slist = gmyth_string_list_new();
+    gmyth_string_list_append_char_array(slist, "QUERY_CHECKFILE");
+
+    gmyth_program_info_to_string_list(program, slist);
+    gmyth_socket_sendreceive_stringlist (sock, slist);
+    res = (gmyth_string_list_get_int(slist, 0) == 1);
+
+    g_object_unref(program);
+    g_object_unref(slist);
+
+    return res;
+}
+                 
+
 /** 
  * Checks if the given remote file exists, and gets its remote directory.
  * 
diff -r b405295259f3 -r 1603bc7804da gmyth/src/gmyth_util.h
--- a/gmyth/src/gmyth_util.h	Tue Jul 03 13:53:26 2007 +0100
+++ b/gmyth/src/gmyth_util.h	Tue Jul 03 21:20:15 2007 +0100
@@ -36,59 +36,38 @@
 #include "gmyth_backendinfo.h"
 #include "gmyth_programinfo.h"
 
-G_BEGIN_DECLS GString *gmyth_util_time_to_isoformat(time_t time_value);
-GString        *gmyth_util_time_to_string(time_t time_value);
-time_t          gmyth_util_string_to_time(GString * time_str);
+G_BEGIN_DECLS 
 
-gchar          *gmyth_util_time_to_isoformat_from_time_val_fmt(const gchar
-                                                               *
-                                                               fmt_string,
-                                                               const
-                                                               GTimeVal *
-                                                               time_val);
 
-GTimeVal       *gmyth_util_string_to_time_val_fmt(const gchar * fmt_string,
-                                                  const gchar * time_str);
-
-GTimeVal       *gmyth_util_string_to_time_val(const gchar * time_str);
-
-gchar          *gmyth_util_time_to_isoformat_from_time_val(const GTimeVal *
-                                                           time);
-gchar          *gmyth_util_time_to_mythformat_from_time_val(const GTimeVal
-                                                            * time);
-
-gchar          *gmyth_util_time_to_string_only_date(const GTimeVal * time);
-
-gchar          *gmyth_util_time_to_string_only_time(const GTimeVal * time);
-
-gchar          *gmyth_util_time_to_string_from_time_val(const GTimeVal *
-                                                        time_val);
-
-struct tm      *gmyth_util_time_val_to_date(const GTimeVal * time);
-
-gboolean        gmyth_util_file_exists(GMythBackendInfo * backend_info,
-                                       const gchar * filename);
-
-gboolean        gmyth_util_file_exists_and_get_remote_dir(GMythBackendInfo
-                                                          * backend_info,
-                                                          const gchar *
-                                                          filename,
-                                                          gchar **
-                                                          current_dir);
-
-gchar          *gmyth_util_create_filename(const gint chan_id,
-                                           const GTimeVal * start_time);
-
-GList          *gmyth_util_get_channel_list(GMythBackendInfo *
-                                            backend_info);
-
-GSList         *gmyth_util_get_all_recordings(GMythBackendInfo *
-                                              backend_info);
-
-GMythProgramInfo *gmyth_util_get_recording_from_channel(GMythBackendInfo *
-                                                        backend_info,
-                                                        const gchar *
-                                                        channel);
+GString*        gmyth_util_time_to_isoformat                (time_t time_value);
+GString*        gmyth_util_time_to_string                   (time_t time_value);
+time_t          gmyth_util_string_to_time                   (GString *time_str);
+gchar*          gmyth_util_time_to_isoformat_from_time_val_fmt
+                                                            (const gchar *fmt_string,
+                                                             const GTimeVal *time_val);
+GTimeVal*       gmyth_util_string_to_time_val_fmt           (const gchar *fmt_string,
+                                                             const gchar *time_str);
+GTimeVal*       gmyth_util_string_to_time_val               (const gchar *time_str);
+gchar*          gmyth_util_time_to_isoformat_from_time_val  (const GTimeVal *time);
+gchar*          gmyth_util_time_to_mythformat_from_time_val (const GTimeVal *time);
+gchar*          gmyth_util_time_to_string_only_date         (const GTimeVal *time);
+gchar*          gmyth_util_time_to_string_only_time         (const GTimeVal *time);
+gchar*          gmyth_util_time_to_string_from_time_val     (const GTimeVal *time_val);
+struct tm*      gmyth_util_time_val_to_date                 (const GTimeVal * time);
+gboolean        gmyth_util_file_exists_from_socket          (GMythSocket *sock, 
+                                                             const gchar *filename);
+gboolean        gmyth_util_file_exists                      (GMythBackendInfo *backend_info,
+                                                             const gchar * filename);
+gboolean        gmyth_util_file_exists_and_get_remote_dir   (GMythBackendInfo *backend_info,
+                                                             const gchar *filename,
+                                                             gchar **current_dir);
+gchar*          gmyth_util_create_filename                  (const gint chan_id,
+                                                             const GTimeVal * start_time);
+GList*          gmyth_util_get_channel_list                 (GMythBackendInfo *backend_info);
+GSList*         gmyth_util_get_all_recordings               (GMythBackendInfo *backend_info);
+GMythProgramInfo*
+                gmyth_util_get_recording_from_channel       (GMythBackendInfo *backend_info,
+                                                             const gchar *channel);
 
 G_END_DECLS
 #endif                          /* GMYTH_UTIL_H_ */