Turn Rust's ping/pong into libraries
We do this to add a pingpong_test.rs in the future that
uses the libraries.
Change-Id: Iab879ea06d40c2ef706f39d23e9b144aa266e0a5
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/aos/events/pong_lib.rs b/aos/events/pong_lib.rs
new file mode 100644
index 0000000..fc1c162
--- /dev/null
+++ b/aos/events/pong_lib.rs
@@ -0,0 +1,28 @@
+use aos_events_event_loop_runtime::{EventLoopRuntime, Sender, Watcher};
+use futures::never::Never;
+use std::borrow::Borrow;
+
+use ping_rust_fbs::aos::examples as ping;
+use pong_rust_fbs::aos::examples as pong;
+
+/// Responds to ping messages with an equivalent pong.
+pub async fn pong(event_loop: EventLoopRuntime<'_>) -> Never {
+ // The watcher gives us incoming ping messages.
+ let mut ping_watcher: Watcher<ping::Ping> = event_loop.make_watcher("/test").unwrap();
+
+ // The sender is used to send messages back to the pong channel.
+ let mut pong_sender: Sender<pong::Pong> = event_loop.make_sender("/test").unwrap();
+
+ let on_run = event_loop.on_run();
+ on_run.borrow().await;
+ loop {
+ let ping = ping_watcher.next().await;
+
+ let mut builder = pong_sender.make_builder();
+ let mut pong = pong::PongBuilder::new(builder.fbb());
+ pong.add_value(ping.message().unwrap().value());
+ pong.add_initial_send_time(event_loop.monotonic_now().into());
+ let pong = pong.finish();
+ builder.send(pong).expect("Can't send pong reponse");
+ }
+}