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, SessionRef};
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(endpoint: String, session_label: Option<String>) -> Self {
Self {
endpoint,
session_label,
_marker: PhantomData,
}
}
}
pub struct AddActor<A>
where
A: Actor + RemoteAddressable,
{
pub label: String,
pub address: Address<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;
}
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: SessionRef,
pub label: String,
pub config: 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(session: SessionRef, label: String, config: String) -> Self {
Self {
session,
label,
config,
_marker: PhantomData,
}
}
}
#[derive(Debug)]
pub struct RemoteGetActor<A>
where
A: Actor + RemoteAddressable,
{
pub session: SessionRef,
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: SessionRef, actor: ActorRef) -> Self {
Self {
session,
actor,
_marker: PhantomData,
}
}
}