Skip to main content

acktor_ipc/node/
command.rs

1//! Commands which can be used to interact with a node.
2//!
3
4use std::fmt::{self, Debug};
5use std::marker::PhantomData;
6
7use acktor::{ActorId, Address, Message};
8
9use crate::actor_handle::ActorHandle;
10use crate::errors::NodeError;
11use crate::ipc_method::{IpcConnection, IpcListener};
12use crate::remote_actor::RemoteActor;
13use crate::remote_address::RemoteAddress;
14use crate::session::{Session, SessionHandle};
15
16type Result<T> = std::result::Result<T, NodeError>;
17
18/// A command which is used to add an IPC listener to a node.
19///
20/// A node can hold multiple listeners at once so that it can accept inbound connections on
21/// several endpoints in parallel.
22#[derive(Message)]
23#[result_type(bool)]
24pub struct AddListener<L>(pub L)
25where
26    L: IpcListener;
27
28impl<L> Debug for AddListener<L>
29where
30    L: IpcListener,
31{
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        f.debug_tuple(&format!("{}", acktor::utils::ShortName::of::<Self>()))
34            .field(&format_args!("{}", self.0.local_endpoint()))
35            .finish()
36    }
37}
38
39/// A command which is used to remove an IPC listener from a node.
40///
41/// The listener is identified by its local endpoint.
42#[derive(Debug, Message)]
43#[result_type(bool)]
44pub struct RemoveListener(pub String);
45
46/// A command which is used to add an actor to a node.
47#[derive(Message)]
48#[result_type(bool)]
49pub struct AddActor<A>(pub Address<A>)
50where
51    A: RemoteActor;
52
53impl<A> Debug for AddActor<A>
54where
55    A: RemoteActor,
56{
57    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58        f.debug_tuple(&format!("{}", acktor::utils::ShortName::of::<Self>()))
59            .field(&format_args!("{}", self.0.index()))
60            .finish()
61    }
62}
63
64/// A command which is used to remove an actor from a node.
65#[derive(Debug, Message)]
66#[result_type(bool)]
67pub struct RemoveActor(pub ActorId);
68
69/// A command which is used by a node to actively connect to another node like a client.
70///
71/// A new session will be created if the operation is successful. The endpoint of the connection
72/// will be used as the actor label of the new session actor. The user can provide a
73/// `session_label` as an alias to the endpoint, both labels can be used to refer to the session
74/// actor in the other commands.
75///
76/// The command will return the address if it succeeds, however users are not recommended to await
77/// the result. The recommended way is to listen to the
78/// [`NodeEvent::SessionCreated`][crate::node::NodeEvent::SessionCreated] event which is emitted
79/// when a session is created, and get the session address from the event.
80#[derive(Message)]
81#[result_type(Result<Address<Session>>)]
82pub struct Connect<C>
83where
84    C: IpcConnection,
85{
86    pub endpoint: String,
87    pub session_label: Option<String>,
88    pub _phantom: PhantomData<fn() -> C>,
89}
90
91impl<C> Debug for Connect<C>
92where
93    C: IpcConnection,
94{
95    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96        f.debug_tuple(&format!("{}", acktor::utils::ShortName::of::<Self>()))
97            .field(&self.endpoint)
98            .field(&self.session_label)
99            .finish()
100    }
101}
102
103impl<C> Connect<C>
104where
105    C: IpcConnection,
106{
107    /// Constructs a new [`Connect`] command for the connection type `C`.
108    pub fn new(endpoint: String, session_label: Option<String>) -> Self {
109        Self {
110            endpoint,
111            session_label,
112            _phantom: PhantomData,
113        }
114    }
115}
116
117/// A command which is used to create an actor in a remote node.
118///
119/// The remote node needs to know how to create the actor with the given type and config. If
120/// the operation is successful, the provided `label` will be used as the actor label of the
121/// new actor created in the remote node.
122#[derive(Debug, Message)]
123#[result_type(Result<RemoteAddress>)]
124pub struct CreateRemoteActor {
125    pub session: SessionHandle,
126    pub label: String,
127    pub r#type: String,
128    pub config: String,
129}
130
131/// A command which is used to get the address of an actor in a remote node.
132#[derive(Debug, Message)]
133#[result_type(Result<RemoteAddress>)]
134pub struct GetRemoteActor {
135    pub session: SessionHandle,
136    pub actor: ActorHandle,
137}