Skip to content

Commit

Permalink
feat: add client callbacks (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl authored Dec 29, 2024
1 parent 8f80b29 commit cafbf15
Showing 1 changed file with 26 additions and 2 deletions.
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

0 comments on commit cafbf15

Please sign in to comment.