annotate src/plugins/plugin_xep_0261.py @ 1547:0632d96f08ad

plugin XEP-0085: fixed bad use of threads resulting in delay and crash when stopping the backend.
author Goffi <goffi@goffi.org>
date Mon, 02 Nov 2015 22:02:41 +0100
parents bfef1934a8f3
children cbfbe028d099
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1527
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin for Jingle (XEP-0261)
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (goffi@goffi.org)
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.core.i18n import _
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core.log import getLogger
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22 log = getLogger(__name__)
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from wokkel import disco, iwokkel
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from zope.interface import implements
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.words.xish import domish
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 import uuid
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28 try:
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from twisted.words.protocols.xmlstream import XMPPHandler
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30 except ImportError:
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from wokkel.subprotocols import XMPPHandler
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34 NS_JINGLE_IBB = 'urn:xmpp:jingle:transports:ibb:1'
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
36 PLUGIN_INFO = {
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37 "name": "Jingle In-Band Bytestreams",
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38 "import_name": "XEP-0261",
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39 "type": "XEP",
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40 "protocols": ["XEP-0261"],
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41 "dependencies": ["XEP-0166", "XEP-0047"],
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42 "main": "XEP_0261",
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43 "handler": "yes",
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44 "description": _("""Implementation of Jingle In-Band Bytestreams""")
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45 }
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
47
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48 class XEP_0261(object):
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50 def __init__(self, host):
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51 log.info(_("plugin Jingle In-Band Bytestreams"))
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52 self.host = host
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53 self._j = host.plugins["XEP-0166"] # shortcut to access jingle
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54 self._ibb = host.plugins["XEP-0047"] # and in-band bytestream
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 self._j.registerTransport(NS_JINGLE_IBB, self._j.TRANSPORT_STREAMING, self, -10000) # must be the lowest priority
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57 def getHandler(self, profile):
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58 return XEP_0261_handler()
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60 def jingleSessionInit(self, session, content_name, profile):
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
61 transport_elt = domish.Element((NS_JINGLE_IBB, "transport"))
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
62 content_data = session['contents'][content_name]
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63 content_data['ibb_block_size'] = self._ibb.BLOCK_SIZE
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
64 transport_elt['block-size'] = unicode(content_data['ibb_block_size'])
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
65 transport_elt['sid'] = content_data['ibb_sid'] = unicode(uuid.uuid4())
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
66 return transport_elt
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
67
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
68 def jingleHandler(self, action, session, content_name, transport_elt, profile):
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
69 content_data = session['contents'][content_name]
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
70 if action in (self._j.A_SESSION_ACCEPT, self._j.A_ACCEPTED_ACK):
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
71 pass
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
72 elif action == self._j.A_SESSION_INITIATE:
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
73 content_data['ibb_sid'] = transport_elt['sid']
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
74 elif action in (self._j.A_START, self._j.A_PREPARE_RESPONDER):
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
75 to_jid = session['to_jid']
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
76 sid = content_data['ibb_sid']
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
77 file_obj = content_data['file_obj']
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
78 args = [session, content_name, profile]
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
79 if action == self._j.A_START:
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
80 block_size = content_data['ibb_block_size']
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
81 d = self._ibb.startStream(file_obj, to_jid, sid, block_size, profile)
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
82 d.addErrback(self._streamEb, *args)
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
83 else:
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
84 d = self._ibb.createSession(file_obj, to_jid, sid, profile)
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
85 d.addCallbacks(self._streamCb, self._streamEb, args, None, args)
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
86 else:
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
87 log.warning(u"FIXME: unmanaged action {}".format(action))
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
88 return transport_elt
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
89
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
90 def _streamCb(self, dummy, session, content_name, profile):
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
91 self._j.contentTerminate(session, content_name, profile=profile)
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
92
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
93 def _streamEb(self, failure, session, content_name, profile):
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
94 log.warning(u"Error while streaming in-band: {}".format(failure))
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
95 self._j.contentTerminate(session, content_name, reason=self._j.REASON_FAILED_TRANSPORT, profile=profile)
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
96
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
97 class XEP_0261_handler(XMPPHandler):
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
98 implements(iwokkel.IDisco)
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
99
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
100 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
101 return [disco.DiscoFeature(NS_JINGLE_IBB)]
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
102
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
103 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
bfef1934a8f3 plugin XEP-0261: jingle in-band bystream first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
104 return []