Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add client callbacks #5

Merged
merged 1 commit into from
Dec 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions hivemind_plugin_manager/protocols.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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']:
Expand All @@ -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
Expand Down
Loading