branches/gmyth-0.1b/src/gmyth_monitor_handler.c
branchtrunk
changeset 329 818deb9ae65d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/branches/gmyth-0.1b/src/gmyth_monitor_handler.c	Wed Feb 07 00:03:05 2007 +0000
     1.3 @@ -0,0 +1,595 @@
     1.4 +/**
     1.5 + * GMyth Library
     1.6 + *
     1.7 + * @file gmyth/gmyth_monitor_handler.c
     1.8 + * 
     1.9 + * @brief <p> GMythMonitorHandler deals with the streaming media events remote/local
    1.10 + * that are sent to the MythTV frontend.
    1.11 + *
    1.12 + * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
    1.13 + * @author Rosfran Lins Borges <rosfran.borges@indt.org.br>
    1.14 + *
    1.15 + *//*
    1.16 + * 
    1.17 + * This program is free software; you can redistribute it and/or modify
    1.18 + * it under the terms of the GNU Lesser General Public License as published by
    1.19 + * the Free Software Foundation; either version 2 of the License, or
    1.20 + * (at your option) any later version.
    1.21 + *
    1.22 + * This program is distributed in the hope that it will be useful,
    1.23 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.24 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.25 + * GNU General Public License for more details.
    1.26 + *
    1.27 + * You should have received a copy of the GNU Lesser General Public License
    1.28 + * along with this program; if not, write to the Free Software
    1.29 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.30 + *
    1.31 + * GStreamer MythTV plug-in properties:
    1.32 + * - location (backend server hostname/URL) [ex.: myth://192.168.1.73:28722/1000_1092091.nuv]
    1.33 + * - path (qurl - remote file to be opened)
    1.34 + * - port number *   
    1.35 + */
    1.36 +
    1.37 +#ifdef HAVE_CONFIG_H
    1.38 +#include "config.h"
    1.39 +#endif
    1.40 +
    1.41 +#include <unistd.h>
    1.42 +#include <glib.h>
    1.43 +#include <arpa/inet.h>
    1.44 +#include <sys/types.h>
    1.45 +#include <sys/socket.h>
    1.46 +#include <netdb.h>
    1.47 +#include <errno.h>
    1.48 +#include <stdlib.h>
    1.49 +#include <assert.h>
    1.50 +
    1.51 +#include "gmyth_marshal.h"
    1.52 +
    1.53 +#include "gmyth_monitor_handler.h"
    1.54 +
    1.55 +#include "gmyth.h"
    1.56 + 
    1.57 +#define GMYTHTV_QUERY_HEADER		"QUERY_FILETRANSFER "
    1.58 +
    1.59 +#define GMYTHTV_VERSION							30
    1.60 +
    1.61 +#define GMYTHTV_TRANSFER_MAX_WAITS	700
    1.62 +
    1.63 +#define GMYTHTV_BUFFER_SIZE					8*1024
    1.64 +
    1.65 +#ifdef GMYTHTV_ENABLE_DEBUG
    1.66 +#define GMYTHTV_ENABLE_DEBUG				1
    1.67 +#else
    1.68 +#undef GMYTHTV_ENABLE_DEBUG
    1.69 +#endif
    1.70 +
    1.71 +/* this NDEBUG is to maintain compatibility with GMyth library */
    1.72 +#ifndef NDEBUG
    1.73 +#define GMYTHTV_ENABLE_DEBUG				1
    1.74 +#endif
    1.75 +
    1.76 +//GMainContext *io_watcher_context = NULL;
    1.77 +
    1.78 +//GThread *monitor_th = NULL;
    1.79 +
    1.80 +//static gboolean* myth_control_sock_listener( GIOChannel *io_channel );
    1.81 +//static gboolean gmyth_monitor_handler_listener( GIOChannel *io_channel, 
    1.82 +//						GIOCondition condition, gpointer data );
    1.83 +
    1.84 +//gboolean* gmyth_monitor_handler_listener( GMythMonitorHandler *monitor );
    1.85 +
    1.86 +void gmyth_monitor_handler_listener( GMythMonitorHandler *monitor, gpointer user_data );
    1.87 +
    1.88 +static void gmyth_monitor_handler_default_listener( GMythMonitorHandler *monitor, gint msg_code, gchar* message );
    1.89 +
    1.90 +static GMutex*				mutex 					 = NULL;
    1.91 +
    1.92 +//static GCond*					io_watcher_cond  = NULL;
    1.93 +
    1.94 +static void gmyth_monitor_handler_class_init          (GMythMonitorHandlerClass *klass);
    1.95 +static void gmyth_monitor_handler_init                (GMythMonitorHandler *object);
    1.96 +
    1.97 +static void gmyth_monitor_handler_dispose  (GObject *object);
    1.98 +static void gmyth_monitor_handler_finalize (GObject *object);
    1.99 +
   1.100 +static gboolean gmyth_connect_to_backend_monitor (GMythMonitorHandler *monitor);
   1.101 +
   1.102 +void gmyth_monitor_handler_close( GMythMonitorHandler *monitor );
   1.103 +
   1.104 +G_DEFINE_TYPE(GMythMonitorHandler, gmyth_monitor_handler, G_TYPE_OBJECT)
   1.105 +
   1.106 +static void
   1.107 +gmyth_monitor_handler_class_init (GMythMonitorHandlerClass *klass)
   1.108 +{
   1.109 +  GObjectClass *gobject_class;
   1.110 +  GMythMonitorHandlerClass *gmonitor_class;
   1.111 +
   1.112 +  gobject_class = (GObjectClass *) klass;
   1.113 +  gmonitor_class = (GMythMonitorHandlerClass *) gobject_class;
   1.114 +
   1.115 +  gobject_class->dispose  = gmyth_monitor_handler_dispose;
   1.116 +  gobject_class->finalize = gmyth_monitor_handler_finalize;
   1.117 +  
   1.118 +	gmonitor_class->backend_events_handler_signal_id = 
   1.119 +		  g_signal_new ("backend-events-handler",
   1.120 +		                 G_TYPE_FROM_CLASS (gmonitor_class),
   1.121 +		                 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
   1.122 +		                 0,
   1.123 +										 NULL,
   1.124 +										 NULL,
   1.125 +										 gmyth_marshal_VOID__INT_STRING,
   1.126 +										 G_TYPE_NONE,
   1.127 +										 2,
   1.128 +										 G_TYPE_INT,
   1.129 +										 G_TYPE_STRING);
   1.130 +										 
   1.131 +	gmonitor_class->backend_events_handler = gmyth_monitor_handler_default_listener;
   1.132 +
   1.133 +}
   1.134 +
   1.135 +static void
   1.136 +gmyth_monitor_handler_init (GMythMonitorHandler *monitor)
   1.137 +{ 
   1.138 +  g_return_if_fail( monitor != NULL );
   1.139 +
   1.140 +  monitor->event_sock = NULL;
   1.141 +  monitor->hostname = NULL;
   1.142 +  monitor->port = 0;
   1.143 +  monitor->actual_index = 0;
   1.144 +  
   1.145 +  monitor->allow_msgs_listener = TRUE;
   1.146 +  
   1.147 +  //monitor->backend_msgs = g_hash_table_new( g_int_hash, g_int_equal );
   1.148 +    
   1.149 +  /* it is used for signalizing the event socket consumer thread */
   1.150 +  //io_watcher_cond = g_cond_new();
   1.151 +  
   1.152 +  /* mutex to control access to the event socket consumer thread */
   1.153 +  //mutex = g_mutex_new();
   1.154 +  
   1.155 +  monitor->monitor_th = NULL;
   1.156 +  
   1.157 +  monitor->gmyth_monitor_handler_listener = gmyth_monitor_handler_listener;
   1.158 +}
   1.159 +
   1.160 +static void
   1.161 +gmyth_monitor_handler_dispose  (GObject *object)
   1.162 +{
   1.163 +	
   1.164 +	GMythMonitorHandler *monitor = GMYTH_MONITOR_HANDLER (object);
   1.165 +	
   1.166 +	monitor->allow_msgs_listener = FALSE;
   1.167 +	
   1.168 +	if ( monitor->monitor_th != NULL )  
   1.169 +  {
   1.170 +  	g_thread_pool_free( monitor->monitor_th, TRUE, FALSE );
   1.171 +  	//g_thread_exit( monitor->monitor_th );
   1.172 +  	if ( monitor->monitor_th != NULL )
   1.173 +  		g_object_unref( monitor->monitor_th );
   1.174 +  	monitor->monitor_th = NULL;
   1.175 +  }
   1.176 +	
   1.177 +  if ( monitor->event_sock != NULL )  
   1.178 +  {
   1.179 +  	g_object_unref( monitor->event_sock );
   1.180 +  	monitor->event_sock = NULL;
   1.181 +  }
   1.182 +  
   1.183 +  if ( monitor->hostname != NULL )  
   1.184 +  {
   1.185 +  	g_free( monitor->hostname );
   1.186 +  	monitor->hostname = NULL;
   1.187 +  }
   1.188 +
   1.189 +  if ( monitor->backend_msgs != NULL )
   1.190 +  {
   1.191 +  	g_hash_table_destroy ( monitor->backend_msgs );
   1.192 +  	monitor->backend_msgs = NULL;
   1.193 +  }
   1.194 +  
   1.195 +  if ( mutex != NULL )  
   1.196 +  {
   1.197 +  	g_mutex_free( mutex );
   1.198 +  	mutex = NULL;
   1.199 +  }
   1.200 +  
   1.201 +  /*
   1.202 +  if ( io_watcher_cond != NULL )  
   1.203 +  {
   1.204 +  	g_cond_free( io_watcher_cond );
   1.205 +  	io_watcher_cond = NULL;
   1.206 +  }
   1.207 +  */
   1.208 +  
   1.209 +  G_OBJECT_CLASS (gmyth_monitor_handler_parent_class)->dispose (object);
   1.210 +}
   1.211 +
   1.212 +static void
   1.213 +gmyth_monitor_handler_finalize (GObject *object)
   1.214 +{
   1.215 +  g_signal_handlers_destroy (object);
   1.216 +
   1.217 +  G_OBJECT_CLASS (gmyth_monitor_handler_parent_class)->finalize (object);
   1.218 +}
   1.219 +
   1.220 +// fixme: do we need the card_id????
   1.221 +GMythMonitorHandler*
   1.222 +gmyth_monitor_handler_new ( void )
   1.223 +{
   1.224 +  GMythMonitorHandler *monitor = GMYTH_MONITOR_HANDLER ( g_object_new ( GMYTH_MONITOR_HANDLER_TYPE, 
   1.225 +  					FALSE ) );
   1.226 +				
   1.227 +  return monitor;
   1.228 +}
   1.229 +
   1.230 +static gboolean 
   1.231 +myth_control_acquire_context( gboolean do_wait ) 
   1.232 +{
   1.233 +	
   1.234 +	gboolean ret = TRUE;	
   1.235 +	//guint max_iter = 50;
   1.236 +	
   1.237 +	//g_mutex_lock( mutex );
   1.238 +	
   1.239 +  //while ( !has_io_access ) 
   1.240 +  //	g_cond_wait( io_watcher_cond, mutex );
   1.241 +  	
   1.242 +  //has_io_access = FALSE;
   1.243 +  /*
   1.244 +  if ( do_wait ) {
   1.245 +  	while ( --max_iter > 0 && !g_main_context_wait( io_watcher_context, io_watcher_cond, mutex ) )
   1.246 +  		ret = FALSE;
   1.247 +  } else if ( !g_main_context_acquire( io_watcher_context ) )
   1.248 +  	ret = FALSE;
   1.249 +  */
   1.250 +  	
   1.251 +  //g_static_mutex_lock( &st_mutex );
   1.252 +  
   1.253 +  return ret;
   1.254 +  
   1.255 +}
   1.256 +
   1.257 +static gboolean 
   1.258 +myth_control_release_context( ) 
   1.259 +{
   1.260 +	
   1.261 +	gboolean ret = TRUE;
   1.262 +    
   1.263 +  //g_static_mutex_unlock( &st_mutex );
   1.264 +  
   1.265 +	//g_main_context_release( io_watcher_context );
   1.266 +  
   1.267 +  //g_main_context_wakeup( io_watcher_context );
   1.268 +  
   1.269 +  //has_io_access = TRUE;
   1.270 +
   1.271 +  //g_cond_broadcast( io_watcher_cond );
   1.272 +  
   1.273 +  //g_mutex_unlock( mutex );  
   1.274 + 
   1.275 +  return ret;
   1.276 +  
   1.277 +}
   1.278 +
   1.279 +gboolean
   1.280 +gmyth_monitor_handler_open (GMythMonitorHandler *monitor, const gchar *hostname, gint port)
   1.281 +{
   1.282 +  gboolean ret = TRUE;
   1.283 +  
   1.284 +  g_return_val_if_fail( hostname != NULL, FALSE );
   1.285 +
   1.286 +  if (monitor->hostname != NULL) {
   1.287 +    g_free (monitor->hostname);
   1.288 +    monitor->hostname = NULL;
   1.289 +  }
   1.290 +
   1.291 +  monitor->hostname = g_strdup( hostname );
   1.292 +  monitor->port = port;
   1.293 +
   1.294 +  gmyth_debug ("Monitor event socket --- hostname: %s, port %d\n", monitor->hostname, monitor->port);
   1.295 +  
   1.296 +  /* configure the event socket */
   1.297 +  if ( NULL == monitor->event_sock ) { 
   1.298 +    if (!gmyth_connect_to_backend_monitor (monitor)) {
   1.299 +      g_printerr( "Connection to backend failed (Event Socket).\n" );
   1.300 +      ret = FALSE;
   1.301 +    }
   1.302 +  } else {
   1.303 +    g_warning("Remote monitor event socket already created.\n");
   1.304 +  }
   1.305 +
   1.306 +  return ret;
   1.307 +
   1.308 +}
   1.309 +
   1.310 +static gint
   1.311 +gmyth_monitor_handler_is_backend_message( GMythMonitorHandler *monitor,
   1.312 +                        GMythStringList* strlist, gchar **back_msg_action )
   1.313 +{
   1.314 +	gint msg_type = GMYTH_BACKEND_NO_MESSAGE;
   1.315 +	GString *back_msg = NULL;
   1.316 +	
   1.317 +	back_msg = gmyth_string_list_get_string( strlist, 0 );
   1.318 +	if ( back_msg != NULL && back_msg->str != NULL &&
   1.319 +	                                strstr( back_msg->str, "BACKEND" ) != NULL )
   1.320 +	{
   1.321 +		gmyth_debug( "MONITOR HANDLER - Received backend message = %s", back_msg->str );
   1.322 +  	*back_msg_action = gmyth_string_list_get_char_array( strlist, 1 );
   1.323 +  	
   1.324 +  	if ( back_msg_action != NULL )
   1.325 +  	{	        	
   1.326 +  	
   1.327 +    	if ( g_strstr_len( *back_msg_action, strlen( *back_msg_action ), "LIVETV_CHAIN" ) ||
   1.328 +    			g_strstr_len( *back_msg_action, strlen( *back_msg_action ), "RECORDING_LIST_CHANGE" ) || 
   1.329 +    			g_strstr_len( *back_msg_action, strlen( *back_msg_action ), "SCHEDULE_CHANGE" ) ||
   1.330 +    			g_strstr_len( *back_msg_action, strlen( *back_msg_action ), "LIVETV_WATCH" ) )
   1.331 +    	{
   1.332 +    		gmyth_debug( "MONITOR: message type == GMYTH_BACKEND_PROGRAM_INFO_CHANGED, msg = %s", *back_msg_action );
   1.333 +    		msg_type = GMYTH_BACKEND_PROGRAM_INFO_CHANGED;
   1.334 +    	} else if ( g_strstr_len( *back_msg_action, strlen( *back_msg_action ), "DONE_RECORDING" ) ) {
   1.335 +    		gmyth_debug( "MONITOR: message type == GMYTH_BACKEND_DONE_RECORDING, msg = %s", *back_msg_action );
   1.336 +    		msg_type = GMYTH_BACKEND_DONE_RECORDING;
   1.337 +    	}    	  	
   1.338 +
   1.339 +      //g_hash_table_insert ( monitor->backend_msgs,
   1.340 +      //                       &(monitor->actual_index), *back_msg_action );
   1.341 +	    
   1.342 +	  } // if
   1.343 +	}
   1.344 +	
   1.345 +	if ( back_msg != NULL )
   1.346 +	{
   1.347 +		g_string_free( back_msg, TRUE );
   1.348 +		back_msg = NULL;        	
   1.349 +	}
   1.350 +	
   1.351 +	return msg_type;
   1.352 +
   1.353 +}
   1.354 +
   1.355 +static void
   1.356 +gmyth_monitor_handler_default_listener( GMythMonitorHandler *monitor, gint msg_code, gchar* message )
   1.357 +{
   1.358 +	//assert( message!= NULL );  
   1.359 +	gmyth_debug( "DEFAULT Signal handler ( msg = %s, code = %d )\n", 
   1.360 +				message, msg_code );
   1.361 +}
   1.362 +
   1.363 +static void
   1.364 +gmyth_monitor_handler_print( GString *str, gpointer ptr )
   1.365 +{
   1.366 +	gmyth_debug( "Backend message event: %s --- ", str->str );
   1.367 +}
   1.368 +
   1.369 +//static void
   1.370 +//gmyth_monitor_handler_listener (GMythMonitorHandler *monitor, gpointer user_data)
   1.371 +//static gboolean
   1.372 +//gmyth_monitor_handler_listener( GIOChannel *io_channel, GIOCondition condition, gpointer data )
   1.373 +//gboolean* gmyth_monitor_handler_listener( GMythMonitorHandler *monitor )
   1.374 +void
   1.375 +gmyth_monitor_handler_listener (GMythMonitorHandler *monitor, gpointer user_data)
   1.376 +{
   1.377 +	//GMythMonitorHandler *monitor = (GMythMonitorHandler*)data; 
   1.378 +  GIOStatus io_status;
   1.379 +  GIOCondition io_cond;  
   1.380 +  guint recv = 0;
   1.381 +  gboolean *ret = g_new0( gboolean, 1 );
   1.382 +  *ret = TRUE;
   1.383 +  //gboolean ret = TRUE;
   1.384 +  gsize len = 0;
   1.385 +  
   1.386 +  static guint count = 0;
   1.387 +  
   1.388 +  GIOChannel *io_channel = monitor->event_sock->sd_io_ch;
   1.389 +  //GIOCondition condition = g_io_channel_get_buffer_condition( io_channel );
   1.390 +  
   1.391 +  GMythStringList *strlist = NULL;
   1.392 +  
   1.393 +  //GMythMonitorHandler *monitor = (GMythMonitorHandler*)data;
   1.394 +  
   1.395 +  myth_control_acquire_context (TRUE);
   1.396 +  
   1.397 +  if ( io_channel == NULL ) {
   1.398 +  	g_debug ("Monitor socket is NULL!\n");
   1.399 +  	*ret = FALSE;
   1.400 +  	goto clean_up;
   1.401 +  }
   1.402 +    
   1.403 +  while (monitor->allow_msgs_listener) {
   1.404 +  	++count;
   1.405 +  	
   1.406 +  	gmyth_debug ("%d - Listening on Monitor socket...!\n", count);
   1.407 +	
   1.408 +    do 
   1.409 +    {
   1.410 +    	
   1.411 +    	gint bytes_sent = 0;
   1.412 +    	
   1.413 +    	strlist = gmyth_string_list_new();
   1.414 +    	
   1.415 +    	if ( monitor->event_sock != NULL )
   1.416 +    	{
   1.417 +    		
   1.418 +	      len = gmyth_socket_read_stringlist( monitor->event_sock, strlist );
   1.419 +	      
   1.420 +		    if ( strlist != NULL && gmyth_string_list_length( strlist ) > 0 ) 
   1.421 +		    { 
   1.422 +			    bytes_sent = gmyth_string_list_get_int( strlist,  0 ); // -1 on backend error
   1.423 +	
   1.424 +		      gmyth_debug ( "[%s] MONITOR: received data buffer from IO event channel... %d strings gone!\n", 
   1.425 +		      		__FUNCTION__, len );
   1.426 +		      		
   1.427 +		      recv += len;
   1.428 +		      
   1.429 +		      /* debug purpose: prints out all the string list elements */
   1.430 +		      g_list_foreach( strlist->glist, (GFunc)gmyth_monitor_handler_print, NULL );
   1.431 +		      
   1.432 +		      gchar *back_msg_action = g_new0( gchar, 1 );
   1.433 +		      gint msg_type = gmyth_monitor_handler_is_backend_message( monitor, strlist, 
   1.434 +		      		&back_msg_action );
   1.435 +		      
   1.436 +	        g_signal_emit ( monitor,
   1.437 +	               GMYTH_MONITOR_HANDLER_GET_CLASS (monitor)->backend_events_handler_signal_id,
   1.438 +	               0, /* details */
   1.439 +	               msg_type, back_msg_action );
   1.440 +	               
   1.441 +	        if (back_msg_action!= NULL)
   1.442 +	        	g_free( back_msg_action );
   1.443 +		      
   1.444 +		    }
   1.445 +		    
   1.446 +    	}
   1.447 +	    
   1.448 +	    if (strlist!=NULL)
   1.449 +	    	g_object_unref( strlist );
   1.450 +      	
   1.451 +      io_cond = g_io_channel_get_buffer_condition( io_channel );
   1.452 +
   1.453 +    } while ( recv <= 0 && ( io_cond & G_IO_IN ) != 0 );
   1.454 +    
   1.455 +    gmyth_debug ("[%s]\tMONITOR EVENT: Read %d bytes\n", __FUNCTION__, recv );
   1.456 +
   1.457 +	  g_usleep( 300 );
   1.458 +	  
   1.459 +  } /* main GThread while */
   1.460 +  
   1.461 +  myth_control_release_context ();
   1.462 +  
   1.463 +  if ( io_status == G_IO_STATUS_ERROR ) {
   1.464 +    //gmyth_debug ("[%s] Error reading: %s\n", __FUNCTION__, error != NULL ? error->message : "" );
   1.465 +    gmyth_debug ("Error reading MONITOR event socket.\n");
   1.466 +   	*ret = FALSE;
   1.467 +   	goto clean_up;   	
   1.468 +  }
   1.469 +  
   1.470 +clean_up:
   1.471 +
   1.472 +  if (strlist!=NULL)
   1.473 +  	g_object_unref( strlist );
   1.474 +  	
   1.475 + 	return;
   1.476 +
   1.477 +}
   1.478 +
   1.479 +static gboolean
   1.480 +gmyth_connect_to_backend_monitor (GMythMonitorHandler *monitor)
   1.481 +{
   1.482 +  gboolean ret = TRUE;
   1.483 +
   1.484 +  monitor->event_sock = gmyth_socket_new();
   1.485 +
   1.486 +  /* Connects the socket, send Mythtv ANN Monitor and verify Mythtv protocol version */ 
   1.487 +  if (!gmyth_socket_connect_to_backend_events ( monitor->event_sock,
   1.488 +          monitor->hostname, monitor->port, FALSE ) ) 
   1.489 +  {
   1.490 +    g_object_unref (monitor->event_sock);
   1.491 +    monitor->event_sock = NULL;
   1.492 +    ret = FALSE;
   1.493 +  }
   1.494 +  
   1.495 +  return ret;
   1.496 +}    
   1.497 +
   1.498 +static gboolean*
   1.499 +gmyth_monitor_handler_setup( GMythMonitorHandler *monitor, GIOChannel *channel )
   1.500 +{
   1.501 +	gboolean *ret = g_new0( gboolean, 1 );
   1.502 +	guint src_id = 0;
   1.503 +	
   1.504 +	*ret = TRUE;
   1.505 +	
   1.506 +  //io_watcher_context = g_main_context_default();
   1.507 +  //GMainLoop *loop = g_main_loop_new( io_watcher_context, TRUE );
   1.508 +
   1.509 +  //GSource *source;
   1.510 +
   1.511 +  if ( channel != NULL ) {
   1.512 +    //source = g_io_create_watch( channel, G_IO_IN | G_IO_HUP );
   1.513 +    src_id = g_io_add_watch( channel, G_IO_IN, 
   1.514 +    					(GIOFunc)gmyth_monitor_handler_listener, monitor );
   1.515 +  } else {
   1.516 +  	*ret = FALSE;
   1.517 +  	goto cleanup;
   1.518 +  }
   1.519 +
   1.520 +  //g_source_set_callback ( source, (GSourceFunc)gmyth_monitor_handler_listener, NULL, NULL );
   1.521 +
   1.522 +  //g_source_attach( source, io_watcher_context );
   1.523 +  
   1.524 +  //if (NULL == source){
   1.525 +  if (src_id < 0){
   1.526 +    gmyth_debug( "[%s] Error adding watch listener function to the IO control channel!\n", __FUNCTION__ );
   1.527 +    *ret = FALSE;
   1.528 +    goto cleanup;
   1.529 +  }
   1.530 +  
   1.531 +  //g_main_loop_run( loop );
   1.532 +  
   1.533 +cleanup:
   1.534 +  //if ( source != NULL )
   1.535 +  //  g_source_unref( source );
   1.536 +    
   1.537 +  //if ( io_watcher_context != NULL )
   1.538 +  //  g_main_context_unref( io_watcher_context );
   1.539 +
   1.540 +  //if ( loop != NULL )
   1.541 +  //  g_main_loop_unref( loop );
   1.542 +    
   1.543 +  return ret;
   1.544 +  
   1.545 +}
   1.546 +
   1.547 +gboolean 
   1.548 +gmyth_monitor_handler_start (GMythMonitorHandler *monitor)
   1.549 +{
   1.550 +	gboolean *ret = g_new0( gboolean, 1 ); 
   1.551 +	*ret = TRUE;	
   1.552 +	
   1.553 +	/*if (!g_thread_supported () ) 	g_thread_init (NULL);*/
   1.554 +	/*
   1.555 +  monitor->monitor_th = g_thread_pool_new( (GThreadFunc)gmyth_monitor_handler_listener, 
   1.556 +  					monitor, TRUE, NULL );
   1.557 + */				
   1.558 +  monitor->monitor_th = g_thread_pool_new( (GFunc)gmyth_monitor_handler_listener, 
   1.559 +  					monitor, 3, TRUE, NULL );
   1.560 +  g_thread_pool_push( monitor->monitor_th, monitor, NULL ); 
   1.561 +  					
   1.562 +  //if ( ( ret = g_thread_join( monitor_th ) ) == FALSE )
   1.563 +  if ( monitor->monitor_th != NULL )
   1.564 +  //if ( gmyth_monitor_handler_setup( monitor, monitor->event_sock->sd_io_ch ) )
   1.565 +  {
   1.566 +  	gmyth_debug ( "\n[%s]\tOK! Starting listener on the MONITOR event socket...[thread location = %p]\n", 
   1.567 +  				__FUNCTION__, g_thread_self( ) );
   1.568 +  	*ret = TRUE;  	  	
   1.569 +  } else {
   1.570 +  	gmyth_debug ( "\n[%s]\tERROR! Coudn't start listener on the MONITOR event socket...[thread location = %p]\n", 
   1.571 +  				__FUNCTION__, g_thread_self( ) );
   1.572 +  	*ret = FALSE;
   1.573 +  }
   1.574 +
   1.575 +//cleanup:
   1.576 +    
   1.577 +  gmyth_debug( "[%s] Watch listener function over the IO control channel? %s!!!\n", 
   1.578 +  			__FUNCTION__, ( *ret == TRUE ? "YES" : "NO" ) );
   1.579 +    
   1.580 +    return *ret;
   1.581 +}
   1.582 +
   1.583 +void
   1.584 +gmyth_monitor_handler_close( GMythMonitorHandler *monitor )
   1.585 +{
   1.586 +	
   1.587 +  if (monitor->event_sock) {
   1.588 +    g_object_unref( monitor->event_sock );
   1.589 +    monitor->event_sock = NULL;
   1.590 +  }
   1.591 +
   1.592 +  if (monitor->hostname) {
   1.593 +    g_free( monitor->hostname );
   1.594 +    monitor->hostname = NULL;
   1.595 +  }
   1.596 +  
   1.597 +}
   1.598 +