comparison src/plugins/plugin_xep_0047.py @ 1558:6a8dd91476f0

plugin XEP-0047: minor improvements
author Goffi <goffi@goffi.org>
date Mon, 02 Nov 2015 22:02:41 +0100
parents 7b0fcefd52d4
children 7fef6cdf5953
comparison
equal deleted inserted replaced
1557:22f0307864b4 1558:6a8dd91476f0
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
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 import exceptions 24 from sat.core import exceptions
24 from twisted.words.protocols.jabber import client as jabber_client 25 from twisted.words.protocols.jabber import client as jabber_client
25 from twisted.words.protocols.jabber import jid 26 from twisted.words.protocols.jabber import jid
26 from twisted.words.protocols.jabber import xmlstream 27 from twisted.words.protocols.jabber import xmlstream
27 from twisted.words.protocols.jabber import error 28 from twisted.words.protocols.jabber import error
275 log.warning(u"Error while managing in-band bytestream session, cancelling: {}".format(error_condition)) 276 log.warning(u"Error while managing in-band bytestream session, cancelling: {}".format(error_condition))
276 if sid is not None: 277 if sid is not None:
277 self._killSession(sid, client, error_condition) 278 self._killSession(sid, client, error_condition)
278 client.xmlstream.send(iq_elt) 279 client.xmlstream.send(iq_elt)
279 280
280 def startStream(self, file_obj, to_jid, sid, block_size=None, profile=None): 281 def startStream(self, file_obj, to_jid, sid, block_size=None, profile=C.PROF_KEY_NONE):
281 """Launch the stream workflow 282 """Launch the stream workflow
282 283
283 @param file_obj(file): file_obj to send 284 @param file_obj(file): file_obj to send
284 @param to_jid(jid.JID): JID of the recipient 285 @param to_jid(jid.JID): JID of the recipient
285 @param sid(unicode): Stream session id 286 @param sid(unicode): Stream session id
286 @param block_size(int, None): size of the block (or None for default) 287 @param block_size(int, None): size of the block (or None for default)
287 @param profile: %(doc_profile)s 288 @param profile: %(doc_profile)s
288 """ 289 """
289 session_data = self._createSession(file_obj, to_jid, sid, profile) 290 session_data = self._createSession(file_obj, to_jid, sid, profile)
290 session_defer = session_data[DEFER_KEY]
291 client = self.host.getClient(profile) 291 client = self.host.getClient(profile)
292 292
293 if block_size is None: 293 if block_size is None:
294 block_size = XEP_0047.BLOCK_SIZE 294 block_size = XEP_0047.BLOCK_SIZE
295 assert block_size <= 65535 295 assert block_size <= 65535
296 session_data["block_size"] = block_size 296 session_data["block_size"] = block_size
297 297
298 iq_elt = jabber_client.IQ(client.xmlstream, 'set') 298 iq_elt = jabber_client.IQ(client.xmlstream, 'set') # FIXME: use client.IQ here
299 iq_elt['from'] = client.jid.full() 299 iq_elt['from'] = client.jid.full()
300 iq_elt['to'] = to_jid.full() 300 iq_elt['to'] = to_jid.full()
301 open_elt = iq_elt.addElement('open', NS_IBB) 301 open_elt = iq_elt.addElement('open', NS_IBB)
302 open_elt['block-size'] = str(block_size) 302 open_elt['block-size'] = str(block_size)
303 open_elt['sid'] = sid 303 open_elt['sid'] = sid
304 open_elt['stanza'] = 'iq' # TODO: manage <message> stanza ? 304 open_elt['stanza'] = 'iq' # TODO: manage <message> stanza ?
305 iq_elt.addCallback(self._IQDataStream, session_data, client) 305 iq_elt.addCallback(self._IQDataStream, session_data, client)
306 iq_elt.send() 306 iq_elt.send()
307 return session_defer 307 return session_data[DEFER_KEY]
308 308
309 def _IQDataStream(self, session_data, client, iq_elt): 309 def _IQDataStream(self, session_data, client, iq_elt):
310 """Called during the whole data streaming 310 """Called during the whole data streaming
311 311
312 @param session_data(dict): data of this streaming session 312 @param session_data(dict): data of this streaming session
320 320
321 session_data["timer"].reset(TIMEOUT) 321 session_data["timer"].reset(TIMEOUT)
322 322
323 buffer_ = session_data["file_obj"].read(session_data["block_size"]) 323 buffer_ = session_data["file_obj"].read(session_data["block_size"])
324 if buffer_: 324 if buffer_:
325 next_iq_elt = jabber_client.IQ(client.xmlstream, 'set') 325 next_iq_elt = jabber_client.IQ(client.xmlstream, 'set') # FIXME: use client.IQ here
326 next_iq_elt['to'] = session_data["to"].full() 326 next_iq_elt['to'] = session_data["to"].full()
327 data_elt = next_iq_elt.addElement('data', NS_IBB) 327 data_elt = next_iq_elt.addElement('data', NS_IBB)
328 seq = session_data['seq'] = (session_data['seq'] + 1) % 65535 328 seq = session_data['seq'] = (session_data['seq'] + 1) % 65535
329 data_elt['seq'] = unicode(seq) 329 data_elt['seq'] = unicode(seq)
330 data_elt['sid'] = session_data['id'] 330 data_elt['sid'] = session_data['id']