use std::fmt::{self, Debug};
use std::marker::PhantomData;
use acktor::{Actor, Address, Message, utils::ShortName};
use crate::actor_ref::ActorRef;
use crate::error::NodeError;
use crate::ipc_method::{IpcConnection, IpcListener};
use crate::remote::{RemoteAddressable, RemoteSpawnable};
use crate::session::Session;
type Result<T> = std::result::Result<T, NodeError>;
pub struct AddListener<L>(pub L)
where
L: IpcListener;
impl<L> Debug for AddListener<L>
where
L: IpcListener,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple(&ShortName::of::<Self>().to_string())
.field(&self.0.local_endpoint())
.finish()
}
}
impl<L> Message for AddListener<L>
where
L: IpcListener,
{
type Result = bool;
}
#[derive(Debug)]
pub struct RemoveListener(pub String);
impl Message for RemoveListener {
type Result = bool;
}
pub struct Connect<C>
where
C: IpcConnection,
{
pub endpoint: String,
pub session_label: Option<String>,
_marker: PhantomData<fn(C) -> C>,
}
impl<C> Debug for Connect<C>
where
C: IpcConnection,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple(&ShortName::of::<Self>().to_string())
.field(&self.endpoint)
.field(&self.session_label)
.finish()
}
}
impl<C> Message for Connect<C>
where
C: IpcConnection,
{
type Result = Result<Address<Session>>;
}
impl<C> Connect<C>
where
C: IpcConnection,
{
pub fn new<S1, S2>(endpoint: S1, label: S2) -> Self
where
S1: AsRef<str>,
S2: AsRef<str>,
{
Self {
endpoint: endpoint.as_ref().to_string(),
session_label: Some(label.as_ref().to_string()),
_marker: PhantomData,
}
}
pub fn no_label<S>(endpoint: S) -> Self
where
S: AsRef<str>,
{
Self {
endpoint: endpoint.as_ref().to_string(),
session_label: None,
_marker: PhantomData,
}
}
}
pub struct AddActor<A>
where
A: Actor + RemoteAddressable,
{
pub label: String,
pub address: Address<A>,
_marker: PhantomData<fn(A) -> A>,
}
impl<A> Debug for AddActor<A>
where
A: Actor + RemoteAddressable,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple(&ShortName::of::<Self>().to_string())
.field(&self.label)
.field(&self.address.index())
.finish()
}
}
impl<A> Message for AddActor<A>
where
A: Actor + RemoteAddressable,
{
type Result = bool;
}
impl<A> AddActor<A>
where
A: Actor + RemoteAddressable,
{
pub fn new<S>(label: S, address: Address<A>) -> Self
where
S: AsRef<str>,
{
Self {
label: label.as_ref().to_string(),
address,
_marker: PhantomData,
}
}
}
pub struct RemoveActor(pub ActorRef);
impl Debug for RemoveActor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("RemoveActor")
.field(match &self.0 {
ActorRef::Index(index) => index,
ActorRef::Label(label) => label,
})
.finish()
}
}
impl Message for RemoveActor {
type Result = bool;
}
#[derive(Debug)]
pub struct RemoteCreateActor<A>
where
A: Actor + RemoteSpawnable,
{
pub session: ActorRef,
pub label: String,
pub config: Option<String>,
_marker: PhantomData<fn(A) -> A>,
}
impl<A> Message for RemoteCreateActor<A>
where
A: Actor + RemoteSpawnable,
{
type Result = Result<Address<A>>;
}
impl<A> RemoteCreateActor<A>
where
A: Actor + RemoteSpawnable,
{
pub fn new<S>(session: ActorRef, label: S, config: Option<String>) -> Self
where
S: AsRef<str>,
{
Self {
session,
label: label.as_ref().to_string(),
config,
_marker: PhantomData,
}
}
}
#[derive(Debug)]
pub struct RemoteGetActor<A>
where
A: Actor + RemoteAddressable,
{
pub session: ActorRef,
pub actor: ActorRef,
_marker: PhantomData<fn(A) -> A>,
}
impl<A> Message for RemoteGetActor<A>
where
A: Actor + RemoteAddressable,
{
type Result = Result<Address<A>>;
}
impl<A> RemoteGetActor<A>
where
A: Actor + RemoteAddressable,
{
pub fn new(session: ActorRef, actor: ActorRef) -> Self {
Self {
session,
actor,
_marker: PhantomData,
}
}
}