[svn r102] Removed unused code, remade buffer fillment in the file transfer read methods. trunk
authorrosfran
Wed Nov 22 19:53:52 2006 +0000 (2006-11-22)
branchtrunk
changeset 101cf4a26332b8c
parent 100 f4be69980934
child 102 4f7789e3bfd4
[svn r102] Removed unused code, remade buffer fillment in the file transfer read methods.
gmyth/src/gmyth_file_transfer.c
gmyth/src/gmyth_socket.c
gmyth/src/gmyth_socket.h
     1.1 --- a/gmyth/src/gmyth_file_transfer.c	Wed Nov 22 14:37:47 2006 +0000
     1.2 +++ b/gmyth/src/gmyth_file_transfer.c	Wed Nov 22 19:53:52 2006 +0000
     1.3 @@ -37,6 +37,7 @@
     1.4  #include "gmyth_livetv.h"
     1.5  #include "gmyth_util.h"
     1.6  #include "gmyth_socket.h"
     1.7 +#include "gmyth_context.h"
     1.8  #include "gmyth_stringlist.h"
     1.9  
    1.10  #include <unistd.h>
    1.11 @@ -167,7 +168,7 @@
    1.12    G_OBJECT_CLASS (gmyth_file_transfer_parent_class)->dispose (object);
    1.13  }
    1.14  
    1.15 -  static void
    1.16 +static void
    1.17  gmyth_file_transfer_finalize (GObject *object)
    1.18  {
    1.19    g_signal_handlers_destroy (object);
    1.20 @@ -181,8 +182,7 @@
    1.21    GMythFileTransfer *transfer = GMYTH_FILE_TRANSFER ( g_object_new (
    1.22  	GMYTH_FILE_TRANSFER_TYPE, FALSE ));
    1.23  
    1.24 -  if ( mythtv_version > 0 )
    1.25 -    transfer->mythtv_version = mythtv_version;
    1.26 +  transfer->mythtv_version = MYTHTV_CANNOT_NEGOTIATE_VERSION;
    1.27  
    1.28    transfer->card_id = num;
    1.29  
    1.30 @@ -269,7 +269,6 @@
    1.31  
    1.32    printf("[%s] Running config to the %s...\n", __FUNCTION__, live_tv ? "LiveTV" : "FileTransfer" );
    1.33  
    1.34 -#if 0
    1.35    /* configure the control socket */
    1.36    if ((*transfer)->event_sock == NULL) { 
    1.37  
    1.38 @@ -282,8 +281,6 @@
    1.39      g_warning("Remote transfer control socket already created.\n");
    1.40    }
    1.41  
    1.42 -#endif
    1.43 -
    1.44    /* configure the socket */
    1.45    if ( (*transfer)->sock == NULL ) { 
    1.46  
    1.47 @@ -293,8 +290,54 @@
    1.48        g_printerr ("Connection to backend failed (Raw Transfer Socket).\n");
    1.49        ret = FALSE;
    1.50      }
    1.51 +    
    1.52 +    GIOChannel* io_channel = (*transfer)->sock->sd_io_ch, 
    1.53 +    			*io_channel_control = (*transfer)->control_sock->sd_io_ch;
    1.54 +    GIOCondition io_cond, io_cond_control;
    1.55 +    GIOStatus io_status = G_IO_STATUS_NORMAL;
    1.56 +    GError *error = NULL;
    1.57 +    gchar *trash = NULL;
    1.58 +    guint bytes_read = 0;    
    1.59 +    
    1.60 +    io_cond = g_io_channel_get_buffer_condition( io_channel );    
    1.61 +	
    1.62 +	  if ( ( io_cond & G_IO_IN ) != 0 ) {
    1.63 +	    do 
    1.64 +	    {
    1.65 +	      trash = g_new0( gchar, GMYTHTV_BUFFER_SIZE );
    1.66 +	
    1.67 +	      io_status = g_io_channel_read_chars( io_channel, trash, 
    1.68 +	      		GMYTHTV_BUFFER_SIZE, &bytes_read, &error);
    1.69 +	
    1.70 +	      g_print( "[%s] cleaning buffer on IO binary channel... %d bytes gone!\n", 
    1.71 +	      		__FUNCTION__, bytes_read );
    1.72 +	      
    1.73 +	      if ( trash != NULL )
    1.74 +	      	g_free( trash );
    1.75 +	      	
    1.76 +	      io_cond = g_io_channel_get_buffer_condition( io_channel );
    1.77 +	
    1.78 +	    } while ( ( io_cond & G_IO_IN ) != 0 && ( io_status != G_IO_STATUS_ERROR ) && (error == NULL) );
    1.79 +	
    1.80 +	  }
    1.81 +	  
    1.82 +	  io_cond_control = g_io_channel_get_buffer_condition( io_channel_control );
    1.83 +	  
    1.84 +	  if ( ( io_cond_control & G_IO_IN ) != 0 ) {
    1.85 +	  	g_print( "[%s] Finishind reading function: cleaning all data on the I/O control socket...\n", __FUNCTION__ );  
    1.86 +		  while ( gmyth_socket_read_stringlist( (*transfer)->control_sock, strlist ) > 0 )
    1.87 +		  {
    1.88 +		    if ( strlist != NULL && gmyth_string_list_length(strlist) > 0 ) 
    1.89 +		    {
    1.90 +					gint backend_code = gmyth_string_list_get_int( strlist, 0 ); // -1 on backend error
    1.91 +					g_print( "[%s]\t backend code = %d -\t"\
    1.92 +					 		"[%s prepared for reading]! (G_IO_IN) !!!\n\n", __FUNCTION__, 
    1.93 +		    			backend_code, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
    1.94 +		    }
    1.95 +		  }	
    1.96 +	  }
    1.97  
    1.98 -    if ( !(*transfer)->live_tv && (*transfer)->control_sock != NULL) {
    1.99 +    if ( /*!(*transfer)->live_tv && */ (*transfer)->control_sock != NULL ) {
   1.100        strlist = gmyth_string_list_new();
   1.101        g_string_printf ( (*transfer)->query, "%s %d", GMYTHTV_QUERY_HEADER, (*transfer)->recordernum );
   1.102  
   1.103 @@ -330,21 +373,13 @@
   1.104    //myth_control_acquire_context (TRUE);
   1.105  
   1.106    gchar *path_dir = gmyth_uri_getpath( (*transfer)->uri );
   1.107 -  //g_print( "\t--> %s: path_dir = %s\n", __FUNCTION__, path_dir );
   1.108  
   1.109    gchar *stype = g_strdup( "" );
   1.110  
   1.111 -  //  if ( (*transfer)->live_tv == FALSE ) {
   1.112 -
   1.113    sock = gmyth_socket_new();
   1.114  
   1.115    gmyth_socket_connect( &sock, (*transfer)->hostname->str, (*transfer)->port );
   1.116  
   1.117 -  /*
   1.118 -     } else {
   1.119 -     sock = (*transfer)->sock;
   1.120 -     }
   1.121 -     */
   1.122  #ifdef GMYTHTV_ENABLE_DEBUG
   1.123  
   1.124    g_print( "[%s] --> Creating socket... (%s, %d)\n", __FUNCTION__, (*transfer)->hostname->str, (*transfer)->port );
   1.125 @@ -355,7 +390,9 @@
   1.126    GString *hostname = g_string_new( gmyth_uri_gethost( (*transfer)->uri ) );
   1.127    GString *base_str = g_string_new( "" );
   1.128  
   1.129 -  if ( gmyth_socket_check_protocol_version_number (sock, (*transfer)->mythtv_version) ) {
   1.130 +  if ( (*transfer)->mythtv_version != MYTHTV_CANNOT_NEGOTIATE_VERSION || 
   1.131 +  			( (*transfer)->mythtv_version = gmyth_socket_check_protocol_version_number (sock, 
   1.132 +  					MYTHTV_VERSION_DEFAULT ) ) != MYTHTV_CANNOT_NEGOTIATE_VERSION ) {
   1.133  
   1.134      if (sock == NULL) {
   1.135        stype = (sock_type==GMYTH_PLAYBACK_TYPE) ? "control socket" : "file data socket";
   1.136 @@ -374,7 +411,8 @@
   1.137      if ( sock_type == GMYTH_PLAYBACK_TYPE )
   1.138      {
   1.139        (*transfer)->control_sock = sock;
   1.140 -      g_string_printf( base_str, "ANN Playback %s %d", hostname->str, FALSE );
   1.141 +      
   1.142 +     	g_string_printf( base_str, "ANN Playback %s %d", hostname->str, FALSE );
   1.143  
   1.144        gmyth_socket_send_command( (*transfer)->control_sock, base_str );
   1.145        GString *resp = gmyth_socket_receive_response( (*transfer)->control_sock );
   1.146 @@ -390,18 +428,22 @@
   1.147        GString *resp = gmyth_socket_receive_response( (*transfer)->event_sock );
   1.148        g_print( "[%s] Got Monitor response from %s: %s\n", __FUNCTION__, base_str->str, resp->str );
   1.149        //g_thread_create( myth_init_io_watchers, (void*)(*transfer), FALSE, NULL );
   1.150 -      g_thread_create( myth_init_io_watchers, (*transfer), FALSE, NULL );
   1.151 +      //g_thread_create( myth_init_io_watchers, (*transfer), FALSE, NULL );
   1.152  
   1.153 -      g_printerr( "[%s] Watch listener function to the IO control channel on thread %p.\n", __FUNCTION__, g_thread_self() );
   1.154 +      g_printerr( "[%s] Watch listener function to the IO control channel on thread %p.\n", 
   1.155 +      			__FUNCTION__, g_thread_self() );
   1.156  
   1.157      }
   1.158      else if ( sock_type == GMYTH_FILETRANSFER_TYPE )
   1.159      {
   1.160        (*transfer)->sock = sock;
   1.161        strlist = gmyth_string_list_new();
   1.162 -      g_string_printf( base_str, "ANN FileTransfer %s %d %d", hostname->str,
   1.163 +      
   1.164 +      if ( gmyth_socket_get_protocol_version( (*transfer)->control_sock ) > 26 )      
   1.165 +      	g_string_printf( base_str, "ANN FileTransfer %s %d %d", hostname->str,
   1.166          		(*transfer)->userreadahead, (*transfer)->retries );
   1.167 -      //g_string_printf( base_str, "ANN FileTransfer %s", hostname->str );
   1.168 +      else
   1.169 +      	g_string_printf( base_str, "ANN FileTransfer %s", hostname->str );
   1.170  
   1.171        gmyth_string_list_append_string( strlist, base_str );
   1.172        gmyth_string_list_append_char_array( strlist, path_dir );
   1.173 @@ -745,16 +787,8 @@
   1.174    GIOCondition io_cond;
   1.175    gchar *trash = g_new0( gchar, GMYTHTV_BUFFER_SIZE*10 );
   1.176    guint recv = 0;
   1.177 -  //gchar *msg = g_strdup("");
   1.178    
   1.179 -  while ( !has_io_access ) 
   1.180 -  	g_cond_wait( io_watcher_cond, mutex );  
   1.181 -    
   1.182 -  //myth_control_acquire_context (TRUE);
   1.183 -  
   1.184 -  has_io_access = FALSE;
   1.185 -  
   1.186 -  g_mutex_lock( mutex );
   1.187 +  myth_control_acquire_context (TRUE);
   1.188    
   1.189    gsize len = 0;
   1.190    if (condition & G_IO_HUP)
   1.191 @@ -788,12 +822,7 @@
   1.192      g_print ("[%s] Error reading: %s\n", __FUNCTION__, error != NULL ? error->message : "" );
   1.193    g_print ("\n[%s]\tEVENT: Read %d bytes: %s\n\n", __FUNCTION__, len, trash != NULL ? trash : "[no event data]" );
   1.194    
   1.195 -  has_io_access = TRUE;
   1.196 -    
   1.197 -  //myth_control_release_context( );
   1.198 -  g_cond_broadcast( io_watcher_cond );
   1.199 -  
   1.200 -  g_mutex_unlock( mutex );    
   1.201 +  myth_control_release_context( );
   1.202    
   1.203    return TRUE;
   1.204  
   1.205 @@ -872,27 +901,15 @@
   1.206    GIOCondition io_cond_control;
   1.207    GIOStatus io_status = G_IO_STATUS_NORMAL, io_status_control = G_IO_STATUS_NORMAL;   
   1.208  
   1.209 -  gint buf_len = GMYTHTV_BUFFER_SIZE;
   1.210 -
   1.211    GMythStringList *strlist = NULL;
   1.212    GError *error = NULL;
   1.213 -  gboolean found_backend_msg = FALSE;
   1.214  
   1.215    gchar *trash = g_strdup("");
   1.216  
   1.217    g_return_val_if_fail ( data != NULL, -2 );
   1.218 -  
   1.219 -  #if 0
   1.220 -  while ( !has_io_access ) 
   1.221 -  	g_cond_wait( io_watcher_cond, mutex );
   1.222 -  	
   1.223 -  has_io_access = FALSE;
   1.224 -  #endif
   1.225 -  
   1.226 +
   1.227    myth_control_acquire_context (FALSE);
   1.228    
   1.229 -  //g_mutex_lock( mutex );
   1.230 -
   1.231    /* gets the size of the entire file, if the size requested is lesser than 0 */
   1.232    if ( size <= 0 )
   1.233      size = transfer->filesize;
   1.234 @@ -900,10 +917,6 @@
   1.235    io_channel = transfer->sock->sd_io_ch;
   1.236    io_channel_control = transfer->control_sock->sd_io_ch;
   1.237  
   1.238 -  //g_io_channel_set_flags( io_channel, G_IO_FLAG_APPEND | 
   1.239 -  //    G_IO_STATUS_AGAIN | G_IO_FLAG_IS_READABLE | G_IO_FLAG_IS_WRITEABLE | 
   1.240 -  //    G_IO_FLAG_IS_SEEKABLE, NULL );
   1.241 -
   1.242    io_status = g_io_channel_set_encoding( io_channel, NULL, &error );
   1.243    if ( io_status == G_IO_STATUS_NORMAL )
   1.244      g_print( "[%s] Setting encoding to binary data socket).\n", __FUNCTION__ );
   1.245 @@ -926,197 +939,84 @@
   1.246      goto cleanup;
   1.247    }
   1.248  
   1.249 -  /*
   1.250 -     if (!controlSock->isOpen() || controlSock->error())
   1.251 -     return -1;
   1.252 -     */
   1.253 -
   1.254 -  if ( ( io_cond & G_IO_IN ) != 0 ) {
   1.255 -    do 
   1.256 -    {
   1.257 -      trash = g_new0( gchar, GMYTHTV_BUFFER_SIZE );
   1.258 -
   1.259 -      io_status = g_io_channel_read_chars( io_channel, trash, 
   1.260 -      		GMYTHTV_BUFFER_SIZE, &bytes_read, &error);
   1.261 -
   1.262 -      g_print( "[%s] cleaning buffer on IO binary channel... %d bytes gone!\n", 
   1.263 -      		__FUNCTION__, bytes_read );
   1.264 -      
   1.265 -      if ( trash != NULL )
   1.266 -      	g_free( trash );
   1.267 -      	
   1.268 -      io_cond = g_io_channel_get_buffer_condition( io_channel );
   1.269 -
   1.270 -    } while ( ( io_cond & G_IO_IN ) != 0 && ( io_status != G_IO_STATUS_ERROR ) && (error == NULL) );
   1.271 -
   1.272 -    //if ( trash!= NULL )
   1.273 -    //  g_free( trash );
   1.274 -  }
   1.275 -
   1.276 -  if ( ( io_cond_control & G_IO_IN ) != 0 ) {
   1.277 -    GMythStringList *strlist_tmp = gmyth_string_list_new();
   1.278 -    gmyth_socket_read_stringlist( transfer->control_sock, strlist_tmp );
   1.279 -    g_object_unref( strlist_tmp );
   1.280 -  }
   1.281 -
   1.282    wait_to_transfer = 0;
   1.283  
   1.284 -  //g_thread_create( myth_init_io_watchers, (void*)transfer, FALSE, NULL );
   1.285 -  //g_printerr( "[%s] Watch listener function to the IO control channel on thread %p.\n", __FUNCTION__, g_thread_self() );
   1.286 -
   1.287 -  //g_mutex_lock (mutex);
   1.288 -  //strlist = gmyth_string_list_new();
   1.289 -
   1.290 -  g_string_printf ( transfer->query, "%s %d",
   1.291 -  	  /*transfer->live_tv ? GMYTHTV_RECORDER_HEADER :*/  GMYTHTV_QUERY_HEADER, 
   1.292 -      /* transfer->live_tv ? transfer->card_id :*/ transfer->recordernum ); // transfer->recordernum
   1.293 +  g_string_printf ( transfer->query, "%s %d", GMYTHTV_QUERY_HEADER, 
   1.294 +      transfer->recordernum ); // transfer->recordernum
   1.295    g_print( "\t[%s] Transfer_query = %s\n", __FUNCTION__, transfer->query->str );
   1.296    
   1.297    sent = size;
   1.298    remaining = size - recv;  
   1.299 -  //g_mutex_unlock( mutex );
   1.300 -  //data = (void*)g_new0( gchar, size );
   1.301  
   1.302 -  //g_mutex_lock( mutex );
   1.303 +  while ( recv < size  && !response )
   1.304 +  {
   1.305  
   1.306 -  io_cond = g_io_channel_get_buffer_condition( io_channel );
   1.307 -  
   1.308 -  if ( ( io_cond_control & G_IO_IN ) != 0 ) {
   1.309 -  	g_print( "[%s] Finishind reading function: cleaning all data on the I/O control socket...\n", __FUNCTION__ );  
   1.310 -	  while ( gmyth_socket_read_stringlist( transfer->control_sock, strlist ) > 0 )
   1.311 -	  {
   1.312 -	    if ( strlist != NULL && gmyth_string_list_length(strlist) > 0 ) 
   1.313 -	    {
   1.314 -				gint backend_code = gmyth_string_list_get_int( strlist, 0 ); // -1 on backend error
   1.315 -				g_print( "[%s]\t backend code = %d -\t"\
   1.316 -				 		"[%s prepared for reading]! (G_IO_IN) !!!\n\n", __FUNCTION__, 
   1.317 -	    			backend_code, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
   1.318 -	    }
   1.319 -	  }  	
   1.320 -  }
   1.321 -
   1.322 -  while ( recv < size  && !response )//&& ( io_cond & G_IO_IN ) != 0 )
   1.323 -  {
   1.324 -  	//g_io_channel_flush( io_channel_control, NULL );
   1.325 -  	gint backend_msg_count = 1;
   1.326 -
   1.327 -resend_request:
   1.328      strlist = gmyth_string_list_new();
   1.329      gmyth_string_list_append_char_array( strlist, transfer->query->str );
   1.330 -    gmyth_string_list_append_char_array( strlist, 
   1.331 -    		/*transfer->live_tv ? "REQUEST_BLOCK_RINGBUF" :*/ "REQUEST_BLOCK" );
   1.332 +    gmyth_string_list_append_char_array( strlist, "REQUEST_BLOCK" );
   1.333      gmyth_string_list_append_int( strlist, remaining );
   1.334  		gmyth_socket_write_stringlist( transfer->control_sock, strlist );
   1.335  				
   1.336 -		/* iterates until find some non-MythTV backend message */
   1.337 -		do {		
   1.338 -	    gmyth_socket_read_stringlist( transfer->control_sock, strlist );
   1.339 -	    if ( strlist != NULL && gmyth_string_list_length( strlist ) > 0 ) 
   1.340 -	    { 
   1.341 -	    	if ( gmyth_file_transfer_is_backend_message( transfer, strlist ) )
   1.342 -	    	{
   1.343 -	    		recv = 0;
   1.344 -	    		sent = 0;
   1.345 -	    		response = FALSE;
   1.346 -	    		found_backend_msg = TRUE;
   1.347 -	    		goto cleanup;
   1.348 -	    	}
   1.349 -	    	else 
   1.350 -	    	{
   1.351 -	      	sent = gmyth_string_list_get_int( strlist,  0 ); // -1 on backend error
   1.352 -	      	g_print( "[%s] got SENT buffer message = %d\n", __FUNCTION__, sent );
   1.353 -	      	if ( sent != 0 )
   1.354 -	      	{
   1.355 -	      		g_print( "[%s]\t received = %d bytes, backend says %d bytes sent, "\
   1.356 -	      				"io_cond %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__, 
   1.357 -		  					recv, sent, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
   1.358 -	
   1.359 -			  		if ( sent == remaining )
   1.360 -			  		{
   1.361 -			  			g_print( "[%s]\t\tsent %d, which is equals to requested size = %d\n\n", 
   1.362 -			  						__FUNCTION__, sent, remaining );
   1.363 -			  			break;
   1.364 -			  		}
   1.365 -			  		else
   1.366 -			  		{
   1.367 -			  			g_print( "[%s]\t\tsent %d, which is NOT equals to requested size = %d\n\n", 
   1.368 -			  						__FUNCTION__, sent, remaining );
   1.369 -			  			size = remaining = sent;
   1.370 -			  			if ( sent < 0 ) {
   1.371 -				  			if ( --backend_msg_count > 0 )
   1.372 -				  				goto resend_request;
   1.373 -				  			else
   1.374 -				  				goto cleanup;
   1.375 -			  			} else
   1.376 -			  				break;
   1.377 -			  		}
   1.378 -	      	} else {
   1.379 -						if ( --backend_msg_count > 0 )
   1.380 -						{							
   1.381 -	      			g_usleep( 200 );
   1.382 -		  				goto resend_request;
   1.383 -						}	else
   1.384 -		  				goto cleanup;	      		
   1.385 -	      	} // if
   1.386 -	    	}
   1.387 -	    }
   1.388 -		} while ( found_backend_msg && --backend_msg_count > 0 );
   1.389 +		/* see the number of bytes transferred by the MythTV backend */
   1.390 +    gmyth_socket_read_stringlist( transfer->control_sock, strlist );
   1.391 +    if ( strlist != NULL && gmyth_string_list_length( strlist ) > 0 ) 
   1.392 +    { 
   1.393 +    	sent = gmyth_string_list_get_int( strlist,  0 ); // -1 on backend error
   1.394 +    	g_print( "[%s] got SENT buffer message = %d\n", __FUNCTION__, sent );
   1.395 +    	if ( sent != 0 )
   1.396 +    	{
   1.397 +    		g_print( "[%s]\t received = %d bytes, backend says %d bytes sent, "\
   1.398 +    				"io_cond %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__, 
   1.399 +  					recv, sent, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
   1.400 +
   1.401 +	  		if ( sent == remaining )
   1.402 +	  		{
   1.403 +	  			g_print( "[%s]\t\tsent %d, which is equals to requested size = %d\n\n", 
   1.404 +	  						__FUNCTION__, sent, remaining );	  			
   1.405 +	  		}
   1.406 +	  		else
   1.407 +	  		{
   1.408 +	  			g_print( "[%s]\t\tsent %d, which is NOT equals to requested size = %d\n\n", 
   1.409 +	  						__FUNCTION__, sent, remaining );
   1.410 +	  			size = remaining = sent;
   1.411 +	  			if ( sent < 0 ) {
   1.412 +	  				goto cleanup;
   1.413 +	  			} 
   1.414 +	  		}
   1.415 +    	} else {
   1.416 +  				goto cleanup;	      		
   1.417 +    	} // if
   1.418 +  	}
   1.419  
   1.420      guint count_bytes = 0;
   1.421        
   1.422 -    do
   1.423 +    bytes_read = 0;
   1.424 +
   1.425 +    io_status = g_io_channel_read_chars( io_channel, data + recv, 
   1.426 +  		remaining, &bytes_read, &error );
   1.427 +
   1.428 +    if ( bytes_read > 0 )
   1.429      {
   1.430 -      if ( remaining > GMYTHTV_BUFFER_SIZE ) {
   1.431 -      	buf_len = GMYTHTV_BUFFER_SIZE;
   1.432 -      } else {
   1.433 -      	buf_len = remaining;
   1.434 -      }
   1.435 -	  
   1.436 -      bytes_read = 0;
   1.437 +		  recv += bytes_read;
   1.438 +		  count_bytes += bytes_read;
   1.439 +		  remaining -= bytes_read;
   1.440 +		  g_print( "[%s] Reading buffer (bytes read = %d, remaining = %d)\n", __FUNCTION__, bytes_read, remaining );
   1.441 +		  if ( remaining == 0 ) {
   1.442 +		  	response = TRUE;
   1.443 +		  	break;
   1.444 +		  }
   1.445  
   1.446 -      io_status = g_io_channel_read_chars( io_channel, data + recv, 
   1.447 -	  		buf_len, &bytes_read, &error );
   1.448 +    } else {
   1.449 +	    if ( io_status == G_IO_STATUS_EOF ) {
   1.450 +				g_print( "[%s] got EOS!", __FUNCTION__ );
   1.451 +	    } else if ( io_status == G_IO_STATUS_ERROR ) {
   1.452 +				g_print( "[%s] gmyth_file_transfer_read(): socket error.\n", __FUNCTION__ );
   1.453 +	    }
   1.454 +    	break;
   1.455 +    }
   1.456  
   1.457 -      if ( bytes_read > 0 )
   1.458 -      {
   1.459 -			  recv += bytes_read;
   1.460 -			  count_bytes += bytes_read;
   1.461 -			  remaining -= bytes_read;
   1.462 -			  g_print( "[%s] Reading buffer (bytes read = %d, remaining = %d)\n", __FUNCTION__, bytes_read, remaining );
   1.463 -			  if ( remaining == 0 )
   1.464 -			  	break;
   1.465 -
   1.466 -      } else {
   1.467 -      	break;
   1.468 -      }
   1.469 -
   1.470 -      if ( io_status == G_IO_STATUS_EOF ) {
   1.471 -				g_print( "[%s] got EOS!", __FUNCTION__ );
   1.472 -				break;
   1.473 -      } else if ( io_status == G_IO_STATUS_ERROR ) {
   1.474 -				g_print( "[%s] gmyth_file_transfer_read(): socket error.\n", __FUNCTION__ );
   1.475 -				break;
   1.476 -      }
   1.477 -
   1.478 -      /* increase buffer size, to allow get more data (do not obey to the buffer size) */
   1.479 -      if ( read_unlimited == TRUE ) {
   1.480 -      	// FOR NOW, DO NOTHING!!!
   1.481 -      }
   1.482 -
   1.483 -      /* verify if the input (read) buffer is ready to receive data */
   1.484 -      //io_cond = g_io_channel_get_buffer_condition( io_channel );
   1.485 -
   1.486 -      g_print( "[%s]\t io_cond %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__, 
   1.487 -      		( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
   1.488 -
   1.489 -    } while ( remaining > 0 );//&& ( ( io_cond & G_IO_IN ) != 0 ) );
   1.490 -    
   1.491 -    //io_cond_control = g_io_channel_get_buffer_condition( io_channel_control );
   1.492 -    if ( remaining == 0 )//( io_cond_control & G_IO_IN ) != 0  )
   1.493 -    {
   1.494 -    	response = TRUE;
   1.495 -   		break;
   1.496 -	  }
   1.497 +    g_print( "[%s]\t io_cond %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__, 
   1.498 +    		( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
   1.499  	  
   1.500  		if ( ( io_cond_control & G_IO_IN ) != 0 ) {
   1.501  	  	g_print( "[%s] Finishind reading function: cleaning all data on the I/O control socket...\n", __FUNCTION__ );  
   1.502 @@ -1138,12 +1038,6 @@
   1.503  cleanup:
   1.504    myth_control_release_context (mutex);
   1.505    
   1.506 -  //has_io_access = TRUE;
   1.507 -
   1.508 -  //g_cond_broadcast( io_watcher_cond );  
   1.509 -  
   1.510 -  //g_mutex_unlock( mutex );
   1.511 -    
   1.512    if ( trash != NULL )
   1.513      g_free( trash );
   1.514  
     2.1 --- a/gmyth/src/gmyth_socket.c	Wed Nov 22 14:37:47 2006 +0000
     2.2 +++ b/gmyth/src/gmyth_socket.c	Wed Nov 22 19:53:52 2006 +0000
     2.3 @@ -44,9 +44,12 @@
     2.4  #include "gmyth_stringlist.h"
     2.5  #include "gmyth_context.h"
     2.6  
     2.7 -#define BUFLEN 				        512
     2.8 -#define MYTH_SEPARATOR 			    "[]:[]"
     2.9 -#define MYTH_PROTOCOL_FIELD_SIZE	8
    2.10 +#define BUFLEN 				        			512
    2.11 +#define MYTH_SEPARATOR 			    		"[]:[]"
    2.12 +#define MYTH_PROTOCOL_FIELD_SIZE		8
    2.13 +
    2.14 +/* max number of iterations */
    2.15 +#define MYTHTV_MAX_VERSION_CHECKS		15
    2.16  
    2.17  static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
    2.18  
    2.19 @@ -195,6 +198,8 @@
    2.20      gmyth_socket->hostname = g_strdup("");
    2.21  
    2.22      gmyth_socket->port = 6543;
    2.23 +    
    2.24 +    gmyth_socket->mythtv_version = MYTHTV_VERSION_DEFAULT;
    2.25  
    2.26      return gmyth_socket;
    2.27  }
    2.28 @@ -330,7 +335,7 @@
    2.29   * @param command The string command to be sent.
    2.30   */
    2.31  gboolean
    2.32 -gmyth_socket_send_command(GMythSocket *gmyth_socket, const GString *command) 
    2.33 +gmyth_socket_send_command(GMythSocket *gmyth_socket, GString *command) 
    2.34  {
    2.35      gboolean ret = TRUE;
    2.36  
    2.37 @@ -422,7 +427,7 @@
    2.38  		return FALSE;
    2.39      }
    2.40  
    2.41 -    if (gmyth_socket_check_protocol_version (gmyth_socket)) {
    2.42 +    if ( gmyth_socket_check_protocol_version (gmyth_socket) ) {
    2.43  
    2.44  	GString *result;
    2.45  	GString *base_str = g_string_new("");
    2.46 @@ -472,14 +477,18 @@
    2.47   * 
    2.48   * @param gmyth_socket The GMythSocket instance.
    2.49   * @param mythtv_version The Mythtv protocol version to be tested
    2.50 + * 
    2.51 + * @return The actual MythTV the client is connected to.
    2.52   */
    2.53 -gboolean
    2.54 +gint
    2.55  gmyth_socket_check_protocol_version_number (GMythSocket *gmyth_socket, gint mythtv_version)
    2.56  {
    2.57      GString *response;
    2.58      GString *payload;
    2.59      gchar *new_version = g_strdup("");
    2.60      gboolean res = TRUE;
    2.61 +    gint mythtv_new_version = MYTHTV_CANNOT_NEGOTIATE_VERSION;
    2.62 +    guint max_iterations = MYTHTV_MAX_VERSION_CHECKS;
    2.63  
    2.64  try_new_version:
    2.65      payload = g_string_new ("MYTH_PROTO_VERSION");
    2.66 @@ -503,26 +512,31 @@
    2.67  	    if (new_version!=NULL) {
    2.68  		++new_version; /* skip ']' character */
    2.69  		if ( new_version != NULL ) {
    2.70 -		    g_print( "[%s] got MythTV version = %s\n", __FUNCTION__, new_version );
    2.71 -		    mythtv_version = g_ascii_strtoull( g_strdup( new_version ), NULL, 10 );
    2.72 +		    g_print( "[%s] got MythTV version = %s.\n", __FUNCTION__, new_version );
    2.73 +		    mythtv_version = (gint)g_ascii_strtoull( g_strdup( new_version ), NULL, 10 );
    2.74  		    /* do reconnection to the socket (socket is closed if the MythTV version was wrong) */
    2.75  		    gmyth_socket_connect( &gmyth_socket, gmyth_socket->hostname, gmyth_socket->port );
    2.76  		    /* g_free( new_version ); */
    2.77 -		    goto try_new_version;
    2.78 +		    if ( --max_iterations > 0 ) 
    2.79 +		    	goto try_new_version;
    2.80 +		    else
    2.81 +		    	goto done;
    2.82  		}
    2.83  	    }
    2.84  	}
    2.85      }
    2.86 +    
    2.87 +    /* change the return value to a valid one */
    2.88 +    if ( res )
    2.89 +    	mythtv_new_version = mythtv_version;
    2.90  
    2.91  done:
    2.92      if ( payload != NULL )
    2.93  		g_string_free (payload, TRUE);
    2.94      if ( response != NULL )
    2.95  		g_string_free (response, TRUE);
    2.96 -//   if (new_version!=NULL)
    2.97 -//	g_free( new_version );
    2.98  
    2.99 -    return res;
   2.100 +    return mythtv_new_version;
   2.101  }
   2.102  
   2.103  /** Verifies if the Mythtv backend supported the GMyth supported version.
   2.104 @@ -533,7 +547,20 @@
   2.105  gboolean
   2.106  gmyth_socket_check_protocol_version (GMythSocket *gmyth_socket)
   2.107  {
   2.108 -    return gmyth_socket_check_protocol_version_number( gmyth_socket, MYTHTV_VERSION_DEFAULT );
   2.109 +    return ( ( gmyth_socket->mythtv_version = 
   2.110 +    		gmyth_socket_check_protocol_version_number ( gmyth_socket, 
   2.111 +    							MYTHTV_VERSION_DEFAULT ) ) != MYTHTV_CANNOT_NEGOTIATE_VERSION );
   2.112 +}
   2.113 +
   2.114 +/** Returns the Mythtv backend supported version.
   2.115 + * 
   2.116 + * @param gmyth_socket The GMythSocket instance.
   2.117 + * @return The actual MythTV version number.
   2.118 + */
   2.119 +gint
   2.120 +gmyth_socket_get_protocol_version (GMythSocket *gmyth_socket) 
   2.121 +{
   2.122 +	return gmyth_socket->mythtv_version;
   2.123  }
   2.124  
   2.125  /** Receives a backend answer after a gmyth_socket_send_command_call ().
   2.126 @@ -564,7 +591,6 @@
   2.127  
   2.128      io_status = g_io_channel_read_chars( gmyth_socket->sd_io_ch, buffer, MYTH_PROTOCOL_FIELD_SIZE, &bytes_read, &error );
   2.129  
   2.130 -
   2.131      /* verify if the input (read) buffer is ready to receive data */
   2.132      io_cond = g_io_channel_get_buffer_condition( gmyth_socket->sd_io_ch );
   2.133  
   2.134 @@ -588,7 +614,7 @@
   2.135  		    /* removes trailing whitespace */
   2.136  		    buffer = g_strstrip( buffer );
   2.137  	
   2.138 -		    len = (gint)strtoull ( buffer, NULL, 10 );  
   2.139 +		    len = (gint)g_ascii_strtoull ( buffer, NULL, 10 );  
   2.140  	
   2.141  		    bytes_read = 0;
   2.142  		    io_status = g_io_channel_read_chars( gmyth_socket->sd_io_ch, buffer, len, &bytes_read, &error );
     3.1 --- a/gmyth/src/gmyth_socket.h	Wed Nov 22 14:37:47 2006 +0000
     3.2 +++ b/gmyth/src/gmyth_socket.h	Wed Nov 22 19:53:52 2006 +0000
     3.3 @@ -74,8 +74,11 @@
     3.4  
     3.5  	gchar *hostname;
     3.6  	gint port;
     3.7 +	gint mythtv_version;
     3.8  };
     3.9  
    3.10 +/* used when no protocol version number was negotiated */
    3.11 +#define	MYTHTV_CANNOT_NEGOTIATE_VERSION		0
    3.12  
    3.13  GType           gmyth_socket_get_type (void);
    3.14  
    3.15 @@ -87,7 +90,7 @@
    3.16  gboolean        gmyth_socket_is_able_to_write (GMythSocket *gmyth_socket );
    3.17  
    3.18  gboolean        gmyth_socket_send_command (GMythSocket *gmyth_socket, 
    3.19 -                                           const GString *command);
    3.20 +                                           GString *command);
    3.21  GString *       gmyth_socket_receive_response (GMythSocket *gmyth_socket);
    3.22  int             gmyth_socket_sendreceive_stringlist (GMythSocket * gmyth_socket, 
    3.23                                                       GMythStringList *str_list);
    3.24 @@ -103,14 +106,15 @@
    3.25  void            gmyth_socket_close_connection (GMythSocket *gmyth_socket);
    3.26  
    3.27  gboolean		gmyth_socket_check_protocol_version (GMythSocket *gmyth_socket);
    3.28 -gboolean		gmyth_socket_check_protocol_version_number (GMythSocket *gmyth_socket, 
    3.29 +gint				gmyth_socket_check_protocol_version_number (GMythSocket *gmyth_socket, 
    3.30                                                              gint mythtv_version);
    3.31  
    3.32 +gint				gmyth_socket_get_protocol_version (GMythSocket *gmyth_socket);
    3.33 +
    3.34  gboolean		gmyth_socket_write_stringlist(GMythSocket *gmyth_socket, 
    3.35                                                GMythStringList* str_list);
    3.36  int			    gmyth_socket_read_stringlist(GMythSocket *gmyth_socket, 
    3.37                                               GMythStringList* str_list);
    3.38 -
    3.39  G_END_DECLS
    3.40  
    3.41  #endif /* __GMYTH_SOCKET_H__ */