Mercurial > libervia-backend
comparison src/plugins/plugin_xep_0047.py @ 1578:7fef6cdf5953
plugin XEP-0047: some cleaning
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 11 Nov 2015 18:21:20 +0100 |
parents | 6a8dd91476f0 |
children | d46aae87c03a |
comparison
equal
deleted
inserted
replaced
1577:d04d7402b8e9 | 1578:7fef6cdf5953 |
---|---|
20 from sat.core.i18n import _ | 20 from sat.core.i18n import _ |
21 from sat.core.log import getLogger | 21 from sat.core.log import getLogger |
22 log = getLogger(__name__) | 22 log = getLogger(__name__) |
23 from sat.core.constants import Const as C | 23 from sat.core.constants import Const as C |
24 from sat.core import exceptions | 24 from sat.core import exceptions |
25 from twisted.words.protocols.jabber import client as jabber_client | |
26 from twisted.words.protocols.jabber import jid | 25 from twisted.words.protocols.jabber import jid |
27 from twisted.words.protocols.jabber import xmlstream | 26 from twisted.words.protocols.jabber import xmlstream |
28 from twisted.words.protocols.jabber import error | 27 from twisted.words.protocols.jabber import error |
29 from twisted.internet import reactor | 28 from twisted.internet import reactor |
30 from twisted.internet import defer | 29 from twisted.internet import defer |
293 if block_size is None: | 292 if block_size is None: |
294 block_size = XEP_0047.BLOCK_SIZE | 293 block_size = XEP_0047.BLOCK_SIZE |
295 assert block_size <= 65535 | 294 assert block_size <= 65535 |
296 session_data["block_size"] = block_size | 295 session_data["block_size"] = block_size |
297 | 296 |
298 iq_elt = jabber_client.IQ(client.xmlstream, 'set') # FIXME: use client.IQ here | 297 iq_elt = client.IQ() |
299 iq_elt['from'] = client.jid.full() | |
300 iq_elt['to'] = to_jid.full() | 298 iq_elt['to'] = to_jid.full() |
301 open_elt = iq_elt.addElement('open', NS_IBB) | 299 open_elt = iq_elt.addElement((NS_IBB, 'open')) |
302 open_elt['block-size'] = str(block_size) | 300 open_elt['block-size'] = str(block_size) |
303 open_elt['sid'] = sid | 301 open_elt['sid'] = sid |
304 open_elt['stanza'] = 'iq' # TODO: manage <message> stanza ? | 302 open_elt['stanza'] = 'iq' # TODO: manage <message> stanza ? |
305 iq_elt.addCallback(self._IQDataStream, session_data, client) | 303 args = [session_data, client] |
306 iq_elt.send() | 304 d = iq_elt.send() |
305 d.addCallbacks(self._IQDataStreamCb, self._IQDataStreamEb, args, None, args) | |
307 return session_data[DEFER_KEY] | 306 return session_data[DEFER_KEY] |
308 | 307 |
309 def _IQDataStream(self, session_data, client, iq_elt): | 308 def _IQDataStreamCb(self, iq_elt, session_data, client): |
310 """Called during the whole data streaming | 309 """Called during the whole data streaming |
311 | 310 |
311 @param iq_elt(domish.Element): iq result | |
312 @param session_data(dict): data of this streaming session | 312 @param session_data(dict): data of this streaming session |
313 @param client: %(doc_client)s | 313 @param client: %(doc_client)s |
314 @param iq_elt(domish.Element): iq result | 314 """ |
315 """ | |
316 if iq_elt['type'] == 'error': | |
317 log.warning(_(u"IBB transfer failed: {}").format(iq_elt)) | |
318 self.terminateStream(session_data, client, "IQ_ERROR") | |
319 return | |
320 | |
321 session_data["timer"].reset(TIMEOUT) | 315 session_data["timer"].reset(TIMEOUT) |
322 | 316 |
323 buffer_ = session_data["file_obj"].read(session_data["block_size"]) | 317 buffer_ = session_data["file_obj"].read(session_data["block_size"]) |
324 if buffer_: | 318 if buffer_: |
325 next_iq_elt = jabber_client.IQ(client.xmlstream, 'set') # FIXME: use client.IQ here | 319 next_iq_elt = client.IQ() |
326 next_iq_elt['to'] = session_data["to"].full() | 320 next_iq_elt['to'] = session_data["to"].full() |
327 data_elt = next_iq_elt.addElement('data', NS_IBB) | 321 data_elt = next_iq_elt.addElement((NS_IBB, 'data')) |
328 seq = session_data['seq'] = (session_data['seq'] + 1) % 65535 | 322 seq = session_data['seq'] = (session_data['seq'] + 1) % 65535 |
329 data_elt['seq'] = unicode(seq) | 323 data_elt['seq'] = unicode(seq) |
330 data_elt['sid'] = session_data['id'] | 324 data_elt['sid'] = session_data['id'] |
331 data_elt.addContent(base64.b64encode(buffer_)) | 325 data_elt.addContent(base64.b64encode(buffer_)) |
332 next_iq_elt.addCallback(self._IQDataStream, session_data, client) | 326 args = [session_data, client] |
333 next_iq_elt.send() | 327 d = next_iq_elt.send() |
328 d.addCallbacks(self._IQDataStreamCb, self._IQDataStreamEb, args, None, args) | |
334 else: | 329 else: |
335 self.terminateStream(session_data, client) | 330 self.terminateStream(session_data, client) |
336 | 331 |
332 def _IQDataStreamEb(self, failure, session_data, client): | |
333 if failure.check(error.StanzaError): | |
334 log.warning(u"IBB transfer failed: {}".format(failure.condition)) | |
335 else: | |
336 log.error(u"IBB transfer failed: {}".format(failure.condition)) | |
337 self.terminateStream(session_data, client, "IQ_ERROR") | |
338 | |
337 def terminateStream(self, session_data, client, failure_reason=None): | 339 def terminateStream(self, session_data, client, failure_reason=None): |
338 """Terminate the stream session | 340 """Terminate the stream session |
339 | 341 |
340 @param session_data(dict): data of this streaming session | 342 @param session_data(dict): data of this streaming session |
341 @param client: %(doc_client)s | 343 @param client: %(doc_client)s |
342 @param failure_reason(unicode, None): reason of the failure, or None if steam was successful | 344 @param failure_reason(unicode, None): reason of the failure, or None if steam was successful |
343 """ | 345 """ |
344 iq_elt = jabber_client.IQ(client.xmlstream, 'set') | 346 iq_elt = client.IQ() |
345 iq_elt['to'] = session_data["to"].full() | 347 iq_elt['to'] = session_data["to"].full() |
346 close_elt = iq_elt.addElement('close', NS_IBB) | 348 close_elt = iq_elt.addElement((NS_IBB, 'close')) |
347 close_elt['sid'] = session_data['id'] | 349 close_elt['sid'] = session_data['id'] |
348 iq_elt.send() | 350 iq_elt.send() |
349 self._killSession(session_data['id'], client, failure_reason) | 351 self._killSession(session_data['id'], client, failure_reason) |
350 | 352 |
351 | 353 |