use crate::actors::proxy::ActorReport;
use crate::services::handle::{Listen, Serve};
use crate::services::service::Service;
use crate::system_director::SystemDirector;
use crate::{Actor, Receive, Respond};
use async_std::task;
use lazy_static::lazy_static;
use std::any::TypeId;
use std::fmt::Debug;
lazy_static! {
static ref SYSTEM_DIRECTOR: SystemDirector = SystemDirector::new();
}
pub struct Acteur {
system_director: SystemDirector,
}
impl Default for Acteur {
fn default() -> Self {
Acteur::new()
}
}
impl Acteur {
pub fn new() -> Acteur {
Acteur {
system_director: SYSTEM_DIRECTOR.clone(),
}
}
pub async fn send_to_actor<A: Actor + Receive<M>, M: Debug + Send + 'static>(
&self,
actor_id: A::Id,
message: M,
) {
self.system_director
.send_to_actor::<A, M>(actor_id, message)
.await;
}
pub fn send_to_actor_sync<A: Actor + Receive<M>, M: Debug + Send + 'static>(
&self,
actor_id: A::Id,
message: M,
) {
task::block_on(async move { self.send_to_actor::<A, M>(actor_id, message).await })
}
pub async fn schedule_send_to_actor<A: Actor + Receive<M>, M: Debug + Send + 'static>(
&self,
actor_id: A::Id,
duration: std::time::Duration,
message: M,
) {
self.system_director
.schedule_send_to_actor::<A, M>(actor_id, duration, message)
.await;
}
pub fn schedule_send_to_actor_sync<A: Actor + Receive<M>, M: Debug + Send + 'static>(
&self,
actor_id: A::Id,
duration: std::time::Duration,
message: M,
) {
task::block_on(async move {
self.schedule_send_to_actor::<A, M>(actor_id, duration, message)
.await
})
}
pub async fn send_to_all_actors<A: Actor + Receive<M>, M: Debug + Send + 'static>(
&self,
message: M,
) {
self.system_director
.send_to_all_actors::<A, M>(message)
.await;
}
pub async fn send_to_all_actors_sync<A: Actor + Receive<M>, M: Debug + Send + 'static>(
&self,
message: M,
) {
task::block_on(async move { self.send_to_all_actors::<A, M>(message).await })
}
pub async fn schedule_send_to_all_actors<A: Actor + Receive<M>, M: Debug + Send + 'static>(
&self,
duration: std::time::Duration,
message: M,
) {
self.system_director
.schedule_send_to_all_actors::<A, M>(duration, message)
.await;
}
pub async fn schedule_send_to_all_actors_sync<
A: Actor + Receive<M>,
M: Debug + Send + 'static,
>(
&self,
duration: std::time::Duration,
message: M,
) {
task::block_on(async move {
self.schedule_send_to_all_actors::<A, M>(duration, message)
.await
})
}
pub async fn call_actor<A: Actor + Respond<M>, M: Debug + Send + 'static>(
&self,
actor_id: A::Id,
message: M,
) -> Result<<A as Respond<M>>::Response, &str> {
self.system_director
.call_actor::<A, M>(actor_id, message)
.await
}
pub fn call_actor_sync<A: Actor + Respond<M>, M: Debug + Send + 'static>(
&self,
actor_id: A::Id,
message: M,
) -> Result<<A as Respond<M>>::Response, &str> {
task::block_on(async move { self.call_actor::<A, M>(actor_id, message).await })
}
pub async fn send_to_service<S: Service + Listen<M>, M: Debug + Send + 'static>(
&self,
message: M,
) {
self.system_director.send_to_service::<S, M>(message).await;
}
pub fn send_to_service_sync<S: Service + Listen<M>, M: Debug + Send + 'static>(
&self,
message: M,
) {
task::block_on(async move { self.send_to_service::<S, M>(message).await })
}
pub async fn call_service<S: Service + Serve<M>, M: Debug + Send + 'static>(
&self,
message: M,
) -> Result<<S as Serve<M>>::Response, &str> {
self.system_director.call_service::<S, M>(message).await
}
pub fn call_service_sync<S: Service + Serve<M>, M: Debug + Send + 'static>(
&self,
message: M,
) -> Result<<S as Serve<M>>::Response, &str> {
task::block_on(async move { self.call_service::<S, M>(message).await })
}
pub fn stop(&self) {
let system = self.system_director.clone();
task::spawn(async move {
system.stop().await;
});
}
pub async fn preload_service<S: Service>(&self) {
self.system_director.preload_service::<S>().await;
}
pub fn preload_service_sync<S: Service>(&self) {
let system = self.system_director.clone();
task::block_on(async move {
system.preload_service::<S>().await;
});
}
pub fn wait_until_stopped(&self) {
task::block_on(async { self.system_director.wait_until_stopped().await });
}
pub fn get_statistics(&self) -> Vec<(TypeId, Vec<ActorReport>)> {
self.system_director.get_statistics()
}
pub async fn publish<M: Send + Clone + 'static>(&mut self, message: M) {
self.system_director.publish(message).await
}
pub fn publish_sync<M: Send + Clone + 'static>(&mut self, message: M) {
task::block_on(async { self.system_director.publish(message).await });
}
}
impl Debug for Acteur {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Acteur ()")
}
}
impl Clone for Acteur {
fn clone(&self) -> Self {
Acteur {
system_director: self.system_director.clone(),
}
}
}