From cafbf1509854fa49bac0dbc3f30c50c03028c746 Mon Sep 17 00:00:00 2001 From: JarbasAI <33701864+JarbasAl@users.noreply.github.com> Date: Sun, 29 Dec 2024 02:41:08 +0000 Subject: [PATCH] feat: add client callbacks (#5) --- hivemind_plugin_manager/protocols.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/hivemind_plugin_manager/protocols.py b/hivemind_plugin_manager/protocols.py index 0a3f971..5f3bed4 100644 --- a/hivemind_plugin_manager/protocols.py +++ b/hivemind_plugin_manager/protocols.py @@ -1,7 +1,7 @@ import abc import dataclasses from dataclasses import dataclass -from typing import Dict, Any, Union, Optional +from typing import Dict, Any, Union, Optional, Callable from ovos_bus_client import MessageBusClient from ovos_utils.fakebus import FakeBus @@ -10,11 +10,33 @@ from hivemind_bus_client.identity import NodeIdentity +def on_disconnect(client: 'HiveMindClientConnection'): + LOG.debug(f"callback: client disconnected: {client}") + +def on_connect(client: 'HiveMindClientConnection'): + LOG.debug(f"callback: client connected: {client}") + +def on_invalid_key(client: 'HiveMindClientConnection'): + LOG.debug(f"callback: invalid access key: {client}") + +def on_invalid_protocol(client: 'HiveMindClientConnection'): + LOG.debug(f"callback: protocol requirements failure: {client}") + + +@dataclass +class ClientCallbacks: + on_connect: Callable[['HiveMindClientConnection'], None] = on_connect + on_disconnect: Callable[['HiveMindClientConnection'], None] = on_disconnect + on_invalid_key: Callable[['HiveMindClientConnection'], None] = on_invalid_key + on_invalid_protocol: Callable[['HiveMindClientConnection'], None] = on_invalid_protocol + + @dataclass class _SubProtocol: """base class all protocols derive from""" config: Dict[str, Any] = dataclasses.field(default_factory=dict) hm_protocol: Optional['HiveMindListenerProtocol'] = None + callbacks: ClientCallbacks = dataclasses.field(default_factory=ClientCallbacks) @property def identity(self) -> NodeIdentity: @@ -42,13 +64,14 @@ class AgentProtocol(_SubProtocol): config: Dict[str, Any] = dataclasses.field(default_factory=dict) hm_protocol: Optional['HiveMindListenerProtocol'] = None # usually AgentProtocol is passed as kwarg to hm_protocol # and only then assigned in hm_protocol.__post_init__ - + callbacks: ClientCallbacks = dataclasses.field(default_factory=ClientCallbacks) @dataclass class NetworkProtocol(_SubProtocol): """protocol to transport HiveMessage objects around""" config: Dict[str, Any] = dataclasses.field(default_factory=dict) hm_protocol: Optional['HiveMindListenerProtocol'] = None + callbacks: ClientCallbacks = dataclasses.field(default_factory=ClientCallbacks) @property def agent_protocol(self) -> Optional['AgentProtocol']: @@ -68,6 +91,7 @@ class BinaryDataHandlerProtocol(_SubProtocol): hm_protocol: Optional['HiveMindListenerProtocol'] = None # usually BinaryDataHandlerProtocol is passed as kwarg to hm_protocol # and only then assigned in hm_protocol.__post_init__ agent_protocol: Optional['AgentProtocol'] = None + callbacks: ClientCallbacks = dataclasses.field(default_factory=ClientCallbacks) def __post_init__(self): # NOTE: the most common scenario is having self.agent_protocol but not having self.hm_protocol yet