XEP-0199: Fix handler default parameter, add typing

Clear futures when disabling the keepalive, and do it on every
disconnect instead of only at session end.
This commit is contained in:
mathieui 2021-01-23 15:38:23 +01:00
parent c6a0da63ae
commit 9cb5131f1c

View file

@ -9,7 +9,8 @@
import time import time
import logging import logging
from typing import Optional, Callable from asyncio import Future
from typing import Optional, Callable, List
from slixmpp.jid import JID from slixmpp.jid import JID
from slixmpp.stanza import Iq from slixmpp.stanza import Iq
@ -64,9 +65,9 @@ class XEP_0199(BasePlugin):
""" """
Start the XEP-0199 plugin. Start the XEP-0199 plugin.
""" """
register_stanza_plugin(Iq, Ping) register_stanza_plugin(Iq, Ping)
self.__pending_futures = []
self.__pending_futures: List[Future] = []
self.xmpp.register_handler( self.xmpp.register_handler(
Callback('Ping', Callback('Ping',
@ -76,7 +77,9 @@ class XEP_0199(BasePlugin):
if self.keepalive: if self.keepalive:
self.xmpp.add_event_handler('session_start', self.xmpp.add_event_handler('session_start',
self.enable_keepalive) self.enable_keepalive)
self.xmpp.add_event_handler('session_end', self.xmpp.add_event_handler('session_resumed',
self.enable_keepalive)
self.xmpp.add_event_handler('disconnected',
self.disable_keepalive) self.disable_keepalive)
def plugin_end(self): def plugin_end(self):
@ -85,13 +88,19 @@ class XEP_0199(BasePlugin):
if self.keepalive: if self.keepalive:
self.xmpp.del_event_handler('session_start', self.xmpp.del_event_handler('session_start',
self.enable_keepalive) self.enable_keepalive)
self.xmpp.del_event_handler('session_end', self.xmpp.del_event_handler('session_resumed',
self.enable_keepalive)
self.xmpp.del_event_handler('disconnected',
self.disable_keepalive) self.disable_keepalive)
def session_bind(self, jid): def session_bind(self, jid):
self.xmpp['xep_0030'].add_feature(Ping.namespace) self.xmpp['xep_0030'].add_feature(Ping.namespace)
def session_end(self, event):
def _clear_pending_futures(self):
"""Cancel all pending ping futures"""
if self.__pending_futures:
log.debug('Clearing %s pdnding pings', len(self.__pending_futures))
for future in self.__pending_futures: for future in self.__pending_futures:
future.cancel() future.cancel()
self.__pending_futures.clear() self.__pending_futures.clear()
@ -103,7 +112,7 @@ class XEP_0199(BasePlugin):
self.timeout = timeout self.timeout = timeout
self.keepalive = True self.keepalive = True
def handler(event): def handler(event=None):
# Cleanup futures # Cleanup futures
if self.__pending_futures: if self.__pending_futures:
tmp_futures = [] tmp_futures = []
@ -123,8 +132,11 @@ class XEP_0199(BasePlugin):
repeat=True) repeat=True)
def disable_keepalive(self, event=None): def disable_keepalive(self, event=None):
self._clear_pending_futures()
self.xmpp.cancel_schedule('Ping keepalive') self.xmpp.cancel_schedule('Ping keepalive')
session_end = disable_keepalive
async def _keepalive(self, event=None): async def _keepalive(self, event=None):
log.debug("Keepalive ping...") log.debug("Keepalive ping...")
try: try: