comparison src/plugins/plugin_xep_0065.py @ 1581:8cc7d83141a4

plugin XEP-0065: chunk size optimization: 64Kio is used except for proxy were it is set to 4Kio, to avoid wild disconnection by Prosody's proxy
author Goffi <goffi@goffi.org>
date Wed, 11 Nov 2015 22:28:48 +0100
parents d04d7402b8e9
children d46aae87c03a
comparison
equal deleted inserted replaced
1580:641cfd2faefe 1581:8cc7d83141a4
272 query_elt['sid'] = sid 272 query_elt['sid'] = sid
273 query_elt.addElement('activate', content=peer_jid.full()) 273 query_elt.addElement('activate', content=peer_jid.full())
274 return iq_elt.send() 274 return iq_elt.send()
275 275
276 def startTransfer(self, session_hash=None): 276 def startTransfer(self, session_hash=None):
277 self.factory.startTransfer(session_hash) 277 if self.type == XEP_0065.TYPE_PROXY:
278 chunk_size = 4096 # Prosody's proxy reject bigger chunks by default
279 else:
280 chunk_size = None
281 self.factory.startTransfer(session_hash, chunk_size=chunk_size)
278 282
279 283
280 def getSessionHash(from_jid, to_jid, sid): 284 def getSessionHash(from_jid, to_jid, sid):
281 """Calculate SHA1 Hash according to XEP-0065 §5.3.2 285 """Calculate SHA1 Hash according to XEP-0065 §5.3.2
282 286
287 """ 291 """
288 return hashlib.sha1((sid + from_jid.full() + to_jid.full()).encode('utf-8')).hexdigest() 292 return hashlib.sha1((sid + from_jid.full() + to_jid.full()).encode('utf-8')).hexdigest()
289 293
290 294
291 class SOCKSv5(protocol.Protocol, FileSender): 295 class SOCKSv5(protocol.Protocol, FileSender):
292 CHUNK_SIZE = 4096 296 CHUNK_SIZE = 2**16
293 297
294 def __init__(self, session_hash=None): 298 def __init__(self, session_hash=None):
295 """ 299 """
296 @param session_hash(str): hash of the session 300 @param session_hash(str): hash of the session
297 must only be used in client mode 301 must only be used in client mode
482 .format(host=self.transport.getPeer().host)) 486 .format(host=self.transport.getPeer().host))
483 return 487 return
484 self._session_hash = addr 488 self._session_hash = addr
485 self.connectCompleted(addr, 0) 489 self.connectCompleted(addr, 0)
486 490
487 def startTransfer(self): 491 def startTransfer(self, chunk_size):
488 """Callback called when the result iq is received""" 492 """Callback called when the result iq is received
493
494 @param chunk_size(None, int): size of the buffer, or None for default
495 """
496 if chunk_size is not None:
497 self.CHUNK_SIZE = chunk_size
489 log.debug(u"Starting file transfer") 498 log.debug(u"Starting file transfer")
490 d = self.beginFileTransfer(self.file_obj, self.transport) 499 d = self.beginFileTransfer(self.file_obj, self.transport)
491 d.addCallback(self.fileTransfered) 500 d.addCallback(self.fileTransfered)
492 501
493 def fileTransfered(self, d): 502 def fileTransfered(self, d):
552 self.parent = parent 561 self.parent = parent
553 562
554 def getSession(self, session_hash): 563 def getSession(self, session_hash):
555 return self.parent.getSession(session_hash, None) 564 return self.parent.getSession(session_hash, None)
556 565
557 def startTransfer(self, session_hash): 566 def startTransfer(self, session_hash, chunk_size=None):
558 session = self.getSession(session_hash) 567 session = self.getSession(session_hash)
559 try: 568 try:
560 protocol = session['protocols'][0] 569 protocol = session['protocols'][0]
561 except (KeyError, IndexError): 570 except (KeyError, IndexError):
562 log.error(u"Can't start file transfer, can't find protocol") 571 log.error(u"Can't start file transfer, can't find protocol")
563 else: 572 else:
564 protocol.startTransfer() 573 protocol.startTransfer(chunk_size)
565 574
566 def addToSession(self, session_hash, protocol): 575 def addToSession(self, session_hash, protocol):
567 """Check is session_hash is valid, and associate protocol with it 576 """Check is session_hash is valid, and associate protocol with it
568 577
569 the session will be associated to the corresponding candidate 578 the session will be associated to the corresponding candidate
629 self._discarded = True 638 self._discarded = True
630 639
631 def getSession(self): 640 def getSession(self):
632 return self.session 641 return self.session
633 642
634 def startTransfer(self, dummy=None): 643 def startTransfer(self, dummy=None, chunk_size=None):
635 self._protocol_instance.startTransfer() 644 self._protocol_instance.startTransfer(chunk_size)
636 645
637 def clientConnectionFailed(self, connector, reason): 646 def clientConnectionFailed(self, connector, reason):
638 log.debug(u"Connection failed") 647 log.debug(u"Connection failed")
639 self.connection.errback(reason) 648 self.connection.errback(reason)
640 649