settings/message/
message_client.rsuse crate::message::action_fuse::{ActionFuse, ActionFuseHandle};
use crate::message::base::{
Attribution, Audience, Message, MessageAction, MessageClientId, MessageType, Signature, Status,
};
use crate::message::beacon::BeaconBuilder;
use crate::message::messenger::Messenger;
use crate::message::receptor::Receptor;
#[derive(Clone, Debug)]
pub struct MessageClient {
id: MessageClientId,
message: Message,
messenger: Messenger,
forwarder: ActionFuseHandle,
}
impl PartialEq for MessageClient {
fn eq(&self, other: &MessageClient) -> bool {
other.id == self.id
}
}
impl MessageClient {
pub(super) fn new(
id: MessageClientId,
message: Message,
messenger: Messenger,
) -> MessageClient {
let fuse_messenger_clone = messenger.clone();
let fuse_message_clone = message.clone();
MessageClient {
id,
message,
messenger,
forwarder: ActionFuse::create(Box::new(move || {
fuse_messenger_clone.forward(fuse_message_clone.clone(), None);
})),
}
}
#[cfg(test)]
pub(crate) fn get_modifiers(&self) -> Vec<Signature> {
self.message.get_modifiers()
}
pub(crate) fn get_author(&self) -> Signature {
self.message.get_author()
}
pub(crate) fn get_audience(&self) -> Audience {
match self.message.get_type() {
MessageType::Origin(audience) => audience.clone(),
MessageType::Reply(message) => Audience::Messenger(message.get_author()),
}
}
pub(crate) fn spawn_observer(&mut self) -> Receptor {
let (beacon, receptor) = BeaconBuilder::new(self.messenger.clone()).build();
self.messenger.forward(self.message.clone(), Some(beacon));
ActionFuse::defuse(self.forwarder.clone());
receptor
}
pub(crate) fn reply(&self, payload: crate::Payload) -> Receptor {
self.send(payload, Attribution::Source(MessageType::Reply(Box::new(self.message.clone()))))
}
#[cfg(test)]
pub(crate) fn propagate(&self, payload: crate::Payload) -> Receptor {
self.send(
payload,
Attribution::Derived(Box::new(self.message.clone()), self.messenger.get_signature()),
)
}
pub(crate) async fn acknowledge(&self) {
self.message.report_status(Status::Acknowledged).await;
}
pub(crate) async fn bind_to_recipient(&mut self, fuse: ActionFuseHandle) {
self.message.bind_to_author(fuse).await;
}
fn send(&self, payload: crate::Payload, attribution: Attribution) -> Receptor {
let (beacon, receptor) = BeaconBuilder::new(self.messenger.clone()).build();
self.messenger.transmit(MessageAction::Send(payload, attribution), Some(beacon));
ActionFuse::defuse(self.forwarder.clone());
receptor
}
}