[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.
2 * @author Hallyson Melo <hallyson.melo@indt.org.br>
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.
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.
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
27 #include <libgnomevfs/gnome-vfs-module.h>
28 #include <libgnomevfs/gnome-vfs-utils.h>
30 #include <gmyth_file_transfer.h>
31 #include <gmyth_backendinfo.h>
32 #include <gmyth_util.h>
34 #define GST_MYTHTV_ID_NUM 1
35 #define MYTHTV_VERSION_DEFAULT 30
37 #define MYTHTV_BUFFER_SIZE 1024*64
39 static GnomeVFSResult do_read (GnomeVFSMethod *method,
40 GnomeVFSMethodHandle *method_handle,
42 GnomeVFSFileSize num_bytes,
43 GnomeVFSFileSize *bytes_read,
44 GnomeVFSContext *context);
47 GMythFileTransfer *file_transfer;
60 do_open (GnomeVFSMethod *method,
61 GnomeVFSMethodHandle **method_handle,
63 GnomeVFSOpenMode mode,
64 GnomeVFSContext *context)
66 MythtvHandle *myth_handle;
67 GMythBackendInfo *backend_info;
70 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
71 _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
73 myth_handle = g_new0 (MythtvHandle, 1);
75 if (mode & GNOME_VFS_OPEN_WRITE) {
76 return GNOME_VFS_ERROR_NOT_PERMITTED;
79 if (gnome_vfs_uri_get_host_name (uri) == NULL) {
80 return GNOME_VFS_ERROR_INVALID_HOST_NAME;
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;
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),
93 gnome_vfs_uri_get_host_port (uri));
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;
102 /* Connect to the backend */
103 myth_handle->file_transfer = gmyth_file_transfer_new (backend_info);
104 g_object_unref (backend_info);
106 g_return_val_if_fail (myth_handle->file_transfer != NULL, GNOME_VFS_ERROR_NOT_OPEN);
108 /* sets the Playback monitor connection */
109 ret = gmyth_file_transfer_open (myth_handle->file_transfer, gnome_vfs_uri_get_path (uri));
111 g_warning ("Mythtv FileTransfer open error\n");
112 return GNOME_VFS_ERROR_NOT_OPEN;
115 myth_handle->content_size = myth_handle->file_transfer->filesize;
117 myth_handle->buffer = g_byte_array_sized_new (MYTHTV_BUFFER_SIZE);
118 myth_handle->buffer_remain = 0;
120 *method_handle = (GnomeVFSMethodHandle *) myth_handle;
125 static GnomeVFSResult
126 do_read (GnomeVFSMethod *method,
127 GnomeVFSMethodHandle *method_handle,
129 GnomeVFSFileSize num_bytes,
130 GnomeVFSFileSize *bytes_read,
131 GnomeVFSContext *context)
133 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
134 GnomeVFSFileSize bytes_to_read;
138 if (myth_handle->bytes_read >= myth_handle->content_size)
139 return GNOME_VFS_ERROR_EOF;
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;
145 bytes_to_read = num_bytes;
147 /* Loop sending the Myth File Transfer request:
148 * Retry whilst authentication fails and we supply it. */
150 if ( bytes_to_read > myth_handle->buffer_remain ) {
151 GByteArray *tmp_buffer = g_byte_array_new();
153 gint len = gmyth_file_transfer_read (myth_handle->file_transfer,
154 tmp_buffer, MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain, TRUE);
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;
166 myth_handle->buffer = g_byte_array_append (myth_handle->buffer,
167 tmp_buffer->data, len);
169 myth_handle->buffer_remain += len;
171 g_byte_array_free (tmp_buffer, TRUE);
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 */
178 g_memmove (buffer, myth_handle->buffer->data, bytes_to_read);
180 myth_handle->bytes_read += bytes_to_read;
181 myth_handle->buffer_remain -= bytes_to_read;
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;
190 static GnomeVFSResult
191 do_close (GnomeVFSMethod *method,
192 GnomeVFSMethodHandle *method_handle,
193 GnomeVFSContext *context)
196 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
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;
204 if (myth_handle->buffer) {
205 g_byte_array_free (myth_handle->buffer, TRUE);
206 myth_handle->buffer = NULL;
209 g_free (myth_handle);
214 static GnomeVFSResult
215 do_get_file_info (GnomeVFSMethod *method,
217 GnomeVFSFileInfo *file_info,
218 GnomeVFSFileInfoOptions options,
219 GnomeVFSContext *context)
221 GMythFileTransfer *file_transfer = NULL;
222 GMythBackendInfo *backend_info = NULL;
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;
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),
241 gnome_vfs_uri_get_host_port (uri));
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;
250 g_object_unref (file_transfer);
251 g_object_unref (backend_info);
256 do_is_local (GnomeVFSMethod *method,
257 const GnomeVFSURI *uri)
262 static GnomeVFSMethod method = {
263 sizeof (GnomeVFSMethod),
294 vfs_module_init (const char *method_name, const char *args)
300 vfs_module_shutdown (GnomeVFSMethod *method)