Skip to content

Commit

Permalink
Merge pull request #79 from bastion-rs/getting-started-example
Browse files Browse the repository at this point in the history
Copied the example from Bastion's documentation into its own file.
  • Loading branch information
r3v2d0g authored Nov 8, 2019
2 parents 75a3826 + df916ee commit 74cb344
Showing 1 changed file with 130 additions and 0 deletions.
130 changes: 130 additions & 0 deletions bastion/examples/getting_started.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
use bastion::prelude::*;

fn main() {
// Initializing the system (this is required)...
Bastion::init();

// Starting the system...
Bastion::start();

// Creating a new supervisor...
let supervisor = Bastion::supervisor(|sp| {
sp
// ...with a specific supervision strategy...
.with_strategy(SupervisionStrategy::OneForAll)
// ...and some supervised children groups...
.children(|children| {
// ...
children
})
.children(|children| {
// ...
children
})
// ...or even supervised supervisors...
.supervisor(|sp| {
// ...
sp
})
})
.expect("Couldn't create the supervisor.");

// ...which can start supervising new children groups
// later on...
supervisor
.children(|children| {
// ...
children
})
.expect("Couldn't create the supervised children group.");

// ...or broadcast messages to all its supervised children
// and supervisors...
supervisor
.broadcast("A message containing data.")
.expect("Couldn't broadcast the message.");

// ...and then can even be stopped or killed...
supervisor.stop().expect("Couldn't stop the supervisor");
// supervisor.kill().expect("Couldn't kill the supervisor");

// Creating a new top-level children group...
let children = Bastion::children(|children| {
children
// ...containing a defined number of elements...
.with_redundancy(4)
// ...all executing a similar future...
.with_exec(|ctx: BastionContext| {
async move {
// ...receiving and matching messages...
msg! { ctx.recv().await?,
ref msg: &'static str => {
// ...
};
msg: &'static str => {
// ...
};
msg: &'static str =!> {
// ...
};
// ...
_: _ => ();
}

// ...

Ok(())
}
})
})
.expect("Couldn't create the children group.");

// ...which can broadcast messages to all its elements...
children
.broadcast("A message containing data.")
.expect("Couldn't broadcast the message.");

// ...and then can even be stopped or killed...
children.stop().expect("Couldn't stop the children group.");
// children.kill().expect("Couldn't kill the children group.");

// Create a new top-level children group and getting a list
// of reference to its elements...
let children = Bastion::children(|children| {
// ...
children
})
.expect("Couldn't create the children group.");
let elems: &[ChildRef] = children.elems();

// ...to then get one of its elements' reference...
let child = &elems[0];

// ...to then "tell" it messages...
child
.tell("A message containing data.")
.expect("Couldn't send the message.");

// ...or "ask" it messages...
let answer: Answer = child
.ask("A message containing data.")
.expect("Couldn't send the message.");
async {
// ...until the child eventually answers back...
let answer: Result<Msg, ()> = answer.await;
};

// ...and then even stop or kill it...
child.stop().expect("Couldn't stop the child.");
// child.kill().expect("Couldn't kill the child.");

// Broadcasting a message to all the system's children...
Bastion::broadcast("A message containing data.").expect("Couldn't send the message.");

// Stopping or killing the system...
Bastion::stop();
// Bastion::kill();

// Blocking until the system has stopped (or got killed)...
Bastion::block_until_stopped();
}

0 comments on commit 74cb344

Please sign in to comment.