Adam Snaider | 4351678 | 2023-06-26 15:14:18 -0700 | [diff] [blame] | 1 | use aos_configuration as config; |
| 2 | use aos_events_event_loop_runtime::{EventLoopRuntime, Sender, Watcher}; |
| 3 | use aos_events_shm_event_loop::ShmEventLoop; |
| 4 | use core::future::Future; |
| 5 | use futures::never::Never; |
| 6 | use std::path::Path; |
| 7 | |
| 8 | use ping_rust_fbs::aos::examples as ping; |
| 9 | use pong_rust_fbs::aos::examples as pong; |
| 10 | |
| 11 | fn 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. |
| 21 | fn 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 | } |