# HG changeset patch
# User rosfran
# Date 1171488529 0
# Node ID 6d5596b9eb95e98273d4e6b9b4b57300fe46a740
# Parent  c7d4b123ac3100cf2842ba3579340c91070ef176
[svn r362] Some fixes in the GIOWatcher clean-ups, and changed the API version number.

diff -r c7d4b123ac31 -r 6d5596b9eb95 gmyth/configure.ac
--- a/gmyth/configure.ac	Tue Feb 13 23:22:07 2007 +0000
+++ b/gmyth/configure.ac	Wed Feb 14 21:28:49 2007 +0000
@@ -10,7 +10,7 @@
 
 dnl when going to/from release please set the nano (fourth number) right !
 dnl releases only do Wall, SVN and prerelease does Werror too
-AS_VERSION(gmyth, GMYTH, 0, 1, 0, 3, GMYTH_SVN="no", GMYTH_SVN="yes")
+AS_VERSION(gmyth, GMYTH, 0, 1, 0, 8, GMYTH_SVN="no", GMYTH_SVN="yes")
 GMYTH_MAJORMINOR=$GMYTH_MAJOR_VERSION.$GMYTH_MINOR_VERSION
 AC_SUBST(GMYTH_MAJORMINOR)
 
diff -r c7d4b123ac31 -r 6d5596b9eb95 gmyth/src/gmyth_monitor_handler.c
--- a/gmyth/src/gmyth_monitor_handler.c	Tue Feb 13 23:22:07 2007 +0000
+++ b/gmyth/src/gmyth_monitor_handler.c	Wed Feb 14 21:28:49 2007 +0000
@@ -137,14 +137,14 @@
   monitor->port = 0;
   monitor->actual_index = 0;
   
-  monitor->allow_msgs_listener = TRUE;
+  monitor->allow_msgs_listener = FALSE;
   
-  //monitor->backend_msgs = g_hash_table_new( g_int_hash, g_int_equal );
+  /* monitor->backend_msgs = g_hash_table_new( g_int_hash, g_int_equal ); */
     
   /* it is used for signalizing the event socket consumer thread */
-  //io_watcher_cond = g_cond_new();
+  monitor->mutex = g_mutex_new();
   
-  monitor->monitor_th = NULL;
+  monitor->sid_io_watch = -1;
   
   monitor->gmyth_monitor_handler_listener = gmyth_monitor_handler_listener;
 }
@@ -153,28 +153,35 @@
 gmyth_monitor_handler_dispose  (GObject *object)
 {
 	GMythMonitorHandler *monitor = GMYTH_MONITOR_HANDLER (object);
+	GSource* source = NULL;
+	GMainContext* context = g_main_context_default();
   
   gmyth_monitor_handler_close(monitor);
 
 	monitor->allow_msgs_listener = FALSE;
-	
-	/*
-  if ( monitor->mutex != NULL )  
+
+	if ( context != NULL && monitor->sid_io_watch != -1 )
+	{
+		g_main_context_acquire( context );
+		
+		source = g_main_context_find_source_by_id( g_main_context_default(), 
+						monitor->sid_io_watch );
+		
+		if ( source != NULL )
+		{
+			g_source_destroy( source );
+		}
+		
+		g_main_context_release( context );
+		
+	}
+
+  if ( monitor->mutex != NULL )
   {
-  	g_mutex_unlock( monitor->mutex ); 
+  	//g_mutex_unlock( monitor->mutex ); 
   	g_mutex_free( monitor->mutex );
   	monitor->mutex = NULL;
   }
-  */
-
-	if ( monitor->monitor_th != NULL )  
-  {
-  	g_thread_pool_free( monitor->monitor_th, TRUE, FALSE );
-  	//g_thread_exit( monitor->monitor_th );
-  	//if ( monitor->monitor_th != NULL )
-  	//	g_object_unref( monitor->monitor_th );
-  	monitor->monitor_th = NULL;
-  }
 	
   if ( monitor->event_sock != NULL )  
   {
@@ -182,7 +189,7 @@
   	/*gmyth_socket_close_connection( monitor->event_sock );*/  	
   	monitor->event_sock = NULL;
   }
-  
+
   if ( monitor->hostname != NULL )  
   {
   	g_free( monitor->hostname );
@@ -248,7 +255,7 @@
   	ret = FALSE;
   */
   	
-  //g_mutex_lock( monitor->mutex );
+  g_mutex_lock( monitor->mutex );
   
   return ret;
   
@@ -260,7 +267,7 @@
 	
 	gboolean ret = TRUE;
     
-  //g_mutex_unlock( monitor->mutex );
+  g_mutex_unlock( monitor->mutex );
   
 	//g_main_context_release( io_watcher_context );
   
@@ -360,13 +367,14 @@
 	    	} else if ( g_strstr_len( *back_msg_action, strlen( *back_msg_action ), "DONE_RECORDING" ) ) {
 	    		gmyth_debug( "MONITOR: message type == GMYTH_BACKEND_DONE_RECORDING, msg = %s", *back_msg_action );
 	    		msg_type = GMYTH_BACKEND_DONE_RECORDING;
-	    	}    	  	
+	    	}
 	
 	      //g_hash_table_insert ( monitor->backend_msgs,
 	      //                       &(monitor->actual_index), *back_msg_action );
 		    
-		  } // if
-		}
+		  } /*  if  */
+		  
+		} /* if */
 		
 		if ( back_msg != NULL )
 		{
@@ -397,12 +405,8 @@
 	gmyth_debug( "Backend message event: %s --- ", str->str );
 }
 
-//static void
-//gmyth_monitor_handler_listener (GMythMonitorHandler *monitor, gpointer user_data)
 gboolean
 gmyth_monitor_handler_listener( GIOChannel *io_channel, GIOCondition io_cond, gpointer data )
-//gboolean* gmyth_monitor_handler_listener( GMythMonitorHandler *monitor )
-//void gmyth_monitor_handler_listener (GMythMonitorHandler *monitor, gpointer user_data)
 {
 	GMythMonitorHandler *monitor = (GMythMonitorHandler*)data; 
   GIOStatus io_status;
@@ -494,8 +498,6 @@
     
     gmyth_debug ("\tMONITOR EVENT: Read %d bytes\n", recv );
 
-	  //g_usleep( 300 );
-	  
   } /* main GThread while */
   
   myth_control_release_context (monitor);
@@ -536,46 +538,27 @@
 gmyth_monitor_handler_setup( GMythMonitorHandler *monitor, 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 );
-
-  //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, 
+  	
+  	monitor->allow_msgs_listener = TRUE;
+  	
+    monitor->sid_io_watch = g_io_add_watch( channel, G_IO_IN | G_IO_HUP, 
     					(GIOFunc)gmyth_monitor_handler_listener, monitor );
   } else {
   	*ret = FALSE;
   	goto cleanup;
   }
 
-  //g_source_set_callback ( source, (GSourceFunc)gmyth_monitor_handler_listener, NULL, NULL );
-
-  //g_source_attach( source, io_watcher_context );
-  
-  //if (NULL == source){
-  if (src_id < 0){
+  if (monitor->sid_io_watch < 0){
     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 );
-    
-  //if ( io_watcher_context != NULL )
-  //  g_main_context_unref( io_watcher_context );
-
-  //if ( loop != NULL )
-  //  g_main_loop_unref( loop );
     
   return ret;
   
@@ -587,20 +570,6 @@
 	gboolean *ret = g_new0( gboolean, 1 ); 
 	*ret = TRUE;	
 	
-	/*if (!g_thread_supported () ) 	g_thread_init (NULL);*/
-	/*
-  monitor->monitor_th = g_thread_pool_new( (GThreadFunc)gmyth_monitor_handler_listener, 
-  					monitor, TRUE, NULL );
- */
- 			
- /*
- 
-  monitor->monitor_th = g_thread_pool_new( (GFunc)gmyth_monitor_handler_listener, 
-  					monitor, 3, TRUE, NULL );
-  g_thread_pool_push( monitor->monitor_th, monitor, NULL ); 
-  					*/
-  //if ( ( ret = g_thread_join( monitor_th ) ) == FALSE )
-  //if ( monitor->monitor_th != NULL )
   ret = gmyth_monitor_handler_setup( monitor, monitor->event_sock->sd_io_ch ); 
   if ( *ret )
   {
@@ -613,8 +582,6 @@
   	*ret = FALSE;
   }
 
-//cleanup:
-    
   gmyth_debug( "[%s] Watch listener function over the IO control channel? %s!!!\n", 
   			__FUNCTION__, ( *ret == TRUE ? "YES" : "NO" ) );
     
diff -r c7d4b123ac31 -r 6d5596b9eb95 gmyth/src/gmyth_monitor_handler.h
--- a/gmyth/src/gmyth_monitor_handler.h	Tue Feb 13 23:22:07 2007 +0000
+++ b/gmyth/src/gmyth_monitor_handler.h	Wed Feb 14 21:28:49 2007 +0000
@@ -85,10 +85,6 @@
 	/* socket descriptors */
 	GMythSocket *event_sock;
 	
-	GThreadPool *monitor_th;
-	
-	//gboolean* (*gmyth_monitor_handler_listener)( GMythMonitorHandler *monitor );
-	//void (*gmyth_monitor_handler_listener)( GMythMonitorHandler *monitor, gpointer user_data );
 	gboolean (*gmyth_monitor_handler_listener) ( GIOChannel *io_channel, 
 						GIOCondition io_cond, gpointer data );
 	
@@ -102,7 +98,9 @@
 	/* stores the messages coming from the backend */
 	GHashTable *backend_msgs;	
 	
-	//GMutex	*mutex;
+	GMutex	*mutex;
+	
+	guint sid_io_watch;
 
 };