-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CAP] Abstraction of actor_connector to go along with runtime factory…
… and runtime abstraction (#3296) * Added Runtime Factory to support multiple implementations * Rename to ComponentEnsemble to ZMQRuntime * rename zmq_runtime * rename zmq_runtime * pre-commit fixes * pre-commit fix * pre-commit fixes and default runtime * pre-commit fixes * Rename constants * Rename Constants * Create interfaces for connectors * pre-commit fixes * pre-commit fixes * pre-commit fixes * lower case file names * rename files to lower _case * rename files to _lowercase * removed _ * Refactored to make Actor zmq agnostic * fix for refactor * fix refactor, circular dependency * pre-commit fixes * document classes * pre-commit ruff * fix ruff issues * ruff fixes * ruff fixes * actor connector documentation * better docs --------- Co-authored-by: Li Jiang <[email protected]> Co-authored-by: Chi Wang <[email protected]> Co-authored-by: Ryan Sweet <[email protected]> Co-authored-by: Eric Zhu <[email protected]>
- Loading branch information
1 parent
8a2a40d
commit 1c5baf0
Showing
38 changed files
with
401 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import Any, Optional, Tuple | ||
|
||
|
||
class IActorConnector(ABC): | ||
""" | ||
Abstract base class for actor connectors. Each runtime will have a different implementation. | ||
Obtain an instance of the correct connector from the runtime by calling the runtime's find_by_xyz | ||
method. | ||
""" | ||
|
||
@abstractmethod | ||
def send_txt_msg(self, msg: str) -> None: | ||
""" | ||
Send a text message to the actor. | ||
Args: | ||
msg (str): The text message to send. | ||
""" | ||
pass | ||
|
||
@abstractmethod | ||
def send_bin_msg(self, msg_type: str, msg: bytes) -> None: | ||
""" | ||
Send a binary message to the actor. | ||
Args: | ||
msg_type (str): The type of the binary message. | ||
msg (bytes): The binary message to send. | ||
""" | ||
pass | ||
|
||
@abstractmethod | ||
def send_proto_msg(self, msg: Any) -> None: | ||
""" | ||
Send a protocol buffer message to the actor. | ||
Args: | ||
msg (Any): The protocol buffer message to send. | ||
""" | ||
pass | ||
|
||
@abstractmethod | ||
def send_recv_proto_msg( | ||
self, msg: Any, num_attempts: int = 5 | ||
) -> Tuple[Optional[str], Optional[str], Optional[bytes]]: | ||
""" | ||
Send a protocol buffer message and receive a response from the actor. | ||
Args: | ||
msg (Any): The protocol buffer message to send. | ||
num_attempts (int, optional): Number of attempts to send and receive. Defaults to 5. | ||
Returns: | ||
Tuple[Optional[str], Optional[str], Optional[bytes]]: A tuple containing the topic, | ||
message type, and response message, or None if no response is received. | ||
""" | ||
pass | ||
|
||
@abstractmethod | ||
def send_recv_msg( | ||
self, msg_type: str, msg: bytes, num_attempts: int = 5 | ||
) -> Tuple[Optional[str], Optional[str], Optional[bytes]]: | ||
""" | ||
Send a binary message and receive a response from the actor. | ||
Args: | ||
msg_type (str): The type of the binary message. | ||
msg (bytes): The binary message to send. | ||
num_attempts (int, optional): Number of attempts to send and receive. Defaults to 5. | ||
Returns: | ||
Tuple[Optional[str], Optional[str], Optional[bytes]]: A tuple containing the topic, | ||
message type, and response message, or None if no response is received. | ||
""" | ||
pass | ||
|
||
@abstractmethod | ||
def close(self) -> None: | ||
""" | ||
Close the actor connector and release any resources. | ||
""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,108 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import List | ||
|
||
from .Actor import Actor | ||
from .ActorConnector import ActorConnector | ||
from .actor_connector import IActorConnector | ||
from .proto.CAP_pb2 import ActorInfo | ||
|
||
|
||
class IMsgActor(ABC): | ||
"""Abstract base class for message based actors.""" | ||
|
||
@abstractmethod | ||
def on_connect(self, runtime: "IRuntime"): | ||
"""Called when the actor connects to the runtime.""" | ||
pass | ||
|
||
@abstractmethod | ||
def on_txt_msg(self, msg: str, msg_type: str, receiver: str, sender: str) -> bool: | ||
"""Handle incoming text messages.""" | ||
pass | ||
|
||
@abstractmethod | ||
def on_bin_msg(self, msg: bytes, msg_type: str, receiver: str, sender: str) -> bool: | ||
"""Handle incoming binary messages.""" | ||
pass | ||
|
||
@abstractmethod | ||
def on_start(self): | ||
"""Called when the actor starts.""" | ||
pass | ||
|
||
@abstractmethod | ||
def stop(self): | ||
"""Stop the actor.""" | ||
pass | ||
|
||
@abstractmethod | ||
def dispatch_message(self, message): | ||
"""Dispatch the received message based on its type.""" | ||
pass | ||
|
||
|
||
class IMessageReceiver(ABC): | ||
"""Abstract base class for message receivers. Implementations are runtime specific.""" | ||
|
||
@abstractmethod | ||
def init(self, actor_name: str): | ||
"""Initialize the message receiver.""" | ||
pass | ||
|
||
@abstractmethod | ||
def add_listener(self, topic: str): | ||
"""Add a topic to the message receiver.""" | ||
pass | ||
|
||
@abstractmethod | ||
def get_message(self): | ||
"""Retrieve a message from the runtime implementation.""" | ||
pass | ||
|
||
@abstractmethod | ||
def stop(self): | ||
"""Stop the message receiver.""" | ||
pass | ||
|
||
|
||
# Abstract base class for the runtime environment | ||
class IRuntime(ABC): | ||
"""Abstract base class for the actor runtime environment.""" | ||
|
||
@abstractmethod | ||
def register(self, actor: IMsgActor): | ||
"""Register an actor with the runtime.""" | ||
pass | ||
|
||
@abstractmethod | ||
def register(self, actor: Actor): | ||
def get_new_msg_receiver(self) -> IMessageReceiver: | ||
"""Create and return a new message receiver.""" | ||
pass | ||
|
||
@abstractmethod | ||
def connect(self): | ||
"""Connect the runtime to the messaging system.""" | ||
pass | ||
|
||
@abstractmethod | ||
def disconnect(self): | ||
"""Disconnect the runtime from the messaging system.""" | ||
pass | ||
|
||
@abstractmethod | ||
def find_by_topic(self, topic: str) -> ActorConnector: | ||
def find_by_topic(self, topic: str) -> IActorConnector: | ||
"""Find an actor connector by topic.""" | ||
pass | ||
|
||
@abstractmethod | ||
def find_by_name(self, name: str) -> ActorConnector: | ||
def find_by_name(self, name: str) -> IActorConnector: | ||
"""Find an actor connector by name.""" | ||
pass | ||
|
||
@abstractmethod | ||
def find_termination(self) -> ActorConnector: | ||
def find_termination(self) -> IActorConnector: | ||
"""Find the termination actor connector.""" | ||
pass | ||
|
||
@abstractmethod | ||
def find_by_name_regex(self, name_regex) -> List[ActorInfo]: | ||
def find_by_name_regex(self, name_regex) -> List["ActorInfo"]: | ||
"""Find actors by name using a regular expression.""" | ||
pass |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from autogencap.actor import Actor | ||
from autogencap.constants import Termination_Topic | ||
from autogencap.debug_log import Debug | ||
|
||
|
||
class AGActor(Actor): | ||
def on_start(self, runtime): | ||
super().on_start(runtime) | ||
str_topic = Termination_Topic | ||
self._msg_receiver.add_listener(str_topic) | ||
Debug(self.actor_name, f"subscribe to: {str_topic}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.