settings/message/
messenger.rs1use crate::message::action_fuse::ActionFuseHandle;
6use crate::message::base::{
7 ActionSender, Attribution, Audience, Fingerprint, Message, MessageAction, MessageType,
8 MessengerId, Signature,
9};
10use crate::message::beacon::{Beacon, BeaconBuilder};
11use crate::message::receptor::Receptor;
12
13use zx::MonotonicDuration;
14
15#[derive(Clone, Debug)]
17pub struct MessengerClient {
18 messenger: Messenger,
19 _fuse: ActionFuseHandle, }
21
22impl MessengerClient {
23 pub(super) fn new(messenger: Messenger, fuse: ActionFuseHandle) -> MessengerClient {
24 MessengerClient { messenger, _fuse: fuse }
25 }
26
27 pub(crate) fn message(&self, payload: crate::Payload, audience: Audience) -> Receptor {
30 self.message_with_timeout(payload, audience, None)
31 }
32
33 pub(crate) fn message_with_timeout(
36 &self,
37 payload: crate::Payload,
38 audience: Audience,
39 duration: Option<MonotonicDuration>,
40 ) -> Receptor {
41 let (beacon, receptor) =
42 BeaconBuilder::new(self.messenger.clone()).set_timeout(duration).build();
43 self.messenger.transmit(
44 MessageAction::Send(payload, Attribution::Source(MessageType::Origin(audience))),
45 Some(beacon),
46 );
47
48 receptor
49 }
50
51 pub fn get_signature(&self) -> Signature {
53 self.messenger.get_signature()
54 }
55}
56
57#[derive(Clone, Debug)]
60pub struct Messenger {
61 fingerprint: Fingerprint,
62 action_tx: ActionSender,
63}
64
65impl Messenger {
66 pub(super) fn new(fingerprint: Fingerprint, action_tx: ActionSender) -> Messenger {
67 Messenger { fingerprint, action_tx }
68 }
69
70 pub(super) fn get_id(&self) -> MessengerId {
72 self.fingerprint.id
73 }
74
75 pub(super) fn forward(&self, message: Message, beacon: Option<Beacon>) {
78 self.transmit(MessageAction::Forward(message), beacon);
79 }
80
81 pub(super) fn transmit(&self, action: MessageAction, beacon: Option<Beacon>) {
85 if self.action_tx.is_closed() {
87 return;
88 }
89
90 self.action_tx.unbounded_send((self.fingerprint, action, beacon)).unwrap_or_else(|_| {
93 log::warn!("Messenger::transmit, action_tx failed to send message")
94 });
95 }
96
97 pub(super) fn get_signature(&self) -> Signature {
98 self.fingerprint.signature
99 }
100}