use std::fmt::Debug;
use std::io::ErrorKind;
use bytes::Bytes;
use crate::actor::{ActorRef, BoxActorProd};
use crate::actors::{Evt, Topic};
use crate::system::{LogEntry, IoType, IoAddress};
#[derive(Debug, Clone)]
#[doc(hidden)]
pub enum Enqueued<T: Message> {
ActorMsg(Envelope<T>),
SystemMsg(SystemEnvelope<T>)
}
#[derive(Debug, Clone)]
pub struct Envelope<T: Message> {
pub sender: Option<ActorRef<T>>,
pub msg: ActorMsg<T>,
}
#[derive(Clone, Debug)]
pub struct SystemEnvelope<T: Message> {
pub sender: Option<ActorRef<T>>,
pub msg: SystemMsg<T>,
}
#[derive(Debug, Clone)]
pub enum ActorMsg<Msg: Message> {
User(Msg),
Channel(ChannelMsg<Msg>),
IO(IOMsg<Msg>),
ES(ESMsg<Msg>),
CQ(CQMsg<Msg>),
Identify,
Info(Info),
DeadLetter(Box<DeadLetter<Msg>>),
Tick,
}
#[derive(Debug, Clone)]
pub enum ChannelMsg<Msg: Message> {
Publish(Topic, Msg),
PublishEvent(SystemEvent<Msg>),
PublishDeadLetter(Box<DeadLetter<Msg>>),
Subscribe(Topic, ActorRef<Msg>),
Unsubscribe(Topic, ActorRef<Msg>),
UnsubscribeAll(ActorRef<Msg>),
}
impl<Msg: Message> Into<ActorMsg<Msg>> for ChannelMsg<Msg> {
fn into(self) -> ActorMsg<Msg> {
ActorMsg::Channel(self)
}
}
#[derive(Debug, Clone)]
pub enum IOMsg<Msg: Message> {
Manage(IoType, BoxActorProd<Msg>),
Bind(IoType, IoAddress, ActorRef<Msg>),
Bound(IoAddress),
Unbind,
Unbound(IoAddress),
Connect(IoType, IoAddress),
Connected(IoAddress, IoAddress),
Register(ActorRef<Msg>),
Close,
Closed,
Ready,
Write(Bytes),
Send(IoType),
TryRead,
Received(Bytes),
Flush,
Failed(ErrorKind),
}
impl<Msg: Message> Into<ActorMsg<Msg>> for IOMsg<Msg> {
fn into(self) -> ActorMsg<Msg> {
ActorMsg::IO(self)
}
}
#[derive(Debug, Clone)]
pub enum ESMsg<Msg: Message> {
Persist(Evt<Msg>, String, String, Option<ActorRef<Msg>>),
Load(String, String),
LoadResult(Vec<Msg>),
}
impl<Msg: Message> Into<ActorMsg<Msg>> for ESMsg<Msg> {
fn into(self) -> ActorMsg<Msg> {
ActorMsg::ES(self)
}
}
#[derive(Clone, Debug)]
pub enum CQMsg<Msg: Message> {
Cmd(String, Msg),
}
impl<Msg: Message> Into<ActorMsg<Msg>> for CQMsg<Msg> {
fn into(self) -> ActorMsg<Msg> {
ActorMsg::CQ(self)
}
}
pub struct Identify;
impl<Msg: Message> Into<ActorMsg<Msg>> for Identify {
fn into(self) -> ActorMsg<Msg> {
ActorMsg::Identify
}
}
#[derive(Debug, Clone)]
pub struct Info;
impl<Msg: Message> Into<ActorMsg<Msg>> for Info {
fn into(self) -> ActorMsg<Msg> {
ActorMsg::Info(self)
}
}
#[derive(Clone, Debug)]
pub enum SystemMsg<Msg: Message> {
ActorInit,
ActorCmd(ActorCmd),
Event(SystemEvent<Msg>),
Failed(ActorRef<Msg>),
Persisted(Msg, Option<ActorRef<Msg>>),
Replay(Vec<Msg>),
Log(LogEntry),
}
#[derive(Clone, Debug)]
pub enum ActorCmd {
Stop,
Restart,
}
#[derive(Clone, Debug)]
pub enum SystemEvent<Msg: Message> {
ActorTerminated(ActorRef<Msg>),
ActorRestarted(ActorRef<Msg>),
ActorCreated(ActorRef<Msg>),
}
#[derive(Clone, Debug)]
pub enum SystemEventType {
ActorTerminated,
ActorRestarted,
ActorCreated,
}
#[derive(Clone, Debug)]
pub struct DeadLetter<Msg: Message> {
pub msg: ActorMsg<Msg>,
pub sender: String,
pub recipient: String,
}
impl<Msg: Message> Into<ActorMsg<Msg>> for DeadLetter<Msg> {
fn into(self) -> ActorMsg<Msg> {
ActorMsg::DeadLetter(Box::new(self))
}
}
impl Into<ActorMsg<String>> for String {
fn into(self) -> ActorMsg<String> {
ActorMsg::User(self)
}
}
impl<'a> Into<ActorMsg<String>> for &'a str {
fn into(self) -> ActorMsg<String> {
ActorMsg::User(self.to_string())
}
}
impl Into<ActorMsg<u32>> for u32 {
fn into(self) -> ActorMsg<u32> {
ActorMsg::User(self)
}
}
unsafe impl<T: Message> Send for Envelope<T> {}
pub trait Message: Debug + Clone + Send + Into<ActorMsg<Self>> + 'static {}
impl<T: Debug + Clone + Send + Into<ActorMsg<Self>> + 'static> Message for T {}