# HG changeset patch
# User rosfran
# Date 1165413055 0
# Node ID a43fed947ec89da05041b1af20a51eb52eae98b9
# Parent  0ef699880d591997fa6440f78dff02a66cc6fd8b
[svn r204] Fixed problem with the MonitorHandler thread, removed from FileTransfer.

diff -r 0ef699880d59 -r a43fed947ec8 gmyth/src/gmyth_file_transfer.c
--- a/gmyth/src/gmyth_file_transfer.c	Wed Dec 06 13:33:22 2006 +0000
+++ b/gmyth/src/gmyth_file_transfer.c	Wed Dec 06 13:50:55 2006 +0000
@@ -121,8 +121,6 @@
   transfer->control_sock = NULL;
   transfer->sock = NULL;
   
-  transfer->monitor = NULL;
-
 }
 
 static void
@@ -141,19 +139,7 @@
   	g_object_unref( transfer->sock );
   	transfer->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->filename != NULL )  
   {
   	g_free( transfer->filename );
@@ -293,22 +279,6 @@
       goto cleanup;
   }
   
-  transfer->monitor = gmyth_monitor_handler_new ( );
-  
-  ret = gmyth_monitor_handler_open (transfer->monitor, transfer->backend_info->hostname, transfer->backend_info->port );
-  
-  if ( ret == TRUE )
-  {
-  	gmyth_debug("Connect MythTV Monitor event socket! Trying to start the message handler...");
-  	
-  	ret = gmyth_monitor_handler_start ( transfer->monitor );
-  	
-  	if (ret)
-  		gmyth_debug("MythTV Monitor event socket connected and listening!");
-  	else
-  		gmyth_debug("Problems when trying to start MythTV Monitor event socket!");
-  }
-  
 cleanup:
 
   if ( strlist != NULL )
@@ -378,11 +348,6 @@
     transfer->control_sock = NULL;
   } 
   
-  if ( transfer->monitor ) {
-    g_object_unref( transfer->monitor );
-    transfer->monitor = NULL;
-  }
-
 }
 
 gint64
diff -r 0ef699880d59 -r a43fed947ec8 gmyth/src/gmyth_file_transfer.h
--- a/gmyth/src/gmyth_file_transfer.h	Wed Dec 06 13:33:22 2006 +0000
+++ b/gmyth/src/gmyth_file_transfer.h	Wed Dec 06 13:50:55 2006 +0000
@@ -37,7 +37,6 @@
 #include "gmyth_uri.h"
 #include "gmyth_livetv.h"
 #include "gmyth_backendinfo.h"
-#include "gmyth_monitor_handler.h"
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -81,10 +80,7 @@
 
 	/* socket descriptors */
 	GMythSocket *control_sock;
-	GMythSocket *event_sock;
 	GMythSocket *sock;
-	
-	GMythMonitorHandler *monitor;
 
 	gint64 readposition;
 	guint64 filesize;
diff -r 0ef699880d59 -r a43fed947ec8 gmyth/src/gmyth_livetv.c
--- a/gmyth/src/gmyth_livetv.c	Wed Dec 06 13:33:22 2006 +0000
+++ b/gmyth/src/gmyth_livetv.c	Wed Dec 06 13:50:55 2006 +0000
@@ -129,6 +129,27 @@
 	}
 
 	livetv->is_livetv = TRUE;
+	
+  livetv->monitor = gmyth_monitor_handler_new ( );
+  
+  res = gmyth_monitor_handler_open (livetv->monitor, backend_info->hostname, backend_info->port );
+  
+  if ( res == TRUE )
+  {
+  	gmyth_debug("Connect MythTV Monitor event socket! Trying to start the message handler...");
+  	
+  	res = gmyth_monitor_handler_start ( livetv->monitor );
+  	
+  	if (res)
+  	{
+  		gmyth_debug("MythTV Monitor event socket connected and listening!");
+  	}
+  	else
+  	{
+  		gmyth_debug("Problems when trying to start MythTV Monitor event socket!");
+  		goto error;  		
+  	}
+  }
 
 	hostname = gmyth_backend_info_get_hostname(backend_info);
 	livetv->backend_hostname = g_string_new (hostname);
diff -r 0ef699880d59 -r a43fed947ec8 gmyth/src/gmyth_livetv.h
--- a/gmyth/src/gmyth_livetv.h	Wed Dec 06 13:33:22 2006 +0000
+++ b/gmyth/src/gmyth_livetv.h	Wed Dec 06 13:50:55 2006 +0000
@@ -34,6 +34,7 @@
 #include "gmyth_tvchain.h"
 #include "gmyth_common.h"
 #include "gmyth_backendinfo.h"
+#include "gmyth_monitor_handler.h"
 
 #include "gmyth_file_transfer.h"
 
@@ -68,6 +69,8 @@
 	GMythRecorder *recorder;
 	GMythTVChain *tvchain;
 	GMythProgramInfo *proginfo;
+	
+	GMythMonitorHandler *monitor;
 
 	gboolean is_livetv;
 
diff -r 0ef699880d59 -r a43fed947ec8 gmyth/src/gmyth_monitor_handler.c
--- a/gmyth/src/gmyth_monitor_handler.c	Wed Dec 06 13:33:22 2006 +0000
+++ b/gmyth/src/gmyth_monitor_handler.c	Wed Dec 06 13:50:55 2006 +0000
@@ -108,11 +108,26 @@
 gmyth_monitor_handler_class_init (GMythMonitorHandlerClass *klass)
 {
   GObjectClass *gobject_class;
+  GMythMonitorHandlerClass *gmonitor_class;
 
   gobject_class = (GObjectClass *) klass;
+  gmonitor_class = (GMythMonitorHandlerClass *) gobject_class;
 
   gobject_class->dispose  = gmyth_monitor_handler_dispose;
   gobject_class->finalize = gmyth_monitor_handler_finalize;
+  
+	gmonitor_class->backend_events_handler_signal_id = 
+	  g_signal_newv ("backend-events-handler",
+	                 G_TYPE_FROM_CLASS (gmonitor_class),
+	                 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
+	                 NULL /* class closure */,
+	                 NULL /* accumulator */,
+	                 NULL /* accu_data */,
+	                 g_cclosure_marshal_VOID__VOID,
+	                 G_TYPE_NONE /* return_type */,
+	                 0     /* n_params */,
+	                 NULL  /* param_types */);
+
 }
 
 static void
@@ -253,7 +268,7 @@
   static guint count = 0;
   
   GIOChannel *io_channel = gmyth_socket->sd_io_ch;
-  GIOCondition condition = g_io_channel_get_buffer_condition( io_channel );
+  //GIOCondition condition = g_io_channel_get_buffer_condition( io_channel );
   
   GMythStringList *strlist = NULL;
   
@@ -271,50 +286,7 @@
   	++count;
   	
   	gmyth_debug ("%d - Listening on Monitor socket...!\n", count);
-	  /*	
-	  condition = g_io_channel_get_buffer_condition( io_channel );	  
-	  //while ( !has_io_access ) 
-	  //	g_cond_wait( io_watcher_cond, mutex );
-	  	
-	  //myth_control_acquire_context (TRUE);  
-	  
-	  if (condition & G_IO_HUP) {
-	    gmyth_debug ("Read end of pipe died!\n");
-	    ret = FALSE;
-	    //goto clean_up;
-	  }
-	    
-	  //if ( ( condition & G_IO_IN ) != 0 ) {
-	  	byte_array = g_byte_array_new();
-	  	io_status = g_io_channel_set_encoding( io_channel, NULL, &error );
-	  	guint buffer_size = GMYTHTV_BUFFER_SIZE;
-	    do 
-	    {
-	      trash = g_new0( gchar, buffer_size );
 	
-	      io_status = g_io_channel_read_chars( io_channel, trash, 
-	      		buffer_size, &len, &error);
-	
-	      gmyth_debug ( "[%s] Received data buffer from IO binary channel... %d bytes gone!\n", 
-	      		__FUNCTION__, len );
-	      		
-	      recv += len;
-	      
-	      byte_array = g_byte_array_append( byte_array, (const guint8*)trash, len );
-	      
-	      if ( trash != NULL )
-	      	g_free( trash );
-	      	
-	      io_cond = g_io_channel_get_buffer_condition( io_channel );
-	
-	    } while ( ( io_cond & G_IO_IN ) != 0 );
-	    
-	    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]" );
-	    
-	  //}
-	   */	   
-  	//io_status = g_io_channel_set_encoding( io_channel, NULL, &error );
-  	  	
   	guint buffer_size = GMYTHTV_BUFFER_SIZE;
     do 
     {
@@ -350,15 +322,10 @@
     gmyth_debug ("\n\n\n[%s]\tMONITOR EVENT: Read %d bytes: %s\n\n\n", __FUNCTION__, recv, 
     					strlist != NULL && strlist->glist != NULL ? 
     					gmyth_string_list_get_char_array( strlist, 0 ) : "[no event data]" );
-/*
-	  if ( byte_array != NULL ) {
-	  	g_byte_array_free( byte_array, TRUE );
-	  	byte_array = NULL;
-	  }
-	*/  
+
 	  g_usleep( 300 );
 	  
-  }
+  } /* main GThread while */
   
   if ( io_status == G_IO_STATUS_ERROR ) {
     //gmyth_debug ("[%s] Error reading: %s\n", __FUNCTION__, error != NULL ? error->message : "" );
@@ -377,14 +344,6 @@
   if (strlist!=NULL)
   	g_object_unref( strlist );
 
-/*
-  if ( byte_array != NULL ) {
-  	g_byte_array_free( byte_array, TRUE );
-  	byte_array = NULL;
-  }
-  */
-  //return ret;
-
 }
 
 static gboolean
@@ -469,9 +428,9 @@
 	if (!g_thread_supported () ) 	g_thread_init (NULL);
 	
   monitor_th = g_thread_pool_new( (GFunc)gmyth_monitor_handler_listener, 
-  					monitor->event_sock, 3, FALSE, NULL );
+  					monitor->event_sock, 3, TRUE, NULL );
   					
-  g_thread_pool_push( monitor_th, monitor->event_sock, NULL ); 
+  //g_thread_pool_push( monitor_th, monitor->event_sock, NULL ); 
   					
   //if ( ( ret = g_thread_join( monitor_th ) ) == FALSE )
   if ( monitor_th != NULL )
diff -r 0ef699880d59 -r a43fed947ec8 gmyth/src/gmyth_monitor_handler.h
--- a/gmyth/src/gmyth_monitor_handler.h	Wed Dec 06 13:33:22 2006 +0000
+++ b/gmyth/src/gmyth_monitor_handler.h	Wed Dec 06 13:50:55 2006 +0000
@@ -62,8 +62,11 @@
 {
 	GObjectClass parent_class;
 
-	/* callbacks */
-	/* no one for now */
+	/* callbacks */	
+	guint backend_events_handler_signal_id;
+
+  /* signal default handlers */
+  void (*backend_events_handler) (GMythMonitorHandler *self);
 };
 
 struct _GMythMonitorHandler