extern crate riker; use riker::actors::*; use riker::system::ActorSystem; use std::time::Duration; #[derive(Clone, Debug)] pub struct PowerStatus; #[actor(PowerStatus)] struct GpsActor { chan: ChannelRef, } impl ActorFactoryArgs> for GpsActor { fn create_args(chan: ChannelRef) -> Self { GpsActor { chan } } } impl Actor for GpsActor { type Msg = GpsActorMsg; fn pre_start(&mut self, ctx: &Context) { let topic = Topic::from("my-topic"); println!("{}: pre_start subscribe to {:?}", ctx.myself.name(), topic); let sub = Box::new(ctx.myself()); self.chan.tell(Subscribe { actor: sub, topic }, None); } fn recv(&mut self, ctx: &Context, msg: Self::Msg, sender: Sender) { self.receive(ctx, msg, sender); } } impl Receive for GpsActor { type Msg = GpsActorMsg; fn receive(&mut self, ctx: &Context, msg: PowerStatus, _sender: Sender) { println!("{}: -> got msg: {:?}", ctx.myself.name(), msg); } } #[actor(PowerStatus)] struct NavigationActor { chan: ChannelRef, } impl ActorFactoryArgs> for NavigationActor { fn create_args(chan: ChannelRef) -> Self { NavigationActor { chan } } } impl Actor for NavigationActor { type Msg = NavigationActorMsg; fn pre_start(&mut self, ctx: &Context) { let topic = Topic::from("my-topic"); println!("{}: pre_start subscribe to {:?}", ctx.myself.name(), topic); let sub = Box::new(ctx.myself()); self.chan.tell(Subscribe { actor: sub, topic }, None); } fn recv(&mut self, ctx: &Context, msg: Self::Msg, sender: Sender) { self.receive(ctx, msg, sender); } } impl Receive for NavigationActor { type Msg = NavigationActorMsg; fn receive(&mut self, ctx: &Context, msg: PowerStatus, _sender: Sender) { println!("{}: -> got msg: {:?}", ctx.myself.name(), msg); } } fn main() { let sys = ActorSystem::new().unwrap(); let chan: ChannelRef = channel("power-status", &sys).unwrap(); sys.actor_of_args::("gps-actor", chan.clone()) .unwrap(); sys.actor_of_args::("navigation-actor", chan.clone()) .unwrap(); std::thread::sleep(Duration::from_millis(500)); // sys.print_tree(); let topic = Topic::from("my-topic"); println!( "Sending PowerStatus message to all subscribers and {:?}", topic ); chan.tell( Publish { msg: PowerStatus, topic, }, None, ); // sleep another half seconds to process messages std::thread::sleep(Duration::from_millis(500)); sys.print_tree(); }