acton_core/traits/
broker.rs

1/*
2 * Copyright (c) 2024. Govcraft
3 *
4 * Licensed under either of
5 *   * Apache License, Version 2.0 (the "License");
6 *     you may not use this file except in compliance with the License.
7 *     You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8 *   * MIT license: http://opensource.org/licenses/MIT
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the applicable License for the specific language governing permissions and
14 * limitations under that License.
15 */
16
17use std::fmt::Debug;
18use std::future::Future;
19
20use async_trait::async_trait;
21
22use crate::message::BrokerRequest;
23use crate::prelude::ActonMessage;
24use crate::traits::Actor;
25
26/// A broker is a message broker that can broadcast messages to all connected clients.
27#[async_trait]
28pub trait Broker: Clone + Debug + Default {
29    /// Broadcast a message from the broker.
30    fn broadcast(&self, message: impl ActonMessage) -> impl Future<Output=()> + Send + Sync + '_;
31    /// Broadcast a message from the broker synchronously.
32    fn broadcast_sync(&self, message: impl ActonMessage) -> anyhow::Result<()>
33    where
34        Self: Actor,
35    {
36        let envelope = self.create_envelope(Some(self.reply_address()));
37        envelope.reply(BrokerRequest::new(message))?;
38        Ok(())
39    }
40}
41
42
43