Commit 08162c33 authored by Kevin Perry's avatar Kevin Perry Committed by GitHub

Explicitly cancel automatic discovery renewal (#326)

* Explicitly cancel automatic discovery renewal

* Update handle when rescheduling the callback

* Don't store the handle for the one-shot renewal callback
Co-authored-by: default avatarKevin Perry <perry@coffee-break.at>
parent d79d2049
......@@ -87,6 +87,7 @@ class Server:
self.bserver: Optional[BinaryServer] = None
self._discovery_clients = {}
self._discovery_period = 60
self._discovery_handle = None
self._policies = []
self.nodes = Shortcuts(self.iserver.isession)
# enable all endpoints by default
......@@ -223,16 +224,19 @@ class Server:
if period:
self.loop.call_soon(self._schedule_renew_registration)
def unregister_to_discovery(self, url: str = "opc.tcp://localhost:4840"):
async def unregister_to_discovery(self, url: str = "opc.tcp://localhost:4840"):
"""
stop registration thread
"""
# FIXME: is there really no way to deregister?
self._discovery_clients[url].disconnect()
await self._discovery_clients[url].disconnect()
del self._discovery_clients[url]
if not self._discovery_clients and self._discovery_handle:
self._discovery_handle.cancel()
def _schedule_renew_registration(self):
self.loop.create_task(self._renew_registration())
self.loop.call_later(self._discovery_period, self._schedule_renew_registration)
self._discovery_handle = self.loop.call_later(self._discovery_period, self._schedule_renew_registration)
async def _renew_registration(self):
for client in self._discovery_clients.values():
......@@ -380,6 +384,8 @@ class Server:
"""
Stop server
"""
if self._discovery_handle:
self._discovery_handle.cancel()
if self._discovery_clients:
await asyncio.wait([client.disconnect() for client in self._discovery_clients.values()])
await self.bserver.stop()
......
......@@ -32,6 +32,22 @@ async def test_discovery(server, discovery_server):
assert new_app_uri in [s.ApplicationUri for s in new_servers]
async def test_unregister_discovery(server, discovery_server):
client = Client(discovery_server.endpoint.geturl())
async with client:
new_app_uri = 'urn:freeopcua:python:server:test_discovery2'
await server.set_application_uri(new_app_uri)
# register without automatic renewal
await server.register_to_discovery(discovery_server.endpoint.geturl(), period=0)
await asyncio.sleep(0.1)
# unregister, no automatic renewal to stop
await server.unregister_to_discovery(discovery_server.endpoint.geturl())
# reregister with automatic renewal
await server.register_to_discovery(discovery_server.endpoint.geturl(), period=60)
# unregister, cancel scheduled renewal
await server.unregister_to_discovery(discovery_server.endpoint.geturl())
async def test_find_servers2(server, discovery_server):
client = Client(discovery_server.endpoint.geturl())
async with client:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment