logo

Struct bastion::distributor::Distributor[][src]

pub struct Distributor(_);
Expand description

The Distributor is the main message passing mechanism we will use. it provides methods that will allow us to send messages and add/remove actors to the Distribution list

Implementations

Create a new distributor to send messages to

Example
let distributor = Distributor::named("my target group");
// distributor is now ready to use

Ask a question to a recipient attached to the Distributor and wait for a reply.

This can be achieved manually using a MessageHandler and ask_one. Ask a question to a recipient attached to the Distributor


// attach a named distributor to the children
children
    .with_distributor(Distributor::named("my distributor"))
    .with_exec(|ctx: BastionContext| {
       async move {
           loop {
               // The message handler needs an `on_question` section
               // that matches the `question` you're going to send,
               // and that will reply with the Type the request expects.
               // In our example, we ask a `&str` question, and expect a `bool` reply.                    
               MessageHandler::new(ctx.recv().await?)
                   .on_question(|message: &str, sender| {
                       if message == "is it raining today?" {
                           sender.reply(true).unwrap();
                       }
                   });
           }
           Ok(())
       }
    })

let distributor = Distributor::named("my distributor");

let reply: Result<String, SendError> = distributor
    .request("is it raining today?")
    .await
    .expect("couldn't receive reply");

Ask a question to a recipient attached to the Distributor and wait for a reply.

this is the sync variant of the request function, backed by a futures::channel::oneshot

Example
// attach a named distributor to the children
children
    .with_distributor(Distributor::named("my distributor"))
    .with_exec(|ctx: BastionContext| {
       async move {
           loop {
               // The message handler needs an `on_question` section
               // that matches the `question` you're going to send,
               // and that will reply with the Type the request expects.
               // In our example, we ask a `&str` question, and expect a `bool` reply.                    
               MessageHandler::new(ctx.recv().await?)
                   .on_question(|message: &str, sender| {
                       if message == "is it raining today?" {
                           sender.reply(true).unwrap();
                       }
                   });
           }
           Ok(())
       }
    })

let distributor = Distributor::named("my distributor");

let reply: Result<bool, SendError> = distributor
   .request_sync("is it raining today?")
   .recv()
   .expect("couldn't receive reply"); // Ok(true)

Ask a question to a recipient attached to the Distributor

Example
    children
        .with_redundancy(1)
        .with_distributor(Distributor::named("my distributor"))
        .with_exec(|ctx: BastionContext| { // ...
        })

let distributor = Distributor::named("my distributor");

let answer: Answer = distributor.ask_one("hello?").expect("couldn't send question");

Ask a question to all recipients attached to the Distributor

Requires a Message that implements Clone. (it will be cloned and passed to each recipient)

Example

let distributor = Distributor::named("my distributor");

let answer: Vec<Answer> = distributor.ask_everyone("hello?".to_string()).expect("couldn't send question");

Send a Message to a recipient attached to the Distributor

Example

let distributor = Distributor::named("my distributor");

let answer: () = distributor.tell_one("hello?").expect("couldn't send question");

Send a Message to each recipient attached to the Distributor

Requires a Message that implements Clone. (it will be cloned and passed to each recipient)

Example

let distributor = Distributor::named("my distributor");

let answer: () = distributor.tell_one("hello?").expect("couldn't send question");

subscribe a ChildRef to the named Distributor

let child_ref = children.elems()[0].clone();

let distributor = Distributor::named("my distributor");

// child_ref will now be elligible to receive messages dispatched through distributor
distributor.subscribe(child_ref).expect("couldn't subscribe child to distributor");

unsubscribe a ChildRef to the named Distributor

let child_ref = children.elems()[0].clone();

let distributor = Distributor::named("my distributor");

// child_ref will not receive messages dispatched through the distributor anymore
distributor.unsubscribe(child_ref).expect("couldn't unsubscribe child to distributor");

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Performs the conversion.

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Downcast implemented type to Any. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more