summaryrefslogtreecommitdiff
path: root/sleekxmpp/plugins/xep_0047/stream.py
diff options
context:
space:
mode:
Diffstat (limited to 'sleekxmpp/plugins/xep_0047/stream.py')
-rw-r--r--sleekxmpp/plugins/xep_0047/stream.py67
1 files changed, 39 insertions, 28 deletions
diff --git a/sleekxmpp/plugins/xep_0047/stream.py b/sleekxmpp/plugins/xep_0047/stream.py
index 49f56f36..9651edf8 100644
--- a/sleekxmpp/plugins/xep_0047/stream.py
+++ b/sleekxmpp/plugins/xep_0047/stream.py
@@ -1,11 +1,9 @@
import socket
import threading
import logging
-try:
- import queue
-except ImportError:
- import Queue as queue
+from sleekxmpp.stanza import Iq
+from sleekxmpp.util import Queue
from sleekxmpp.exceptions import XMPPError
@@ -14,14 +12,17 @@ log = logging.getLogger(__name__)
class IBBytestream(object):
- def __init__(self, xmpp, sid, block_size, to, ifrom, window_size=1):
+ def __init__(self, xmpp, sid, block_size, jid, peer, window_size=1, use_messages=False):
self.xmpp = xmpp
self.sid = sid
self.block_size = block_size
self.window_size = window_size
+ self.use_messages = use_messages
- self.receiver = to
- self.sender = ifrom
+ if jid is None:
+ jid = xmpp.boundjid
+ self.self_jid = jid
+ self.peer_jid = peer
self.send_seq = -1
self.recv_seq = -1
@@ -33,7 +34,7 @@ class IBBytestream(object):
self.stream_in_closed = threading.Event()
self.stream_out_closed = threading.Event()
- self.recv_queue = queue.Queue()
+ self.recv_queue = Queue()
self.send_window = threading.BoundedSemaphore(value=self.window_size)
self.window_ids = set()
@@ -49,16 +50,27 @@ class IBBytestream(object):
with self._send_seq_lock:
self.send_seq = (self.send_seq + 1) % 65535
seq = self.send_seq
- iq = self.xmpp.Iq()
- iq['type'] = 'set'
- iq['to'] = self.receiver
- iq['from'] = self.sender
- iq['ibb_data']['sid'] = self.sid
- iq['ibb_data']['seq'] = seq
- iq['ibb_data']['data'] = data
- self.window_empty.clear()
- self.window_ids.add(iq['id'])
- iq.send(block=False, callback=self._recv_ack)
+ if self.use_messages:
+ msg = self.xmpp.Message()
+ msg['to'] = self.peer_jid
+ msg['from'] = self.self_jid
+ msg['id'] = self.xmpp.new_id()
+ msg['ibb_data']['sid'] = self.sid
+ msg['ibb_data']['seq'] = seq
+ msg['ibb_data']['data'] = data
+ msg.send()
+ self.send_window.release()
+ else:
+ iq = self.xmpp.Iq()
+ iq['type'] = 'set'
+ iq['to'] = self.peer_jid
+ iq['from'] = self.self_jid
+ iq['ibb_data']['sid'] = self.sid
+ iq['ibb_data']['seq'] = seq
+ iq['ibb_data']['data'] = data
+ self.window_empty.clear()
+ self.window_ids.add(iq['id'])
+ iq.send(block=False, callback=self._recv_ack)
return len(data)
def sendall(self, data):
@@ -74,23 +86,25 @@ class IBBytestream(object):
if iq['type'] == 'error':
self.close()
- def _recv_data(self, iq):
+ def _recv_data(self, stanza):
with self._recv_seq_lock:
- new_seq = iq['ibb_data']['seq']
+ new_seq = stanza['ibb_data']['seq']
if new_seq != (self.recv_seq + 1) % 65535:
self.close()
raise XMPPError('unexpected-request')
self.recv_seq = new_seq
- data = iq['ibb_data']['data']
+ data = stanza['ibb_data']['data']
if len(data) > self.block_size:
self.close()
raise XMPPError('not-acceptable')
self.recv_queue.put(data)
self.xmpp.event('ibb_stream_data', {'stream': self, 'data': data})
- iq.reply()
- iq.send()
+
+ if isinstance(stanza, Iq):
+ stanza.reply()
+ stanza.send()
def recv(self, *args, **kwargs):
return self.read(block=True)
@@ -109,8 +123,8 @@ class IBBytestream(object):
def close(self):
iq = self.xmpp.Iq()
iq['type'] = 'set'
- iq['to'] = self.receiver
- iq['from'] = self.sender
+ iq['to'] = self.peer_jid
+ iq['from'] = self.self_jid
iq['ibb_close']['sid'] = self.sid
self.stream_out_closed.set()
iq.send(block=False,
@@ -120,9 +134,6 @@ class IBBytestream(object):
def _closed(self, iq):
self.stream_in_closed.set()
self.stream_out_closed.set()
- while not self.window_empty.is_set():
- log.info('waiting for send window to empty')
- self.window_empty.wait(timeout=1)
iq.reply()
iq.send()
self.xmpp.event('ibb_stream_end', self)