Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0166.py @ 3040:fee60f17ebac
jp: jp asyncio port:
/!\ this commit is huge. Jp is temporarily not working with `dbus` bridge /!\
This patch implements the port of jp to asyncio, so it is now correctly using the bridge
asynchronously, and it can be used with bridges like `pb`. This also simplify the code,
notably for things which were previously implemented with many callbacks (like pagination
with RSM).
During the process, some behaviours have been modified/fixed, in jp and backends, check
diff for details.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 25 Sep 2019 08:56:41 +0200 |
parents | ab2696e34d29 |
children | 9d0df638c8b4 |
comparison
equal
deleted
inserted
replaced
3039:a1bc34f90fa5 | 3040:fee60f17ebac |
---|---|
111 | 111 |
112 def _delSession(self, client, sid): | 112 def _delSession(self, client, sid): |
113 try: | 113 try: |
114 del client.jingle_sessions[sid] | 114 del client.jingle_sessions[sid] |
115 except KeyError: | 115 except KeyError: |
116 log.debug("Jingle session id [{}] is unknown, nothing to delete".format(sid)) | 116 log.debug( |
117 f"Jingle session id {sid!r} is unknown, nothing to delete " | |
118 f"[{client.profile}]") | |
117 else: | 119 else: |
118 log.debug("Jingle session id [{}] deleted".format(sid)) | 120 log.debug(f"Jingle session id {sid!r} deleted [{client.profile}]") |
119 | 121 |
120 ## helpers methods to build stanzas ## | 122 ## helpers methods to build stanzas ## |
121 | 123 |
122 def _buildJingleElt(self, client, session, action): | 124 def _buildJingleElt(self, client, session, action): |
123 iq_elt = client.IQ("set") | 125 iq_elt = client.IQ("set") |
144 log.warning( | 146 log.warning( |
145 "Error while managing jingle session, cancelling: {condition}".format( | 147 "Error while managing jingle session, cancelling: {condition}".format( |
146 condition=error_condition | 148 condition=error_condition |
147 ) | 149 ) |
148 ) | 150 ) |
149 client.send(iq_elt) | 151 return client.send(iq_elt) |
150 | 152 |
151 def _terminateEb(self, failure_): | 153 def _terminateEb(self, failure_): |
152 log.warning(_("Error while terminating session: {msg}").format(msg=failure_)) | 154 log.warning(_("Error while terminating session: {msg}").format(msg=failure_)) |
153 | 155 |
154 def terminate(self, client, reason, session): | 156 def terminate(self, client, reason, session): |
186 failure_=failure_.value | 188 failure_=failure_.value |
187 ) | 189 ) |
188 ) | 190 ) |
189 self._delSession(client, sid) | 191 self._delSession(client, sid) |
190 | 192 |
191 def _jingleErrorCb(self, fail, sid, request, client): | 193 def _jingleErrorCb(self, failure_, session, request, client): |
192 """Called when something is going wrong while parsing jingle request | 194 """Called when something is going wrong while parsing jingle request |
193 | 195 |
194 The error condition depend of the exceptions raised: | 196 The error condition depend of the exceptions raised: |
195 exceptions.DataError raise a bad-request condition | 197 exceptions.DataError raise a bad-request condition |
196 @param fail(failure.Failure): the exceptions raised | 198 @param fail(failure.Failure): the exceptions raised |
197 @param sid(unicode): jingle session id | 199 @param session(dict): data of the session |
198 @param request(domsih.Element): jingle request | 200 @param request(domsih.Element): jingle request |
199 @param client: %(doc_client)s | 201 @param client: %(doc_client)s |
200 """ | 202 """ |
201 log.warning("Error while processing jingle request") | 203 log.warning(f"Error while processing jingle request [{client.profile}]") |
202 if isinstance(fail, exceptions.DataError): | 204 if isinstance(failure_, exceptions.DataError): |
203 self.sendError(client, "bad-request", sid, request) | 205 return self.sendError(client, "bad-request", session['id'], request) |
204 else: | 206 else: |
205 log.error("Unmanaged jingle exception") | 207 log.error(f"Unmanaged jingle exception: {failure_}") |
206 self._delSession(client, sid) | 208 return self.terminate(client, self.REASON_FAILED_APPLICATION, session) |
207 raise fail | |
208 | 209 |
209 ## methods used by other plugins ## | 210 ## methods used by other plugins ## |
210 | 211 |
211 def registerApplication(self, namespace, handler): | 212 def registerApplication(self, namespace, handler): |
212 """Register an application plugin | 213 """Register an application plugin |
704 | 705 |
705 must be used as app_default_cb and/or transp_default_cb | 706 must be used as app_default_cb and/or transp_default_cb |
706 """ | 707 """ |
707 return elt | 708 return elt |
708 | 709 |
709 def _callPlugins( | 710 def _callPlugins(self, client, action, session, app_method_name="jingleHandler", |
710 self, | 711 transp_method_name="jingleHandler", app_default_cb=None, |
711 client, | 712 transp_default_cb=None, delete=True, elements=True, |
712 action, | 713 force_element=None): |
713 session, | |
714 app_method_name="jingleHandler", | |
715 transp_method_name="jingleHandler", | |
716 app_default_cb=None, | |
717 transp_default_cb=None, | |
718 delete=True, | |
719 elements=True, | |
720 force_element=None, | |
721 ): | |
722 """Call application and transport plugin methods for all contents | 714 """Call application and transport plugin methods for all contents |
723 | 715 |
724 @param action(unicode): jingle action name | 716 @param action(unicode): jingle action name |
725 @param session(dict): jingle session data | 717 @param session(dict): jingle session data |
726 @param app_method_name(unicode, None): name of the method to call for applications | 718 @param app_method_name(unicode, None): name of the method to call for applications |
817 delete=False, | 809 delete=False, |
818 ) | 810 ) |
819 | 811 |
820 confirm_dlist = defer.gatherResults(confirm_defers) | 812 confirm_dlist = defer.gatherResults(confirm_defers) |
821 confirm_dlist.addCallback(self._confirmationCb, session, jingle_elt, client) | 813 confirm_dlist.addCallback(self._confirmationCb, session, jingle_elt, client) |
822 confirm_dlist.addErrback(self._jingleErrorCb, session["id"], request, client) | 814 confirm_dlist.addErrback(self._jingleErrorCb, session, request, client) |
823 | 815 |
824 def _confirmationCb(self, confirm_results, session, jingle_elt, client): | 816 def _confirmationCb(self, confirm_results, session, jingle_elt, client): |
825 """Method called when confirmation from user has been received | 817 """Method called when confirmation from user has been received |
826 | 818 |
827 This method is only called for the responder | 819 This method is only called for the responder |
950 session["state"] = STATE_ACTIVE | 942 session["state"] = STATE_ACTIVE |
951 | 943 |
952 negociate_defers = [] | 944 negociate_defers = [] |
953 negociate_defers = self._callPlugins(client, XEP_0166.A_SESSION_ACCEPT, session) | 945 negociate_defers = self._callPlugins(client, XEP_0166.A_SESSION_ACCEPT, session) |
954 | 946 |
955 negociate_dlist = defer.DeferredList(negociate_defers) | 947 negociate_dlist = defer.gatherResults(negociate_defers) |
956 | 948 |
957 # after negociations we start the transfer | 949 # after negociations we start the transfer |
958 negociate_dlist.addCallback( | 950 negociate_dlist.addCallback( |
959 lambda __: self._callPlugins( | 951 lambda __: self._callPlugins( |
960 client, XEP_0166.A_START, session, app_method_name=None, elements=False | 952 client, XEP_0166.A_START, session, app_method_name=None, elements=False |