class AsyncRunner: def __init__(self, ip_version: IPVersion) -> None: self.ip_version = ip_version self.aiozc: Optional[AsyncZeroconf] = None async def register_services(self, infos: List[AsyncServiceInfo]) -> None: self.aiozc = AsyncZeroconf(ip_version=self.ip_version) tasks = [ self.aiozc.async_register_service(info, allow_name_change=True) for info in infos ] background_tasks = await asyncio.gather(*tasks) await asyncio.gather(*background_tasks) async def unregister_services(self, infos: List[AsyncServiceInfo]) -> None: assert self.aiozc is not None tasks = [self.aiozc.async_unregister_service(info) for info in infos] background_tasks = await asyncio.gather(*tasks) await asyncio.gather(*background_tasks) await self.aiozc.async_close() async def update_services(self, infos: List[AsyncServiceInfo]) -> None: assert self.aiozc is not None tasks = [self.aiozc.async_update_service(info) for info in infos] background_tasks = await asyncio.gather(*tasks) await asyncio.gather(*background_tasks)
class AsyncRunner: def __init__(self, ip_version: IPVersion, interface: str, interface_name: str) -> None: self.ip_version = ip_version self.aiozc: Optional[AsyncZeroconf] = None self.interface: str = interface self.services: List[AsyncServiceInfo] = [] self.interface_name = interface_name def add_services(self, service: AsyncServiceInfo) -> None: logger.info("Adding services:") logger.info(service) self.services.append(service) def get_services(self) -> List[MdnsEntry]: return [ MdnsEntry( ip=self.interface, fullname=service.name, hostname=service.name.split(".")[0], service_type=service.name.split(".")[1], interface=self.interface_name, interface_type=InterfaceType.guess_from_name( self.interface_name), ) for service in self.services ] async def register_services(self) -> None: self.aiozc = AsyncZeroconf(ip_version=self.ip_version, interfaces=[self.interface]) # type: ignore tasks = [ self.aiozc.async_register_service(info, cooperating_responders=True, ttl=25) for info in self.services ] background_tasks = await asyncio.gather(*tasks) await asyncio.gather(*background_tasks) logger.info("Finished registration, press Ctrl-C to exit...") async def unregister_services(self) -> None: assert self.aiozc is not None tasks = [ self.aiozc.async_unregister_service(info) for info in self.services ] background_tasks = await asyncio.gather(*tasks) await asyncio.gather(*background_tasks) await self.aiozc.async_close() def __eq__(self, other: Any) -> bool: return {str(service) for service in self.services} == { str(service) for service in other.services } and self.interface == other.interface def __repr__(self) -> str: return f"Runner on {self.interface}, serving {[service.name for service in self.services]}."