# HG changeset patch # User renatofilho # Date 1164895115 0 # Node ID f1f98329934ef4216cf4a8c9aa2bbf17d9be3ad1 # Parent 998bdad22bfe6de4b09f17d7445c256ff5a483b8 [svn r144] code cleanup diff -r 998bdad22bfe -r f1f98329934e gmyth/src/gmyth_file_transfer.c --- a/gmyth/src/gmyth_file_transfer.c Wed Nov 29 22:36:28 2006 +0000 +++ b/gmyth/src/gmyth_file_transfer.c Thu Nov 30 13:58:35 2006 +0000 @@ -84,16 +84,16 @@ GMYTH_RINGBUFFER_TYPE }; -static GStaticMutex st_mutex = G_STATIC_MUTEX_INIT; +//static GStaticMutex st_mutex = G_STATIC_MUTEX_INIT; -static gboolean* myth_control_sock_listener( GIOChannel *io_channel ); +//static gboolean* myth_control_sock_listener( GIOChannel *io_channel ); /* static gboolean myth_control_sock_listener( GIOChannel *io_channel, GIOCondition condition, gpointer data ); */ static GMutex* mutex = NULL; static GCond* io_watcher_cond = NULL; -static GMainContext* io_watcher_context = NULL; +//static GMainContext* io_watcher_context = NULL; static void gmyth_file_transfer_class_init (GMythFileTransferClass *klass); static void gmyth_file_transfer_init (GMythFileTransfer *object); @@ -177,16 +177,31 @@ { gboolean ret = TRUE; - transfer->uri = gmyth_uri_new_with_value ( uri_str->str ); - transfer->hostname = g_string_new( gmyth_uri_get_host(transfer->uri) ); - transfer->port = gmyth_uri_get_port( transfer->uri ); + if (transfer->uri != NULL) { + g_object_unref (G_OBJECT (transfer->uri)); + transfer->uri = NULL; + } + + if (transfer->hostname != NULL) { + g_string_free (transfer->hostname, TRUE); + transfer->hostname = NULL; + } + + if (transfer->hostname != NULL) { + g_string_free (transfer->hostname, TRUE); + transfer->hostname = NULL; + } + + transfer->uri = gmyth_uri_new_with_value (uri_str->str); + transfer->hostname = g_string_new (gmyth_uri_get_host(transfer->uri)); + transfer->port = gmyth_uri_get_port (transfer->uri); gmyth_debug ("[%s] URI: %s\n", __FUNCTION__, uri_str->str); gmyth_debug ("hostname: %s, port %d\n", transfer->hostname->str, transfer->port); /* configure the control socket */ if (transfer->control_sock == NULL) { - if ( !gmyth_connect_to_backend (transfer)) { + if (!gmyth_connect_to_backend (transfer)) { g_printerr( "Connection to backend failed (Control Socket).\n" ); ret = FALSE; } @@ -198,6 +213,7 @@ } +/* static gboolean* //myth_control_sock_listener( GIOChannel *io_channel, GIOCondition condition, gpointer data ) myth_control_sock_listener( GIOChannel *io_channel ) @@ -282,7 +298,9 @@ return ret; } +*/ +/* static gboolean gmyth_connect_to_backend_monitor (GMythFileTransfer *transfer, GString *hostname ) { @@ -296,10 +314,9 @@ g_string_printf( base_str, "ANN Monitor %s %d", hostname->str, TRUE ); gmyth_socket_send_command( transfer->event_sock, base_str ); - GString *resp = gmyth_socket_receive_response( transfer->event_sock ); + //GString *resp = gmyth_socket_receive_response( transfer->event_sock ); gmyth_debug ( "[%s] Got Monitor response from %s: %s\n", __FUNCTION__, base_str->str, resp->str ); monitor_th = g_thread_create( (GThreadFunc)myth_control_sock_listener, transfer->event_sock->sd_io_ch, FALSE, NULL ); - /* io_watcher_context = g_main_context_default(); GMainLoop *loop = g_main_loop_new( NULL, TRUE ); @@ -317,23 +334,17 @@ g_source_attach( source, io_watcher_context ); - */ - -/* if (source==NULL) { g_printerr( "[%s] Error adding watch listener function to the IO control channel!\n", __FUNCTION__ ); goto cleanup; } -*/ gmyth_debug ( "[%s]\tOK! Starting listener on the MONITOR event socket...\n", __FUNCTION__ ); //g_main_loop_run( loop ); -cleanup: - /* +//cleanup: if ( source != NULL ) g_source_unref( source ); - */ gmyth_debug( "[%s] Watch listener function over the IO control channel? %s!!!\n", __FUNCTION__, ( ret == TRUE ? "YES" : "NO" ) ); @@ -341,22 +352,31 @@ return ret; } +*/ static gboolean gmyth_connect_to_backend (GMythFileTransfer *transfer) { - GString *base_str = g_string_new( "" ); + GString *base_str = NULL; GString *hostname = NULL; - GMythStringList *strlist; + GMythStringList *strlist = NULL; + gchar *path_dir = NULL; + gboolean ret = TRUE; g_return_val_if_fail (transfer != NULL, FALSE ); g_return_val_if_fail (transfer->uri != NULL, FALSE ); g_return_val_if_fail (transfer->hostname != NULL, FALSE); g_return_val_if_fail (transfer->port > 0, FALSE); - gchar *path_dir = gmyth_uri_get_path (transfer->uri); + base_str = g_string_new (""); + path_dir = gmyth_uri_get_path (transfer->uri); /* Creates the control socket */ + if (transfer->control_sock != NULL) { + g_object_unref (transfer->control_sock); + transfer->control_sock = NULL; + } + transfer->control_sock = gmyth_socket_new(); // Connects the socket, send Mythtv ANN command and verify Mythtv protocol version @@ -369,6 +389,11 @@ } /* Creates the data socket */ + if (transfer->sock != NULL) { + g_object_unref (transfer->sock); + transfer->sock = NULL; + } + transfer->sock = gmyth_socket_new (); gmyth_socket_connect (transfer->sock, transfer->hostname->str, transfer->port); @@ -397,12 +422,14 @@ if (transfer->filesize < 0 ) { gmyth_debug ( "[%s] Got filesize equals to %llu is lesser than 0 [invalid stream file]\n", __FUNCTION__, transfer->filesize ); - g_object_unref(transfer->sock); + g_object_unref (transfer->sock); transfer->sock = NULL; - return FALSE; + ret = FALSE; + goto cleanup; } //gmyth_connect_to_backend_monitor( transfer, hostname ); +cleanup: if ( strlist != NULL ) g_object_unref( strlist ); @@ -410,7 +437,7 @@ g_string_free (base_str, TRUE); g_string_free (hostname, TRUE); - return TRUE; + return ret; } gboolean @@ -579,11 +606,8 @@ GIOCondition io_cond_control; GIOStatus io_status = G_IO_STATUS_NORMAL, io_status_control = G_IO_STATUS_NORMAL; - GMythStringList *strlist = NULL; GString *query; - gchar *data_buffer = NULL; - g_return_val_if_fail ( data != NULL, -2 ); io_channel = transfer->sock->sd_io_ch; @@ -615,8 +639,7 @@ myth_control_acquire_context( TRUE ); while (total_read == 0) { - - strlist = gmyth_string_list_new(); + GMythStringList *strlist = gmyth_string_list_new(); gmyth_string_list_append_char_array( strlist, query->str ); gmyth_string_list_append_char_array( strlist, "REQUEST_BLOCK" ); @@ -629,44 +652,39 @@ gmyth_socket_read_stringlist( transfer->control_sock, strlist ); if ( strlist != NULL && gmyth_string_list_length( strlist ) > 0 ) { - bytes_sent = gmyth_string_list_get_int( strlist, 0 ); // -1 on backend error + bytes_sent = gmyth_string_list_get_int( strlist, 0 ); // -1 on backend error + gmyth_debug ( "[%s] got SENT buffer message = %d\n", __FUNCTION__, bytes_sent ); - gmyth_debug ( "[%s] got SENT buffer message = %d\n", __FUNCTION__, bytes_sent ); if ( bytes_sent != 0 ) { - - data_buffer = g_new0( gchar, bytes_sent ); - - while (total_read != bytes_sent) { - + gchar *data_buffer = g_new0 (gchar, bytes_sent); + while ( 0 != bytes_sent) { io_status = g_io_channel_read_chars( io_channel, data_buffer + total_read, - (gsize) bytes_sent, // buffer_len - &bytes_read, &error ); - - total_read += bytes_read; + (gsize) bytes_sent, // buffer_len + &bytes_read, &error ); + total_read += bytes_read; + bytes_sent -= bytes_read; - /* append new data to the increasing byte array */ - data = g_byte_array_append ( data, g_memdup( data_buffer, bytes_read ), bytes_read ); - - printf ("Total file transfer data read: %d\n", total_read); + /* append new data to the increasing byte array */ + data = g_byte_array_append (data, g_memdup (data_buffer, bytes_read), bytes_read); + gmyth_debug ("Total file transfer data read: %d\n", total_read); + } + g_free (data_buffer); } - } } + g_object_unref (strlist); + strlist = NULL; } myth_control_release_context( ); + g_string_free (query, TRUE); - g_object_unref( strlist ); - g_string_free (query, TRUE); - - - if ( error != NULL ) { - g_printerr( "Cleaning-up ERROR: %s [msg = %s, code = %d]\n", __FUNCTION__, error->message, - error->code ); - g_error_free( error ); - } + if ( error != NULL ) { + gmyth_debug ("Cleaning-up ERROR: %s [msg = %s, code = %d]\n", __FUNCTION__, error->message, + error->code); + g_error_free (error); + } transfer->readposition += total_read; - return total_read; } diff -r 998bdad22bfe -r f1f98329934e gmyth/src/gmyth_uri.c --- a/gmyth/src/gmyth_uri.c Wed Nov 29 22:36:28 2006 +0000 +++ b/gmyth/src/gmyth_uri.c Thu Nov 30 13:58:35 2006 +0000 @@ -59,14 +59,6 @@ static void gmyth_uri_init (GMythURI *gmyth_uri) { - gmyth_uri->host = g_string_new(""); - gmyth_uri->fragment = g_string_new(""); - gmyth_uri->password = g_string_new(""); - gmyth_uri->path = g_string_new(""); - gmyth_uri->protocol = g_string_new(""); - gmyth_uri->query = g_string_new(""); - gmyth_uri->uri = g_string_new(""); - gmyth_uri->user = g_string_new(""); } static void @@ -127,7 +119,7 @@ * @return a new instance of GMythURI. */ GMythURI * -gmyth_uri_new( ) +gmyth_uri_new (void) { GMythURI *gmyth_uri = GMYTH_URI (g_object_new (GMYTH_URI_TYPE, NULL)); @@ -139,17 +131,17 @@ * @return a new instance of GMythURI. */ GMythURI * -gmyth_uri_new_with_value( const gchar *value ) +gmyth_uri_new_with_value (const gchar *value) { GMythURI *gmyth_uri = GMYTH_URI (g_object_new (GMYTH_URI_TYPE, NULL)); - gmyth_uri_parser_setup_and_new( gmyth_uri, value ); + gmyth_uri_parser_setup_and_new (gmyth_uri, value); return gmyth_uri; } static gint -gmyth_strstr( const gchar *haystack, const gchar *needle ) +gmyth_strstr (const gchar *haystack, const gchar *needle) { gchar *strPos; @@ -165,7 +157,7 @@ } static gboolean -gmyth_uri_isabsolute( const GMythURI *uri ) +gmyth_uri_isabsolute (const GMythURI *uri) { gboolean ret = FALSE; @@ -203,17 +195,14 @@ gmyth_uri_parser_setup_and_new( GMythURI *uri, const gchar *value ) { - gchar *protocol; gint uriLen; gint currIdx; gint protoIdx; gint atIdx; gint colonIdx; gint shashIdx; - gchar *host; + gchar *host; gint eblacketIdx; - GString *hostStr; - GString *portStr; gint hostLen; gint sharpIdx; gint questionIdx; @@ -225,9 +214,9 @@ currIdx = 0; /*** Protocol ****/ - protoIdx = gmyth_strstr( value, GMYTH_URI_PROTOCOL_DELIM ); + protoIdx = gmyth_strstr (value, GMYTH_URI_PROTOCOL_DELIM); if (0 < protoIdx) { - uri->protocol = g_string_append_len( uri->protocol, value, protoIdx ); + uri->protocol = g_string_append_len (uri->protocol, value, protoIdx); currIdx += protoIdx + strlen( GMYTH_URI_PROTOCOL_DELIM ); } @@ -252,16 +241,16 @@ else if ( gmyth_uri_isabsolute(uri) == TRUE ) uri->host = g_string_append_len( uri->host, value+currIdx, strlen(value) - currIdx ); host = g_strdup( gmyth_uri_get_host(uri) ); - colonIdx = gmyth_strrchr( host, GMYTH_URI_COLON_DELIM, 1 ); - eblacketIdx = gmyth_strrchr( host, GMYTH_URI_EBLACET_DELIM, 1 ); + colonIdx = gmyth_strrchr (host, GMYTH_URI_COLON_DELIM, 1); + eblacketIdx = gmyth_strrchr (host, GMYTH_URI_EBLACET_DELIM, 1); if ( ( 0 < colonIdx ) && ( eblacketIdx < colonIdx ) ) { - hostStr = g_string_new( host ); + GString *portStr = NULL; + GString *hostStr = g_string_new (host); hostLen = hostStr->len; /**** host ****/ - uri->host = g_string_erase( uri->host, 0, hostLen ); - uri->host = g_string_insert_len( uri->host, 0, hostStr->str, colonIdx ); - //host = gmyth_uri_gethost( uri ); + uri->host = g_string_erase (uri->host, 0, hostLen); + uri->host = g_string_insert_len (uri->host, 0, hostStr->str, colonIdx); if (0 < hostLen) { if (host[0] == '[' && host[hostLen-1] == ']') uri->host = g_string_append_len( uri->host, hostStr->str+1, colonIdx-2 ); @@ -274,13 +263,14 @@ g_string_free( hostStr, FALSE ); } else { + const gchar* protocol = gmyth_uri_get_protocol(uri); uri->port = GMYTH_URI_KNKOWN_PORT; - protocol = gmyth_uri_get_protocol(uri); if ( strcmp(protocol, GMYTH_URI_PROTOCOL_HTTP) == 0 ) uri->port = GMYTH_URI_DEFAULT_HTTP_PORT; 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 998bdad22bfe -r f1f98329934e gmyth/src/gmyth_uri.h --- a/gmyth/src/gmyth_uri.h Wed Nov 29 22:36:28 2006 +0000 +++ b/gmyth/src/gmyth_uri.h Thu Nov 30 13:58:35 2006 +0000 @@ -101,22 +101,18 @@ }; -GType gmyth_uri_get_type (void); - -GMythURI* gmyth_uri_new ( void ); - -GMythURI* gmyth_uri_new_with_value ( const gchar *value ); - -gboolean gmyth_uri_is_equals( GMythURI* uri1, GMythURI* uri2 ); - -#define gmyth_uri_get_host(urip) (urip->host->str) -#define gmyth_uri_get_port(urip) (urip->port) -#define gmyth_uri_get_protocol(urip) (urip->protocol->str) -#define gmyth_uri_get_path(urip) (urip->path->str) -#define gmyth_uri_get_user(urip) (urip->user->str) -#define gmyth_uri_get_password(urip) (urip->password->str) -#define gmyth_uri_get_fragment(urip) (urip->fragment->str) -#define gmyth_uri_get_query(urip) (urip->query->str) +GType gmyth_uri_get_type (void); +GMythURI* gmyth_uri_new (void); +GMythURI* gmyth_uri_new_with_value (const gchar *value); +gboolean gmyth_uri_is_equals (GMythURI* uri1, GMythURI* uri2); +#define gmyth_uri_get_host(urip) (urip->host->str) +#define gmyth_uri_get_port(urip) (urip->port) +#define gmyth_uri_get_protocol(urip) (urip->protocol->str) +#define gmyth_uri_get_path(urip) (urip->path->str) +#define gmyth_uri_get_user(urip) (urip->user->str) +#define gmyth_uri_get_password(urip) (urip->password->str) +#define gmyth_uri_get_fragment(urip) (urip->fragment->str) +#define gmyth_uri_get_query(urip) (urip->query->str) G_END_DECLS