Skip to content
Merged
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ mypy:
mypy examples/*.py zeroconf/*.py

test:
pytest -v zeroconf/test.py
pytest -v zeroconf/test.py zeroconf/test_asyncio.py

test_coverage:
pytest -v --cov=zeroconf --cov-branch --cov-report html --cov-report term-missing zeroconf/test.py
pytest -v --cov=zeroconf --cov-branch --cov-report html --cov-report term-missing zeroconf/test.py zeroconf/test_asyncio.py

autopep8:
autopep8 --max-line-length=$(MAX_LINE_LENGTH) -i setup.py examples zeroconf
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ mypy;implementation_name=="cpython"
# 0.11.0 breaks things https://github.com/PyCQA/pep8-naming/issues/152
pep8-naming!=0.6.0,!=0.11.0
pytest
pytest-asyncio
pytest-cov
74 changes: 41 additions & 33 deletions zeroconf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,16 @@ def service_type_name(type_: str, *, strict: bool = True) -> str:
return service_name + trailer


def instance_name_from_service_info(info: "ServiceInfo") -> str:
"""Calculate the instance name from the ServiceInfo."""
# This is kind of funky because of the subtype based tests
# need to make subtypes a first class citizen
service_name = service_type_name(info.name)
if not info.type.endswith(service_name):
raise BadTypeInNameException
return info.name[: -len(service_name) - 1]


# Exceptions


Expand Down Expand Up @@ -2505,8 +2515,6 @@ def __init__(
for s in self._respond_sockets:
self.engine.add_reader(self.listener, s)

self.debug = None # type: Optional[DNSOutgoing]

@property
def done(self) -> bool:
return self._GLOBAL_DONE
Expand Down Expand Up @@ -2580,6 +2588,7 @@ def update_service(self, info: ServiceInfo) -> None:
self._broadcast_service(info, _REGISTER_TIME, None)

def _broadcast_service(self, info: ServiceInfo, interval: int, ttl: Optional[int]) -> None:
"""Send a broadcasts to announce a service at intervals."""
now = current_time_millis()
next_time = now
i = 0
Expand All @@ -2589,12 +2598,23 @@ def _broadcast_service(self, info: ServiceInfo, interval: int, ttl: Optional[int
now = current_time_millis()
continue

out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA)
self._add_broadcast_answer(out, info, ttl)
self.send(out)
self.send_service_broadcast(info, ttl)
i += 1
next_time += interval

def send_service_broadcast(self, info: ServiceInfo, ttl: Optional[int]) -> None:
"""Send a broadcast to announce a service."""
out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA)
self._add_broadcast_answer(out, info, ttl)
self.send(out)

def send_service_query(self, info: ServiceInfo) -> None:
"""Send a query to lookup a service."""
out = DNSOutgoing(_FLAGS_QR_QUERY | _FLAGS_AA)
out.add_question(DNSQuestion(info.type, _TYPE_PTR, _CLASS_IN))
out.add_authorative_answer(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, info.other_ttl, info.name))
self.send(out)

def _add_broadcast_answer(self, out: DNSOutgoing, info: ServiceInfo, override_ttl: Optional[int]) -> None:
"""Add answers to broadcast a service."""
other_ttl = info.other_ttl if override_ttl is None else override_ttl
Expand Down Expand Up @@ -2653,43 +2673,31 @@ def check_service(
) -> None:
"""Checks the network for a unique service name, modifying the
ServiceInfo passed in if it is not unique."""

# This is kind of funky because of the subtype based tests
# need to make subtypes a first class citizen
service_name = service_type_name(info.name)
if not info.type.endswith(service_name):
raise BadTypeInNameException

instance_name = info.name[: -len(service_name) - 1]
instance_name = instance_name_from_service_info(info)
if cooperating_responders:
return
next_instance_number = 2

now = current_time_millis()
next_time = now
next_time = now = current_time_millis()
i = 0
while i < 3:
if not cooperating_responders:
# check for a name conflict
while self.cache.current_entry_with_name_and_alias(info.type, info.name):
if not allow_name_change:
raise NonUniqueNameException

# change the name and look for a conflict
info.name = '%s-%s.%s' % (instance_name, next_instance_number, info.type)
next_instance_number += 1
service_type_name(info.name)
next_time = now
i = 0
# check for a name conflict
while self.cache.current_entry_with_name_and_alias(info.type, info.name):
if not allow_name_change:
raise NonUniqueNameException

# change the name and look for a conflict
info.name = '%s-%s.%s' % (instance_name, next_instance_number, info.type)
next_instance_number += 1
service_type_name(info.name)
next_time = now
i = 0

if now < next_time:
self.wait(next_time - now)
now = current_time_millis()
continue

out = DNSOutgoing(_FLAGS_QR_QUERY | _FLAGS_AA)
self.debug = out
out.add_question(DNSQuestion(info.type, _TYPE_PTR, _CLASS_IN))
out.add_authorative_answer(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, info.other_ttl, info.name))
self.send(out)
self.send_service_query(info)
i += 1
next_time += _CHECK_TIME

Expand Down
137 changes: 137 additions & 0 deletions zeroconf/asyncio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
""" Multicast DNS Service Discovery for Python, v0.14-wmcbrine
Copyright 2003 Paul Scott-Murphy, 2014 William McBrine

This module provides a framework for the use of DNS Service Discovery
using IP multicast.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA
"""
import asyncio
from typing import Optional

from . import (
IPVersion,
InterfaceChoice,
InterfacesType,
NonUniqueNameException,
ServiceInfo,
Zeroconf,
_CHECK_TIME,
_REGISTER_TIME,
_UNREGISTER_TIME,
instance_name_from_service_info,
)


class AsyncZeroconf:
"""Implementation of Zeroconf Multicast DNS Service Discovery

Supports registration, unregistration, queries and browsing.

The async version is currently a wrapper around the sync version
with I/O being done in the executor for backwards compatibility.
"""

def __init__(
self,
interfaces: InterfacesType = InterfaceChoice.All,
unicast: bool = False,
ip_version: Optional[IPVersion] = None,
apple_p2p: bool = False,
) -> None:
"""Creates an instance of the Zeroconf class, establishing
multicast communications, listening and reaping threads.

:param interfaces: :class:`InterfaceChoice` or a list of IP addresses
(IPv4 and IPv6) and interface indexes (IPv6 only).

IPv6 notes for non-POSIX systems:
* `InterfaceChoice.All` is an alias for `InterfaceChoice.Default`
on Python versions before 3.8.

Also listening on loopback (``::1``) doesn't work, use a real address.
:param ip_version: IP versions to support. If `choice` is a list, the default is detected
from it. Otherwise defaults to V4 only for backward compatibility.
:param apple_p2p: use AWDL interface (only macOS)
"""
self.zeroconf = Zeroconf(
interfaces=interfaces,
unicast=unicast,
ip_version=ip_version,
apple_p2p=apple_p2p,
)
self.loop = asyncio.get_event_loop()

async def _async_broadcast_service(self, info: ServiceInfo, interval: int, ttl: Optional[int]) -> None:
"""Send a broadcasts to announce a service at intervals."""
for i in range(3):
if i != 0:
await asyncio.sleep(interval / 1000)
await self.loop.run_in_executor(None, self.zeroconf.send_service_broadcast, info, ttl)

async def async_register_service(
self,
info: ServiceInfo,
cooperating_responders: bool = False,
) -> None:
"""Registers service information to the network with a default TTL.
Zeroconf will then respond to requests for information for that
service. The name of the service may be changed if needed to make
it unique on the network. Additionally multiple cooperating responders
can register the same service on the network for resilience
(if you want this behavior set `cooperating_responders` to `True`).

The service will be broadcast in a task.
"""
await self.async_check_service(info, cooperating_responders)
await self.loop.run_in_executor(None, self.zeroconf.registry.add, info)
asyncio.ensure_future(self._async_broadcast_service(info, _REGISTER_TIME, None))

async def async_check_service(self, info: ServiceInfo, cooperating_responders: bool = False) -> None:
"""Checks the network for a unique service name."""
instance_name_from_service_info(info)
if cooperating_responders:
return
for i in range(3):
# check for a name conflict
if self.zeroconf.cache.current_entry_with_name_and_alias(info.type, info.name):
raise NonUniqueNameException
if i != 0:
await asyncio.sleep(_CHECK_TIME / 1000)
await self.loop.run_in_executor(None, self.zeroconf.send_service_query, info)

async def async_unregister_service(self, info: ServiceInfo) -> None:
"""Unregister a service.

The service will be broadcast in a task.
"""
await self.loop.run_in_executor(None, self.zeroconf.registry.remove, info)
asyncio.ensure_future(self._async_broadcast_service(info, _UNREGISTER_TIME, 0))

async def async_update_service(self, info: ServiceInfo) -> None:
"""Registers service information to the network with a default TTL.
Zeroconf will then respond to requests for information for that
service.

The service will be broadcast in a task.
"""
await self.loop.run_in_executor(None, self.zeroconf.registry.update, info)
asyncio.ensure_future(self._async_broadcast_service(info, _REGISTER_TIME, None))

async def async_close(self) -> None:
"""Ends the background threads, and prevent this instance from
servicing further queries."""
await self.loop.run_in_executor(None, self.zeroconf.close)
Loading