diff src/plugins/plugin_xep_0096.py @ 1524:7b0fcefd52d4

plugin XEP-0047, XEP-0096: In-Band Bystream plugin cleaning: - some renaming, comments improvments, etc - progress callback is no more managed here, as it will be managed by application - no more file data is used, beside file_obj - a proper Deferred is used instead of success and error callbacks - more clean error sending method plugin XEP-0096 has been updated to handle changes. Its temporarily partially broken though
author Goffi <goffi@goffi.org>
date Fri, 25 Sep 2015 19:19:12 +0200
parents 3265a2639182
children 7cc29634b6ef
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0096.py	Fri Sep 25 19:19:12 2015 +0200
+++ b/src/plugins/plugin_xep_0096.py	Fri Sep 25 19:19:12 2015 +0200
@@ -26,6 +26,7 @@
 from twisted.words.protocols import jabber
 import os
 from twisted.internet import reactor
+from twisted.python import failure
 
 from wokkel import data_form
 
@@ -163,7 +164,9 @@
             elif stream_method == self.host.plugins["XEP-0047"].NAMESPACE:
                 file_obj = self._getFileObject(dest_path, can_range)
                 range_offset = file_obj.tell()
-                self.host.plugins["XEP-0047"].prepareToReceive(jid.JID(data['from']), sid, file_obj, int(data["size"]), self._transferSucceeded, self._transferFailed, profile)
+                d = self.host.plugins["XEP-0047"].createSession(file_obj, jid.JID(data['from']), sid, int(data["size"]), profile)
+                d.addCallback(self._transferSucceeded, sid, file_obj, self.host.plugins["XEP-0047"].NAMESPACE, profile)
+                d.addErrback(self._transferFailed, sid, file_obj, self.host.plugins["XEP-0047"].NAMESPACE, profile)
             else:
                 log.error(_("Unknown stream method, this should not happen at this stage, cancelling transfer"))
                 del(client._xep_0096_waiting_for_approval[sid])
@@ -184,7 +187,10 @@
             self.host.plugins["XEP-0095"].sendRejectedError(data["id"], data['from'], profile=profile)
             del(client._xep_0096_waiting_for_approval[sid])
 
-    def _transferSucceeded(self, sid, file_obj, stream_method, profile):
+    def _transferSucceeded(self, dummy, sid, file_obj, stream_method, profile):
+        self.transferSucceeded(sid, file_obj, stream_method, profile)
+
+    def transferSucceeded(self, dummy, sid, file_obj, stream_method, profile):
         """Called by the stream method when transfer successfuly finished
         @param id: stream id"""
         client = self.host.getClient(profile)
@@ -193,15 +199,19 @@
         del(client._xep_0096_waiting_for_approval[sid])
 
     def _transferFailed(self, sid, file_obj, stream_method, reason, profile):
+        self.transferFailed(failure.Failure(Exception(reason)), sid, file_obj, stream_method, profile)
+
+    def transferFailed(self, failure, sid, file_obj, stream_method, profile):
         """Called when something went wrong with the transfer
+
         @param id: stream id
-        @param reason: can be TIMEOUT, IO_ERROR, PROTOCOL_ERROR"""
+        """
         client = self.host.getClient(profile)
         data, timeout, stream_method, failed_methods = client._xep_0096_waiting_for_approval[sid]
         log.warning(_(u'Transfer %(id)s failed with stream method %(s_method)s: %(reason)s') % {
             'id': sid,
             's_method': stream_method,
-            'reason': reason})
+            'reason': unicode(failure)})
         filepath = file_obj.name
         file_obj.close()
         os.remove(filepath)
@@ -250,12 +260,14 @@
             file_obj = open(filepath, 'r')
             if range_offset:
                 file_obj.seek(range_offset)
-            self.host.plugins["XEP-0065"].startStream(file_obj, jid.JID(IQ['from']), sid, range_length, self.sendSuccessCb, self.sendFailureCb, size, profile)
+            self.host.plugins["XEP-0065"].startStream(file_obj, jid.JID(IQ['from']), sid, range_length, self._sendSuccessCb, self._sendFailureCb, size, profile)
         elif stream_method == self.host.plugins["XEP-0047"].NAMESPACE:
             file_obj = open(filepath, 'r')
             if range_offset:
                 file_obj.seek(range_offset)
-            self.host.plugins["XEP-0047"].startStream(file_obj, jid.JID(IQ['from']), sid, range_length, self.sendSuccessCb, self.sendFailureCb, size, profile)
+            d = self.host.plugins["XEP-0047"].startStream(file_obj, jid.JID(IQ['from']), sid, profile=profile)
+            d.addCallback(self.sendSuccessCb, sid, file_obj, self.host.plugins["XEP-0047"].NAMESPACE, profile)
+            d.addErrback(self.sendFailureCb, sid, file_obj, self.host.plugins["XEP-0047"].NAMESPACE, profile)
         else:
             log.warning(_("Invalid stream method received"))
 
@@ -288,11 +300,17 @@
         offer.addCallback(self.fileCb, filepath, sid, size, profile)
         return sid
 
-    def sendSuccessCb(self, sid, file_obj, stream_method, profile):
+    def _sendSuccessCb(self, sid, file_obj, stream_method, profile):
+        self.sendSuccessCb(sid, file_obj, stream_method, profile)
+
+    def sendSuccessCb(self, dummy, sid, file_obj, stream_method, profile):
         log.info(_(u'Transfer %(sid)s successfuly finished [%(profile)s]')
              % {"sid": sid, "profile": profile})
         file_obj.close()
 
-    def sendFailureCb(self, sid, file_obj, stream_method, reason, profile):
+    def _sendFailureCb(self, sid, file_obj, stream_method, reason, profile):
+        self.sendFailureCb(failure.Failure(Exception(reason)), sid, file_obj, stream_method, profile)
+
+    def sendFailureCb(self, failure, sid, file_obj, stream_method, profile):
         file_obj.close()
-        log.warning(_(u'Transfer %(id)s failed with stream method %(s_method)s: %(reason)s [%(profile)s]') % {'id': sid, "s_method": stream_method, 'reason': reason, 'profile': profile})
+        log.warning(_(u'Transfer %(id)s failed with stream method %(s_method)s: %(reason)s [%(profile)s]') % {'id': sid, "s_method": stream_method, 'reason': unicode(failure), 'profile': profile})