Remove asyncio and do not support subdomains

This commit is contained in:
tituspijean 2021-07-27 20:38:02 +00:00
parent 92a1a12226
commit 7c58aa198c

View file

@ -16,11 +16,10 @@ import yaml
import asyncio import asyncio
import logging import logging
import socket import socket
import time from time import sleep
from typing import List, Dict from typing import List, Dict
from zeroconf import DNSEntry, DNSRecord from zeroconf import Zeroconf, ServiceInfo
from zeroconf.asyncio import AsyncServiceInfo, AsyncZeroconf
# Helper command taken from Moulinette # Helper command taken from Moulinette
def check_output(args, stderr=subprocess.STDOUT, shell=True, **kwargs): def check_output(args, stderr=subprocess.STDOUT, shell=True, **kwargs):
@ -98,30 +97,6 @@ def get_network_interfaces():
return devices return devices
# async commands
async def register_services(aiozcs: Dict[AsyncZeroconf, List[AsyncServiceInfo]]) -> None:
tasks = []
for aiozc, infos in aiozcs.items():
for info in infos:
tasks.append(aiozc.async_register_service(info))
background_tasks = await asyncio.gather(*tasks)
await asyncio.gather(*background_tasks)
async def unregister_services(aiozcs: Dict[AsyncZeroconf, List[AsyncServiceInfo]]) -> None:
for aiozc, infos in aiozcs.items():
for info in infos:
tasks.append(aiozc.async_unregister_service(info))
background_tasks = await asyncio.gather(*tasks)
await asyncio.gather(*background_tasks)
async def close_aiozcs(aiozcs: Dict[AsyncZeroconf, List[AsyncServiceInfo]]) -> None:
tasks = []
for aiozc in aiozcs:
tasks.append(aiozc.async_close())
background_tasks = await asyncio.gather(*tasks)
await asyncio.gather(*background_tasks)
if __name__ == '__main__': if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
@ -212,9 +187,7 @@ if __name__ == '__main__':
print('No interface listed for broadcast.') print('No interface listed for broadcast.')
sys.exit(0) sys.exit(0)
aiozcs = {} zcs = {}
tasks = []
loop = asyncio.get_event_loop()
interfaces = get_network_interfaces() interfaces = get_network_interfaces()
for interface in config['interfaces']: for interface in config['interfaces']:
infos = [] # List of ServiceInfo objects, to feed Zeroconf infos = [] # List of ServiceInfo objects, to feed Zeroconf
@ -240,44 +213,39 @@ if __name__ == '__main__':
# If at least one IP is listed # If at least one IP is listed
if addressed: if addressed:
# Create a ServiceInfo object for each .local domain # Create a Zeroconf object, and store the ServiceInfos
zc = Zeroconf(interfaces=ips)
zcs[zc]=[]
for d in config['domains']: for d in config['domains']:
d_domain=d.replace('.local','') d_domain=d.replace('.local','')
infos.append( if '.' in d_domain:
AsyncServiceInfo( print(d_domain+'.local: subdomains are not supported.')
type_='_device-info._tcp.local.', else:
name=interface+' '+d_domain+'._device-info._tcp.local.', # Create a ServiceInfo object for each .local domain
addresses=b_ips, zcs[zc].append(ServiceInfo(
port=80, type_='_device-info._tcp.local.',
server=d+'.', name=interface+': '+d_domain+'._device-info._tcp.local.',
) addresses=b_ips,
) port=80,
infos.append( server=d+'.',
AsyncServiceInfo( ))
type_='_http._tcp.local.', print('Adding '+d+' with addresses '+str(ips)+' on interface '+interface)
name=interface+' '+d_domain+'._http._tcp.local.',
addresses=b_ips,
port=80,
server=d+'.',
)
)
print('Adding '+d+' with addresses '+str(ips)+' on interface '+interface)
# Create an AsyncZeroconf object, and store registration task
aiozc = AsyncZeroconf(interfaces=ips)
aiozcs[aiozc]=infos
# Run registration # Run registration
loop.run_until_complete(register_services(aiozcs)) print("Registering...")
print("Registration complete. Press Ctrl-c or stop service to exit...") for zc, infos in zcs.items():
for info in infos:
zc.register_service(info)
try: try:
print("Registered. Press Ctrl+C or stop service to stop.")
while True: while True:
time.sleep(1) sleep(1)
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
finally: finally:
print("Unregistering...") print("Unregistering...")
loop.run_until_complete(unregister_services(aiozcs)) for zc, infos in zcs.items():
print("Unregistration complete.") for info in infos:
loop.run_until_complete(close_aiozcs(aiozcs)) zc.unregister_service(info)
zc.close()