comparison sat/plugins/plugin_xep_0198.py @ 2727:59ac9284dee8

plugin XEP-0198: allow to customise/disable ack_timeout: "ack_timeout" setting can be set in [DEFAULT] section to the number of seconds before timing out, or to 0 to disable the timeout.
author Goffi <goffi@goffi.org>
date Wed, 26 Dec 2018 17:21:38 +0100
parents d0466af33483
children edd230651138
comparison
equal deleted inserted replaced
2726:a86f494457c2 2727:59ac9284dee8
58 # Max number of seconds before requesting ack 58 # Max number of seconds before requesting ack
59 MAX_DELAY_ACK_R = 30 59 MAX_DELAY_ACK_R = 30
60 MAX_COUNTER = 2**32 60 MAX_COUNTER = 2**32
61 RESUME_MAX = 5*60 61 RESUME_MAX = 5*60
62 # if we don't have an answer to ACK REQUEST after this delay, connection is aborted 62 # if we don't have an answer to ACK REQUEST after this delay, connection is aborted
63 ACK_R_TIMEOUT = 25 63 ACK_TIMEOUT = 25
64 64
65 65
66 class ProfileSessionData(object): 66 class ProfileSessionData(object):
67 out_counter = 0 67 out_counter = 0
68 in_counter = 0 68 in_counter = 0
127 host.registerNamespace(u'sm', NS_SM) 127 host.registerNamespace(u'sm', NS_SM)
128 host.trigger.add("stream_hooks", self.addHooks) 128 host.trigger.add("stream_hooks", self.addHooks)
129 host.trigger.add("xml_init", self._XMLInitTrigger) 129 host.trigger.add("xml_init", self._XMLInitTrigger)
130 host.trigger.add("disconnecting", self._disconnectingTrigger) 130 host.trigger.add("disconnecting", self._disconnectingTrigger)
131 host.trigger.add("disconnected", self._disconnectedTrigger) 131 host.trigger.add("disconnected", self._disconnectedTrigger)
132 try:
133 self._ack_timeout = int(host.memory.getConfig("", "ack_timeout", ACK_TIMEOUT))
134 except ValueError:
135 log.error(_(u"Invalid ack_timeout value, please check your configuration"))
136 self._ack_timeout = ACK_TIMEOUT
137 if not self._ack_timeout:
138 log.info(_(u"Ack timeout disabled"))
139 else:
140 log.info(_(u"Ack timeout set to {timeout} s").format(
141 timeout=self._ack_timeout))
132 142
133 def profileConnecting(self, client): 143 def profileConnecting(self, client):
134 client._xep_0198_session = ProfileSessionData(callback=self.checkAcks, 144 client._xep_0198_session = ProfileSessionData(callback=self.checkAcks,
135 client=client) 145 client=client)
136 146
238 session = client._xep_0198_session 248 session = client._xep_0198_session
239 r_elt = domish.Element((NS_SM, 'r')) 249 r_elt = domish.Element((NS_SM, 'r'))
240 client.send(r_elt) 250 client.send(r_elt)
241 if session.req_timer is not None: 251 if session.req_timer is not None:
242 raise exceptions.InternalError("req_timer should not be set") 252 raise exceptions.InternalError("req_timer should not be set")
243 session.req_timer = reactor.callLater(ACK_R_TIMEOUT, self.onAckTimeOut, client) 253 if self._ack_timeout:
254 session.req_timer = reactor.callLater(self._ack_timeout, self.onAckTimeOut,
255 client)
244 256
245 def _connectionFailed(self, failure_, connector): 257 def _connectionFailed(self, failure_, connector):
246 normal_host, normal_port = connector.normal_location 258 normal_host, normal_port = connector.normal_location
247 del connector.normal_location 259 del connector.normal_location
248 log.warning(_( 260 log.warning(_(
411 self.sendAck(client) 423 self.sendAck(client)
412 424
413 def onAckAnswer(self, a_elt, client): 425 def onAckAnswer(self, a_elt, client):
414 session = client._xep_0198_session 426 session = client._xep_0198_session
415 session.ack_requested = False 427 session.ack_requested = False
416 if session.req_timer is None: 428 if self._ack_timeout:
417 log.error("reg_timer should be set") 429 if session.req_timer is None:
418 else: 430 log.error("reg_timer should be set")
419 session.req_timer.cancel() 431 else:
420 session.req_timer = None 432 session.req_timer.cancel()
433 session.req_timer = None
421 try: 434 try:
422 server_acked = int(a_elt['h']) 435 server_acked = int(a_elt['h'])
423 except ValueError: 436 except ValueError:
424 log.warning(_(u"Server returned invalid ack element, disabling stream " 437 log.warning(_(u"Server returned invalid ack element, disabling stream "
425 u"management: {xml}").format(xml=a_elt)) 438 u"management: {xml}").format(xml=a_elt))