extern crate riker;
use riker::actors::*;
use std::time::Duration;
#[derive(Default)]
struct Child;
impl Actor for Child {
type Msg = String;
fn recv(&mut self, _ctx: &Context<Self::Msg>, msg: Self::Msg, _sender: Sender) {
println!("child got a message {}", msg);
}
}
#[derive(Default)]
struct MyActor {
child: Option<ActorRef<String>>,
}
impl Actor for MyActor {
type Msg = String;
fn pre_start(&mut self, ctx: &Context<Self::Msg>) {
self.child = Some(ctx.actor_of::<Child>("my-child").unwrap());
}
fn recv(&mut self, _ctx: &Context<Self::Msg>, msg: Self::Msg, sender: Sender) {
println!("parent got a message {}", msg);
self.child.as_ref().unwrap().tell(msg, sender);
}
}
fn main() {
let sys = ActorSystem::new().unwrap();
let my_actor = sys.actor_of::<MyActor>("my-actor").unwrap();
my_actor.tell("Hello my actor!".to_string(), None);
println!("Child not added yet");
sys.print_tree();
println!("Child added already");
std::thread::sleep(Duration::from_millis(500));
sys.print_tree();
}