# HG changeset patch
# User rosfran
# Date 1165355855 0
# Node ID 109630cf20ef100eca84dc6428f12721863c5ddb
# Parent  c57ba96bda159a66c2b3d5f3c9d9b5266f3c8df4
[svn r199] Fixed problem with COxa.

diff -r c57ba96bda15 -r 109630cf20ef gmyth/src/gmyth_monitor_handler.c
--- a/gmyth/src/gmyth_monitor_handler.c	Tue Dec 05 21:56:57 2006 +0000
+++ b/gmyth/src/gmyth_monitor_handler.c	Tue Dec 05 21:57:35 2006 +0000
@@ -72,7 +72,7 @@
 
 GMainContext *io_watcher_context = NULL;
 
-GThread *monitor_th = NULL;
+GThreadPool *monitor_th = NULL;
 
 enum myth_sock_types {
   GMYTH_PLAYBACK_TYPE = 0,
@@ -82,8 +82,10 @@
 };
 
 //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, GIOCondition condition, 
+//				gpointer data );
+
+static void myth_control_sock_listener( GIOChannel *io_channel, gboolean* ret );
 
 static GMutex*				mutex 					 = NULL;
 
@@ -169,8 +171,8 @@
   
   if ( monitor_th != NULL )  
   {
-  	g_thread_join( monitor_th );
-  	g_object_unref( monitor_th );
+  	g_thread_pool_free( monitor_th, TRUE, FALSE );
+  	//g_object_unref( monitor_th );
   	monitor_th = NULL;
   }
 
@@ -224,37 +226,42 @@
 
 }
 
-static gboolean
-myth_control_sock_listener( GIOChannel *io_channel, GIOCondition condition, gpointer data )
-//myth_control_sock_listener( GIOChannel *io_channel )
+static void
+//myth_control_sock_listener( GIOChannel *io_channel, GIOCondition condition, gpointer data )
+myth_control_sock_listener( GIOChannel *io_channel, gboolean* ret )
 {
 
   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;
+  ret = g_new0( gboolean, 1 );
+  *ret = TRUE;
+  //gboolean ret = TRUE;
   gsize len = 0;
   
+  static guint count = 0;  
+  
+  
   //GMythMonitorHandler *monitor = (GMythMonitorHandler*)data;
   
   //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) {
+  	++count;
+  	
+  	gmyth_debug ("%d - Listening on Monitor socket...!\n", count);
 	  	
-	  //condition = g_io_channel_get_buffer_condition( io_channel );
-	  
+	  condition = g_io_channel_get_buffer_condition( io_channel );	  
 	  //while ( !has_io_access ) 
 	  //	g_cond_wait( io_watcher_cond, mutex );
 	  	
@@ -266,7 +273,7 @@
 	    //goto clean_up;
 	  }
 	    
-	  if ( ( condition & G_IO_IN ) != 0 ) {
+	  //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;
@@ -290,8 +297,10 @@
 	      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]" );
-	  }	  
+	    
+	  //}	  
 	  
 	  if ( byte_array != NULL ) {
 	  	g_byte_array_free( byte_array, TRUE );
@@ -300,10 +309,11 @@
 	  
 	  g_usleep( 300 );
 	  
-  //}
-  if ( io_status == G_IO_STATUS_ERROR ) {
+  }
+  
+  if ( io_status == G_IO_STATUS_ERROR || error != NULL ) {
     gmyth_debug ("[%s] Error reading: %s\n", __FUNCTION__, error != NULL ? error->message : "" );
-   	ret = FALSE;
+   	*ret = FALSE;
    	goto clean_up;   	
   }
   
@@ -313,8 +323,13 @@
   //monitor->actual_index += recv;
 
 clean_up:
+
+  if ( byte_array != NULL ) {
+  	g_byte_array_free( byte_array, TRUE );
+  	byte_array = NULL;
+  }
   
-  return ret;
+  //return ret;
 
 }
 
@@ -347,16 +362,19 @@
 myth_control_sock_setup( GIOChannel *channel )
 {
 	gboolean *ret = g_new0( gboolean, 1 );
+	//guint src_id = 0;
 	
 	*ret = TRUE;
 	
   io_watcher_context = g_main_context_default();
-  //GMainLoop *loop = g_main_loop_new( io_watcher_context, TRUE );
+  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 );
+    //src_id = g_io_add_watch( channel, G_IO_IN | G_IO_HUP, 
+    //					(GSourceFunc)myth_control_sock_listener, NULL );
   } else {
   	*ret = FALSE;
   	goto cleanup;
@@ -366,18 +384,24 @@
 
   g_source_attach( source, io_watcher_context );
   
-  if (source==NULL) {
+  if (NULL == source){
     gmyth_debug( "[%s] Error adding watch listener function to the IO control channel!\n", __FUNCTION__ );
     *ret = FALSE;
     goto cleanup;
   }
   
-  //g_main_loop_run( loop );
+  g_main_loop_run( loop );
   
 cleanup:
-  //if ( source != NULL )
-  //  g_source_unref( source );
-  
+  if ( source != NULL )
+    g_source_unref( source );
+    
+  //if ( io_watcher_context != NULL )
+  //  g_main_context_unref( io_watcher_context );
+
+  //if ( loop != NULL )
+  //  g_main_loop_unref( loop );
+    
   return ret;
   
 }
@@ -388,28 +412,29 @@
 	gboolean *ret = g_new0( gboolean, 1 ); 
 	*ret = TRUE;	
 	
-	if (!g_thread_supported ()) g_thread_init (NULL);
+	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 );
+  monitor_th = g_thread_pool_new( (GFunc)myth_control_sock_listener, 
+  					monitor->event_sock->sd_io_ch, 3, FALSE, NULL );
   					
-  if ( ( ret = g_thread_join( monitor_th ) ) == FALSE )
+  //if ( ( ret = g_thread_join( monitor_th ) ) == FALSE )
+  if ( monitor_th != NULL )
   {
-  	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( ) );
+  	*ret = TRUE;  	  	
+  } else {
+  	gmyth_debug ( "\n[%s]\tERROR! Coudn't start listener on the MONITOR event socket...[thread location = %p]\n", 
+  				__FUNCTION__, g_thread_self( ) );
+  	*ret = FALSE;
   }
 
-cleanup:
-  //if ( source != NULL )
-  //  g_source_unref( source );
+//cleanup:
     
   gmyth_debug( "[%s] Watch listener function over the IO control channel? %s!!!\n", 
   			__FUNCTION__, ( *ret == TRUE ? "YES" : "NO" ) );
     
-    return TRUE;
+    return *ret;
 }
 
 void