Adam Snaider | c5bdbd3 | 2023-10-19 18:20:56 -0600 | [diff] [blame^] | 1 | use aos_events_event_loop_runtime::{EventLoopRuntime, Sender, Watcher}; |
| 2 | use futures::never::Never; |
| 3 | use std::borrow::Borrow; |
| 4 | |
| 5 | use ping_rust_fbs::aos::examples as ping; |
| 6 | use pong_rust_fbs::aos::examples as pong; |
| 7 | |
| 8 | /// Responds to ping messages with an equivalent pong. |
| 9 | pub async fn pong(event_loop: EventLoopRuntime<'_>) -> Never { |
| 10 | // The watcher gives us incoming ping messages. |
| 11 | let mut ping_watcher: Watcher<ping::Ping> = event_loop.make_watcher("/test").unwrap(); |
| 12 | |
| 13 | // The sender is used to send messages back to the pong channel. |
| 14 | let mut pong_sender: Sender<pong::Pong> = event_loop.make_sender("/test").unwrap(); |
| 15 | |
| 16 | let on_run = event_loop.on_run(); |
| 17 | on_run.borrow().await; |
| 18 | loop { |
| 19 | let ping = ping_watcher.next().await; |
| 20 | |
| 21 | let mut builder = pong_sender.make_builder(); |
| 22 | let mut pong = pong::PongBuilder::new(builder.fbb()); |
| 23 | pong.add_value(ping.message().unwrap().value()); |
| 24 | pong.add_initial_send_time(event_loop.monotonic_now().into()); |
| 25 | let pong = pong.finish(); |
| 26 | builder.send(pong).expect("Can't send pong reponse"); |
| 27 | } |
| 28 | } |