From d55d05826f11d14080535e2fded2a8dc64082838 Mon Sep 17 00:00:00 2001 From: Joscha Date: Wed, 1 Aug 2018 21:41:08 +0000 Subject: [PATCH] Notice when connection runs out of retries --- yaboli/bot.py | 5 +++++ yaboli/connection.py | 34 +++++++++++++++++++++------------- yaboli/room.py | 7 +++++++ 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/yaboli/bot.py b/yaboli/bot.py index 005706c..4a0772b 100644 --- a/yaboli/bot.py +++ b/yaboli/bot.py @@ -77,6 +77,11 @@ class Bot(Inhabitant): if room: await room.exit() + # INHABITED FUNCTIONS + + async def on_stopped(self, room): + await self.part_room(room.roomname) + # BOTRULEZ @command("ping", specific=False, args=False) diff --git a/yaboli/connection.py b/yaboli/connection.py index c0bb174..0a83e9e 100644 --- a/yaboli/connection.py +++ b/yaboli/connection.py @@ -12,10 +12,11 @@ __all__ = ["Connection"] class Connection: - def __init__(self, url, packet_callback, disconnect_callback, cookiejar=None, ping_timeout=10, ping_delay=30, reconnect_attempts=10): + def __init__(self, url, packet_callback, disconnect_callback, stop_callback, cookiejar=None, ping_timeout=10, ping_delay=30, reconnect_attempts=10): self.url = url self.packet_callback = packet_callback self.disconnect_callback = disconnect_callback + self.stop_callback = stop_callback # is called when the connection stops on its own self.cookiejar = cookiejar self.ping_timeout = ping_timeout # how long to wait for websocket ping reply self.ping_delay = ping_delay # how long to wait between pings @@ -65,9 +66,10 @@ class Connection: This means that stop() can only be called once. """ - self._stopped = True - await self.reconnect() # _run() does the cleaning up now. - await self._runtask + if not self._stopped: + self._stopped = True + await self.reconnect() # _run() does the cleaning up now. + await self._runtask async def reconnect(self): """ @@ -153,16 +155,22 @@ class Connection: """ while not self._stopped: - await self._connect(self.reconnect_attempts) - logger.debug(f"{self.url}:Connected") + connected = await self._connect(self.reconnect_attempts) + if connected: + logger.debug(f"{self.url}:Connected") + try: + while True: + await self._handle_next_message() + except websockets.ConnectionClosed: + pass + finally: + await self._disconnect() # disconnect and clean up + else: + logger.debug(f"{self.url}:Stopping") + asyncio.ensure_future(self.stop_callback) + self._stopped = True + await self._disconnect() - try: - while True: - await self._handle_next_message() - except websockets.ConnectionClosed: - pass - finally: - await self._disconnect() # disconnect and clean up async def _ping(self): """ diff --git a/yaboli/room.py b/yaboli/room.py index 840ebda..76650cb 100644 --- a/yaboli/room.py +++ b/yaboli/room.py @@ -58,6 +58,7 @@ class Room: self.format_room_url(self.roomname, human=self.human), self._receive_packet, self._disconnected, + self._stopped, cookiejar ) @@ -172,6 +173,9 @@ class Room: await self._inhabitant.on_disconnected(self) + async def _stopped(self): + await self._inhabitant.on_stopped(self) + async def _receive_packet(self, ptype, data, error, throttled): # Ignoring errors and throttling for now functions = { @@ -410,6 +414,9 @@ class Inhabitant: async def on_disconnected(self, room): pass + async def on_stopped(self, room): + pass + async def on_join(self, room, session): pass