# HG changeset patch
# User rosfran
# Date 1165273656 0
# Node ID b13b1e81f5afcda9357a162643085439db38f2e9
# Parent  bacafcff7883a6c3b9ab3fd946660ab662e8fa0c
[svn r193] Merged with Coxinhas' fixes.

diff -r bacafcff7883 -r b13b1e81f5af gmyth/src/gmyth_file_transfer.c
--- a/gmyth/src/gmyth_file_transfer.c	Mon Dec 04 22:29:39 2006 +0000
+++ b/gmyth/src/gmyth_file_transfer.c	Mon Dec 04 23:07:36 2006 +0000
@@ -84,10 +84,6 @@
 
 //static GStaticMutex st_mutex = G_STATIC_MUTEX_INIT;
 
-static GMutex*				mutex 					 = NULL;
-
-static GCond*					io_watcher_cond  = NULL;
-
 static void gmyth_file_transfer_class_init          (GMythFileTransferClass *klass);
 static void gmyth_file_transfer_init                (GMythFileTransfer *object);
 
@@ -120,60 +116,49 @@
 { 
   g_return_if_fail( transfer != NULL );
 
-  //transfer->card_id = num;
-  //transfer->rec_id = -1;
-  //transfer->recordernum = num;
-
   transfer->readposition = 0;
   transfer->filesize = GMYTHTV_FILE_SIZE;
-  //transfer->timeoutisfast = FALSE;
-
-  //transfer->userreadahead = GMYTHTV_USER_READ_AHEAD;
-  //transfer->retries = GMYTHTV_RETRIES;  
 
   transfer->control_sock = NULL;
   transfer->sock = NULL;
   
   transfer->monitor = NULL;
-  
-  /* it is used for signalizing the event socket consumer thread */
-  io_watcher_cond = g_cond_new();
-  
-  /* mutex to control access to the event socket consumer thread */
-  mutex = g_mutex_new();
+
 }
 
 static void
 gmyth_file_transfer_dispose  (GObject *object)
 {
-  GMythFileTransfer *transfer = GMYTH_FILE_TRANSFER (object);
-
-  g_free (transfer->filename);
-  transfer->filename = NULL;
-
-  if (transfer->backend_info != NULL) {
-    g_object_unref (transfer->backend_info);
-    transfer->backend_info = NULL;
+	GMythFileTransfer *transfer = GMYTH_FILE_TRANSFER (object);
+	
+  if ( transfer->control_sock != NULL )  
+  {
+  	g_object_unref( transfer->control_sock );
+  	transfer->control_sock = NULL;
+  }  
+  
+  if ( transfer->sock != NULL )  
+  {
+  	g_object_unref( transfer->sock );
+  	transfer->sock = NULL;
   }
-
-  if (transfer->control_sock != NULL) {
-    g_object_unref (transfer->control_sock);
-    transfer->control_sock = NULL;
+  
+  if ( transfer->monitor != NULL )  
+  {
+  	g_object_unref( transfer->monitor );
+  	transfer->monitor = NULL;
   }
-
-  if (transfer->event_sock != NULL) {
-    g_object_unref (transfer->event_sock);
-    transfer->event_sock = NULL;
+  
+  if ( transfer->event_sock != NULL )  
+  {
+  	g_object_unref( transfer->event_sock );
+  	transfer->event_sock = NULL;
   }
-
-  if (transfer->sock != NULL) {
-    g_object_unref (transfer->sock);
-    transfer->sock = NULL;
-  }
-
-  if (transfer->monitor != NULL) {
-    g_object_unref (transfer->monitor);
-    transfer->monitor = NULL;
+  
+  if ( transfer->filename != NULL )  
+  {
+  	g_free( transfer->filename );
+  	transfer->filename = NULL;
   }
 
   G_OBJECT_CLASS (gmyth_file_transfer_parent_class)->dispose (object);
@@ -248,8 +233,6 @@
   gboolean ret = TRUE;
 
   g_return_val_if_fail (transfer != NULL, FALSE );
-  g_return_val_if_fail (transfer->hostname != NULL, FALSE);
-  g_return_val_if_fail (transfer->port > 0, FALSE);
 
   base_str = g_string_new ("");
 
diff -r bacafcff7883 -r b13b1e81f5af gmyth/src/gmyth_monitor_handler.c
--- a/gmyth/src/gmyth_monitor_handler.c	Mon Dec 04 22:29:39 2006 +0000
+++ b/gmyth/src/gmyth_monitor_handler.c	Mon Dec 04 23:07:36 2006 +0000
@@ -70,6 +70,8 @@
 #define GMYTHTV_ENABLE_DEBUG				1
 #endif
 
+GMainContext *io_watcher_context = NULL;
+
 GThread *monitor_th = NULL;
 
 enum myth_sock_types {
@@ -79,9 +81,9 @@
   GMYTH_RINGBUFFER_TYPE
 };
 
-static gboolean* myth_control_sock_listener( GIOChannel *io_channel );
-//static gboolean myth_control_sock_listener( GIOChannel *io_channel, GIOCondition condition, 
-//				gpointer data );
+//static gboolean* myth_control_sock_listener( GIOChannel *io_channel );
+static gboolean myth_control_sock_listener( GIOChannel *io_channel, GIOCondition condition, 
+				gpointer data );
 
 static GMutex*				mutex 					 = NULL;
 
@@ -132,6 +134,46 @@
 static void
 gmyth_monitor_handler_dispose  (GObject *object)
 {
+	
+	GMythMonitorHandler *monitor = GMYTH_MONITOR_HANDLER (object);
+	
+  if ( monitor->event_sock != NULL )  
+  {
+  	g_object_unref( monitor->event_sock );
+  	monitor->event_sock = NULL;
+  }
+  
+  if ( monitor->hostname != NULL )  
+  {
+  	g_free( monitor->hostname );
+  	monitor->hostname = NULL;
+  }
+
+  if ( monitor->backend_msgs != NULL )  
+  {
+  	g_hash_table_unref( monitor->backend_msgs );
+  	monitor->backend_msgs = NULL;
+  }
+  
+  if ( mutex != NULL )  
+  {
+  	g_mutex_free( mutex );
+  	mutex = NULL;
+  }
+  
+  if ( io_watcher_cond != NULL )  
+  {
+  	g_cond_free( io_watcher_cond );
+  	io_watcher_cond = NULL;
+  }
+  
+  if ( monitor_th != NULL )  
+  {
+  	g_thread_join( monitor_th );
+  	g_object_unref( monitor_th );
+  	monitor_th = NULL;
+  }
+
   G_OBJECT_CLASS (gmyth_monitor_handler_parent_class)->dispose (object);
 }
 
@@ -182,39 +224,36 @@
 
 }
 
-static gboolean*
-//myth_control_sock_listener( GIOChannel *io_channel, GIOCondition condition, gpointer data )
-myth_control_sock_listener( GIOChannel *io_channel )
+static gboolean
+myth_control_sock_listener( GIOChannel *io_channel, GIOCondition condition, gpointer data )
+//myth_control_sock_listener( GIOChannel *io_channel )
 {
 
   GIOStatus io_status;
   GError *error = NULL;
   GIOCondition io_cond;
-  GIOCondition condition;
+  //GIOCondition condition;
   gchar *trash = NULL;
   GByteArray *byte_array = NULL;
   guint recv = 0;
-  gboolean* ret = g_new0( gboolean, 1 );
-  //gboolean ret = TRUE;
+  //gboolean* ret = g_new0( gboolean, 1 );
+  gboolean ret = TRUE;
   gsize len = 0;
   
   //GMythMonitorHandler *monitor = (GMythMonitorHandler*)data;
   
-  *ret = TRUE;
   //myth_control_acquire_context (TRUE);
   
   if ( io_channel == NULL ) {
   	g_debug ("Monitor socket is NULL!\n");
-  	*ret = FALSE;
+  	ret = FALSE;
   	goto clean_up;
   }
   gmyth_debug ("Listening on Monitor socket...!\n");
   
-  while (TRUE) {
+  //while (TRUE) {
 	  	
-	  condition = g_io_channel_get_buffer_condition( io_channel );
-	  
-	  //myth_control_acquire_context (TRUE);
+	  //condition = g_io_channel_get_buffer_condition( io_channel );
 	  
 	  //while ( !has_io_access ) 
 	  //	g_cond_wait( io_watcher_cond, mutex );
@@ -223,7 +262,7 @@
 	  
 	  if (condition & G_IO_HUP) {
 	    gmyth_debug ("Read end of pipe died!\n");
-	    *ret = FALSE;
+	    ret = FALSE;
 	    //goto clean_up;
 	  }
 	    
@@ -251,7 +290,7 @@
 	      io_cond = g_io_channel_get_buffer_condition( io_channel );
 	
 	    } while ( ( io_cond & G_IO_IN ) != 0 );
-	    gmyth_debug ("\n[%s]\tEVENT: Read %d bytes: %s\n\n", __FUNCTION__, recv, byte_array != NULL && byte_array->data != NULL ? (gchar*)byte_array->data : "[no event data]" );
+	    gmyth_debug ("\n\n\n[%s]\tEVENT: Read %d bytes: %s\n\n\n", __FUNCTION__, recv, byte_array != NULL && byte_array->data != NULL ? (gchar*)byte_array->data : "[no event data]" );
 	  }	  
 	  
 	  if ( byte_array != NULL ) {
@@ -261,11 +300,10 @@
 	  
 	  g_usleep( 300 );
 	  
-  }
-  //ret = g_io_channel_read_chars ( source, &msg, &len, NULL, &err);
+  //}
   if ( io_status == G_IO_STATUS_ERROR ) {
     gmyth_debug ("[%s] Error reading: %s\n", __FUNCTION__, error != NULL ? error->message : "" );
-   	*ret = FALSE;
+   	ret = FALSE;
    	goto clean_up;   	
   }
   
@@ -305,14 +343,71 @@
   return ret;
 }    
 
+static gboolean*
+myth_control_sock_setup( GIOChannel *channel )
+{
+	gboolean *ret = g_new0( gboolean, 1 );
+	
+	*ret = TRUE;
+	
+  io_watcher_context = g_main_context_default();
+  //GMainLoop *loop = g_main_loop_new( io_watcher_context, TRUE );
+
+  GSource *source;
+
+  if ( channel != NULL ) {
+    source = g_io_create_watch( channel, G_IO_IN | G_IO_HUP );
+  } else {
+  	*ret = FALSE;
+  	goto cleanup;
+  }
+
+  g_source_set_callback ( source, (GSourceFunc)myth_control_sock_listener, NULL, NULL );
+
+  g_source_attach( source, io_watcher_context );
+  
+  if (source==NULL) {
+    gmyth_debug( "[%s] Error adding watch listener function to the IO control channel!\n", __FUNCTION__ );
+    *ret = FALSE;
+    goto cleanup;
+  }
+  
+  //g_main_loop_run( loop );
+  
+cleanup:
+  //if ( source != NULL )
+  //  g_source_unref( source );
+  
+  return ret;
+  
+}
+
 gboolean 
 gmyth_monitor_handler_start (GMythMonitorHandler *monitor)
 {
-    monitor_th = g_thread_create( (GThreadFunc)myth_control_sock_listener, 
+	gboolean *ret = g_new0( gboolean, 1 ); 
+	*ret = TRUE;	
+	
+	if (!g_thread_supported ()) g_thread_init (NULL);
+	
+  monitor_th = g_thread_create( (GThreadFunc)myth_control_sock_setup, 
   					monitor->event_sock->sd_io_ch, TRUE, NULL );
+  					
+  if ( ( ret = g_thread_join( monitor_th ) ) == FALSE )
+  {
+  	gmyth_debug ( "\n[%s]\tERROR! Coudn't start listener on the MONITOR event socket...[thread location = %p]\n", 
+  				__FUNCTION__, g_thread_self( ) );  	  	
+  } else {
+  	gmyth_debug ( "\n[%s]\tOK! Starting listener on the MONITOR event socket...[thread location = %p]\n", 
+  				__FUNCTION__, g_thread_self( ) );
+  }
 
-    gmyth_debug( "[%s] Watch listener function over the IO control channel? %s!!!\n", 
-  			__FUNCTION__, ( ret == TRUE ? "YES" : "NO" ) );
+cleanup:
+  //if ( source != NULL )
+  //  g_source_unref( source );
+    
+  gmyth_debug( "[%s] Watch listener function over the IO control channel? %s!!!\n", 
+  			__FUNCTION__, ( *ret == TRUE ? "YES" : "NO" ) );
     
     return TRUE;
 }