1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
45use 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;
1213use zx::MonotonicDuration;
1415/// MessengerClient is a wrapper around a messenger with a fuse.
16#[derive(Clone, Debug)]
17pub struct MessengerClient {
18 messenger: Messenger,
19 _fuse: ActionFuseHandle, // Handle that maintains scoped messenger cleanup
20}
2122impl MessengerClient {
23pub(super) fn new(messenger: Messenger, fuse: ActionFuseHandle) -> MessengerClient {
24 MessengerClient { messenger, _fuse: fuse }
25 }
2627/// Creates a MessageBuilder for a new message with the specified payload
28 /// and audience.
29pub(crate) fn message(&self, payload: crate::Payload, audience: Audience) -> Receptor {
30self.message_with_timeout(payload, audience, None)
31 }
3233/// Creates a MessageBuilder for a new message with the specified payload,
34 /// audience, and timeout.
35pub(crate) fn message_with_timeout(
36&self,
37 payload: crate::Payload,
38 audience: Audience,
39 duration: Option<MonotonicDuration>,
40 ) -> Receptor {
41let (beacon, receptor) =
42 BeaconBuilder::new(self.messenger.clone()).set_timeout(duration).build();
43self.messenger.transmit(
44 MessageAction::Send(payload, Attribution::Source(MessageType::Origin(audience))),
45Some(beacon),
46 );
4748 receptor
49 }
5051/// Returns the signature of the client that will handle any sent messages.
52pub fn get_signature(&self) -> Signature {
53self.messenger.get_signature()
54 }
55}
5657/// Messengers provide clients the ability to send messages to other registered
58/// clients. They can only be created through a MessageHub.
59#[derive(Clone, Debug)]
60pub struct Messenger {
61 fingerprint: Fingerprint,
62 action_tx: ActionSender,
63}
6465impl Messenger {
66pub(super) fn new(fingerprint: Fingerprint, action_tx: ActionSender) -> Messenger {
67 Messenger { fingerprint, action_tx }
68 }
6970/// Returns the identification for this Messenger.
71pub(super) fn get_id(&self) -> MessengerId {
72self.fingerprint.id
73 }
7475/// Forwards the message to the next Messenger. Note that this method is
76 /// private and only called through the MessageClient.
77pub(super) fn forward(&self, message: Message, beacon: Option<Beacon>) {
78self.transmit(MessageAction::Forward(message), beacon);
79 }
8081/// Tranmits a given action to the message hub. This is a common utility
82 /// method to be used for immediate actions (forwarding, observing) and
83 /// deferred actions as well (sending, replying).
84pub(super) fn transmit(&self, action: MessageAction, beacon: Option<Beacon>) {
85// Do not transmit if the message hub has exited.
86if self.action_tx.is_closed() {
87return;
88 }
8990// Log info. transmit is called by forward. However, forward might fail if there is no next
91 // Messenger exists.
92self.action_tx.unbounded_send((self.fingerprint, action, beacon)).unwrap_or_else(|_| {
93log::warn!("Messenger::transmit, action_tx failed to send message")
94 });
95 }
9697pub(super) fn get_signature(&self) -> Signature {
98self.fingerprint.signature
99 }
100}