How to use ret_* to send the stop reason to a parent actor? #36
-
As the title already says, how do I tell Stakker to send a message to a parent actor when something goes wrong? I think it should somehow be a |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Nevermind, I figured it out. use std::{
io::Write,
time::{Duration, Instant},
};
use mio::{Events, Poll};
use stakker::*;
use stakker_mio::MioPoll;
struct Animal {
is_cat: bool,
cat: ActorOwn<Cat>,
dog: ActorOwn<Dog>,
}
impl Animal {
fn init(cx: CX![Animal]) -> Option<Self> {
let this = cx.this().clone();
Some(Self {
is_cat: true,
cat: Self::make_cat(cx, this.clone()),
dog: Self::make_dog(cx, this),
})
}
fn make_cat(cx: CX![], parent: Actor<Self>) -> ActorOwn<Cat> {
actor!(
cx,
Cat::init(parent.clone()),
ret_some_to!([parent], child_stopped("cat") as (StopCause))
)
}
fn make_dog(cx: CX![], parent: Actor<Self>) -> ActorOwn<Dog> {
actor!(
cx,
Dog::init(parent.clone()),
ret_some_to!([parent], child_stopped("dog") as (StopCause))
)
}
pub fn child_stopped(&mut self, cx: CX![], who: &str, stop_cause: StopCause) {
match stop_cause {
StopCause::Failed(err) => {
eprintln!("Child {} failed with error: {}", who, err);
match who {
"cat" => self.cat = Self::make_cat(cx, cx.this().clone()),
"dog" => self.dog = Self::make_dog(cx, cx.this().clone()),
_ => (),
}
}
_ => (),
}
}
pub fn sound(&mut self, _cx: CX![]) {
if self.is_cat {
call!([self.cat], sound());
} else {
call!([self.dog], sound());
}
}
pub fn switch(&mut self, _cx: CX![]) {
self.is_cat = !self.is_cat;
}
}
struct Cat {
parent: Actor<Animal>,
count: usize,
}
impl Cat {
fn init(_cx: CX![], parent: Actor<Animal>) -> Option<Self> {
Some(Self { parent, count: 3 })
}
fn sound(&mut self, cx: CX![]) {
if self.count <= 0 {
fail!(cx, "Count reached 0!");
return;
}
self.count -= 1;
println!("Meow");
let mut lock = std::io::stdout().lock();
lock.flush().unwrap();
call!([self.parent], switch());
}
}
struct Dog {
parent: Actor<Animal>,
}
impl Dog {
fn init(_: CX![], parent: Actor<Animal>) -> Option<Self> {
Some(Self { parent })
}
fn sound(&mut self, _cx: CX![]) {
println!("Woof");
let mut lock = std::io::stdout().lock();
lock.flush().unwrap();
call!([self.parent], switch());
}
}
fn main() -> anyhow::Result<()> {
let mut stakker = Stakker::new(Instant::now());
let s = &mut stakker;
let miopoll = MioPoll::new(s, Poll::new()?, Events::with_capacity(1024), 0)?;
// This variable can hold any kind of animal
// let animal: ActorOwn<Animal>;
let animal = actor_new!(s, Animal, ret_nop!());
call!([animal], Animal::init());
let mut idle_pending = s.run(Instant::now(), false);
let mut i = 20;
while s.not_shutdown() {
let maxdur = s.next_wait_max(Instant::now(), Duration::from_secs(1), idle_pending);
let activity = miopoll.poll(maxdur)?;
idle_pending = s.run(Instant::now(), !activity);
call!([animal], sound());
i -= 1;
if i <= 0 {
break;
}
}
Ok(())
} |
Beta Was this translation helpful? Give feedback.
-
Yes, that's it. The |
Beta Was this translation helpful? Give feedback.
-
I think the most confusing thing for me was the |
Beta Was this translation helpful? Give feedback.
Nevermind, I figured it out.