use std::fmt::Debug; use std::future::Future;
use acton_ern::Ern;
use async_trait::async_trait;
use dashmap::DashMap;
use tokio_util::task::TaskTracker;
use tracing::{instrument, trace};
use crate::common::*; use crate::message::{BrokerRequest, MessageAddress}; use crate::traits::acton_message::ActonMessage;
#[async_trait]
pub trait AgentHandleInterface: Send + Sync + Debug + Clone + 'static {
fn reply_address(&self) -> MessageAddress;
fn create_envelope(&self, recipient_address: Option<MessageAddress>) -> OutboundEnvelope;
fn children(&self) -> DashMap<String, AgentHandle>;
fn find_child(&self, id: &Ern) -> Option<AgentHandle>;
fn tracker(&self) -> TaskTracker;
fn id(&self) -> Ern;
fn name(&self) -> String;
fn clone_ref(&self) -> AgentHandle;
#[instrument(skip(self, message), fields(message_type = std::any::type_name_of_val(&message)))]
fn send(&self, message: impl ActonMessage) -> impl Future<Output = ()> + Send + Sync + '_
where
Self: Sync, {
async move {
let envelope = self.create_envelope(Some(self.reply_address()));
trace!(sender = %self.id(), recipient = %self.id(), "Default send implementation");
envelope.send(message).await;
}
}
fn send_sync(&self, message: impl ActonMessage, recipient: &AgentHandle) -> anyhow::Result<()>
where
Self: Sized, {
trace!(sender = %self.id(), recipient = %recipient.id(), "Sending message synchronously");
let envelope = self.create_envelope(Some(recipient.reply_address()));
envelope.reply(BrokerRequest::new(message))?; Ok(())
}
fn stop(&self) -> impl Future<Output = anyhow::Result<()>> + Send + Sync + '_;
}