libgnomevfs2-mythtv/modules/mythtv-method.c
author rosfran
Wed Jan 17 20:08:44 2007 +0000 (2007-01-17)
branchtrunk
changeset 272 8cab5caf8d09
parent 190 201327c993e5
child 277 890b023c759f
permissions -rwxr-xr-x
[svn r273] Fixes the timeout bug, it uses to happen in some network topologies, like the WLANs, which throws TIMED_OUT sockets when trying to opening them.
     1 /*
     2  * @author Hallyson Melo <hallyson.melo@indt.org.br>
     3  *
     4  * This program is free software; you can redistribute it and/or modify
     5  * it under the terms of the GNU Lesser General Public License as published by
     6  * the Free Software Foundation; either version 2 of the License, or
     7  * (at your option) any later version.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU Lesser General Public License
    15  * along with this program; if not, write to the Free Software
    16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    17  */
    18 
    19 #ifdef HAVE_CONFIG_H
    20 #include <config.h>
    21 #endif
    22 
    23 #include <string.h>
    24 #include <glib.h>
    25 #include <math.h>
    26 
    27 #include <libgnomevfs/gnome-vfs-module.h>
    28 #include <libgnomevfs/gnome-vfs-utils.h>
    29 
    30 #include <gmyth_file_transfer.h>
    31 #include <gmyth_backendinfo.h>
    32 #include <gmyth_util.h>
    33 
    34 #define GST_MYTHTV_ID_NUM               1
    35 #define MYTHTV_VERSION_DEFAULT          30
    36 
    37 #define MYTHTV_BUFFER_SIZE		1024*64
    38 
    39 static GnomeVFSResult do_read (GnomeVFSMethod *method,
    40                                GnomeVFSMethodHandle *method_handle,
    41                                gpointer buffer,
    42                                GnomeVFSFileSize num_bytes,
    43                                GnomeVFSFileSize *bytes_read,
    44                                GnomeVFSContext *context);
    45 
    46 typedef struct {
    47     GMythFileTransfer *file_transfer;
    48     
    49     gint mythtv_version;
    50     guint64 content_size;
    51     guint64 bytes_read;
    52 
    53     GByteArray *buffer;
    54     gsize buffer_remain;
    55 } MythtvHandle;
    56 
    57 
    58 
    59 static GnomeVFSResult
    60 do_open (GnomeVFSMethod *method,
    61          GnomeVFSMethodHandle **method_handle,
    62          GnomeVFSURI *uri,
    63          GnomeVFSOpenMode mode,
    64          GnomeVFSContext *context)
    65 {
    66     MythtvHandle *myth_handle;
    67     GMythBackendInfo *backend_info;
    68     gboolean ret;
    69 
    70     _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
    71     _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
    72 
    73     myth_handle = g_new0 (MythtvHandle, 1);
    74 
    75     if (mode & GNOME_VFS_OPEN_WRITE) {
    76         return GNOME_VFS_ERROR_NOT_PERMITTED;
    77     }
    78 
    79     if (gnome_vfs_uri_get_host_name (uri) == NULL) {
    80     	return GNOME_VFS_ERROR_INVALID_HOST_NAME;
    81     }
    82 
    83     /* Initialize mythtv handler*/
    84     myth_handle->file_transfer = NULL;
    85     myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
    86     myth_handle->bytes_read = 0;
    87     myth_handle->content_size = -1;
    88 
    89     /* Creates and fills out the backend info structure */
    90     backend_info = gmyth_backend_info_new_full (
    91         gnome_vfs_uri_get_host_name (uri),
    92 	    NULL, NULL, NULL,
    93 	    gnome_vfs_uri_get_host_port (uri));
    94 	    
    95 
    96     /* Verifies if the file exists */
    97     if (!gmyth_util_file_exists (backend_info, gnome_vfs_uri_get_path (uri))) {
    98         g_object_unref (backend_info);
    99 	return GNOME_VFS_ERROR_NOT_FOUND;
   100     }
   101 		    
   102     /* Connect to the backend */
   103     myth_handle->file_transfer = gmyth_file_transfer_new (backend_info);
   104     g_object_unref (backend_info);
   105 
   106     g_return_val_if_fail (myth_handle->file_transfer != NULL, GNOME_VFS_ERROR_NOT_OPEN);
   107 
   108     /* sets the Playback monitor connection */
   109     ret = gmyth_file_transfer_open (myth_handle->file_transfer, gnome_vfs_uri_get_path (uri));
   110     if (ret == FALSE) {
   111         g_warning ("Mythtv FileTransfer open error\n");
   112         return GNOME_VFS_ERROR_NOT_OPEN;
   113     }
   114 
   115     myth_handle->content_size = myth_handle->file_transfer->filesize;
   116     
   117     myth_handle->buffer = g_byte_array_sized_new (MYTHTV_BUFFER_SIZE);
   118     myth_handle->buffer_remain = 0;
   119 
   120     *method_handle = (GnomeVFSMethodHandle *) myth_handle;
   121 
   122     return GNOME_VFS_OK;
   123 }
   124 
   125 static GnomeVFSResult
   126 do_read (GnomeVFSMethod *method,
   127          GnomeVFSMethodHandle *method_handle,
   128          gpointer buffer,
   129          GnomeVFSFileSize num_bytes,
   130          GnomeVFSFileSize *bytes_read,
   131          GnomeVFSContext *context)
   132 {
   133     MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   134     GnomeVFSFileSize bytes_to_read;
   135 
   136     *bytes_read = 0;
   137 
   138     if (myth_handle->bytes_read >= myth_handle->content_size)
   139         return GNOME_VFS_ERROR_EOF;
   140 
   141     // fixme: change this to min math function
   142     if (num_bytes > myth_handle->content_size - myth_handle->bytes_read)
   143 	    bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
   144     else
   145 	    bytes_to_read = num_bytes;
   146 
   147     /* Loop sending the Myth File Transfer request:
   148     * Retry whilst authentication fails and we supply it. */
   149   
   150     if ( bytes_to_read > myth_handle->buffer_remain ) {
   151         GByteArray *tmp_buffer = g_byte_array_new();
   152 
   153         gint len = gmyth_file_transfer_read (myth_handle->file_transfer,
   154               tmp_buffer, MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain, TRUE);
   155 
   156 	if (len < 0) {
   157             g_byte_array_free (tmp_buffer, TRUE);
   158             g_warning ("Fail to read bytes");
   159 	    return GNOME_VFS_ERROR_IO;
   160         } else if (len == 0) {
   161 	    g_byte_array_free (tmp_buffer, TRUE);
   162 	    g_warning ("End of file probably achieved");
   163 	    return GNOME_VFS_ERROR_EOF;
   164 	}
   165 
   166         myth_handle->buffer = g_byte_array_append (myth_handle->buffer,
   167     		tmp_buffer->data, len);
   168 
   169 		myth_handle->buffer_remain += len;
   170 
   171         g_byte_array_free (tmp_buffer, TRUE);
   172     	tmp_buffer = NULL;    	
   173     }
   174     
   175     bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read;
   176     /* gets the first buffer_size bytes from the byte array buffer variable */ 
   177 
   178     g_memmove (buffer, myth_handle->buffer->data, bytes_to_read);
   179 
   180     myth_handle->bytes_read += bytes_to_read;
   181     myth_handle->buffer_remain -= bytes_to_read;    
   182 
   183   	/* flushs the newly buffer got from byte array */
   184   	myth_handle->buffer = g_byte_array_remove_range (myth_handle->buffer, 0, bytes_to_read);
   185     *bytes_read = bytes_to_read;
   186   
   187     return GNOME_VFS_OK;
   188 }
   189 
   190 static GnomeVFSResult
   191 do_close (GnomeVFSMethod *method,
   192           GnomeVFSMethodHandle *method_handle,
   193           GnomeVFSContext *context)
   194 {
   195 
   196     MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
   197 
   198     if (myth_handle->file_transfer) {
   199 	    gmyth_file_transfer_close (myth_handle->file_transfer);
   200         g_object_unref (myth_handle->file_transfer);
   201     	myth_handle->file_transfer = NULL;
   202     }
   203     
   204     if (myth_handle->buffer) {
   205 		g_byte_array_free (myth_handle->buffer, TRUE);
   206 		myth_handle->buffer = NULL;
   207     }
   208 
   209     g_free (myth_handle);
   210 
   211     return GNOME_VFS_OK;
   212 }
   213 
   214 static GnomeVFSResult
   215 do_get_file_info (GnomeVFSMethod *method,
   216                   GnomeVFSURI *uri,
   217                   GnomeVFSFileInfo *file_info,
   218                   GnomeVFSFileInfoOptions options,
   219                   GnomeVFSContext *context)
   220 {
   221     GMythFileTransfer *file_transfer = NULL;
   222     GMythBackendInfo *backend_info = NULL;
   223 
   224     file_info->name = g_strdup (gnome_vfs_uri_get_path (uri));
   225     file_info->valid_fields = file_info->valid_fields
   226         | GNOME_VFS_FILE_INFO_FIELDS_TYPE
   227         | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
   228         | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
   229     file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
   230     // fixme: get from file extension?
   231     file_info->mime_type = g_strdup ("video/x-nuv");
   232     file_info->permissions =
   233         GNOME_VFS_PERM_USER_READ |
   234         GNOME_VFS_PERM_OTHER_READ |
   235         GNOME_VFS_PERM_GROUP_READ;
   236 
   237     backend_info = gmyth_backend_info_new_full (gnome_vfs_uri_get_host_name (uri),
   238             gnome_vfs_uri_get_user_name (uri),
   239             gnome_vfs_uri_get_password (uri),
   240             NULL,
   241             gnome_vfs_uri_get_host_port (uri));
   242 
   243     file_transfer = gmyth_file_transfer_new (backend_info);
   244     if (gmyth_file_transfer_open (file_transfer, gnome_vfs_uri_get_path (uri)) == TRUE) {
   245         file_info->size = gmyth_file_transfer_get_filesize (file_transfer);
   246         file_info->block_count = GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT;
   247         file_info->io_block_size = GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE;
   248     }
   249 
   250     g_object_unref (file_transfer);
   251     g_object_unref (backend_info);
   252     return GNOME_VFS_OK;
   253 }
   254 
   255 static gboolean
   256 do_is_local (GnomeVFSMethod *method,
   257              const GnomeVFSURI *uri)
   258 {
   259 	return FALSE;
   260 }
   261 
   262 static GnomeVFSMethod method = {
   263 	sizeof (GnomeVFSMethod),
   264 	do_open,
   265 	NULL,
   266 	do_close,
   267 	do_read,
   268 	NULL,
   269 	NULL,
   270 	NULL,
   271 	NULL,
   272 	NULL,
   273 	NULL,
   274 	NULL,
   275 	do_get_file_info,
   276 	NULL,
   277 	do_is_local,
   278 	NULL,
   279 	NULL,
   280 	NULL,
   281 	NULL,
   282 	NULL,
   283 	NULL,
   284 	NULL,
   285 	NULL,
   286 	NULL,
   287 	NULL,
   288 	NULL,
   289 	NULL,
   290 };
   291 
   292 
   293 GnomeVFSMethod *
   294 vfs_module_init (const char *method_name, const char *args)
   295 {
   296 	return &method;
   297 }
   298 
   299 void
   300 vfs_module_shutdown (GnomeVFSMethod *method)
   301 {
   302 }