diff --git a/royalnet/audio/ytdldiscord.py b/royalnet/audio/ytdldiscord.py index ba0b48a9..1d8a8e2d 100644 --- a/royalnet/audio/ytdldiscord.py +++ b/royalnet/audio/ytdldiscord.py @@ -13,6 +13,10 @@ class YtdlDiscord: self.pcm_filename: typing.Optional[str] = None self._fas_spawned: typing.List[FileAudioSource] = [] + def __repr__(self): + return f"<{self.__class__.__name__} {self.info.title or 'Unknown Title'} ({'ready' if self.pcm_available() else 'not ready'}," \ + f" {len(self._fas_spawned)} audiosources spawned)>" + def pcm_available(self): return self.pcm_filename is not None and os.path.exists(self.pcm_filename) @@ -24,7 +28,7 @@ class YtdlDiscord: ffmpeg.input(self.ytdl_file.filename) .output(destination_filename, format="s16le", ac=2, ar="48000") .overwrite_output() - .run_async(quiet=True) + .run(quiet=True) ) self.pcm_filename = destination_filename diff --git a/royalnet/bots/discord.py b/royalnet/bots/discord.py index a83431fd..f158b0cc 100644 --- a/royalnet/bots/discord.py +++ b/royalnet/bots/discord.py @@ -217,7 +217,7 @@ class DiscordBot(GenericBot): guild_music_data = self.music_data[guild] for dfile in dfiles: log.debug(f"Adding {dfile} to music_data") - dfile.ready_up() + await asyncify(dfile.ready_up) guild_music_data.add(dfile) if guild_music_data.now_playing is None: await self.advance_music_data(guild) diff --git a/royalnet/network/networklink.py b/royalnet/network/networklink.py index ef8b8767..d65f44e8 100644 --- a/royalnet/network/networklink.py +++ b/royalnet/network/networklink.py @@ -89,6 +89,16 @@ class NetworkLink: self.connect_event: asyncio.Event = asyncio.Event(loop=self._loop) self.identify_event: asyncio.Event = asyncio.Event(loop=self._loop) + def __repr__(self): + if self.identify_event.is_set(): + return f"<{self.__class__.__name__} (identified)>" + elif self.connect_event.is_set(): + return f"<{self.__class__.__name__} (connected)>" + elif self.error_event.is_set(): + return f"<{self.__class__.__name__} (error)>" + else: + return f"<{self.__class__.__name__} (disconnected)>" + async def connect(self): """Connect to the :py:class:`royalnet.network.NetworkServer` at ``self.master_uri``.""" log.info(f"Connecting to {self.master_uri}...") @@ -149,13 +159,12 @@ class NetworkLink: log.debug(f"Received response: {request} -> {response}") return response - async def run(self, loops: numbers.Real = math.inf): + async def run(self): """Blockingly run the Link.""" log.debug(f"Running main client loop for {self.nid}.") if self.error_event.is_set(): raise ConnectionClosedError("RoyalnetLinks can't be rerun after an error.") - while loops: - loops -= 1 + while True: if not self.connect_event.is_set(): await self.connect() if not self.identify_event.is_set(): diff --git a/royalnet/network/networkserver.py b/royalnet/network/networkserver.py index 2dfabdd6..863bc32d 100644 --- a/royalnet/network/networkserver.py +++ b/royalnet/network/networkserver.py @@ -125,17 +125,13 @@ class NetworkServer: destination_conv_id=package.destination_conv_id) await destination.send(specific_package) - async def serve(self): + def serve(self): log.debug(f"Serving on ws://{self.address}:{self.port}") - await websockets.serve(self.listener, host=self.address, port=self.port, loop=self.loop) - log.debug(f"Serve has finished?!") - - async def run(self): - if self.loop is None: - self.loop = asyncio.get_event_loop() - log.debug(f"Starting main server loop on ws://{self.address}:{self.port}") - # noinspection PyAsyncCall - await self.serve() + server = self.loop.run_until_complete(websockets.serve(self.listener, + host=self.address, + port=self.port, + loop=self.loop)) + self.loop.run_forever() def run_blocking(self, verbose=False): if verbose: @@ -147,4 +143,4 @@ class NetworkServer: core_logger.debug("Logging setup complete.") if self.loop is None: self.loop = asyncio.get_event_loop() - self.loop.run_until_complete(self.run()) + self.serve() diff --git a/tests/test_network.py b/tests/test_network.py index e59da7c6..9a83a339 100644 --- a/tests/test_network.py +++ b/tests/test_network.py @@ -41,8 +41,8 @@ def test_request_creation(): def test_links(async_loop: asyncio.AbstractEventLoop): address, port = "127.0.0.1", 1235 - master = NetworkServer(address, port, "test") - async_loop.run_until_complete(master.start()) + master = NetworkServer(address, port, "test", loop=async_loop) + run_server_task = async_loop.create_task(master.run()) # Test invalid secret wrong_secret_link = NetworkLink("ws://127.0.0.1:1235", "invalid", "test", echo_request_handler, loop=async_loop) with pytest.raises(ConnectionClosedError):