blob: b817ac24fda86253c572143158177c89ee9031bf [file] [log] [blame]
Adam Snaider43516782023-06-26 15:14:18 -07001use aos_configuration as config;
2use aos_events_event_loop_runtime::{EventLoopRuntime, Sender, Watcher};
3use aos_events_shm_event_loop::ShmEventLoop;
4use core::future::Future;
5use futures::never::Never;
6use std::path::Path;
7
8use ping_rust_fbs::aos::examples as ping;
9use pong_rust_fbs::aos::examples as pong;
10
11fn main() {
12 aos_init::init();
13 let config = config::read_config_from(Path::new("pingpong_config.json")).unwrap();
14 ShmEventLoop::new(&config).run_with(|runtime| {
15 let task = pong(runtime);
16 runtime.spawn(task);
17 });
18}
19
20/// Responds to ping messages with an equivalent pong.
21fn pong(event_loop: &mut EventLoopRuntime) -> impl Future<Output = Never> {
22 // The watcher gives us incoming ping messages.
23 let mut ping_watcher: Watcher<ping::Ping> = event_loop.make_watcher("/test").unwrap();
24
25 // The sender is used to send messages back to the pong channel.
26 let mut pong_sender: Sender<pong::Pong> = event_loop.make_sender("/test").unwrap();
27 // Wait for startup.
28 let startup = event_loop.on_run();
29
30 async move {
31 startup.await;
32 loop {
33 let ping = dbg!(ping_watcher.next().await);
34
35 let mut builder = pong_sender.make_builder();
36 let mut pong = pong::PongBuilder::new(builder.fbb());
37 pong.add_value(ping.message().unwrap().value());
38 let pong = pong.finish();
39 builder.send(pong).expect("Can't send pong reponse");
40 }
41 }
42}