settings/message/
message_client.rs1use crate::message::action_fuse::{ActionFuse, ActionFuseHandle};
6use crate::message::base::{
7 Attribution, Audience, Message, MessageAction, MessageClientId, MessageType, Signature, Status,
8};
9use crate::message::beacon::BeaconBuilder;
10use crate::message::messenger::Messenger;
11use crate::message::receptor::Receptor;
12
13#[derive(Clone, Debug)]
18pub struct MessageClient {
19 id: MessageClientId,
22 message: Message,
25 messenger: Messenger,
27 forwarder: ActionFuseHandle,
30}
31
32impl PartialEq for MessageClient {
33 fn eq(&self, other: &MessageClient) -> bool {
34 other.id == self.id
35 }
36}
37
38impl MessageClient {
39 pub(super) fn new(
40 id: MessageClientId,
41 message: Message,
42 messenger: Messenger,
43 ) -> MessageClient {
44 let fuse_messenger_clone = messenger.clone();
45 let fuse_message_clone = message.clone();
46 MessageClient {
47 id,
48 message,
49 messenger,
50 forwarder: ActionFuse::create(Box::new(move || {
51 fuse_messenger_clone.forward(fuse_message_clone.clone(), None);
52 })),
53 }
54 }
55
56 #[cfg(test)]
57 pub(crate) fn get_modifiers(&self) -> Vec<Signature> {
58 self.message.get_modifiers()
59 }
60
61 pub(crate) fn get_author(&self) -> Signature {
65 self.message.get_author()
66 }
67
68 pub(crate) fn get_audience(&self) -> Audience {
72 match self.message.get_type() {
73 MessageType::Origin(audience) => audience.clone(),
74 MessageType::Reply(message) => Audience::Messenger(message.get_author()),
75 }
76 }
77
78 pub(crate) fn spawn_observer(&mut self) -> Receptor {
80 let (beacon, receptor) = BeaconBuilder::new(self.messenger.clone()).build();
81 self.messenger.forward(self.message.clone(), Some(beacon));
82 ActionFuse::defuse(self.forwarder.clone());
83
84 receptor
85 }
86
87 pub(crate) fn reply(&self, payload: crate::Payload) -> Receptor {
89 self.send(payload, Attribution::Source(MessageType::Reply(Box::new(self.message.clone()))))
90 }
91
92 #[cfg(test)]
94 pub(crate) fn propagate(&self, payload: crate::Payload) -> Receptor {
95 self.send(
96 payload,
97 Attribution::Derived(Box::new(self.message.clone()), self.messenger.get_signature()),
98 )
99 }
100
101 pub(crate) async fn acknowledge(&self) {
103 self.message.report_status(Status::Acknowledged).await;
104 }
105
106 pub(crate) async fn bind_to_recipient(&mut self, fuse: ActionFuseHandle) {
109 self.message.bind_to_author(fuse).await;
110 }
111
112 fn send(&self, payload: crate::Payload, attribution: Attribution) -> Receptor {
113 let (beacon, receptor) = BeaconBuilder::new(self.messenger.clone()).build();
114 self.messenger.transmit(MessageAction::Send(payload, attribution), Some(beacon));
115
116 ActionFuse::defuse(self.forwarder.clone());
117
118 receptor
119 }
120}