[svn r221] Added some signals to interact with the MythTV backend messages. trunk
authorrosfran
Wed Dec 13 23:44:04 2006 +0000 (2006-12-13)
branchtrunk
changeset 220462a3c81abd6
parent 219 5f7980370325
child 221 b250ee0438a2
[svn r221] Added some signals to interact with the MythTV backend messages.
gmyth/src/gmyth_file_transfer.c
gmyth/src/gmyth_file_transfer.h
gmyth/src/gmyth_livetv.c
gmyth/src/gmyth_livetv.h
gmyth/src/gmyth_monitor_handler.c
gmyth/src/gmyth_monitor_handler.h
     1.1 --- a/gmyth/src/gmyth_file_transfer.c	Wed Dec 13 23:42:28 2006 +0000
     1.2 +++ b/gmyth/src/gmyth_file_transfer.c	Wed Dec 13 23:44:04 2006 +0000
     1.3 @@ -103,7 +103,7 @@
     1.4  static void gmyth_file_transfer_finalize (GObject *object);
     1.5  
     1.6  static void gmyth_file_transfer_program_info_changed( GMythFileTransfer *transfer, 
     1.7 -										gint msg_code, gpointer livetv_transfer );
     1.8 +										gint msg_code, gpointer livetv_transfer, gpointer user_data );
     1.9  
    1.10  static gboolean gmyth_connect_to_backend (GMythFileTransfer *transfer);
    1.11  
    1.12 @@ -162,6 +162,8 @@
    1.13    transfer->control_sock = NULL;
    1.14    transfer->sock = NULL;
    1.15    
    1.16 +  transfer->mutex = g_mutex_new();
    1.17 +  
    1.18  }
    1.19  
    1.20  static void
    1.21 @@ -169,6 +171,12 @@
    1.22  {
    1.23  	GMythFileTransfer *transfer = GMYTH_FILE_TRANSFER (object);
    1.24  	
    1.25 +  if ( transfer->mutex != NULL )  
    1.26 +  {
    1.27 +  	g_mutex_free( transfer->mutex );
    1.28 +  	transfer->mutex = NULL;
    1.29 +  }
    1.30 +  
    1.31    if ( transfer->control_sock != NULL )  
    1.32    {
    1.33    	g_object_unref( transfer->control_sock );
    1.34 @@ -497,7 +505,9 @@
    1.35  
    1.36    GIOCondition io_cond;
    1.37    GIOCondition io_cond_control;
    1.38 -  GIOStatus io_status = G_IO_STATUS_NORMAL, io_status_control = G_IO_STATUS_NORMAL;   
    1.39 +  GIOStatus io_status = G_IO_STATUS_NORMAL, io_status_control = G_IO_STATUS_NORMAL;
    1.40 +  
    1.41 +  gboolean ret = TRUE;   
    1.42  
    1.43    GString *query;
    1.44    
    1.45 @@ -547,28 +557,58 @@
    1.46      // Receives the backand answer    
    1.47      gmyth_socket_read_stringlist( transfer->control_sock, strlist );
    1.48  
    1.49 -    if ( strlist != NULL && gmyth_string_list_length( strlist ) > 0 ) { 
    1.50 +    if ( strlist != NULL && gmyth_string_list_length( strlist ) > 0 ) 
    1.51 +    { 
    1.52  	    bytes_sent = gmyth_string_list_get_int( strlist,  0 ); // -1 on backend error
    1.53  	    gmyth_debug ( "[%s] got SENT buffer message = %d\n", __FUNCTION__, bytes_sent );
    1.54  
    1.55 -      	if ( bytes_sent != 0 ) 
    1.56 -      	{
    1.57 -            gchar *data_buffer = g_new0 (gchar, bytes_sent);    
    1.58 -     	    while ( 0 != bytes_sent) { 
    1.59 -                io_status = g_io_channel_read_chars( io_channel, data_buffer + total_read, 
    1.60 -    				(gsize) bytes_sent, // buffer_len
    1.61 -					&bytes_read, &error );
    1.62 -    	        total_read += bytes_read;
    1.63 -                bytes_sent -= bytes_read;
    1.64 -	        
    1.65 -	            /* append new data to the increasing byte array */
    1.66 -		    		data = g_byte_array_append (data, (const guint8*)data_buffer, bytes_read);
    1.67 -		        gmyth_debug ("Total file transfer data read: %d\n", total_read);
    1.68 -	        }
    1.69 -            g_free (data_buffer);
    1.70 +    	if ( bytes_sent != 0 ) 
    1.71 +    	{
    1.72 +        gchar *data_buffer = g_new0 ( gchar, bytes_sent );    
    1.73 +   	    while ( 0 != bytes_sent ) 
    1.74 +   	    { 
    1.75 +          io_status = g_io_channel_read_chars( io_channel, data_buffer + total_read, 
    1.76 +	    				(gsize) bytes_sent,	&bytes_read, &error );
    1.77 +
    1.78 +	        total_read += bytes_read;
    1.79 +          bytes_sent -= bytes_read;
    1.80 +      
    1.81 +            /* append new data to the increasing byte array */
    1.82 +	    		data = g_byte_array_append (data, (const guint8*)data_buffer, bytes_read);
    1.83 +	        gmyth_debug ("Total transfer data read: %d\n", total_read);
    1.84 +        }
    1.85 +        g_free (data_buffer);
    1.86  	    } else {
    1.87 -	    	
    1.88 -      }
    1.89 +				transfer->priv = GMYTH_FILE_TRANSFER_GET_PRIVATE(transfer);
    1.90 +				
    1.91 +				if ( transfer->priv != NULL && transfer->priv->livetv != NULL )
    1.92 +				{
    1.93 +				
    1.94 +					total_read = GMYTHTV_FILE_TRANSFER_NEXT_PROG_CHAIN;	
    1.95 +				
    1.96 +			  	if ( transfer->priv->do_next_program_chain )
    1.97 +			  	{			  					  		
    1.98 +			  		
    1.99 +			  		g_mutex_lock( transfer->mutex );
   1.100 +			  		
   1.101 +			  		ret = gmyth_livetv_next_program_chain( transfer->priv->livetv );
   1.102 +			  		
   1.103 +			  		g_mutex_unlock( transfer->mutex );
   1.104 +			  		
   1.105 +						if ( !ret )
   1.106 +						{
   1.107 +							gmyth_debug( "Cannot change to the next program chain!" );
   1.108 +						}
   1.109 +						else
   1.110 +						{
   1.111 +							gmyth_debug( "OK!!! MOVED to the next program chain [%s]!", 
   1.112 +													(gmyth_tvchain_get_id( transfer->priv->livetv->tvchain ))->str );
   1.113 +						}
   1.114 +						
   1.115 +			  	}
   1.116 +			  	
   1.117 +				} /* if */
   1.118 +      } /* if */
   1.119      } else {
   1.120      	total_read = GMYTHTV_FILE_TRANSFER_READ_ERROR;
   1.121      	g_object_unref (strlist);
   1.122 @@ -589,46 +629,21 @@
   1.123    }
   1.124    
   1.125    if ( total_read > 0 )
   1.126 -  	transfer->readposition += total_read;
   1.127 -  else
   1.128 -  {
   1.129 -		transfer->priv = GMYTH_FILE_TRANSFER_GET_PRIVATE(transfer);
   1.130 -		
   1.131 -		if ( transfer->priv != NULL && transfer->priv->livetv != NULL )
   1.132 -		{
   1.133 -		
   1.134 -			total_read = GMYTHTV_FILE_TRANSFER_NEXT_PROG_CHAIN;	
   1.135 -		
   1.136 -	  	if ( max_tries <= 0 && transfer->priv->do_next_program_chain )
   1.137 -	  	{
   1.138 -	  		
   1.139 -				if ( !gmyth_livetv_next_program_chain( transfer->priv->livetv ) )
   1.140 -				{
   1.141 -					gmyth_debug( "Cannot change to the next program chain!" );
   1.142 -				}
   1.143 -				else
   1.144 -				{
   1.145 -					gmyth_debug( "OK!!! MOVED to the next program chain [%s]!", 
   1.146 -											(gmyth_tvchain_get_id( transfer->priv->livetv->tvchain ))->str );
   1.147 -				}
   1.148 -	  	}
   1.149 -	  	
   1.150 -		} /* if */
   1.151 -		
   1.152 -  } /* if */
   1.153 +  	transfer->readposition += total_read;  	
   1.154    	
   1.155    return total_read;
   1.156  }
   1.157  
   1.158  static void 
   1.159  gmyth_file_transfer_program_info_changed( GMythFileTransfer *transfer, 
   1.160 -										gint msg_code, gpointer livetv_transfer )
   1.161 +										gint msg_code, gpointer livetv_transfer, gpointer user_data )
   1.162  {
   1.163 -	GMythLiveTV *livetv = (GMythLiveTV*)livetv_transfer;
   1.164 +	GMythLiveTV *livetv = (GMythLiveTV*)user_data;
   1.165  	
   1.166 -	gmyth_debug( "Program info changed! ( file transfer orig. = %p, ptr. = %p )", transfer, livetv_transfer );
   1.167 +	gmyth_debug( "Program info changed! ( file transfer orig. = %p, ptr. = [%s], user data = [%s] )", transfer, 
   1.168 +		livetv_transfer != NULL ? "[NOT NULL]" : "[NULL]", livetv != NULL ? "[NOT NULL]" : "[NULL]" );
   1.169  	
   1.170 -	if ( transfer == livetv->file_transfer )
   1.171 +	if ( livetv != NULL && transfer == livetv->file_transfer )
   1.172  	{
   1.173  		gmyth_debug( "YES, the requested program info movement on the LiveTV transfer is authentical!" );
   1.174  	}
     2.1 --- a/gmyth/src/gmyth_file_transfer.h	Wed Dec 13 23:42:28 2006 +0000
     2.2 +++ b/gmyth/src/gmyth_file_transfer.h	Wed Dec 13 23:44:04 2006 +0000
     2.3 @@ -86,6 +86,8 @@
     2.4  	/* socket descriptors */
     2.5  	GMythSocket 			*control_sock;
     2.6  	GMythSocket 			*sock;
     2.7 +	
     2.8 +	GMutex 						*mutex;
     2.9  
    2.10  	gint64 						readposition;
    2.11  	guint64 					filesize;
     3.1 --- a/gmyth/src/gmyth_livetv.c	Wed Dec 13 23:42:28 2006 +0000
     3.2 +++ b/gmyth/src/gmyth_livetv.c	Wed Dec 13 23:44:04 2006 +0000
     3.3 @@ -83,7 +83,6 @@
     3.4  static void
     3.5  gmyth_livetv_dispose  (GObject *object)
     3.6  {
     3.7 -
     3.8  	G_OBJECT_CLASS (gmyth_livetv_parent_class)->dispose (object);
     3.9  }
    3.10  
    3.11 @@ -139,13 +138,14 @@
    3.12  
    3.13  static void
    3.14  gmyth_livetv_monitor_signal_handler( GMythMonitorHandler *monitor, gint msg_code, 
    3.15 -							gchar* message, gpointer livetv )
    3.16 +							gchar* message, gpointer livetv, gpointer user_data )
    3.17  {
    3.18 -	GMythLiveTV *live_tv = (GMythLiveTV*) livetv;
    3.19 +	GMythLiveTV *live_tv = (GMythLiveTV*) user_data;
    3.20  	
    3.21 -	gmyth_debug( "LIVETV Signal handler ( msg = %s, code = %d )\n", message, msg_code );
    3.22 +	gmyth_debug( "LIVETV Signal handler ( msg = %s, code = %d, livetv param = %s, user_data = %s )\n", message, msg_code, livetv != NULL ? "" : 
    3.23 +					"NULL", user_data != NULL ? "" : "NULL" );
    3.24  	
    3.25 -	if ( NULL == live_tv )
    3.26 +	if ( NULL == live_tv && user_data == NULL )
    3.27  		return;		
    3.28  	
    3.29  	switch ( msg_code ) 
    3.30 @@ -154,31 +154,81 @@
    3.31  		case GMYTH_BACKEND_PROGRAM_INFO_CHANGED:
    3.32  		{
    3.33  			gmyth_debug( "LIVETV Program Changed event received [ msg = %s ]\n", message );
    3.34 -			/*
    3.35 -			if ( !gmyth_livetv_next_program_chain( live_tv ) )
    3.36 -			{
    3.37 -				gmyth_debug( "Cannot change to the next program chain!" );
    3.38 -			}
    3.39 -			else
    3.40 -			{
    3.41 -			*/
    3.42  			gmyth_debug( "OK!!! MOVED to the next program chain [%s]!", 
    3.43  									(gmyth_tvchain_get_id( live_tv->tvchain ))->str );
    3.44  			/* advertises the FileTransfer about the program info changed */
    3.45 -			g_signal_emit ( live_tv->file_transfer,
    3.46 -             GMYTH_FILE_TRANSFER_GET_CLASS (live_tv->file_transfer)->program_info_changed_handler_signal_id,
    3.47 -             0, /* details */
    3.48 -             msg_code, live_tv );
    3.49 +			if ( live_tv->file_transfer != NULL )
    3.50 +			{
    3.51 +				g_signal_emit ( live_tv->file_transfer,
    3.52 +	             GMYTH_FILE_TRANSFER_GET_CLASS (live_tv->file_transfer)->program_info_changed_handler_signal_id,
    3.53 +	             0, /* details */
    3.54 +	             msg_code, live_tv, live_tv );
    3.55 +	             
    3.56 +	      gmyth_livetv_monitor_handler_stop( live_tv );	      
    3.57 +			} else
    3.58 +				gmyth_debug( "LIVETV file_transfer is NULL!!! Cannot move to the next program chain event received.\n");
    3.59  			/*}*/
    3.60  			break;
    3.61  		}
    3.62  		default:
    3.63 -			break;
    3.64 -		
    3.65 +			break;		
    3.66 +	} /* switch (Monitor Handler messages) */
    3.67 +	
    3.68 +}
    3.69 +
    3.70 +gboolean
    3.71 +gmyth_livetv_monitor_handler_start( GMythLiveTV *livetv )
    3.72 +{
    3.73 +	gboolean res = TRUE;
    3.74 +	
    3.75 +	if ( livetv->monitor != NULL )
    3.76 +	{
    3.77 +		g_object_unref( livetv->monitor );
    3.78 +		livetv->monitor	= NULL;
    3.79  	}
    3.80  	
    3.81 +  livetv->monitor = gmyth_monitor_handler_new ( );
    3.82 +  
    3.83 +  res = gmyth_monitor_handler_open (livetv->monitor, livetv->backend_info->hostname, 
    3.84 +  				livetv->backend_info->port );
    3.85 +  
    3.86 +  if ( res == TRUE )
    3.87 +  {
    3.88 +  	gmyth_debug("Connect MythTV Monitor event socket! Trying to start the message handler...");
    3.89 +  	
    3.90 +  	res = gmyth_monitor_handler_start ( livetv->monitor );
    3.91 +  	
    3.92 +  	if (res)
    3.93 +  	{
    3.94 +  		gmyth_debug("MythTV Monitor event socket connected and listening!");
    3.95 +  		g_signal_connect ( G_OBJECT (livetv->monitor), "backend-events-handler",
    3.96 +                  (GCallback)gmyth_livetv_monitor_signal_handler,
    3.97 +                  livetv );
    3.98 +  	}
    3.99 +  	else
   3.100 +  	{
   3.101 +  		gmyth_debug("Problems when trying to start MythTV Monitor event socket!");
   3.102 +  		goto error;  		
   3.103 +  	}
   3.104 +  }
   3.105 +  
   3.106 +error:
   3.107 +	return res;
   3.108 +  
   3.109  }
   3.110  
   3.111 +void
   3.112 +gmyth_livetv_monitor_handler_stop( GMythLiveTV *livetv )
   3.113 +{
   3.114 +	
   3.115 +  if ( livetv->monitor != NULL )
   3.116 +  {
   3.117 +  	g_object_unref( livetv->monitor );
   3.118 +  	livetv->monitor = NULL;
   3.119 +  } 
   3.120 +  
   3.121 +}  
   3.122 +
   3.123  gboolean
   3.124  gmyth_livetv_setup ( GMythLiveTV *livetv, GMythBackendInfo *backend_info )
   3.125  {
   3.126 @@ -199,31 +249,6 @@
   3.127  
   3.128  	livetv->is_livetv = TRUE;
   3.129  	
   3.130 -  livetv->monitor = gmyth_monitor_handler_new ( );
   3.131 -  
   3.132 -  res = gmyth_monitor_handler_open (livetv->monitor, livetv->backend_info->hostname, 
   3.133 -  				livetv->backend_info->port );
   3.134 -  
   3.135 -  if ( res == TRUE )
   3.136 -  {
   3.137 -  	gmyth_debug("Connect MythTV Monitor event socket! Trying to start the message handler...");
   3.138 -  	
   3.139 -  	res = gmyth_monitor_handler_start ( livetv->monitor );
   3.140 -  	
   3.141 -  	if (res)
   3.142 -  	{
   3.143 -  		gmyth_debug("MythTV Monitor event socket connected and listening!");
   3.144 -  		g_signal_connect (G_OBJECT (livetv->monitor), "backend-events-handler",
   3.145 -                  (GCallback)gmyth_livetv_monitor_signal_handler,
   3.146 -                  livetv);
   3.147 -  	}
   3.148 -  	else
   3.149 -  	{
   3.150 -  		gmyth_debug("Problems when trying to start MythTV Monitor event socket!");
   3.151 -  		goto error;  		
   3.152 -  	}
   3.153 -  }
   3.154 -
   3.155  	livetv->local_hostname  = gmyth_socket_get_local_hostname ( );
   3.156  
   3.157  	if ( livetv->local_hostname == NULL ) {
   3.158 @@ -326,6 +351,11 @@
   3.159  		livetv->proginfo = NULL;
   3.160  	}
   3.161  
   3.162 +	if ( livetv->monitor != NULL ) {
   3.163 +		g_object_unref (livetv->monitor);
   3.164 +		livetv->monitor = NULL;
   3.165 +	}
   3.166 +
   3.167  	return res;
   3.168  
   3.169  }
   3.170 @@ -342,6 +372,9 @@
   3.171  		goto error;		
   3.172  	}
   3.173  	
   3.174 +	//if ( !gmyth_livetv_monitor_handler_start( livetv ) )
   3.175 +	//	goto error;
   3.176 +	
   3.177  	/* Reload all TV chain from Mysql database. */
   3.178  	gmyth_tvchain_reload_all (livetv->tvchain);
   3.179  
   3.180 @@ -359,6 +392,8 @@
   3.181  	} else {
   3.182  		gmyth_debug ("[%s] GMythLiveTV: All requests to backend to start TV were OK, TV chain changed.\n", __FUNCTION__ );
   3.183  	}
   3.184 +	
   3.185 +	livetv->setup_done = TRUE;
   3.186  
   3.187  	return res;
   3.188  
   3.189 @@ -416,7 +451,7 @@
   3.190    uri = gmyth_uri_new_with_value( livetv->proginfo->pathname->str );
   3.191    if ( NULL == uri )
   3.192    {
   3.193 -  	gmyth_debug( "Couldn't parse the URI to start LiveTV! [uri = %s]", livetv->proginfo->pathname->str );  	
   3.194 +  	gmyth_debug( "Couldn't parse the URI to start LiveTV! [ uri = %s ]", livetv->proginfo->pathname->str );  	
   3.195    }
   3.196  
   3.197  	if ( !gmyth_file_transfer_open( livetv->file_transfer, uri != NULL ? gmyth_uri_get_path(uri) : 
   3.198 @@ -428,6 +463,9 @@
   3.199  		goto done;
   3.200  	}
   3.201  
   3.202 +	if ( !gmyth_livetv_monitor_handler_start( livetv ) )
   3.203 +		goto done;
   3.204 +
   3.205  done:
   3.206  	if ( uri != NULL )
   3.207  	{
   3.208 @@ -446,7 +484,7 @@
   3.209  	gmyth_debug ("[%s] Stopping the LiveTV...\n", __FUNCTION__);
   3.210  
   3.211  	if (livetv->is_livetv) {
   3.212 -		if (!gmyth_recorder_stop_livetv (livetv->recorder)) {
   3.213 +		if ( !gmyth_recorder_stop_livetv (livetv->recorder) ) {
   3.214  			g_warning ("[%s] Error while stoping remote encoder", __FUNCTION__);	
   3.215  		}
   3.216  	}
     4.1 --- a/gmyth/src/gmyth_livetv.h	Wed Dec 13 23:42:28 2006 +0000
     4.2 +++ b/gmyth/src/gmyth_livetv.h	Wed Dec 13 23:44:04 2006 +0000
     4.3 @@ -92,6 +92,9 @@
     4.4  
     4.5  GMythFileTransfer *gmyth_livetv_create_file_transfer( GMythLiveTV *livetv );
     4.6  
     4.7 +gboolean gmyth_livetv_monitor_handler_start( GMythLiveTV *livetv );
     4.8 +void gmyth_livetv_monitor_handler_stop( GMythLiveTV *livetv );
     4.9 +
    4.10  G_END_DECLS
    4.11  
    4.12  #endif /*GMYTH_LIVETV_H_*/
     5.1 --- a/gmyth/src/gmyth_monitor_handler.c	Wed Dec 13 23:42:28 2006 +0000
     5.2 +++ b/gmyth/src/gmyth_monitor_handler.c	Wed Dec 13 23:44:04 2006 +0000
     5.3 @@ -72,18 +72,18 @@
     5.4  
     5.5  GMainContext *io_watcher_context = NULL;
     5.6  
     5.7 -GThreadPool *monitor_th = NULL;
     5.8 +//GThread *monitor_th = NULL;
     5.9  
    5.10  //static gboolean* myth_control_sock_listener( GIOChannel *io_channel );
    5.11 -//static gboolean myth_control_sock_listener( GIOChannel *io_channel, GIOCondition condition, 
    5.12 -//				gpointer data );
    5.13 -//static void myth_control_sock_listener( GIOChannel *io_channel, gboolean* ret );
    5.14 -static void
    5.15 -gmyth_monitor_handler_default_listener( GMythMonitorHandler *monitor, gint msg_code, gchar* message, 
    5.16 -			gpointer live_tv );
    5.17 +//static gboolean gmyth_monitor_handler_listener( GIOChannel *io_channel, 
    5.18 +//						GIOCondition condition, gpointer data );
    5.19  
    5.20 -static void gmyth_monitor_handler_listener (GMythMonitorHandler *monitor, 
    5.21 -		gpointer user_data);
    5.22 +gboolean* gmyth_monitor_handler_listener( GMythMonitorHandler *monitor );
    5.23 +
    5.24 +static void gmyth_monitor_handler_default_listener( GMythMonitorHandler *monitor, gint msg_code, gchar* message,
    5.25 +			gpointer live_tv, gpointer user_data );
    5.26 +//static void gmyth_monitor_handler_listener (GMythMonitorHandler *monitor, 
    5.27 +//		gpointer user_data);
    5.28  
    5.29  static GMutex*				mutex 					 = NULL;
    5.30  
    5.31 @@ -148,6 +148,10 @@
    5.32    
    5.33    /* mutex to control access to the event socket consumer thread */
    5.34    mutex = g_mutex_new();
    5.35 +  
    5.36 +  monitor->monitor_th = NULL;
    5.37 +  
    5.38 +  monitor->gmyth_monitor_handler_listener = gmyth_monitor_handler_listener;
    5.39  }
    5.40  
    5.41  static void
    5.42 @@ -156,6 +160,13 @@
    5.43  	
    5.44  	GMythMonitorHandler *monitor = GMYTH_MONITOR_HANDLER (object);
    5.45  	
    5.46 +	if ( monitor->monitor_th != NULL )  
    5.47 +  {
    5.48 +  	//g_thread_exit( monitor->monitor_th, TRUE, FALSE );
    5.49 +  	g_thread_exit( monitor->monitor_th );
    5.50 +  	monitor->monitor_th = NULL;
    5.51 +  }
    5.52 +	
    5.53    if ( monitor->event_sock != NULL )  
    5.54    {
    5.55    	g_object_unref( monitor->event_sock );
    5.56 @@ -186,12 +197,6 @@
    5.57    	io_watcher_cond = NULL;
    5.58    }
    5.59    
    5.60 -  if ( monitor_th != NULL )  
    5.61 -  {
    5.62 -  	g_thread_pool_free( monitor_th, TRUE, FALSE );
    5.63 -  	monitor_th = NULL;
    5.64 -  }
    5.65 -
    5.66    G_OBJECT_CLASS (gmyth_monitor_handler_parent_class)->dispose (object);
    5.67  }
    5.68  
    5.69 @@ -213,6 +218,55 @@
    5.70    return monitor;
    5.71  }
    5.72  
    5.73 +static gboolean 
    5.74 +myth_control_acquire_context( gboolean do_wait ) 
    5.75 +{
    5.76 +	
    5.77 +	gboolean ret = TRUE;	
    5.78 +	//guint max_iter = 50;
    5.79 +	
    5.80 +	//g_mutex_lock( mutex );
    5.81 +	
    5.82 +  //while ( !has_io_access ) 
    5.83 +  //	g_cond_wait( io_watcher_cond, mutex );
    5.84 +  	
    5.85 +  //has_io_access = FALSE;
    5.86 +  /*
    5.87 +  if ( do_wait ) {
    5.88 +  	while ( --max_iter > 0 && !g_main_context_wait( io_watcher_context, io_watcher_cond, mutex ) )
    5.89 +  		ret = FALSE;
    5.90 +  } else if ( !g_main_context_acquire( io_watcher_context ) )
    5.91 +  	ret = FALSE;
    5.92 +  */
    5.93 +  	
    5.94 +  //g_static_mutex_lock( &st_mutex );
    5.95 +  
    5.96 +  return ret;
    5.97 +  
    5.98 +}
    5.99 +
   5.100 +static gboolean 
   5.101 +myth_control_release_context( ) 
   5.102 +{
   5.103 +	
   5.104 +	gboolean ret = TRUE;
   5.105 +    
   5.106 +  //g_static_mutex_unlock( &st_mutex );
   5.107 +  
   5.108 +	//g_main_context_release( io_watcher_context );
   5.109 +  
   5.110 +  //g_main_context_wakeup( io_watcher_context );
   5.111 +  
   5.112 +  //has_io_access = TRUE;
   5.113 +
   5.114 +  //g_cond_broadcast( io_watcher_cond );
   5.115 +  
   5.116 +  //g_mutex_unlock( mutex );  
   5.117 + 
   5.118 +  return ret;
   5.119 +  
   5.120 +}
   5.121 +
   5.122  gboolean
   5.123  gmyth_monitor_handler_open (GMythMonitorHandler *monitor, gchar *hostname, gint port)
   5.124  {
   5.125 @@ -287,7 +341,7 @@
   5.126  
   5.127  static void
   5.128  gmyth_monitor_handler_default_listener( GMythMonitorHandler *monitor, gint msg_code, gchar* message,
   5.129 -			gpointer live_tv )
   5.130 +			gpointer live_tv, gpointer user_data )
   5.131  {
   5.132  	//assert( message!= NULL );  
   5.133  	gmyth_debug( "DEFAULT Signal handler ( msg = %s, code = %d, file_transfer ptr. = %p )\n", 
   5.134 @@ -300,15 +354,16 @@
   5.135  	gmyth_debug( "Backend message event: %s --- ", str->str );
   5.136  }
   5.137  
   5.138 -static void
   5.139 -gmyth_monitor_handler_listener (GMythMonitorHandler *monitor, gpointer user_data)
   5.140 +//static void
   5.141 +//gmyth_monitor_handler_listener (GMythMonitorHandler *monitor, gpointer user_data)
   5.142 +//static gboolean
   5.143 +//gmyth_monitor_handler_listener( GIOChannel *io_channel, GIOCondition condition, gpointer data )
   5.144 +gboolean*
   5.145 +gmyth_monitor_handler_listener( GMythMonitorHandler *monitor )
   5.146  {
   5.147 -
   5.148 +	//GMythMonitorHandler *monitor = (GMythMonitorHandler*)data; 
   5.149    GIOStatus io_status;
   5.150 -  //GError *error = NULL;
   5.151    GIOCondition io_cond;  
   5.152 -  //gchar *trash = NULL;
   5.153 -  //GByteArray *byte_array = NULL;
   5.154    guint recv = 0;
   5.155    gboolean *ret = g_new0( gboolean, 1 );
   5.156    *ret = TRUE;
   5.157 @@ -324,7 +379,7 @@
   5.158    
   5.159    //GMythMonitorHandler *monitor = (GMythMonitorHandler*)data;
   5.160    
   5.161 -  //myth_control_acquire_context (TRUE);
   5.162 +  myth_control_acquire_context (TRUE);
   5.163    
   5.164    if ( io_channel == NULL ) {
   5.165    	g_debug ("Monitor socket is NULL!\n");
   5.166 @@ -343,42 +398,46 @@
   5.167      	gint bytes_sent = 0;
   5.168      	
   5.169      	strlist = gmyth_string_list_new();
   5.170 -
   5.171 -      len = gmyth_socket_read_stringlist( monitor->event_sock, strlist );
   5.172 -      
   5.173 -	    if ( strlist != NULL && gmyth_string_list_length( strlist ) > 0 ) 
   5.174 -	    { 
   5.175 -		    bytes_sent = gmyth_string_list_get_int( strlist,  0 ); // -1 on backend error
   5.176 -		    gmyth_debug ( "[%s] MONITOR: got SENT buffer message = %d\n", __FUNCTION__, bytes_sent );
   5.177 -
   5.178 -	      gmyth_debug ( "[%s] MONITOR: received data buffer from IO event channel... %d strings gone!\n", 
   5.179 -	      		__FUNCTION__, len );
   5.180 -	      		
   5.181 -	      recv += len;
   5.182 +    	
   5.183 +    	if ( monitor->event_sock != NULL )
   5.184 +    	{
   5.185 +    		
   5.186 +	      len = gmyth_socket_read_stringlist( monitor->event_sock, strlist );
   5.187  	      
   5.188 -	      /* debug purpose: prints out all the string list elements */
   5.189 -	      g_list_foreach( strlist->glist, (GFunc)gmyth_monitor_handler_print, NULL );
   5.190 -	      
   5.191 -	      gchar *back_msg_action = g_new0( gchar, 1 );
   5.192 -	      gint msg_type = gmyth_monitor_handler_is_backend_message( monitor, strlist, 
   5.193 -	      		&back_msg_action );
   5.194 -	      
   5.195 -        g_signal_emit ( monitor,
   5.196 -               GMYTH_MONITOR_HANDLER_GET_CLASS (monitor)->backend_events_handler_signal_id,
   5.197 -               0, /* details */
   5.198 -               msg_type, back_msg_action, NULL );
   5.199 -               
   5.200 -        if (back_msg_action!= NULL)
   5.201 -        	g_free( back_msg_action );
   5.202 -	      
   5.203 -	    }
   5.204 +		    if ( strlist != NULL && gmyth_string_list_length( strlist ) > 0 ) 
   5.205 +		    { 
   5.206 +			    bytes_sent = gmyth_string_list_get_int( strlist,  0 ); // -1 on backend error
   5.207 +	
   5.208 +		      gmyth_debug ( "[%s] MONITOR: received data buffer from IO event channel... %d strings gone!\n", 
   5.209 +		      		__FUNCTION__, len );
   5.210 +		      		
   5.211 +		      recv += len;
   5.212 +		      
   5.213 +		      /* debug purpose: prints out all the string list elements */
   5.214 +		      g_list_foreach( strlist->glist, (GFunc)gmyth_monitor_handler_print, NULL );
   5.215 +		      
   5.216 +		      gchar *back_msg_action = g_new0( gchar, 1 );
   5.217 +		      gint msg_type = gmyth_monitor_handler_is_backend_message( monitor, strlist, 
   5.218 +		      		&back_msg_action );
   5.219 +		      
   5.220 +	        g_signal_emit ( monitor,
   5.221 +	               GMYTH_MONITOR_HANDLER_GET_CLASS (monitor)->backend_events_handler_signal_id,
   5.222 +	               0, /* details */
   5.223 +	               msg_type, back_msg_action, NULL );
   5.224 +	               
   5.225 +	        if (back_msg_action!= NULL)
   5.226 +	        	g_free( back_msg_action );
   5.227 +		      
   5.228 +		    }
   5.229 +		    
   5.230 +    	}
   5.231  	    
   5.232  	    if (strlist!=NULL)
   5.233  	    	g_object_unref( strlist );
   5.234        	
   5.235        io_cond = g_io_channel_get_buffer_condition( io_channel );
   5.236  
   5.237 -    } while ( ( io_cond & G_IO_IN ) != 0 );
   5.238 +    } while ( recv <= 0 && ( io_cond & G_IO_IN ) != 0 );
   5.239      
   5.240      gmyth_debug ("[%s]\tMONITOR EVENT: Read %d bytes\n", __FUNCTION__, recv );
   5.241  
   5.242 @@ -386,6 +445,8 @@
   5.243  	  
   5.244    } /* main GThread while */
   5.245    
   5.246 +  myth_control_release_context ();
   5.247 +  
   5.248    if ( io_status == G_IO_STATUS_ERROR ) {
   5.249      //gmyth_debug ("[%s] Error reading: %s\n", __FUNCTION__, error != NULL ? error->message : "" );
   5.250      gmyth_debug ("Error reading MONITOR event socket.\n");
   5.251 @@ -397,6 +458,8 @@
   5.252  
   5.253    if (strlist!=NULL)
   5.254    	g_object_unref( strlist );
   5.255 +  	
   5.256 + 	return ret;
   5.257  
   5.258  }
   5.259  
   5.260 @@ -425,44 +488,44 @@
   5.261    return ret;
   5.262  }    
   5.263  
   5.264 -/*
   5.265  static gboolean*
   5.266 -gmyth_monitor_handler_setup( GIOChannel *channel )
   5.267 +gmyth_monitor_handler_setup( GMythMonitorHandler *monitor, GIOChannel *channel )
   5.268  {
   5.269  	gboolean *ret = g_new0( gboolean, 1 );
   5.270 -	//guint src_id = 0;
   5.271 +	guint src_id = 0;
   5.272  	
   5.273  	*ret = TRUE;
   5.274  	
   5.275 -  io_watcher_context = g_main_context_default();
   5.276 -  GMainLoop *loop = g_main_loop_new( io_watcher_context, TRUE );
   5.277 +  //io_watcher_context = g_main_context_default();
   5.278 +  //GMainLoop *loop = g_main_loop_new( io_watcher_context, TRUE );
   5.279  
   5.280 -  GSource *source;
   5.281 +  //GSource *source;
   5.282  
   5.283    if ( channel != NULL ) {
   5.284 -    source = g_io_create_watch( channel, G_IO_IN | G_IO_HUP );
   5.285 -    //src_id = g_io_add_watch( channel, G_IO_IN | G_IO_HUP, 
   5.286 -    //					(GSourceFunc)myth_control_sock_listener, NULL );
   5.287 +    //source = g_io_create_watch( channel, G_IO_IN | G_IO_HUP );
   5.288 +    src_id = g_io_add_watch( channel, G_IO_IN, 
   5.289 +    					(GIOFunc)gmyth_monitor_handler_listener, monitor );
   5.290    } else {
   5.291    	*ret = FALSE;
   5.292    	goto cleanup;
   5.293    }
   5.294  
   5.295 -  g_source_set_callback ( source, (GSourceFunc)gmyth_monitor_handler_listener, NULL, NULL );
   5.296 +  //g_source_set_callback ( source, (GSourceFunc)gmyth_monitor_handler_listener, NULL, NULL );
   5.297  
   5.298 -  g_source_attach( source, io_watcher_context );
   5.299 +  //g_source_attach( source, io_watcher_context );
   5.300    
   5.301 -  if (NULL == source){
   5.302 +  //if (NULL == source){
   5.303 +  if (src_id < 0){
   5.304      gmyth_debug( "[%s] Error adding watch listener function to the IO control channel!\n", __FUNCTION__ );
   5.305      *ret = FALSE;
   5.306      goto cleanup;
   5.307    }
   5.308    
   5.309 -  g_main_loop_run( loop );
   5.310 +  //g_main_loop_run( loop );
   5.311    
   5.312  cleanup:
   5.313 -  if ( source != NULL )
   5.314 -    g_source_unref( source );
   5.315 +  //if ( source != NULL )
   5.316 +  //  g_source_unref( source );
   5.317      
   5.318    //if ( io_watcher_context != NULL )
   5.319    //  g_main_context_unref( io_watcher_context );
   5.320 @@ -473,7 +536,6 @@
   5.321    return ret;
   5.322    
   5.323  }
   5.324 -*/
   5.325  
   5.326  gboolean 
   5.327  gmyth_monitor_handler_start (GMythMonitorHandler *monitor)
   5.328 @@ -483,13 +545,14 @@
   5.329  	
   5.330  	if (!g_thread_supported () ) 	g_thread_init (NULL);
   5.331  	
   5.332 -  monitor_th = g_thread_pool_new( (GFunc)gmyth_monitor_handler_listener, 
   5.333 -  					monitor, 3, TRUE, NULL );
   5.334 +  monitor->monitor_th = g_thread_create( (GThreadFunc)gmyth_monitor_handler_listener, 
   5.335 +  					monitor, TRUE, NULL );
   5.336    					
   5.337 -  g_thread_pool_push( monitor_th, monitor, NULL ); 
   5.338 +  //g_thread_pool_push( monitor_th, monitor, NULL ); 
   5.339    					
   5.340    //if ( ( ret = g_thread_join( monitor_th ) ) == FALSE )
   5.341 -  if ( monitor_th != NULL )
   5.342 +  if ( monitor->monitor_th != NULL )
   5.343 +  //if ( gmyth_monitor_handler_setup( monitor, monitor->event_sock->sd_io_ch ) )
   5.344    {
   5.345    	gmyth_debug ( "\n[%s]\tOK! Starting listener on the MONITOR event socket...[thread location = %p]\n", 
   5.346    				__FUNCTION__, g_thread_self( ) );
     6.1 --- a/gmyth/src/gmyth_monitor_handler.h	Wed Dec 13 23:42:28 2006 +0000
     6.2 +++ b/gmyth/src/gmyth_monitor_handler.h	Wed Dec 13 23:44:04 2006 +0000
     6.3 @@ -71,7 +71,8 @@
     6.4  	guint backend_events_handler_signal_id;
     6.5  
     6.6    /* signal default handlers */
     6.7 -  void (*backend_events_handler) (GMythMonitorHandler *monitor, gint msg_code, gchar* message, gpointer livetv );
     6.8 +  void (*backend_events_handler) (GMythMonitorHandler *monitor, gint msg_code, gchar* message, 
     6.9 +  					gpointer livetv, gpointer user_data );
    6.10  };
    6.11  
    6.12  struct _GMythMonitorHandler
    6.13 @@ -84,6 +85,10 @@
    6.14  	/* socket descriptors */
    6.15  	GMythSocket *event_sock;
    6.16  	
    6.17 +	GThread *monitor_th;
    6.18 +	
    6.19 +	gboolean* (*gmyth_monitor_handler_listener)( GMythMonitorHandler *monitor );
    6.20 +	
    6.21  	gchar *hostname;
    6.22  	gint port;
    6.23