[svn r116] A lot of API changes. trunk
authorrosfran
Mon Nov 27 19:25:35 2006 +0000 (2006-11-27)
branchtrunk
changeset 1159f3c698e34a7
parent 114 04cead6ae39b
child 116 e3cedfae0f2e
[svn r116] A lot of API changes.
gmyth/src/gmyth_context.c
gmyth/src/gmyth_file_transfer.c
gmyth/src/gmyth_file_transfer.h
gmyth/src/gmyth_recorder.c
gmyth/src/gmyth_recorder.h
gmyth/src/gmyth_socket.c
     1.1 --- a/gmyth/src/gmyth_context.c	Mon Nov 27 19:24:43 2006 +0000
     1.2 +++ b/gmyth/src/gmyth_context.c	Mon Nov 27 19:25:35 2006 +0000
     1.3 @@ -176,7 +176,7 @@
     1.4  	int server_port = gmyth_settings_get_backend_port(gcontext->gmyth_settings);
     1.5  
     1.6  	gcontext->server_socket = gmyth_socket_new ();
     1.7 -	if (!gmyth_socket_connect_to_backend (gcontext->server_socket,
     1.8 +	if (!gmyth_socket_connect_to_backend ( gcontext->server_socket,
     1.9  				server_hostname->str, server_port, FALSE)) {
    1.10  		g_warning ("[%s] Socket connection to backend error!", __FUNCTION__);
    1.11  		g_object_unref (gcontext->server_socket);
     2.1 --- a/gmyth/src/gmyth_file_transfer.c	Mon Nov 27 19:24:43 2006 +0000
     2.2 +++ b/gmyth/src/gmyth_file_transfer.c	Mon Nov 27 19:25:35 2006 +0000
     2.3 @@ -74,7 +74,7 @@
     2.4  #define GMYTHTV_ENABLE_DEBUG				1
     2.5  #endif
     2.6  
     2.7 -static gboolean has_io_access = TRUE;
     2.8 +//static gboolean has_io_access = TRUE;
     2.9  
    2.10  enum myth_sock_types {
    2.11    GMYTH_PLAYBACK_TYPE = 0,
    2.12 @@ -85,6 +85,9 @@
    2.13  
    2.14  //static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
    2.15  
    2.16 +//static gboolean* myth_control_sock_listener( GIOChannel *io_channel );
    2.17 +static gboolean myth_control_sock_listener( GIOChannel *io_channel, GIOCondition condition, gpointer data );
    2.18 +
    2.19  static GMutex*			mutex 					 = NULL;
    2.20  
    2.21  static GCond*				io_watcher_cond  = NULL;
    2.22 @@ -98,7 +101,7 @@
    2.23  static void gmyth_file_transfer_finalize (GObject *object);
    2.24  
    2.25  static gboolean gmyth_connect_to_backend (GMythFileTransfer *transfer);
    2.26 -static gboolean myth_init_io_watchers( GMythFileTransfer *transfer );
    2.27 +//static gboolean myth_init_io_watchers( GMythFileTransfer *transfer );
    2.28  
    2.29  void gmyth_file_transfer_close( GMythFileTransfer *transfer );
    2.30  
    2.31 @@ -196,6 +199,135 @@
    2.32  }
    2.33  
    2.34  static gboolean
    2.35 +myth_control_sock_listener( GIOChannel *io_channel, GIOCondition condition, gpointer data )
    2.36 +//myth_control_sock_listener( GIOChannel *io_channel )
    2.37 +{
    2.38 +
    2.39 +  GIOStatus io_status;
    2.40 +  GError *error = NULL;
    2.41 +  GIOCondition io_cond;
    2.42 +  //GIOCondition condition;
    2.43 +  gchar *trash = g_new0( gchar, GMYTHTV_BUFFER_SIZE*10 );
    2.44 +  guint recv = 0;
    2.45 +  //gboolean* ret = g_new0( gboolean, 1 );
    2.46 +  gboolean ret = TRUE;
    2.47 +  
    2.48 +  //*ret = TRUE;
    2.49 +  
    2.50 +  if ( io_channel == NULL ) {
    2.51 +  	ret = FALSE;
    2.52 +  	goto clean_up;
    2.53 +  }
    2.54 +  g_print ("Listening on Monitor socket...!\n");
    2.55 +  	
    2.56 +  //condition = g_io_channel_get_buffer_condition( io_channel );
    2.57 +  
    2.58 +  myth_control_acquire_context (TRUE);
    2.59 +  
    2.60 +  //while ( !has_io_access ) 
    2.61 +  //	g_cond_wait( io_watcher_cond, mutex );
    2.62 +  	
    2.63 +  //myth_control_acquire_context (TRUE);  
    2.64 +  
    2.65 +  gsize len = 0;
    2.66 +  if (condition & G_IO_HUP) {
    2.67 +    g_print ("Read end of pipe died!\n");
    2.68 +    ret = FALSE;
    2.69 +    goto clean_up;
    2.70 +  }
    2.71 +    
    2.72 +  if ( ( condition & G_IO_IN ) != 0 ) {
    2.73 +  	io_status = g_io_channel_set_encoding( io_channel, NULL, &error );
    2.74 +    do 
    2.75 +    {
    2.76 +      //trash = g_new0( gchar, GMYTHTV_BUFFER_SIZE );
    2.77 +
    2.78 +      io_status = g_io_channel_read_chars( io_channel, trash + recv, 
    2.79 +      		GMYTHTV_BUFFER_SIZE, &len, &error);
    2.80 +
    2.81 +      g_print( "[%s] Received data buffer from IO binary channel... %d bytes gone!\n", 
    2.82 +      		__FUNCTION__, len );
    2.83 +      		
    2.84 +      recv += len;
    2.85 +      
    2.86 +      //msg = g_strconcat( msg, g_strdup(trash), NULL );
    2.87 +      
    2.88 +      //if ( trash != NULL )
    2.89 +      //	g_free( trash );
    2.90 +      	
    2.91 +      io_cond = g_io_channel_get_buffer_condition( io_channel );
    2.92 +
    2.93 +    } while ( ( io_cond & G_IO_IN ) != 0 && ( io_status != G_IO_STATUS_ERROR ) );
    2.94 +  }
    2.95 +  //ret = g_io_channel_read_chars ( source, &msg, &len, NULL, &err);
    2.96 +  if ( io_status == G_IO_STATUS_ERROR ) {
    2.97 +    g_print ("[%s] Error reading: %s\n", __FUNCTION__, error != NULL ? error->message : "" );
    2.98 +    ret = FALSE;
    2.99 +  }
   2.100 +  g_print ("\n[%s]\tEVENT: Read %d bytes: %s\n\n", __FUNCTION__, len, trash != NULL ? trash : "[no event data]" );
   2.101 +
   2.102 +  //g_cond_signal( io_watcher_cond );
   2.103 +  
   2.104 +  //myth_control_release_context( );
   2.105 +clean_up:
   2.106 +  return ret;
   2.107 +
   2.108 +}
   2.109 +
   2.110 +static gboolean
   2.111 +gmyth_connect_to_backend_monitor (GMythFileTransfer *transfer)
   2.112 +{
   2.113 +	gboolean ret = TRUE;
   2.114 +	GString *base_str = g_string_new( "" );	
   2.115 +	guint source_id = 0;
   2.116 +
   2.117 +  transfer->event_sock = gmyth_socket_new();
   2.118 +  
   2.119 +  gmyth_socket_connect ( transfer->event_sock, transfer->hostname->str, transfer->port);
   2.120 +  g_string_printf( base_str, "ANN Monitor %s %d", transfer->hostname->str, TRUE );
   2.121 +
   2.122 +  gmyth_socket_send_command( transfer->event_sock, base_str );
   2.123 +  GString *resp = gmyth_socket_receive_response( transfer->event_sock );
   2.124 +  g_print( "[%s] Got Monitor response from %s: %s\n", __FUNCTION__, base_str->str, resp->str );
   2.125 +  //g_thread_create( (GThreadFunc)myth_control_sock_listener, transfer->event_sock->sd_io_ch, TRUE, NULL );
   2.126 +  //io_watcher_context = g_main_context_default();
   2.127 +  GMainLoop *loop = g_main_loop_new( NULL, TRUE );
   2.128 +
   2.129 +  //GSource *source;
   2.130 +
   2.131 +  if ( transfer->event_sock->sd_io_ch != NULL )
   2.132 +    source_id = g_io_add_watch( transfer->event_sock->sd_io_ch, G_IO_IN , (GIOFunc)myth_control_sock_listener, NULL );
   2.133 +  else {
   2.134 +  	ret = FALSE;
   2.135 +  	goto cleanup;
   2.136 +  }
   2.137 +
   2.138 +  //g_source_set_callback ( source, (GSourceFunc)myth_control_sock_listener, NULL, NULL );
   2.139 +
   2.140 +  //g_source_attach( source, io_watcher_context );
   2.141 +
   2.142 +/*
   2.143 +  if (source==NULL) {
   2.144 +    g_printerr( "[%s] Error adding watch listener function to the IO control channel!\n", __FUNCTION__ );
   2.145 +    goto cleanup;
   2.146 +  }
   2.147 +*/
   2.148 +  g_print( "[%s]\tOK! Starting listener on the MONITOR event socket...\n", __FUNCTION__ );
   2.149 +  
   2.150 +  //g_main_loop_run( loop );
   2.151 +
   2.152 +cleanup:
   2.153 +  //if ( source != NULL )
   2.154 +  //  g_source_unref( source );
   2.155 +
   2.156 +  g_debug( "[%s] Watch listener function over the IO control channel? %s!!!\n", 
   2.157 +  			__FUNCTION__, ( ret == TRUE ? "YES" : "NO" ) );
   2.158 +  			
   2.159 +  return ret;
   2.160 +
   2.161 +}
   2.162 +
   2.163 +static gboolean
   2.164  gmyth_connect_to_backend (GMythFileTransfer *transfer)
   2.165  {
   2.166    GString *base_str = g_string_new( "" );
   2.167 @@ -227,7 +359,11 @@
   2.168  
   2.169    strlist = gmyth_string_list_new();
   2.170    hostname = gmyth_socket_get_local_hostname();
   2.171 -  g_string_printf( base_str, "ANN FileTransfer %s 1 -1", hostname->str);
   2.172 +  g_debug( "[%s] MythTV version (from backend) = %d.\n", __FUNCTION__, transfer->control_sock->mythtv_version );
   2.173 +  if ( transfer->control_sock->mythtv_version > 26 )
   2.174 +  	g_string_printf( base_str, "ANN FileTransfer %s 1 -1", hostname->str);
   2.175 +  else
   2.176 +  	g_string_printf( base_str, "ANN FileTransfer %s", hostname->str);
   2.177  
   2.178    gmyth_string_list_append_string (strlist, base_str );
   2.179    gmyth_string_list_append_char_array (strlist, path_dir );
   2.180 @@ -251,6 +387,7 @@
   2.181        return FALSE;
   2.182    }
   2.183    
   2.184 +  gmyth_connect_to_backend_monitor( transfer );
   2.185  
   2.186    if ( strlist != NULL )
   2.187      g_object_unref( strlist );
   2.188 @@ -410,7 +547,7 @@
   2.189  }
   2.190  
   2.191  gint 
   2.192 -gmyth_file_transfer_read(GMythFileTransfer *transfer, void *data, gint size, gboolean read_unlimited)
   2.193 +gmyth_file_transfer_read(GMythFileTransfer *transfer, GByteArray *data, gint size, gboolean read_unlimited)
   2.194  {
   2.195    gsize bytes_sent = 0;
   2.196    gsize bytes_read = 0;
   2.197 @@ -427,6 +564,8 @@
   2.198  
   2.199    GMythStringList *strlist = NULL;
   2.200    GString *query;
   2.201 +  
   2.202 +  gchar *data_buffer = NULL;
   2.203  
   2.204    g_return_val_if_fail ( data != NULL, -2 );
   2.205  
   2.206 @@ -475,13 +614,20 @@
   2.207  
   2.208  	g_print( "[%s] got SENT buffer message = %d\n", __FUNCTION__, bytes_sent );
   2.209        	if ( bytes_sent != 0 ) {
   2.210 +      		
   2.211 +      		data_buffer = g_new0( gchar, bytes_sent );    
   2.212 +      		
   2.213       	    while (total_read != bytes_sent) { 
   2.214  
   2.215 -                io_status = g_io_channel_read_chars( io_channel, data + total_read, 
   2.216 -	  		(gsize) bytes_sent, // buffer_len
   2.217 -		       	&bytes_read, &error );
   2.218 +                io_status = g_io_channel_read_chars( io_channel, data_buffer + total_read, 
   2.219 +	  								(gsize) bytes_sent, // buffer_len
   2.220 +		       					&bytes_read, &error );
   2.221  
   2.222  	        total_read += bytes_read;
   2.223 +	        
   2.224 +	        /* append new data to the increasing byte array */
   2.225 +		  		data = g_byte_array_append ( data, g_memdup( data_buffer, bytes_read ), bytes_read );
   2.226 +	        
   2.227  		printf ("Total file transfer data read: %d\n", total_read);
   2.228  	    }
   2.229  	}
     3.1 --- a/gmyth/src/gmyth_file_transfer.h	Mon Nov 27 19:24:43 2006 +0000
     3.2 +++ b/gmyth/src/gmyth_file_transfer.h	Mon Nov 27 19:25:35 2006 +0000
     3.3 @@ -78,6 +78,7 @@
     3.4  
     3.5  	/* socket descriptors */
     3.6  	GMythSocket *control_sock;
     3.7 +	GMythSocket *event_sock;
     3.8  	GMythSocket *sock;
     3.9  
    3.10  	gint64 readposition;
    3.11 @@ -106,11 +107,10 @@
    3.12  void gmyth_file_transfer_close (GMythFileTransfer *transfer);
    3.13  
    3.14  gboolean gmyth_file_transfer_is_open (GMythFileTransfer *transfer);
    3.15 -gint gmyth_file_transfer_read(GMythFileTransfer *transfer, void *data, gint size, gboolean read_unlimited);
    3.16 +gint gmyth_file_transfer_read(GMythFileTransfer *transfer, GByteArray *data, gint size, gboolean read_unlimited);
    3.17  gint64 gmyth_file_transfer_seek(GMythFileTransfer *transfer, guint64 pos, gint whence);
    3.18  gboolean gmyth_file_transfer_settimeout( GMythFileTransfer *transfer, gboolean fast);
    3.19  
    3.20 -
    3.21  //gboolean gmyth_file_transfer_playback_setup( GMythFileTransfer **transfer, gboolean live_tv );
    3.22  //gboolean gmyth_file_transfer_setup( GMythFileTransfer **transfer, gboolean live_tv );
    3.23  //gboolean gmyth_file_transfer_livetv_setup( GMythFileTransfer **transfer, GMythSocket *live_sock );
     4.1 --- a/gmyth/src/gmyth_recorder.c	Mon Nov 27 19:24:43 2006 +0000
     4.2 +++ b/gmyth/src/gmyth_recorder.c	Mon Nov 27 19:25:35 2006 +0000
     4.3 @@ -32,6 +32,9 @@
     4.4  #include <assert.h>
     4.5  
     4.6  #include "gmyth_stringlist.h"
     4.7 +#include "gmyth_util.h"
     4.8 +
     4.9 +#define	 GMYTHTV_RECORDER_HEADER			"QUERY_RECORDER"
    4.10  
    4.11  static void gmyth_recorder_class_init          (GMythRecorderClass *klass);
    4.12  static void gmyth_recorder_init                (GMythRecorder *object);
    4.13 @@ -112,8 +115,8 @@
    4.14  		
    4.15  		recorder->myth_socket = gmyth_socket_new ();
    4.16  		
    4.17 -		if (!gmyth_socket_connect_to_backend (recorder->myth_socket, recorder->hostname->str, 
    4.18 -					recorder->port, TRUE) ) {
    4.19 +		if (!gmyth_socket_connect_to_backend ( recorder->myth_socket, recorder->hostname->str, 
    4.20 +					recorder->port, TRUE ) ) {
    4.21  			g_warning ("GMythRemoteEncoder: Connection to backend failed");	
    4.22  			return FALSE;
    4.23  		}
    4.24 @@ -136,14 +139,13 @@
    4.25  gmyth_recorder_spawntv (GMythRecorder *recorder, GString *tvchain_id)
    4.26  {
    4.27  	GMythStringList *str_list;
    4.28 -	GString *tmp_str;
    4.29 +	GString *tmp_str = g_string_new( GMYTHTV_RECORDER_HEADER );
    4.30  	
    4.31  	g_debug ("[%s] Spawntv with tvchain_id = %s", __FUNCTION__, tvchain_id->str);
    4.32  	
    4.33  	str_list = gmyth_string_list_new ();
    4.34  	
    4.35 -	tmp_str = g_string_new ("QUERY_RECORDER ");
    4.36 -	g_string_append_printf (tmp_str, "%d", recorder->recorder_num); 
    4.37 +	g_string_append_printf ( tmp_str, " %d", recorder->recorder_num ); 
    4.38  	
    4.39  	gmyth_string_list_append_string (str_list, tmp_str);
    4.40  	gmyth_string_list_append_string (str_list, g_string_new ("SPAWN_LIVETV"));
    4.41 @@ -180,15 +182,14 @@
    4.42  gmyth_recorder_stop_livetv (GMythRecorder *recorder)
    4.43  {
    4.44  	GMythStringList *str_list;
    4.45 -	GString *tmp_str;
    4.46 +	GString *tmp_str = g_string_new( GMYTHTV_RECORDER_HEADER );
    4.47  
    4.48  	g_debug ("[%s]", __FUNCTION__);
    4.49  
    4.50  	str_list = gmyth_string_list_new ();
    4.51  
    4.52 -	tmp_str = g_string_new ("QUERY_RECORDER ");
    4.53 -	g_string_append_printf (tmp_str, "%d", recorder->recorder_num); 
    4.54 -	gmyth_string_list_append_string (str_list, g_string_new ("STOP_LIVETV"));
    4.55 +	g_string_append_printf ( tmp_str, " %d", recorder->recorder_num ); 
    4.56 +	gmyth_string_list_append_char_array( str_list, "STOP_LIVETV" );
    4.57  
    4.58  	gmyth_socket_sendreceive_stringlist (recorder->myth_socket, str_list);
    4.59  
    4.60 @@ -216,14 +217,13 @@
    4.61  gmyth_recorder_send_frontend_ready_command (GMythRecorder *recorder)
    4.62  {
    4.63  	GMythStringList *str_list;
    4.64 -	GString *tmp_str;
    4.65 +	GString *tmp_str = g_string_new( GMYTHTV_RECORDER_HEADER );
    4.66  	
    4.67 -	g_debug ( "[%s] FRONTEND_READY with recorder id = %s", __FUNCTION__, recorder->recorder_num );
    4.68 +	g_debug ( "[%s] FRONTEND_READY with recorder id = %d\n", __FUNCTION__, recorder->recorder_num );
    4.69  	
    4.70  	str_list = gmyth_string_list_new ();
    4.71  	
    4.72 -	tmp_str = g_string_new ("QUERY_RECORDER ");
    4.73 -	g_string_append_printf (tmp_str, "%d", recorder->recorder_num); 
    4.74 +	g_string_append_printf ( tmp_str, " %d", recorder->recorder_num ); 
    4.75  	
    4.76  	gmyth_string_list_append_string (str_list, tmp_str);
    4.77  	gmyth_string_list_append_string (str_list, g_string_new ("FRONTEND_READY"));
    4.78 @@ -260,14 +260,13 @@
    4.79  gmyth_recorder_check_channel (GMythRecorder *recorder, gint channel)
    4.80  {
    4.81  	GMythStringList *str_list;
    4.82 -	GString *tmp_str;
    4.83 +	GString *tmp_str = g_string_new( GMYTHTV_RECORDER_HEADER );
    4.84  	
    4.85  	g_debug ("[%s] SET_CHANNEL with channel = %d", __FUNCTION__, channel);
    4.86  	
    4.87  	str_list = gmyth_string_list_new ();
    4.88  	
    4.89 -	tmp_str = g_string_new ("QUERY_RECORDER ");
    4.90 -	g_string_append_printf (tmp_str, "%d", recorder->recorder_num); 
    4.91 +	g_string_append_printf ( tmp_str, " %d", recorder->recorder_num ); 
    4.92  	
    4.93  	gmyth_string_list_append_string (str_list, tmp_str);
    4.94  	gmyth_string_list_append_string (str_list, g_string_new ("CHECK_CHANNEL"));
    4.95 @@ -305,14 +304,13 @@
    4.96  gmyth_recorder_set_channel (GMythRecorder *recorder, gint channel)
    4.97  {
    4.98  	GMythStringList *str_list;
    4.99 -	GString *tmp_str;
   4.100 +	GString *tmp_str = g_string_new( GMYTHTV_RECORDER_HEADER );
   4.101  	
   4.102  	g_debug ("[%s] SET_CHANNEL with channel = %d", __FUNCTION__, channel);
   4.103  	
   4.104  	str_list = gmyth_string_list_new ();
   4.105  	
   4.106 -	tmp_str = g_string_new ("QUERY_RECORDER ");
   4.107 -	g_string_append_printf (tmp_str, "%d", recorder->recorder_num); 
   4.108 +	g_string_append_printf ( tmp_str, " %d", recorder->recorder_num ); 
   4.109  	
   4.110  	gmyth_string_list_append_string (str_list, tmp_str);
   4.111  	gmyth_string_list_append_string (str_list, g_string_new ("SET_CHANNEL"));
   4.112 @@ -338,3 +336,36 @@
   4.113      return TRUE;
   4.114  
   4.115  }
   4.116 +
   4.117 +gint64
   4.118 +gmyth_recorder_get_file_position ( GMythRecorder *recorder )
   4.119 +{
   4.120 +  gint64 pos = 0;
   4.121 +  GString *query = g_string_new( GMYTHTV_RECORDER_HEADER );
   4.122 +
   4.123 +  GMythStringList *str_list = gmyth_string_list_new ();
   4.124 +
   4.125 +  g_string_append_printf( query, " %d", recorder->recorder_num );
   4.126 +
   4.127 +  gmyth_string_list_append_string (str_list, query);
   4.128 +  gmyth_string_list_append_char_array( str_list, "GET_FILE_POSITION" );
   4.129 +
   4.130 +  gmyth_socket_sendreceive_stringlist ( recorder->myth_socket, str_list );
   4.131 +
   4.132 +  if ( str_list != NULL && gmyth_string_list_length(str_list) > 0 ) 
   4.133 +  {
   4.134 +    GString *str = NULL;
   4.135 +    if ( ( str = gmyth_string_list_get_string( str_list, 0 ) ) != NULL && strstr ( str->str, "bad" ) == NULL )
   4.136 +      pos = gmyth_util_decode_long_long( str_list, 0 );
   4.137 +  } 
   4.138 +
   4.139 +#ifndef GMYTHTV_ENABLE_DEBUG
   4.140 +  g_print( "[%s] Got file position = %lld\n", __FUNCTION__, pos );
   4.141 +#endif
   4.142 +  if (str_list!=NULL)
   4.143 +    g_object_unref (str_list);
   4.144 +
   4.145 +  return pos;
   4.146 +
   4.147 +}
   4.148 +
     5.1 --- a/gmyth/src/gmyth_recorder.h	Mon Nov 27 19:24:43 2006 +0000
     5.2 +++ b/gmyth/src/gmyth_recorder.h	Mon Nov 27 19:25:35 2006 +0000
     5.3 @@ -90,6 +90,8 @@
     5.4  
     5.5  gboolean gmyth_recorder_set_channel   (GMythRecorder *recorder,
     5.6                                           gint channel);
     5.7 +                                         
     5.8 +gint64 	 gmyth_recorder_get_file_position ( GMythRecorder *recorder );
     5.9  
    5.10  G_END_DECLS
    5.11  
     6.1 --- a/gmyth/src/gmyth_socket.c	Mon Nov 27 19:24:43 2006 +0000
     6.2 +++ b/gmyth/src/gmyth_socket.c	Mon Nov 27 19:25:35 2006 +0000
     6.3 @@ -148,8 +148,8 @@
     6.4  	
     6.5  	GList *local_addrs = NULL;
     6.6  	
     6.7 -	struct ifaddrs *ifaddr;	
     6.8 -	struct ifaddrs *i;
     6.9 +	struct ifaddrs *ifaddr = g_new0( struct ifaddrs, 1 );	
    6.10 +	struct ifaddrs *i = g_new0( struct ifaddrs, 1 );
    6.11  
    6.12  	gchar addr[NI_MAXHOST+1];
    6.13  	gchar *ifname;