From 3606e86bc969b92d391b78a6605394bdc192de9b Mon Sep 17 00:00:00 2001 From: Tim-Oliver Husser Date: Tue, 20 Aug 2024 11:17:14 +0200 Subject: [PATCH 1/2] XMPP auto-reconnect, closes #336 --- pyobs/comm/xmpp/xmppcomm.py | 50 ++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/pyobs/comm/xmpp/xmppcomm.py b/pyobs/comm/xmpp/xmppcomm.py index 9034919b..8650deac 100644 --- a/pyobs/comm/xmpp/xmppcomm.py +++ b/pyobs/comm/xmpp/xmppcomm.py @@ -110,7 +110,7 @@ def __init__( resource: Resource part of the JID. password: Password for given JID. server: Server to connect to. If not given, domain from JID is used. - use_tls: Whether or not to use TLS. + use_tls: Whether to use TLS. """ Comm.__init__(self, *args, **kwargs) @@ -169,12 +169,23 @@ async def open(self) -> None: Whether opening was successful. """ + # connect + await self._connect() + + # subscribe to events + await self.register_event(LogEvent) + + # open Comm + await Comm.open(self) + + async def _connect(self) -> None: # create client self._xmpp = XmppClient(self._jid, self._password) # self._xmpp = slixmpp.ClientXMPP(self._jid, password) self._xmpp.add_event_handler("pubsub_publish", self._handle_event) self._xmpp.add_event_handler("got_online", self._got_online) self._xmpp.add_event_handler("got_offline", self._got_offline) + self._xmpp.add_event_handler("disconnected", self._disconnected) # server given? server = () if self._server is None else tuple(self._server.split(":")) @@ -198,12 +209,6 @@ async def open(self) -> None: self.module.quit() return - # subscribe to events - await self.register_event(LogEvent) - - # open Comm - await Comm.open(self) - # wait a little and finished await asyncio.sleep(1) self._connected = True @@ -217,6 +222,25 @@ async def close(self) -> None: # disconnect from sleekxmpp server await self._xmpp.disconnect() + async def _reconnect(self): + """Sleep a little and reconnect""" + await asyncio.sleep(2) + await self._connect() + + def _disconnected(self, event: Any): + """Reset connection after disconnect.""" + log.info("Disconnected from server, waiting for reconnect...") + + # disconnect all clients + for jid in self._online_clients: + self._jid_got_offline(jid) + + # reconnect + asyncio.ensure_future( + self._reconnect(), + loop=self._loop, + ) + @property def name(self) -> Optional[str]: """Name of this client.""" @@ -391,9 +415,16 @@ def _got_offline(self, msg: Any) -> None: Args: msg: XMPP message. """ + self._jid_got_offline(msg["from"].full) + + def _jid_got_offline(self, jid: str) -> None: + """If a new client disconnects, remove it from list. + + Args: + jid: JID that got offline. + """ # remove from list - jid = msg["from"].full if jid in self._online_clients: self._online_clients.remove(jid) @@ -402,7 +433,8 @@ def _got_offline(self, msg: Any) -> None: del self._interface_cache[jid] # send event - self._send_event_to_module(ModuleClosedEvent(), msg["from"].username) + username = jid[: jid.find("@")] + self._send_event_to_module(ModuleClosedEvent(), username) @property def clients(self) -> List[str]: From 8ce5a65c7d83bfa93755c5f715f8bf307d7a54c3 Mon Sep 17 00:00:00 2001 From: Tim-Oliver Husser Date: Tue, 20 Aug 2024 11:17:30 +0200 Subject: [PATCH 2/2] v1.16.2 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index dab93bdc..21582ded 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [tool.poetry] name = "pyobs-core" packages = [{ include = "pyobs" }] -version = "1.16.1" +version = "1.16.2" description = "robotic telescope software" authors = ["Tim-Oliver Husser "] license = "MIT"