[svn r148] code cleanup trunk
authorrenatofilho
Thu Nov 30 17:49:08 2006 +0000 (2006-11-30)
branchtrunk
changeset 1475095b85ff808
parent 146 07951674b0c0
child 148 680e5d40ff1a
[svn r148] code cleanup
gmyth/src/Makefile.am
gmyth/src/gmyth_file_transfer.h
gmyth/src/gmyth_socket.c
gmyth/src/gmyth_uri.c
gmyth/src/gmyth_util.c
     1.1 --- a/gmyth/src/Makefile.am	Thu Nov 30 15:20:37 2006 +0000
     1.2 +++ b/gmyth/src/Makefile.am	Thu Nov 30 17:49:08 2006 +0000
     1.3 @@ -27,8 +27,7 @@
     1.4  	$(GST_CFLAGS) 				\
     1.5  	$(GSTBASE_CFLAGS)			\
     1.6  	$(GSTPLUGINSBASE_CFLAGS)	\
     1.7 -	$(MYSQL_CFLAGS)				\
     1.8 -	-g3 -O0
     1.9 +	$(MYSQL_CFLAGS)
    1.10  
    1.11  #libgmyth_la_LIBADD =			\
    1.12  #	$(MYSQL_LIBS)
     2.1 --- a/gmyth/src/gmyth_file_transfer.h	Thu Nov 30 15:20:37 2006 +0000
     2.2 +++ b/gmyth/src/gmyth_file_transfer.h	Thu Nov 30 17:49:08 2006 +0000
     2.3 @@ -110,6 +110,7 @@
     2.4  gint gmyth_file_transfer_read(GMythFileTransfer *transfer, GByteArray *data, gint size, gboolean read_unlimited);
     2.5  gint64 gmyth_file_transfer_seek(GMythFileTransfer *transfer, guint64 pos, gint whence);
     2.6  gboolean gmyth_file_transfer_settimeout( GMythFileTransfer *transfer, gboolean fast);
     2.7 +guint64 gmyth_file_transfer_get_filesize (GMythFileTransfer *transfer);
     2.8  
     2.9  //gboolean gmyth_file_transfer_playback_setup( GMythFileTransfer **transfer, gboolean live_tv );
    2.10  //gboolean gmyth_file_transfer_setup( GMythFileTransfer **transfer, gboolean live_tv );
     3.1 --- a/gmyth/src/gmyth_socket.c	Thu Nov 30 15:20:37 2006 +0000
     3.2 +++ b/gmyth/src/gmyth_socket.c	Thu Nov 30 17:49:08 2006 +0000
     3.3 @@ -103,26 +103,27 @@
     3.4   * @return gint that represents the error number from getaddrinfo(). 
     3.5   */
     3.6  static gint
     3.7 -gmyth_socket_toaddrinfo( gchar *addr, gint port, struct addrinfo **addrInfo )
     3.8 +gmyth_socket_toaddrinfo (gchar *addr, gint port, struct addrinfo **addrInfo )
     3.9  {
    3.10 -    struct addrinfo hints;
    3.11 -    gchar *portStr = g_strnfill( 32, ' ' );
    3.12 +    struct addrinfo *hints;
    3.13 +    gchar *portStr = NULL;
    3.14      gint errorn = EADDRNOTAVAIL;
    3.15  
    3.16 -    memset( &hints, 0, sizeof(hints) );
    3.17 -    hints.ai_family = AF_INET;
    3.18 -    hints.ai_socktype = SOCK_STREAM;
    3.19 +    hints = g_new0 (struct addrinfo, 1);
    3.20 +    hints->ai_family = AF_INET;
    3.21 +    hints->ai_socktype = SOCK_STREAM;
    3.22      /* hints.ai_flags = AI_NUMERICHOST; */
    3.23      if ( port != -1 )	
    3.24 -		sprintf(portStr, "%d", port);
    3.25 +        portStr = g_strdup_printf ("%d", port);
    3.26      else
    3.27  		portStr = NULL;
    3.28  
    3.29      gmyth_debug ("Address: %s, port: %s\n", addr, portStr);
    3.30 -    if ( ( errorn = getaddrinfo(addr, portStr, &hints, addrInfo) ) != 0 ) {
    3.31 +    if ( ( errorn = getaddrinfo(addr, portStr, hints, addrInfo) ) != 0 ) {
    3.32  		g_printerr( "[%s] Socket ERROR: %s\n", __FUNCTION__, gai_strerror(errorn) );
    3.33      }
    3.34 -
    3.35 +    g_free (portStr);
    3.36 +    g_free (hints);
    3.37      return errorn;
    3.38  }
    3.39  
    3.40 @@ -284,21 +285,18 @@
    3.41   * @return GString* get local hostname.
    3.42   */
    3.43  GString *
    3.44 -gmyth_socket_get_local_hostname( )
    3.45 +gmyth_socket_get_local_hostname  ()
    3.46  {
    3.47      GString *str = g_string_sized_new( 1024 );
    3.48  
    3.49 -    gchar *localhostname = g_strnfill( 1024, ' ' );
    3.50 -    gchar *localaddr = g_strdup( "127.0.0.1" );
    3.51 -
    3.52 +    gchar localhostname[1024];
    3.53 +    gchar *localaddr = NULL; 
    3.54      gboolean found_addr = FALSE;
    3.55 -
    3.56      struct addrinfo* addr_info_data = NULL, *addr_info0 = NULL;
    3.57 -
    3.58      struct sockaddr_in* sa = NULL;
    3.59  
    3.60      gethostname(localhostname, 1024);
    3.61 -    gint err = gmyth_socket_toaddrinfo( localhostname, -1,  &addr_info_data );
    3.62 +    gint err = gmyth_socket_toaddrinfo (localhostname, -1,  &addr_info_data );
    3.63      
    3.64      if ( err == EADDRNOTAVAIL )
    3.65      {
    3.66 @@ -321,6 +319,7 @@
    3.67  	    }
    3.68  	    addr_info0 = addr_info0->ai_next;
    3.69      };
    3.70 +    freeaddrinfo (addr_info_data);
    3.71  
    3.72      if ( found_addr == FALSE ) {
    3.73        gchar *prim_addr = gmyth_socket_get_primary_addr();
    3.74 @@ -332,15 +331,12 @@
    3.75  	        str = g_string_assign( str, g_strdup( prim_addr ) );
    3.76  	        g_free( prim_addr );
    3.77      	} else {
    3.78 -					str = g_string_assign( str, g_strdup( localhostname ) );
    3.79 +					str = g_string_assign (str, localhostname);
    3.80      	}
    3.81      }
    3.82  
    3.83      g_static_mutex_unlock( &mutex );
    3.84  
    3.85 -    if (localhostname!=NULL)
    3.86 -	g_free( localhostname );
    3.87 -
    3.88      return str;
    3.89  }
    3.90  
    3.91 @@ -349,8 +345,12 @@
    3.92  {
    3.93      GMythSocket *gmyth_socket = GMYTH_SOCKET(object);
    3.94  
    3.95 +    /* disconnect socket */
    3.96      gmyth_socket_close_connection (gmyth_socket);
    3.97 -    /* disconnect socket */
    3.98 +
    3.99 +    g_free (gmyth_socket->hostname);
   3.100 +    gmyth_socket->hostname = NULL;
   3.101 +
   3.102      G_OBJECT_CLASS (gmyth_socket_parent_class)->dispose (object);
   3.103  }
   3.104  
   3.105 @@ -371,10 +371,6 @@
   3.106  {
   3.107      GMythSocket *gmyth_socket = GMYTH_SOCKET (g_object_new(GMYTH_SOCKET_TYPE, NULL));
   3.108  
   3.109 -    gmyth_socket->sd_io_ch = NULL;
   3.110 -
   3.111 -    gmyth_socket->hostname = g_strdup("");
   3.112 -
   3.113      gmyth_socket->port = 6543;
   3.114      
   3.115      gmyth_socket->mythtv_version = MYTHTV_VERSION_DEFAULT;
   3.116 @@ -497,41 +493,41 @@
   3.117      gmyth_socket->port = port;
   3.118  
   3.119      for ( addr_info0 = addr_info_data; addr_info0; addr_info0 = addr_info_data->ai_next ) {
   3.120 +        struct sockaddr_in *sa = (struct sockaddr_in*)addr_info0->ai_addr;
   3.121 +        /* init socket descriptor */
   3.122 +        gmyth_socket->sd = socket( addr_info0->ai_family, addr_info0->ai_socktype,
   3.123 +                         addr_info0->ai_protocol );
   3.124  
   3.125 -	struct sockaddr_in *sa = (struct sockaddr_in*)addr_info0->ai_addr;
   3.126 -	/* init socket descriptor */
   3.127 -	gmyth_socket->sd = socket( addr_info0->ai_family, addr_info0->ai_socktype,
   3.128 -				     addr_info0->ai_protocol );
   3.129 +        if ( gmyth_socket->sd < 0 )
   3.130 +            continue;
   3.131  
   3.132 -	if ( gmyth_socket->sd < 0 )
   3.133 -	    continue;
   3.134 +        gmyth_debug( "[%s] hostname = %s, sock_fd = %d, addr = %s, addr_len = %d, "\
   3.135 +            "ai_family = %d, ai_protocol = %d\n",
   3.136 +            __FUNCTION__, hostname, gmyth_socket->sd, inet_ntoa( sa->sin_addr ),
   3.137 +            addr_info0->ai_addrlen, addr_info0->ai_family, addr_info0->ai_protocol );
   3.138 +            
   3.139 +        struct timeval *timeout = g_new0( struct timeval, 1 );
   3.140 +        
   3.141 +        /* using 40 seconds timeout */
   3.142 +        timeout->tv_sec = 40;
   3.143 +        timeout->tv_usec = 0;
   3.144 +            
   3.145 +        if ( gmyth_socket_try_connect( gmyth_socket->sd, (struct sockaddr *)addr_info0->ai_addr,
   3.146 +                addr_info0->ai_addrlen, timeout, &ret_code ) < 0 )
   3.147 +        {
   3.148 +            g_printerr( "[%s] Error connecting to backend!\n", __FUNCTION__ );
   3.149 +            if ( ret_code == ETIMEDOUT )
   3.150 +            g_printerr( "[%s]\tBackend host unreachable!\n", __FUNCTION__ );
   3.151  
   3.152 -	gmyth_debug( "[%s] hostname = %s, sock_fd = %d, addr = %s, addr_len = %d, "\
   3.153 -		"ai_family = %d, ai_protocol = %d\n",
   3.154 -		__FUNCTION__, hostname, gmyth_socket->sd, inet_ntoa( sa->sin_addr ),
   3.155 -		addr_info0->ai_addrlen, addr_info0->ai_family, addr_info0->ai_protocol );
   3.156 -		
   3.157 -	struct timeval *timeout = g_new0( struct timeval, 1 );
   3.158 -	
   3.159 -	/* using 40 seconds timeout */
   3.160 -	timeout->tv_sec = 40;
   3.161 -	timeout->tv_usec = 0;
   3.162 -		
   3.163 -	if ( gmyth_socket_try_connect( gmyth_socket->sd, (struct sockaddr *)addr_info0->ai_addr,
   3.164 -			addr_info0->ai_addrlen, timeout, &ret_code ) < 0 )
   3.165 -	{
   3.166 -	    g_printerr( "[%s] Error connecting to backend!\n", __FUNCTION__ );
   3.167 -	    if ( ret_code == ETIMEDOUT )
   3.168 -		g_printerr( "[%s]\tBackend host unreachable!\n", __FUNCTION__ );
   3.169 +            g_printerr( "ERROR: %s\n", gai_strerror(ret_code) );
   3.170 +            continue;
   3.171 +        }
   3.172  
   3.173 -	    g_printerr( "ERROR: %s\n", gai_strerror(ret_code) );
   3.174 -	    continue;
   3.175 -	}
   3.176 -
   3.177 -	/* only will be reached if none of the error above occurred */
   3.178 -	break;
   3.179 -
   3.180 +        /* only will be reached if none of the error above occurred */
   3.181 +        break;
   3.182      }
   3.183 +    freeaddrinfo (addr_info_data);
   3.184 +    addr_info_data = NULL;
   3.185  
   3.186      gmyth_socket->sd_io_ch = g_io_channel_unix_new( gmyth_socket->sd );
   3.187      
   3.188 @@ -758,9 +754,8 @@
   3.189  gint
   3.190  gmyth_socket_check_protocol_version_number (GMythSocket *gmyth_socket, gint mythtv_version)
   3.191  {
   3.192 -    GString *response;
   3.193 -    GString *payload;
   3.194 -    gchar *new_version = g_strdup("");
   3.195 +    GString *response = NULL;
   3.196 +    GString *payload = NULL;
   3.197      gboolean res = TRUE;
   3.198      gint mythtv_new_version = MYTHTV_CANNOT_NEGOTIATE_VERSION;
   3.199      guint max_iterations = MYTHTV_MAX_VERSION_CHECKS;
   3.200 @@ -783,15 +778,16 @@
   3.201  		g_warning ("[%s] Protocol version request error: %s", __FUNCTION__, response->str);
   3.202  	/* get the version number returned by the REJECT message */
   3.203  	if ( ( res = g_str_has_prefix (response->str, "REJECT") ) == TRUE ) {
   3.204 +        gchar *new_version = NULL;
   3.205  	    new_version = g_strrstr( response->str, "]" );
   3.206  	    if (new_version!=NULL) {
   3.207  		++new_version; /* skip ']' character */
   3.208  		if ( new_version != NULL ) {
   3.209  		    gmyth_debug ( "[%s] got MythTV version = %s.\n", __FUNCTION__, new_version );
   3.210 -		    mythtv_version = (gint)g_ascii_strtoull( g_strdup( new_version ), NULL, 10 );
   3.211 +		    mythtv_version = (gint)g_ascii_strtoull (new_version, NULL, 10 );
   3.212  		    /* do reconnection to the socket (socket is closed if the MythTV version was wrong) */
   3.213  		    gmyth_socket_connect( gmyth_socket, gmyth_socket->hostname, gmyth_socket->port );
   3.214 -		    /* g_free( new_version ); */
   3.215 +            new_version =NULL;
   3.216  		    if ( --max_iterations > 0 ) 
   3.217  		    	goto try_new_version;
   3.218  		    else
   3.219 @@ -862,14 +858,13 @@
   3.220  
   3.221      /* verify if the input (read) buffer is ready to receive data */
   3.222  
   3.223 -    buffer = g_strnfill( BUFLEN, ' ' );
   3.224 -
   3.225      g_static_mutex_lock( &mutex );
   3.226  
   3.227 -    io_status = g_io_channel_read_chars( gmyth_socket->sd_io_ch, buffer, MYTH_PROTOCOL_FIELD_SIZE, &bytes_read, &error );
   3.228 +    buffer = g_new0 (gchar, MYTH_PROTOCOL_FIELD_SIZE);
   3.229 +    io_status = g_io_channel_read_chars (gmyth_socket->sd_io_ch, buffer, MYTH_PROTOCOL_FIELD_SIZE, &bytes_read, &error);
   3.230  
   3.231      /* verify if the input (read) buffer is ready to receive data */
   3.232 -    io_cond = g_io_channel_get_buffer_condition( gmyth_socket->sd_io_ch );
   3.233 +    io_cond = g_io_channel_get_buffer_condition (gmyth_socket->sd_io_ch);
   3.234  
   3.235      gmyth_debug ( "[%s] Bytes read = %d\n", __FUNCTION__, bytes_read );
   3.236  
   3.237 @@ -884,17 +879,22 @@
   3.238  		io_cond = g_io_channel_get_buffer_condition( gmyth_socket->sd_io_ch );
   3.239  	
   3.240  		if ( ( io_cond & G_IO_IN ) != 0 ) {
   3.241 +            gchar *buffer_aux = NULL;
   3.242 +
   3.243 +		    /* removes trailing whitespace */
   3.244 +		    buffer_aux = g_strstrip (buffer);
   3.245 +		    len = (gint)g_ascii_strtoull (buffer_aux, NULL, 10 );
   3.246 +
   3.247 +            if (buffer != NULL) {
   3.248 +                g_free (buffer);
   3.249 +                buffer = NULL;
   3.250 +            }
   3.251 +
   3.252 +            buffer = g_new0 (gchar, len+1);
   3.253  	
   3.254 -		    snprintf( buffer, MYTH_PROTOCOL_FIELD_SIZE+1, "%-8s", g_strdup(buffer));
   3.255 -		    gmyth_debug ( "[%s] buffer = [%s]\n", __FUNCTION__, buffer  );
   3.256 -	
   3.257 -		    /* removes trailing whitespace */
   3.258 -		    buffer = g_strstrip( buffer );
   3.259 -	
   3.260 -		    len = (gint)g_ascii_strtoull ( buffer, NULL, 10 );  
   3.261 -	
   3.262 +            g_debug ("BUFF %s : len %d : MYTH_PROTOCOL_FIELD_SIZE %d", buffer, len, MYTH_PROTOCOL_FIELD_SIZE);
   3.263  		    bytes_read = 0;
   3.264 -		    io_status = g_io_channel_read_chars( gmyth_socket->sd_io_ch, buffer, len, &bytes_read, &error );
   3.265 +		    io_status = g_io_channel_read_chars( gmyth_socket->sd_io_ch, buffer, len, &bytes_read, &error);
   3.266  		    buffer[bytes_read] = '\0';
   3.267  		}
   3.268      }  
   3.269 @@ -906,17 +906,15 @@
   3.270      if ( ( bytes_read != len ) || ( io_status == G_IO_STATUS_ERROR ) )
   3.271  		str = NULL;
   3.272      else
   3.273 -		str = g_string_new( buffer );
   3.274 -
   3.275 -    if ( buffer != NULL )
   3.276 -		g_free( buffer );
   3.277 +		str = g_string_new (buffer);
   3.278  
   3.279      if ( error != NULL ) {
   3.280  		g_printerr( "[%s] Error found receiving response from the IO channel: (%d, %s)\n", __FUNCTION__, error->code, error->message );
   3.281  		str = NULL;
   3.282 -		g_error_free( error );
   3.283 +		g_error_free (error);
   3.284      }
   3.285  
   3.286 +    g_free (buffer);
   3.287      return str;
   3.288  }
   3.289  
     4.1 --- a/gmyth/src/gmyth_uri.c	Thu Nov 30 15:20:37 2006 +0000
     4.2 +++ b/gmyth/src/gmyth_uri.c	Thu Nov 30 17:49:08 2006 +0000
     4.3 @@ -1,4 +1,4 @@
     4.4 -/**
     4.5 +/** 
     4.6   * GMyth Library
     4.7   *
     4.8   * @file gmyth/gmyth_uri.c
     4.9 @@ -161,6 +161,7 @@
    4.10  {
    4.11  	gboolean ret = FALSE;
    4.12  	
    4.13 +    g_debug ("URI: %p PROTO: %p", uri->uri, uri->protocol);
    4.14  	g_return_val_if_fail( uri != NULL && uri->uri != NULL && uri->protocol != NULL, FALSE );
    4.15  	
    4.16  	if ( gmyth_strstr( uri->uri->str, GMYTH_URI_PROTOCOL_DELIM ) == 0 || strlen(uri->protocol->str) > 0 )
    4.17 @@ -226,6 +227,7 @@
    4.18  	protoIdx = gmyth_strstr (value, GMYTH_URI_PROTOCOL_DELIM);
    4.19  	if (0 < protoIdx) {
    4.20          uri->protocol = g_string_new_len (value, protoIdx);
    4.21 +        g_debug ("PROTO: %s IDX: %d", uri->protocol->str, protoIdx);
    4.22  		currIdx += protoIdx + strlen( GMYTH_URI_PROTOCOL_DELIM );
    4.23  	}
    4.24  
    4.25 @@ -249,7 +251,8 @@
    4.26          uri->host = g_string_new_len (value+currIdx, shashIdx);
    4.27  	else if ( gmyth_uri_isabsolute(uri) == TRUE )
    4.28          uri->host = g_string_new_len (value+currIdx, strlen (value) - currIdx);
    4.29 -	host = g_strdup( gmyth_uri_get_host(uri) );
    4.30 +
    4.31 +	host = gmyth_uri_get_host(uri);
    4.32  	colonIdx = gmyth_strrchr (host, GMYTH_URI_COLON_DELIM, 1);
    4.33  	eblacketIdx = gmyth_strrchr (host, GMYTH_URI_EBLACET_DELIM, 1);
    4.34  	if ( ( 0 < colonIdx ) && ( eblacketIdx < colonIdx ) ) {
    4.35 @@ -267,8 +270,8 @@
    4.36  		/**** port ****/
    4.37  		portStr = g_string_new_len (hostStr->str+colonIdx+1, hostLen-colonIdx-1);
    4.38  		uri->port = (gint)g_ascii_strtoull( portStr->str, NULL, 10 );
    4.39 -		g_string_free( portStr, TRUE );
    4.40 -		g_string_free( hostStr, FALSE );
    4.41 +		g_string_free (portStr, TRUE);
    4.42 +		g_string_free (hostStr, TRUE);
    4.43  	}
    4.44  	else {
    4.45          const gchar* protocol = gmyth_uri_get_protocol(uri);
    4.46 @@ -278,7 +281,6 @@
    4.47  		if ( strcmp(protocol, GMYTH_URI_PROTOCOL_FTP) == 0 )
    4.48  			uri->port = GMYTH_URI_DEFAULT_FTP_PORT;
    4.49  	}
    4.50 -    g_free (host);
    4.51  	
    4.52  	if (shashIdx > 0) currIdx += shashIdx;
    4.53  	
     5.1 --- a/gmyth/src/gmyth_util.c	Thu Nov 30 15:20:37 2006 +0000
     5.2 +++ b/gmyth/src/gmyth_util.c	Thu Nov 30 17:49:08 2006 +0000
     5.3 @@ -149,32 +149,37 @@
     5.4  gmyth_util_file_exists (GMythBackendInfo *backend_info, const char* filename)
     5.5  {
     5.6      GMythSocket *socket;
     5.7 -    GMythProgramInfo *program;
     5.8 -    GMythStringList *slist;
     5.9      gboolean res;
    5.10      
    5.11      socket = gmyth_socket_new ();
    5.12 -    gmyth_socket_connect_to_backend (socket, backend_info->hostname,
    5.13 +    res = gmyth_socket_connect_to_backend (socket, backend_info->hostname,
    5.14  		    backend_info->port, TRUE);
    5.15  
    5.16 -    program = g_new0 (GMythProgramInfo, 1);
    5.17 -    program->pathname = g_string_new (filename);
    5.18 +    if (res == TRUE) {
    5.19 +        GMythStringList *slist;
    5.20 +        GMythProgramInfo *program;
    5.21  
    5.22 -    slist = gmyth_string_list_new ();
    5.23 -    gmyth_string_list_append_char_array (slist, "QUERY_CHECKFILE");
    5.24 +        program = g_new0 (GMythProgramInfo, 1);
    5.25 +        program->pathname = g_string_new (filename);
    5.26  
    5.27 -    gmyth_program_info_to_string_list (program, slist);
    5.28 +        slist = gmyth_string_list_new ();
    5.29 +        gmyth_string_list_append_char_array (slist, "QUERY_CHECKFILE");
    5.30  
    5.31 -    gmyth_socket_sendreceive_stringlist (socket, slist);
    5.32 +        gmyth_program_info_to_string_list (program, slist);
    5.33  
    5.34 -    res = (gmyth_string_list_get_int (slist, 0) == 1);
    5.35 +        gmyth_socket_sendreceive_stringlist (socket, slist);
    5.36 +
    5.37 +        res = (gmyth_string_list_get_int (slist, 0) == 1);
    5.38      
    5.39 -    // fixme: we should do this in a program_info_free() function
    5.40 -    g_string_free (program->pathname, FALSE);
    5.41 -    g_free (program);
    5.42 +        // fixme: we should do this in a program_info_free() function
    5.43 +        g_string_free (program->pathname, TRUE);
    5.44 +        g_free (program);
    5.45  
    5.46 -    g_object_unref (slist);
    5.47 +        g_object_unref (slist);
    5.48  
    5.49 +        gmyth_socket_close_connection (socket);
    5.50 +    }
    5.51 +    g_object_unref (socket);
    5.52      return res;    
    5.53  }
    5.54