# HG changeset patch # User renatofilho # Date 1164908948 0 # Node ID 5095b85ff808731e15f09e69e535f5f9292ccad3 # Parent 07951674b0c0067f24231ee67fec9fadb087dd60 [svn r148] code cleanup diff -r 07951674b0c0 -r 5095b85ff808 gmyth/src/Makefile.am --- a/gmyth/src/Makefile.am Thu Nov 30 15:20:37 2006 +0000 +++ b/gmyth/src/Makefile.am Thu Nov 30 17:49:08 2006 +0000 @@ -27,8 +27,7 @@ $(GST_CFLAGS) \ $(GSTBASE_CFLAGS) \ $(GSTPLUGINSBASE_CFLAGS) \ - $(MYSQL_CFLAGS) \ - -g3 -O0 + $(MYSQL_CFLAGS) #libgmyth_la_LIBADD = \ # $(MYSQL_LIBS) diff -r 07951674b0c0 -r 5095b85ff808 gmyth/src/gmyth_file_transfer.h --- a/gmyth/src/gmyth_file_transfer.h Thu Nov 30 15:20:37 2006 +0000 +++ b/gmyth/src/gmyth_file_transfer.h Thu Nov 30 17:49:08 2006 +0000 @@ -110,6 +110,7 @@ gint gmyth_file_transfer_read(GMythFileTransfer *transfer, GByteArray *data, gint size, gboolean read_unlimited); gint64 gmyth_file_transfer_seek(GMythFileTransfer *transfer, guint64 pos, gint whence); gboolean gmyth_file_transfer_settimeout( GMythFileTransfer *transfer, gboolean fast); +guint64 gmyth_file_transfer_get_filesize (GMythFileTransfer *transfer); //gboolean gmyth_file_transfer_playback_setup( GMythFileTransfer **transfer, gboolean live_tv ); //gboolean gmyth_file_transfer_setup( GMythFileTransfer **transfer, gboolean live_tv ); diff -r 07951674b0c0 -r 5095b85ff808 gmyth/src/gmyth_socket.c --- a/gmyth/src/gmyth_socket.c Thu Nov 30 15:20:37 2006 +0000 +++ b/gmyth/src/gmyth_socket.c Thu Nov 30 17:49:08 2006 +0000 @@ -103,26 +103,27 @@ * @return gint that represents the error number from getaddrinfo(). */ static gint -gmyth_socket_toaddrinfo( gchar *addr, gint port, struct addrinfo **addrInfo ) +gmyth_socket_toaddrinfo (gchar *addr, gint port, struct addrinfo **addrInfo ) { - struct addrinfo hints; - gchar *portStr = g_strnfill( 32, ' ' ); + struct addrinfo *hints; + gchar *portStr = NULL; gint errorn = EADDRNOTAVAIL; - memset( &hints, 0, sizeof(hints) ); - hints.ai_family = AF_INET; - hints.ai_socktype = SOCK_STREAM; + hints = g_new0 (struct addrinfo, 1); + hints->ai_family = AF_INET; + hints->ai_socktype = SOCK_STREAM; /* hints.ai_flags = AI_NUMERICHOST; */ if ( port != -1 ) - sprintf(portStr, "%d", port); + portStr = g_strdup_printf ("%d", port); else portStr = NULL; gmyth_debug ("Address: %s, port: %s\n", addr, portStr); - if ( ( errorn = getaddrinfo(addr, portStr, &hints, addrInfo) ) != 0 ) { + if ( ( errorn = getaddrinfo(addr, portStr, hints, addrInfo) ) != 0 ) { g_printerr( "[%s] Socket ERROR: %s\n", __FUNCTION__, gai_strerror(errorn) ); } - + g_free (portStr); + g_free (hints); return errorn; } @@ -284,21 +285,18 @@ * @return GString* get local hostname. */ GString * -gmyth_socket_get_local_hostname( ) +gmyth_socket_get_local_hostname () { GString *str = g_string_sized_new( 1024 ); - gchar *localhostname = g_strnfill( 1024, ' ' ); - gchar *localaddr = g_strdup( "127.0.0.1" ); - + gchar localhostname[1024]; + gchar *localaddr = NULL; gboolean found_addr = FALSE; - struct addrinfo* addr_info_data = NULL, *addr_info0 = NULL; - struct sockaddr_in* sa = NULL; gethostname(localhostname, 1024); - gint err = gmyth_socket_toaddrinfo( localhostname, -1, &addr_info_data ); + gint err = gmyth_socket_toaddrinfo (localhostname, -1, &addr_info_data ); if ( err == EADDRNOTAVAIL ) { @@ -321,6 +319,7 @@ } addr_info0 = addr_info0->ai_next; }; + freeaddrinfo (addr_info_data); if ( found_addr == FALSE ) { gchar *prim_addr = gmyth_socket_get_primary_addr(); @@ -332,15 +331,12 @@ str = g_string_assign( str, g_strdup( prim_addr ) ); g_free( prim_addr ); } else { - str = g_string_assign( str, g_strdup( localhostname ) ); + str = g_string_assign (str, localhostname); } } g_static_mutex_unlock( &mutex ); - if (localhostname!=NULL) - g_free( localhostname ); - return str; } @@ -349,8 +345,12 @@ { GMythSocket *gmyth_socket = GMYTH_SOCKET(object); + /* disconnect socket */ gmyth_socket_close_connection (gmyth_socket); - /* disconnect socket */ + + g_free (gmyth_socket->hostname); + gmyth_socket->hostname = NULL; + G_OBJECT_CLASS (gmyth_socket_parent_class)->dispose (object); } @@ -371,10 +371,6 @@ { GMythSocket *gmyth_socket = GMYTH_SOCKET (g_object_new(GMYTH_SOCKET_TYPE, NULL)); - gmyth_socket->sd_io_ch = NULL; - - gmyth_socket->hostname = g_strdup(""); - gmyth_socket->port = 6543; gmyth_socket->mythtv_version = MYTHTV_VERSION_DEFAULT; @@ -497,41 +493,41 @@ gmyth_socket->port = port; for ( addr_info0 = addr_info_data; addr_info0; addr_info0 = addr_info_data->ai_next ) { + struct sockaddr_in *sa = (struct sockaddr_in*)addr_info0->ai_addr; + /* init socket descriptor */ + gmyth_socket->sd = socket( addr_info0->ai_family, addr_info0->ai_socktype, + addr_info0->ai_protocol ); - struct sockaddr_in *sa = (struct sockaddr_in*)addr_info0->ai_addr; - /* init socket descriptor */ - gmyth_socket->sd = socket( addr_info0->ai_family, addr_info0->ai_socktype, - addr_info0->ai_protocol ); + if ( gmyth_socket->sd < 0 ) + continue; - if ( gmyth_socket->sd < 0 ) - continue; + gmyth_debug( "[%s] hostname = %s, sock_fd = %d, addr = %s, addr_len = %d, "\ + "ai_family = %d, ai_protocol = %d\n", + __FUNCTION__, hostname, gmyth_socket->sd, inet_ntoa( sa->sin_addr ), + addr_info0->ai_addrlen, addr_info0->ai_family, addr_info0->ai_protocol ); + + struct timeval *timeout = g_new0( struct timeval, 1 ); + + /* using 40 seconds timeout */ + timeout->tv_sec = 40; + timeout->tv_usec = 0; + + if ( gmyth_socket_try_connect( gmyth_socket->sd, (struct sockaddr *)addr_info0->ai_addr, + addr_info0->ai_addrlen, timeout, &ret_code ) < 0 ) + { + g_printerr( "[%s] Error connecting to backend!\n", __FUNCTION__ ); + if ( ret_code == ETIMEDOUT ) + g_printerr( "[%s]\tBackend host unreachable!\n", __FUNCTION__ ); - gmyth_debug( "[%s] hostname = %s, sock_fd = %d, addr = %s, addr_len = %d, "\ - "ai_family = %d, ai_protocol = %d\n", - __FUNCTION__, hostname, gmyth_socket->sd, inet_ntoa( sa->sin_addr ), - addr_info0->ai_addrlen, addr_info0->ai_family, addr_info0->ai_protocol ); - - struct timeval *timeout = g_new0( struct timeval, 1 ); - - /* using 40 seconds timeout */ - timeout->tv_sec = 40; - timeout->tv_usec = 0; - - if ( gmyth_socket_try_connect( gmyth_socket->sd, (struct sockaddr *)addr_info0->ai_addr, - addr_info0->ai_addrlen, timeout, &ret_code ) < 0 ) - { - g_printerr( "[%s] Error connecting to backend!\n", __FUNCTION__ ); - if ( ret_code == ETIMEDOUT ) - g_printerr( "[%s]\tBackend host unreachable!\n", __FUNCTION__ ); + g_printerr( "ERROR: %s\n", gai_strerror(ret_code) ); + continue; + } - g_printerr( "ERROR: %s\n", gai_strerror(ret_code) ); - continue; - } - - /* only will be reached if none of the error above occurred */ - break; - + /* only will be reached if none of the error above occurred */ + break; } + freeaddrinfo (addr_info_data); + addr_info_data = NULL; gmyth_socket->sd_io_ch = g_io_channel_unix_new( gmyth_socket->sd ); @@ -758,9 +754,8 @@ gint gmyth_socket_check_protocol_version_number (GMythSocket *gmyth_socket, gint mythtv_version) { - GString *response; - GString *payload; - gchar *new_version = g_strdup(""); + GString *response = NULL; + GString *payload = NULL; gboolean res = TRUE; gint mythtv_new_version = MYTHTV_CANNOT_NEGOTIATE_VERSION; guint max_iterations = MYTHTV_MAX_VERSION_CHECKS; @@ -783,15 +778,16 @@ g_warning ("[%s] Protocol version request error: %s", __FUNCTION__, response->str); /* get the version number returned by the REJECT message */ if ( ( res = g_str_has_prefix (response->str, "REJECT") ) == TRUE ) { + gchar *new_version = NULL; new_version = g_strrstr( response->str, "]" ); if (new_version!=NULL) { ++new_version; /* skip ']' character */ if ( new_version != NULL ) { gmyth_debug ( "[%s] got MythTV version = %s.\n", __FUNCTION__, new_version ); - mythtv_version = (gint)g_ascii_strtoull( g_strdup( new_version ), NULL, 10 ); + mythtv_version = (gint)g_ascii_strtoull (new_version, NULL, 10 ); /* do reconnection to the socket (socket is closed if the MythTV version was wrong) */ gmyth_socket_connect( gmyth_socket, gmyth_socket->hostname, gmyth_socket->port ); - /* g_free( new_version ); */ + new_version =NULL; if ( --max_iterations > 0 ) goto try_new_version; else @@ -862,14 +858,13 @@ /* verify if the input (read) buffer is ready to receive data */ - buffer = g_strnfill( BUFLEN, ' ' ); - g_static_mutex_lock( &mutex ); - io_status = g_io_channel_read_chars( gmyth_socket->sd_io_ch, buffer, MYTH_PROTOCOL_FIELD_SIZE, &bytes_read, &error ); + buffer = g_new0 (gchar, MYTH_PROTOCOL_FIELD_SIZE); + io_status = g_io_channel_read_chars (gmyth_socket->sd_io_ch, buffer, MYTH_PROTOCOL_FIELD_SIZE, &bytes_read, &error); /* verify if the input (read) buffer is ready to receive data */ - io_cond = g_io_channel_get_buffer_condition( gmyth_socket->sd_io_ch ); + io_cond = g_io_channel_get_buffer_condition (gmyth_socket->sd_io_ch); gmyth_debug ( "[%s] Bytes read = %d\n", __FUNCTION__, bytes_read ); @@ -884,17 +879,22 @@ io_cond = g_io_channel_get_buffer_condition( gmyth_socket->sd_io_ch ); if ( ( io_cond & G_IO_IN ) != 0 ) { + gchar *buffer_aux = NULL; + + /* removes trailing whitespace */ + buffer_aux = g_strstrip (buffer); + len = (gint)g_ascii_strtoull (buffer_aux, NULL, 10 ); + + if (buffer != NULL) { + g_free (buffer); + buffer = NULL; + } + + buffer = g_new0 (gchar, len+1); - snprintf( buffer, MYTH_PROTOCOL_FIELD_SIZE+1, "%-8s", g_strdup(buffer)); - gmyth_debug ( "[%s] buffer = [%s]\n", __FUNCTION__, buffer ); - - /* removes trailing whitespace */ - buffer = g_strstrip( buffer ); - - len = (gint)g_ascii_strtoull ( buffer, NULL, 10 ); - + g_debug ("BUFF %s : len %d : MYTH_PROTOCOL_FIELD_SIZE %d", buffer, len, MYTH_PROTOCOL_FIELD_SIZE); bytes_read = 0; - io_status = g_io_channel_read_chars( gmyth_socket->sd_io_ch, buffer, len, &bytes_read, &error ); + io_status = g_io_channel_read_chars( gmyth_socket->sd_io_ch, buffer, len, &bytes_read, &error); buffer[bytes_read] = '\0'; } } @@ -906,17 +906,15 @@ if ( ( bytes_read != len ) || ( io_status == G_IO_STATUS_ERROR ) ) str = NULL; else - str = g_string_new( buffer ); - - if ( buffer != NULL ) - g_free( buffer ); + str = g_string_new (buffer); if ( error != NULL ) { g_printerr( "[%s] Error found receiving response from the IO channel: (%d, %s)\n", __FUNCTION__, error->code, error->message ); str = NULL; - g_error_free( error ); + g_error_free (error); } + g_free (buffer); return str; } diff -r 07951674b0c0 -r 5095b85ff808 gmyth/src/gmyth_uri.c --- a/gmyth/src/gmyth_uri.c Thu Nov 30 15:20:37 2006 +0000 +++ b/gmyth/src/gmyth_uri.c Thu Nov 30 17:49:08 2006 +0000 @@ -1,4 +1,4 @@ -/** +/** * GMyth Library * * @file gmyth/gmyth_uri.c @@ -161,6 +161,7 @@ { gboolean ret = FALSE; + g_debug ("URI: %p PROTO: %p", uri->uri, uri->protocol); g_return_val_if_fail( uri != NULL && uri->uri != NULL && uri->protocol != NULL, FALSE ); if ( gmyth_strstr( uri->uri->str, GMYTH_URI_PROTOCOL_DELIM ) == 0 || strlen(uri->protocol->str) > 0 ) @@ -226,6 +227,7 @@ protoIdx = gmyth_strstr (value, GMYTH_URI_PROTOCOL_DELIM); if (0 < protoIdx) { uri->protocol = g_string_new_len (value, protoIdx); + g_debug ("PROTO: %s IDX: %d", uri->protocol->str, protoIdx); currIdx += protoIdx + strlen( GMYTH_URI_PROTOCOL_DELIM ); } @@ -249,7 +251,8 @@ uri->host = g_string_new_len (value+currIdx, shashIdx); else if ( gmyth_uri_isabsolute(uri) == TRUE ) uri->host = g_string_new_len (value+currIdx, strlen (value) - currIdx); - host = g_strdup( gmyth_uri_get_host(uri) ); + + host = gmyth_uri_get_host(uri); colonIdx = gmyth_strrchr (host, GMYTH_URI_COLON_DELIM, 1); eblacketIdx = gmyth_strrchr (host, GMYTH_URI_EBLACET_DELIM, 1); if ( ( 0 < colonIdx ) && ( eblacketIdx < colonIdx ) ) { @@ -267,8 +270,8 @@ /**** port ****/ portStr = g_string_new_len (hostStr->str+colonIdx+1, hostLen-colonIdx-1); uri->port = (gint)g_ascii_strtoull( portStr->str, NULL, 10 ); - g_string_free( portStr, TRUE ); - g_string_free( hostStr, FALSE ); + g_string_free (portStr, TRUE); + g_string_free (hostStr, TRUE); } else { const gchar* protocol = gmyth_uri_get_protocol(uri); @@ -278,7 +281,6 @@ if ( strcmp(protocol, GMYTH_URI_PROTOCOL_FTP) == 0 ) uri->port = GMYTH_URI_DEFAULT_FTP_PORT; } - g_free (host); if (shashIdx > 0) currIdx += shashIdx; diff -r 07951674b0c0 -r 5095b85ff808 gmyth/src/gmyth_util.c --- a/gmyth/src/gmyth_util.c Thu Nov 30 15:20:37 2006 +0000 +++ b/gmyth/src/gmyth_util.c Thu Nov 30 17:49:08 2006 +0000 @@ -149,32 +149,37 @@ gmyth_util_file_exists (GMythBackendInfo *backend_info, const char* filename) { GMythSocket *socket; - GMythProgramInfo *program; - GMythStringList *slist; gboolean res; socket = gmyth_socket_new (); - gmyth_socket_connect_to_backend (socket, backend_info->hostname, + res = gmyth_socket_connect_to_backend (socket, backend_info->hostname, backend_info->port, TRUE); - program = g_new0 (GMythProgramInfo, 1); - program->pathname = g_string_new (filename); + if (res == TRUE) { + GMythStringList *slist; + GMythProgramInfo *program; - slist = gmyth_string_list_new (); - gmyth_string_list_append_char_array (slist, "QUERY_CHECKFILE"); + program = g_new0 (GMythProgramInfo, 1); + program->pathname = g_string_new (filename); - gmyth_program_info_to_string_list (program, slist); + slist = gmyth_string_list_new (); + gmyth_string_list_append_char_array (slist, "QUERY_CHECKFILE"); - gmyth_socket_sendreceive_stringlist (socket, slist); + gmyth_program_info_to_string_list (program, slist); - res = (gmyth_string_list_get_int (slist, 0) == 1); + gmyth_socket_sendreceive_stringlist (socket, slist); + + res = (gmyth_string_list_get_int (slist, 0) == 1); - // fixme: we should do this in a program_info_free() function - g_string_free (program->pathname, FALSE); - g_free (program); + // fixme: we should do this in a program_info_free() function + g_string_free (program->pathname, TRUE); + g_free (program); - g_object_unref (slist); + g_object_unref (slist); + gmyth_socket_close_connection (socket); + } + g_object_unref (socket); return res; }