From d7ea8570b1da8f81aebf5df19e26bab003884d1b Mon Sep 17 00:00:00 2001 From: Manuel Kaufmann Date: Wed, 05 Dec 2012 21:00:32 +0000 Subject: Fix the port of the filetransfer, SL #4242 - Fix some issues related to the port to Gio (introspected): argument numbers and order and method names. - Use 'Gio.InputStream.read_bytes_async'[1] and 'Gio.OutputStream.write_bytes_async' instead of '.read_async'[2] and 'write_async' to transfer the data. This is done because there is no way to pass the correct buffer argument to those functions from Python. So, Gio provides those helper functions. [1] http://developer.gnome.org/gio/stable/GInputStream.html#g-input-stream-read-bytes-async [2] http://developer.gnome.org/gio/stable/GInputStream.html#g-input-stream-read-async Signed-off-by: Manuel Kaufmann Acked-by: Simon Schampijer --- diff --git a/src/jarabe/model/filetransfer.py b/src/jarabe/model/filetransfer.py index 820df74..f121ec2 100644 --- a/src/jarabe/model/filetransfer.py +++ b/src/jarabe/model/filetransfer.py @@ -20,6 +20,7 @@ import socket from gi.repository import GObject from gi.repository import Gio +from gi.repository import GLib import dbus from telepathy.interfaces import CONNECTION_INTERFACE_REQUESTS, CHANNEL from telepathy.constants import CONNECTION_HANDLE_TYPE_CONTACT, \ @@ -73,30 +74,37 @@ class StreamSplicer(GObject.GObject): self._pending_buffers = [] def start(self): - self._input_stream.read_async(self._CHUNK_SIZE, self.__read_async_cb, - GObject.PRIORITY_LOW) - - def __read_async_cb(self, input_stream, result): - data = input_stream.read_finish(result) - - if not data: - logging.debug('closing input stream') - self._input_stream.close() + self._input_stream.read_bytes_async( + self._CHUNK_SIZE, GLib.PRIORITY_LOW, + None, self.__read_async_cb, None) + + def __read_async_cb(self, input_stream, result, user_data): + data = input_stream.read_bytes_finish(result) + + if data is None: + # TODO: an error occured. Report something + logging.error('An error occured in the file transfer.') + elif data.get_size() == 0: + # We read the file completely + logging.debug('Closing input stream. Reading finished.') + self._input_stream.close(None) else: + logging.debug('Data received (bytes): %s', data.get_size()) self._pending_buffers.append(data) - self._input_stream.read_async(self._CHUNK_SIZE, - self.__read_async_cb, - GObject.PRIORITY_LOW) + self._input_stream.read_bytes_async( + self._CHUNK_SIZE, GLib.PRIORITY_LOW, + None, self.__read_async_cb, None) self._write_next_buffer() def __write_async_cb(self, output_stream, result, user_data): - count_ = output_stream.write_finish(result) + size = output_stream.write_bytes_finish(result) + logging.debug('Size written (bytes): %s', size) if not self._pending_buffers and \ not self._output_stream.has_pending() and \ not self._input_stream.has_pending(): - logging.debug('closing output stream') - output_stream.close() + logging.debug('Closing output stream. Writing finished.') + output_stream.close(None) self.emit('finished') else: self._write_next_buffer() @@ -104,11 +112,9 @@ class StreamSplicer(GObject.GObject): def _write_next_buffer(self): if self._pending_buffers and not self._output_stream.has_pending(): data = self._pending_buffers.pop(0) - # TODO: we pass the buffer as user_data because of - # http://bugzilla.gnome.org/show_bug.cgi?id=564102 - self._output_stream.write_async(data, self.__write_async_cb, - GObject.PRIORITY_LOW, - user_data=data) + self._output_stream.write_bytes_async( + data, GLib.PRIORITY_LOW, None, + self.__write_async_cb, None) class BaseFileTransfer(GObject.GObject): @@ -215,11 +221,12 @@ class IncomingFileTransfer(BaseFileTransfer): # close the fd when it goes out of scope self._socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self._socket.connect(self._socket_address) - input_stream = Gio.unix.InputStream(self._socket.fileno(), True) + input_stream = Gio.UnixInputStream.new(self._socket.fileno(), True) - destination_file = Gio.File(self.destination_path) + destination_file = Gio.File.new_for_path(self.destination_path) if self.initial_offset == 0: - output_stream = destination_file.create() + output_stream = destination_file.create( + Gio.FileCreateFlags.PRIVATE, None) else: output_stream = destination_file.append_to() @@ -277,10 +284,11 @@ class OutgoingFileTransfer(BaseFileTransfer): # closes the fd when it goes out of scope self._socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self._socket.connect(self._socket_address) - output_stream = Gio.unix.OutputStream(self._socket.fileno(), True) + output_stream = Gio.UnixOutputStream.new( + self._socket.fileno(), True) logging.debug('opening %s for reading', self._file_name) - input_stream = Gio.File(self._file_name).read() + input_stream = Gio.File.new_for_path(self._file_name).read(None) if self.initial_offset > 0: input_stream.skip(self.initial_offset) @@ -356,9 +364,12 @@ def file_transfer_available(): if __name__ == '__main__': import tempfile - test_file_name = '/home/tomeu/isos/Soas2-200904031934.iso' - test_input_stream = Gio.File(test_file_name).read() - test_output_stream = Gio.File(tempfile.mkstemp()[1]).append_to() + test_file_name = '/home/humitos/test.py' + test_temp_file = tempfile.mkstemp()[1] + print test_temp_file + test_input_stream = Gio.File.new_for_path(test_file_name).read(None) + test_output_stream = Gio.File.new_for_path(test_temp_file)\ + .append_to(Gio.FileCreateFlags.PRIVATE, None) # TODO: Use splice_async when it gets implemented splicer = StreamSplicer(test_input_stream, test_output_stream) -- cgit v0.9.1