|
| 1 | +""" Multicast DNS Service Discovery for Python, v0.14-wmcbrine |
| 2 | + Copyright 2003 Paul Scott-Murphy, 2014 William McBrine |
| 3 | +
|
| 4 | + This module provides a framework for the use of DNS Service Discovery |
| 5 | + using IP multicast. |
| 6 | +
|
| 7 | + This library is free software; you can redistribute it and/or |
| 8 | + modify it under the terms of the GNU Lesser General Public |
| 9 | + License as published by the Free Software Foundation; either |
| 10 | + version 2.1 of the License, or (at your option) any later version. |
| 11 | +
|
| 12 | + This library is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + Lesser General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU Lesser General Public |
| 18 | + License along with this library; if not, write to the Free Software |
| 19 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 |
| 20 | + USA |
| 21 | +""" |
| 22 | + |
| 23 | +import threading |
| 24 | +from typing import Dict, List, Optional |
| 25 | + |
| 26 | + |
| 27 | +from ..exceptions import ServiceNameAlreadyRegistered |
| 28 | +from ..services import ServiceInfo |
| 29 | + |
| 30 | + |
| 31 | +class ServiceRegistry: |
| 32 | + """A registry to keep track of services. |
| 33 | +
|
| 34 | + This class exists to ensure services can |
| 35 | + be safely added and removed with thread |
| 36 | + safety. |
| 37 | + """ |
| 38 | + |
| 39 | + def __init__( |
| 40 | + self, |
| 41 | + ) -> None: |
| 42 | + """Create the ServiceRegistry class.""" |
| 43 | + self.services = {} # type: Dict[str, ServiceInfo] |
| 44 | + self.types = {} # type: Dict[str, List] |
| 45 | + self.servers = {} # type: Dict[str, List] |
| 46 | + self._lock = threading.Lock() # add and remove services thread safe |
| 47 | + |
| 48 | + def add(self, info: ServiceInfo) -> None: |
| 49 | + """Add a new service to the registry.""" |
| 50 | + |
| 51 | + with self._lock: |
| 52 | + self._add(info) |
| 53 | + |
| 54 | + def remove(self, info: ServiceInfo) -> None: |
| 55 | + """Remove a new service from the registry.""" |
| 56 | + |
| 57 | + with self._lock: |
| 58 | + self._remove(info) |
| 59 | + |
| 60 | + def update(self, info: ServiceInfo) -> None: |
| 61 | + """Update new service in the registry.""" |
| 62 | + |
| 63 | + with self._lock: |
| 64 | + self._remove(info) |
| 65 | + self._add(info) |
| 66 | + |
| 67 | + def get_service_infos(self) -> List[ServiceInfo]: |
| 68 | + """Return all ServiceInfo.""" |
| 69 | + return list(self.services.values()) |
| 70 | + |
| 71 | + def get_info_name(self, name: str) -> Optional[ServiceInfo]: |
| 72 | + """Return all ServiceInfo for the name.""" |
| 73 | + return self.services.get(name) |
| 74 | + |
| 75 | + def get_types(self) -> List[str]: |
| 76 | + """Return all types.""" |
| 77 | + return list(self.types.keys()) |
| 78 | + |
| 79 | + def get_infos_type(self, type_: str) -> List[ServiceInfo]: |
| 80 | + """Return all ServiceInfo matching type.""" |
| 81 | + return self._get_by_index("types", type_) |
| 82 | + |
| 83 | + def get_infos_server(self, server: str) -> List[ServiceInfo]: |
| 84 | + """Return all ServiceInfo matching server.""" |
| 85 | + return self._get_by_index("servers", server) |
| 86 | + |
| 87 | + def _get_by_index(self, attr: str, key: str) -> List[ServiceInfo]: |
| 88 | + """Return all ServiceInfo matching the index.""" |
| 89 | + service_infos = [] |
| 90 | + |
| 91 | + for name in getattr(self, attr).get(key, [])[:]: |
| 92 | + info = self.services.get(name) |
| 93 | + # Since we do not get under a lock since it would be |
| 94 | + # a performance issue, its possible |
| 95 | + # the service can be unregistered during the get |
| 96 | + # so we must check if info is None |
| 97 | + if info is not None: |
| 98 | + service_infos.append(info) |
| 99 | + |
| 100 | + return service_infos |
| 101 | + |
| 102 | + def _add(self, info: ServiceInfo) -> None: |
| 103 | + """Add a new service under the lock.""" |
| 104 | + lower_name = info.name.lower() |
| 105 | + if lower_name in self.services: |
| 106 | + raise ServiceNameAlreadyRegistered |
| 107 | + |
| 108 | + self.services[lower_name] = info |
| 109 | + self.types.setdefault(info.type, []).append(lower_name) |
| 110 | + self.servers.setdefault(info.server, []).append(lower_name) |
| 111 | + |
| 112 | + def _remove(self, info: ServiceInfo) -> None: |
| 113 | + """Remove a service under the lock.""" |
| 114 | + lower_name = info.name.lower() |
| 115 | + old_service_info = self.services[lower_name] |
| 116 | + self.types[old_service_info.type].remove(lower_name) |
| 117 | + self.servers[old_service_info.server].remove(lower_name) |
| 118 | + del self.services[lower_name] |
0 commit comments