[svn r98] Fixes some minor bugs on synchronization with monitor socket. trunk
authorrosfran
Wed Nov 22 00:15:38 2006 +0000 (2006-11-22)
branchtrunk
changeset 973b97ffa0634c
parent 96 28041df0da6e
child 98 d0f379fc4173
[svn r98] Fixes some minor bugs on synchronization with monitor socket.
gmyth/src/gmyth_file_transfer.c
     1.1 --- a/gmyth/src/gmyth_file_transfer.c	Fri Nov 17 20:07:38 2006 +0000
     1.2 +++ b/gmyth/src/gmyth_file_transfer.c	Wed Nov 22 00:15:38 2006 +0000
     1.3 @@ -53,8 +53,8 @@
     1.4  #define GMYTHTV_RECORDER_HEADER 	"QUERY_RECORDER"
     1.5  
     1.6  /* default values to the file transfer parameters */
     1.7 -#define GMYTHTV_USER_READ_AHEAD			FALSE
     1.8 -#define GMYTHTV_RETRIES							1
     1.9 +#define GMYTHTV_USER_READ_AHEAD			TRUE
    1.10 +#define GMYTHTV_RETRIES							-1
    1.11  #define GMYTHTV_FILE_SIZE						0
    1.12  
    1.13  #define GMYTHTV_BUFFER_SIZE					16*1024
    1.14 @@ -76,6 +76,8 @@
    1.15  
    1.16  static guint wait_to_transfer = 0;
    1.17  
    1.18 +static gboolean has_io_access = TRUE;
    1.19 +
    1.20  enum myth_sock_types {
    1.21    GMYTH_PLAYBACK_TYPE = 0,
    1.22    GMYTH_MONITOR_TYPE,
    1.23 @@ -83,7 +85,11 @@
    1.24    GMYTH_RINGBUFFER_TYPE
    1.25  };
    1.26  
    1.27 -static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
    1.28 +//static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
    1.29 +
    1.30 +static GMutex*			mutex 					 = NULL;
    1.31 +
    1.32 +static GCond*				io_watcher_cond  = NULL;
    1.33  
    1.34  static GMainContext *io_watcher_context = NULL;
    1.35  
    1.36 @@ -94,10 +100,14 @@
    1.37  static void gmyth_file_transfer_finalize (GObject *object);
    1.38  
    1.39  static GMythSocket *gmyth_connect_to_transfer_backend( GMythFileTransfer **transfer, guint sock_type );
    1.40 -static void* myth_init_io_watchers( void *data );
    1.41 +static gboolean myth_init_io_watchers( GMythFileTransfer *transfer );
    1.42  
    1.43  void gmyth_file_transfer_close( GMythFileTransfer *transfer );
    1.44  
    1.45 +static gboolean myth_control_acquire_context( gboolean do_wait );
    1.46 +
    1.47 +static gboolean myth_control_release_context( );
    1.48 +
    1.49  G_DEFINE_TYPE(GMythFileTransfer, gmyth_file_transfer, G_TYPE_OBJECT)
    1.50  
    1.51  #if 0
    1.52 @@ -139,6 +149,12 @@
    1.53  { 
    1.54    g_return_if_fail( gmyth_file_transfer != NULL );
    1.55    gmyth_file_transfer->mythtv_version = GMYTHTV_VERSION;
    1.56 +  
    1.57 +  /* it is used for signalizing the event socket consumer thread */
    1.58 +  io_watcher_cond = g_cond_new();
    1.59 +  
    1.60 +  /* mutex to control access to the event socket consumer thread */
    1.61 +  mutex = g_mutex_new();
    1.62  }
    1.63  
    1.64  static void
    1.65 @@ -265,6 +281,7 @@
    1.66    } else {
    1.67      g_warning("Remote transfer control socket already created.\n");
    1.68    }
    1.69 +
    1.70  #endif
    1.71  
    1.72    /* configure the socket */
    1.73 @@ -310,7 +327,7 @@
    1.74    g_return_val_if_fail( transfer != NULL && *transfer != NULL, NULL );
    1.75    g_return_val_if_fail( (*transfer)->uri != NULL, NULL );
    1.76  
    1.77 -  g_static_mutex_lock (&mutex);
    1.78 +  //myth_control_acquire_context (TRUE);
    1.79  
    1.80    gchar *path_dir = gmyth_uri_getpath( (*transfer)->uri );
    1.81    //g_print( "\t--> %s: path_dir = %s\n", __FUNCTION__, path_dir );
    1.82 @@ -346,7 +363,7 @@
    1.83  	  "\t\t\tCould not connect to server \"%s\" @ port %d\n", stype, 
    1.84  	  (*transfer)->hostname->str, (*transfer)->port );
    1.85        g_object_unref(sock);
    1.86 -      g_static_mutex_unlock (&mutex);
    1.87 +      //myth_control_release_context( );
    1.88        return NULL;
    1.89      }
    1.90  
    1.91 @@ -357,11 +374,12 @@
    1.92      if ( sock_type == GMYTH_PLAYBACK_TYPE )
    1.93      {
    1.94        (*transfer)->control_sock = sock;
    1.95 -      g_string_printf( base_str, "ANN Playback %s %d", hostname->str, TRUE );
    1.96 +      g_string_printf( base_str, "ANN Playback %s %d", hostname->str, FALSE );
    1.97  
    1.98        gmyth_socket_send_command( (*transfer)->control_sock, base_str );
    1.99        GString *resp = gmyth_socket_receive_response( (*transfer)->control_sock );
   1.100        g_print( "[%s] Got Playback response from %s: %s\n", __FUNCTION__, base_str->str, resp->str );
   1.101 +      //myth_init_io_watchers ( (*transfer) );
   1.102      }
   1.103      else if ( sock_type == GMYTH_MONITOR_TYPE )
   1.104      {
   1.105 @@ -372,7 +390,7 @@
   1.106        GString *resp = gmyth_socket_receive_response( (*transfer)->event_sock );
   1.107        g_print( "[%s] Got Monitor response from %s: %s\n", __FUNCTION__, base_str->str, resp->str );
   1.108        //g_thread_create( myth_init_io_watchers, (void*)(*transfer), FALSE, NULL );
   1.109 -      myth_init_io_watchers ( (void*)(*transfer) );
   1.110 +      g_thread_create( myth_init_io_watchers, (*transfer), FALSE, NULL );
   1.111  
   1.112        g_printerr( "[%s] Watch listener function to the IO control channel on thread %p.\n", __FUNCTION__, g_thread_self() );
   1.113  
   1.114 @@ -381,9 +399,9 @@
   1.115      {
   1.116        (*transfer)->sock = sock;
   1.117        strlist = gmyth_string_list_new();
   1.118 -      //g_string_printf( base_str, "ANN FileTransfer %s %d %d", hostname->str,
   1.119 -      //  		transfer->userreadahead, transfer->retries );
   1.120 -      g_string_printf( base_str, "ANN FileTransfer %s", hostname->str );
   1.121 +      g_string_printf( base_str, "ANN FileTransfer %s %d %d", hostname->str,
   1.122 +        		(*transfer)->userreadahead, (*transfer)->retries );
   1.123 +      //g_string_printf( base_str, "ANN FileTransfer %s", hostname->str );
   1.124  
   1.125        gmyth_string_list_append_string( strlist, base_str );
   1.126        gmyth_string_list_append_char_array( strlist, path_dir );
   1.127 @@ -427,7 +445,7 @@
   1.128    if ( strlist != NULL )
   1.129      g_object_unref( strlist );
   1.130  
   1.131 -  g_static_mutex_unlock (&mutex);
   1.132 +  //myth_control_release_context( );
   1.133  
   1.134    return sock;
   1.135  }    
   1.136 @@ -471,7 +489,7 @@
   1.137    GMythStringList *str_list = gmyth_string_list_new ();
   1.138  
   1.139    g_debug ( "[%s]\n", __FUNCTION__ );
   1.140 -  g_static_mutex_lock (&mutex);
   1.141 +  myth_control_acquire_context (TRUE);
   1.142  
   1.143    g_string_printf( file_transfer->query, "%s %d", GMYTHTV_RECORDER_HEADER, 
   1.144        file_transfer->rec_id >= 0 ? file_transfer->rec_id : file_transfer->card_id );
   1.145 @@ -492,7 +510,7 @@
   1.146      }
   1.147    }  
   1.148    g_print( "[%s] %s, stream is %s being recorded!\n", __FUNCTION__, ret ? "YES" : "NO", ret ? "" : "NOT" );
   1.149 -  g_static_mutex_unlock (&mutex);
   1.150 +  myth_control_release_context( );
   1.151  
   1.152    if ( str_list != NULL )
   1.153      g_object_unref (str_list);
   1.154 @@ -509,7 +527,7 @@
   1.155    GMythStringList *str_list = gmyth_string_list_new ();
   1.156  
   1.157    g_debug ( "[%s]\n", __FUNCTION__ );
   1.158 -  g_static_mutex_lock (&mutex);
   1.159 +  myth_control_acquire_context (TRUE);
   1.160  
   1.161    g_string_printf( file_transfer->query, "%s %d", GMYTHTV_RECORDER_HEADER, 
   1.162        file_transfer->rec_id >= 0 ? file_transfer->rec_id : file_transfer->card_id );
   1.163 @@ -525,7 +543,7 @@
   1.164      if ( ( str = gmyth_string_list_get_string( str_list, 0 ) ) != NULL && strstr ( str->str, "bad" ) == NULL )
   1.165        pos = gmyth_util_decode_long_long( str_list, 0 );
   1.166    } 
   1.167 -  g_static_mutex_unlock (&mutex);
   1.168 +  myth_control_release_context( );
   1.169  
   1.170  #ifndef GMYTHTV_ENABLE_DEBUG
   1.171  
   1.172 @@ -640,6 +658,8 @@
   1.173  
   1.174    // if (!controlSock->isOpen() || controlSock->error())
   1.175    //   return 0;
   1.176 +  
   1.177 +  myth_control_acquire_context( TRUE );
   1.178  
   1.179    GMythStringList *strlist = gmyth_string_list_new();
   1.180    g_string_printf (transfer->query, "%s %d", GMYTHTV_QUERY_HEADER, transfer->recordernum);
   1.181 @@ -661,45 +681,131 @@
   1.182    g_print( "[%s] got reading position pointer from the streaming = %lld\n", 
   1.183        __FUNCTION__, retval );
   1.184  
   1.185 -  //gmyth_file_transfer_reset( transfer );
   1.186 +  myth_control_release_context( );
   1.187  
   1.188    return retval;
   1.189  }
   1.190  
   1.191 +static gboolean 
   1.192 +myth_control_acquire_context( gboolean do_wait ) 
   1.193 +{
   1.194 +	
   1.195 +	gboolean ret = TRUE;	
   1.196 +	
   1.197 +	//g_mutex_lock( mutex );
   1.198 +	
   1.199 +  //while ( !has_io_access ) 
   1.200 +  //	g_cond_wait( io_watcher_cond, mutex );
   1.201 +  	
   1.202 +  //has_io_access = FALSE;
   1.203 +  
   1.204 +  //myth_control_acquire_context (FALSE);
   1.205 +  
   1.206 +  /*
   1.207 +  g_mutex_lock( mutex );
   1.208 +  
   1.209 +  if ( do_wait ) {
   1.210 +  	if ( !g_main_context_wait( io_watcher_context, io_watcher_cond, mutex ) )
   1.211 +  		ret = FALSE;
   1.212 +  } else if ( !g_main_context_acquire( io_watcher_context ) )
   1.213 +  	ret = FALSE;
   1.214 +  	*/
   1.215 +  
   1.216 +  return ret;
   1.217 +  
   1.218 +}
   1.219 +
   1.220 +static gboolean 
   1.221 +myth_control_release_context( ) 
   1.222 +{
   1.223 +	
   1.224 +	gboolean ret = TRUE;
   1.225 +
   1.226 +  //g_main_context_release( io_watcher_context );
   1.227 +  
   1.228 +  //g_mutex_unlock( mutex );
   1.229 +  
   1.230 +  //has_io_access = TRUE;
   1.231 +
   1.232 +  //g_cond_broadcast( io_watcher_cond );
   1.233 +  
   1.234 +  //g_mutex_unlock( mutex );
   1.235 +  
   1.236 + 
   1.237 +  return ret;
   1.238 +  
   1.239 +}
   1.240 +
   1.241  static gboolean
   1.242 -myth_control_sock_listener( GIOChannel *source, GIOCondition condition, gpointer data )
   1.243 +myth_control_sock_listener( GIOChannel *io_channel, GIOCondition condition, gpointer data )
   1.244  {
   1.245  
   1.246 -  GIOStatus ret;
   1.247 -  GError *err = NULL;
   1.248 -  gchar *msg = g_strdup("");
   1.249 +  GIOStatus io_status;
   1.250 +  GError *error = NULL;
   1.251 +  GIOCondition io_cond;
   1.252 +  gchar *trash = g_new0( gchar, GMYTHTV_BUFFER_SIZE*10 );
   1.253 +  guint recv = 0;
   1.254 +  //gchar *msg = g_strdup("");
   1.255    
   1.256 -  g_static_mutex_lock( &mutex );  
   1.257 +  while ( !has_io_access ) 
   1.258 +  	g_cond_wait( io_watcher_cond, mutex );  
   1.259 +    
   1.260 +  //myth_control_acquire_context (TRUE);
   1.261 +  
   1.262 +  has_io_access = FALSE;
   1.263 +  
   1.264 +  g_mutex_lock( mutex );
   1.265 +  
   1.266 +  gsize len = 0;
   1.267 +  if (condition & G_IO_HUP)
   1.268 +    g_print ("Read end of pipe died!\n");
   1.269 +    
   1.270 +  if ( ( condition & G_IO_IN ) != 0 ) {
   1.271 +  	io_status = g_io_channel_set_encoding( io_channel, NULL, &error );
   1.272 +    do 
   1.273 +    {
   1.274 +      //trash = g_new0( gchar, GMYTHTV_BUFFER_SIZE );
   1.275  
   1.276 -  gsize len;
   1.277 -  if (condition & G_IO_HUP)
   1.278 -    g_error ("Read end of pipe died!\n");
   1.279 -  ret = g_io_channel_read_line ( source, &msg, &len, NULL, &err);
   1.280 -  if ( ret == G_IO_STATUS_ERROR )
   1.281 -    g_error ("[%s] Error reading: %s\n", __FUNCTION__, err != NULL ? err->message : "" );
   1.282 -  g_print ("\n\n\n\n\n\n[%s]\t\tEVENT: Read %u bytes: %s\n\n\n\n\n", __FUNCTION__, len, msg != NULL ? msg : "" );
   1.283 -  if ( msg != NULL )
   1.284 -    g_free (msg);
   1.285 +      io_status = g_io_channel_read_chars( io_channel, trash + recv, 
   1.286 +      		GMYTHTV_BUFFER_SIZE, &len, &error);
   1.287 +
   1.288 +      g_print( "[%s] Received data buffer from IO binary channel... %d bytes gone!\n", 
   1.289 +      		__FUNCTION__, len );
   1.290 +      		
   1.291 +      recv += len;
   1.292 +      
   1.293 +      //msg = g_strconcat( msg, g_strdup(trash), NULL );
   1.294 +      
   1.295 +      //if ( trash != NULL )
   1.296 +      //	g_free( trash );
   1.297 +      	
   1.298 +      io_cond = g_io_channel_get_buffer_condition( io_channel );
   1.299 +
   1.300 +    } while ( ( io_cond & G_IO_IN ) != 0 && ( io_status != G_IO_STATUS_ERROR ) );
   1.301 +  }
   1.302 +  //ret = g_io_channel_read_chars ( source, &msg, &len, NULL, &err);
   1.303 +  if ( io_status == G_IO_STATUS_ERROR )
   1.304 +    g_print ("[%s] Error reading: %s\n", __FUNCTION__, error != NULL ? error->message : "" );
   1.305 +  g_print ("\n[%s]\tEVENT: Read %d bytes: %s\n\n", __FUNCTION__, len, trash != NULL ? trash : "[no event data]" );
   1.306 +  
   1.307 +  has_io_access = TRUE;
   1.308      
   1.309 -  g_static_mutex_unlock( &mutex );
   1.310 -
   1.311 +  //myth_control_release_context( );
   1.312 +  g_cond_broadcast( io_watcher_cond );
   1.313 +  
   1.314 +  g_mutex_unlock( mutex );    
   1.315 +  
   1.316    return TRUE;
   1.317  
   1.318  }
   1.319  
   1.320 -static void*
   1.321 -myth_init_io_watchers( void *data )
   1.322 +static gboolean
   1.323 +myth_init_io_watchers( GMythFileTransfer *transfer )
   1.324  {
   1.325 -  GMythFileTransfer *transfer = (GMythFileTransfer*)data;
   1.326 -  io_watcher_context = g_main_context_new();
   1.327 -  GMainLoop *loop = g_main_loop_new( NULL, FALSE );
   1.328 +  io_watcher_context = g_main_context_default();
   1.329 +  //GMainLoop *loop = g_main_loop_new( io_watcher_context, FALSE );
   1.330  
   1.331 -  GSource *source = NULL;
   1.332 +  GSource *source;
   1.333  
   1.334    if ( transfer->event_sock->sd_io_ch != NULL )
   1.335      source = g_io_create_watch( transfer->event_sock->sd_io_ch, G_IO_IN | G_IO_HUP );
   1.336 @@ -716,19 +822,15 @@
   1.337    }
   1.338    
   1.339    g_print( "[%s]\tOK! Starting listener on the MONITOR event socket...\n", __FUNCTION__ );
   1.340 -
   1.341 -  g_main_loop_run( loop );
   1.342 +  
   1.343 +  //g_main_loop_run( loop );
   1.344  
   1.345  cleanup:
   1.346    if ( source != NULL )
   1.347      g_source_unref( source );
   1.348  
   1.349 -  g_main_loop_unref( loop );
   1.350 -
   1.351 -  g_main_context_unref( io_watcher_context );
   1.352 -
   1.353 -  return NULL;
   1.354 -}
   1.355 +  return TRUE;
   1.356 +}	
   1.357  
   1.358  static gboolean
   1.359  gmyth_file_transfer_is_backend_message( GMythFileTransfer *transfer, 
   1.360 @@ -779,6 +881,17 @@
   1.361    gchar *trash = g_strdup("");
   1.362  
   1.363    g_return_val_if_fail ( data != NULL, -2 );
   1.364 +  
   1.365 +  #if 0
   1.366 +  while ( !has_io_access ) 
   1.367 +  	g_cond_wait( io_watcher_cond, mutex );
   1.368 +  	
   1.369 +  has_io_access = FALSE;
   1.370 +  #endif
   1.371 +  
   1.372 +  myth_control_acquire_context (FALSE);
   1.373 +  
   1.374 +  //g_mutex_lock( mutex );
   1.375  
   1.376    /* gets the size of the entire file, if the size requested is lesser than 0 */
   1.377    if ( size <= 0 )
   1.378 @@ -848,14 +961,10 @@
   1.379  
   1.380    wait_to_transfer = 0;
   1.381  
   1.382 -  //while ( transfer->live_tv && ( gmyth_file_transfer_get_file_position( transfer ) < 4096 ) &&
   1.383 -  //		wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS )
   1.384 -  //	g_usleep( 1000*50 ); /* waits just for 2/10 second */
   1.385 -
   1.386    //g_thread_create( myth_init_io_watchers, (void*)transfer, FALSE, NULL );
   1.387    //g_printerr( "[%s] Watch listener function to the IO control channel on thread %p.\n", __FUNCTION__, g_thread_self() );
   1.388  
   1.389 -  //g_static_mutex_lock (&mutex);
   1.390 +  //g_mutex_lock (mutex);
   1.391    //strlist = gmyth_string_list_new();
   1.392  
   1.393    g_string_printf ( transfer->query, "%s %d",
   1.394 @@ -865,27 +974,39 @@
   1.395    
   1.396    sent = size;
   1.397    remaining = size - recv;  
   1.398 -  //g_static_mutex_unlock( &mutex );
   1.399 +  //g_mutex_unlock( mutex );
   1.400    //data = (void*)g_new0( gchar, size );
   1.401  
   1.402 -  g_io_channel_flush( io_channel_control, NULL );
   1.403 -
   1.404 -  //g_static_mutex_lock( &mutex );
   1.405 +  //g_mutex_lock( mutex );
   1.406  
   1.407    io_cond = g_io_channel_get_buffer_condition( io_channel );
   1.408 +  
   1.409 +  if ( ( io_cond_control & G_IO_IN ) != 0 ) {
   1.410 +  	g_print( "[%s] Finishind reading function: cleaning all data on the I/O control socket...\n", __FUNCTION__ );  
   1.411 +	  while ( gmyth_socket_read_stringlist( transfer->control_sock, strlist ) > 0 )
   1.412 +	  {
   1.413 +	    if ( strlist != NULL && gmyth_string_list_length(strlist) > 0 ) 
   1.414 +	    {
   1.415 +				gint backend_code = gmyth_string_list_get_int( strlist, 0 ); // -1 on backend error
   1.416 +				g_print( "[%s]\t backend code = %d -\t"\
   1.417 +				 		"[%s prepared for reading]! (G_IO_IN) !!!\n\n", __FUNCTION__, 
   1.418 +	    			backend_code, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
   1.419 +	    }
   1.420 +	  }  	
   1.421 +  }
   1.422  
   1.423    while ( recv < size  && !response )//&& ( io_cond & G_IO_IN ) != 0 )
   1.424    {
   1.425    	//g_io_channel_flush( io_channel_control, NULL );
   1.426 -  	
   1.427 +  	gint backend_msg_count = 1;
   1.428 +
   1.429 +resend_request:
   1.430      strlist = gmyth_string_list_new();
   1.431      gmyth_string_list_append_char_array( strlist, transfer->query->str );
   1.432      gmyth_string_list_append_char_array( strlist, 
   1.433      		/*transfer->live_tv ? "REQUEST_BLOCK_RINGBUF" :*/ "REQUEST_BLOCK" );
   1.434      gmyth_string_list_append_int( strlist, remaining );
   1.435  		gmyth_socket_write_stringlist( transfer->control_sock, strlist );
   1.436 -		
   1.437 -		gint backend_msg_count = 1;
   1.438  				
   1.439  		/* iterates until find some non-MythTV backend message */
   1.440  		do {		
   1.441 @@ -907,25 +1028,35 @@
   1.442  	      	if ( sent != 0 )
   1.443  	      	{
   1.444  	      		g_print( "[%s]\t received = %d bytes, backend says %d bytes sent, "\
   1.445 -	      			"io_cond %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__, 
   1.446 -		  			recv, sent, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
   1.447 +	      				"io_cond %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__, 
   1.448 +		  					recv, sent, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
   1.449  	
   1.450  			  		if ( sent == remaining )
   1.451  			  		{
   1.452 -			  			//response = ( recv == size ); 
   1.453  			  			g_print( "[%s]\t\tsent %d, which is equals to requested size = %d\n\n", 
   1.454  			  						__FUNCTION__, sent, remaining );
   1.455 +			  			break;
   1.456  			  		}
   1.457  			  		else
   1.458  			  		{
   1.459  			  			g_print( "[%s]\t\tsent %d, which is NOT equals to requested size = %d\n\n", 
   1.460  			  						__FUNCTION__, sent, remaining );
   1.461 -						remaining = sent;
   1.462 -			  			break;
   1.463 -		
   1.464 +			  			size = remaining = sent;
   1.465 +			  			if ( sent < 0 ) {
   1.466 +				  			if ( --backend_msg_count > 0 )
   1.467 +				  				goto resend_request;
   1.468 +				  			else
   1.469 +				  				goto cleanup;
   1.470 +			  			} else
   1.471 +			  				break;
   1.472  			  		}
   1.473  	      	} else {
   1.474 -	      		goto cleanup;
   1.475 +						if ( --backend_msg_count > 0 )
   1.476 +						{							
   1.477 +	      			g_usleep( 200 );
   1.478 +		  				goto resend_request;
   1.479 +						}	else
   1.480 +		  				goto cleanup;	      		
   1.481  	      	} // if
   1.482  	    	}
   1.483  	    }
   1.484 @@ -946,12 +1077,6 @@
   1.485        io_status = g_io_channel_read_chars( io_channel, data + recv, 
   1.486  	  		buf_len, &bytes_read, &error );
   1.487  
   1.488 -      /*
   1.489 -			 GString *sss = g_string_new("");
   1.490 -			 sss = g_string_append_len( sss, (gchar*)data+recv, bytes_read );
   1.491 -		
   1.492 -			 g_print( "[%s] Reading buffer (length = %d)\n", __FUNCTION__, bytes_read);
   1.493 -	 		*/
   1.494        if ( bytes_read > 0 )
   1.495        {
   1.496  			  recv += bytes_read;
   1.497 @@ -984,7 +1109,7 @@
   1.498        g_print( "[%s]\t io_cond %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__, 
   1.499        		( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
   1.500  
   1.501 -    } while ( remaining > 0 );//&& ( io_status == G_IO_STATUS_NORMAL ) );
   1.502 +    } while ( remaining > 0 );//&& ( ( io_cond & G_IO_IN ) != 0 ) );
   1.503      
   1.504      //io_cond_control = g_io_channel_get_buffer_condition( io_channel_control );
   1.505      if ( remaining == 0 )//( io_cond_control & G_IO_IN ) != 0  )
   1.506 @@ -992,31 +1117,33 @@
   1.507      	response = TRUE;
   1.508     		break;
   1.509  	  }
   1.510 +	  
   1.511 +		if ( ( io_cond_control & G_IO_IN ) != 0 ) {
   1.512 +	  	g_print( "[%s] Finishind reading function: cleaning all data on the I/O control socket...\n", __FUNCTION__ );  
   1.513 +		  while ( gmyth_socket_read_stringlist( transfer->control_sock, strlist ) > 0 )
   1.514 +		  {
   1.515 +		    if ( strlist != NULL && gmyth_string_list_length(strlist) > 0 ) 
   1.516 +		    {
   1.517 +					gint backend_code = gmyth_string_list_get_int( strlist, 0 ); // -1 on backend error
   1.518 +					g_print( "[%s]\t backend code = %d -\t"\
   1.519 +					 		"[%s prepared for reading]! (G_IO_IN) !!!\n\n", __FUNCTION__, 
   1.520 +		    			backend_code, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
   1.521 +		    }
   1.522 +		  }
   1.523 +	  }
   1.524 +	  g_io_channel_flush( io_channel_control, NULL );
   1.525      
   1.526    } // while
   1.527 -  
   1.528 -  //io_cond_control = g_io_channel_get_buffer_condition( io_channel_control );
   1.529 -
   1.530 -  if ( ( ( io_cond_control & G_IO_IN ) != 0 ) /*&&  		 
   1.531 -  		( response || ( recv == size ) ) )
   1.532 -  		( recv <= 0  || sent <= 0 ) */ )
   1.533 -  {
   1.534 -    if ( gmyth_socket_read_stringlist( transfer->control_sock, strlist ) > 0 )
   1.535 -    {
   1.536 -      if ( strlist != NULL && gmyth_string_list_length(strlist) > 0 ) 
   1.537 -      {
   1.538 -				gint backend_code = gmyth_string_list_get_int( strlist, 0 ); // -1 on backend error
   1.539 -				g_print( "[%s]\t backend code = %d -\t"\
   1.540 -				 		"[%s prepared for reading]! (G_IO_IN) !!!\n\n", __FUNCTION__, 
   1.541 -	    			backend_code, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
   1.542 -      }
   1.543 -    }
   1.544 -  }
   1.545  
   1.546  cleanup:
   1.547 -  //g_static_mutex_unlock (&mutex);
   1.548 -  //g_io_channel_flush( io_channel_control, NULL );
   1.549 +  myth_control_release_context (mutex);
   1.550 +  
   1.551 +  //has_io_access = TRUE;
   1.552  
   1.553 +  //g_cond_broadcast( io_watcher_cond );  
   1.554 +  
   1.555 +  //g_mutex_unlock( mutex );
   1.556 +    
   1.557    if ( trash != NULL )
   1.558      g_free( trash );
   1.559