Struct aldrin_broker::Broker

source ·
pub struct Broker { /* private fields */ }
Expand description

Aldrin broker.

This is the central message broker present in every Aldrin bus. After creating a Broker with new, it must be turned into future with run and then polled to completion.

BrokerHandles are used to interact with a running Broker and can be acquired with the handle method. Through a BrokerHandle, you can add new connections to the Broker as well as shut it down again.

The Broker will automatically shut down, when there are no active connections and the last BrokerHandle has been dropped.

Examples

use aldrin_broker::Broker;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a new broker:
    let broker = Broker::new();

    // Acquire a BrokerHandle:
    let mut handle = broker.handle().clone();

    // Run the broker:
    let join = tokio::spawn(broker.run());

    // Add connections to the broker:
    // ...

    // Shut down the broker:
    handle.shutdown().await;
    join.await?;

    Ok(())
}

Implementations§

source§

impl Broker

source

pub fn new() -> Self

Creates a new broker.

After creating a Broker, it must be turned into a future with run and polled to completion.

source

pub fn handle(&self) -> &BrokerHandle

Returns a reference to the broker handle.

It is important to acquire at least one BrokerHandle before running the Broker. BrokerHandles are the only way to add new connections to the Broker.

Note also, that this method returns only a reference. However, BrokerHandles are cheap to clone.

source

pub async fn run(self)

Runs the broker.

This is a long running method, that will only return when explicitly shut down or when there are no connected clients and the last BrokerHandle has been dropped.

Make sure to acquire a BrokerHandle before running the Broker.

Trait Implementations§

source§

impl Debug for Broker

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Broker

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Broker

§

impl Send for Broker

§

impl Sync for Broker

§

impl Unpin for Broker

§

impl !UnwindSafe for Broker

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.