aboutsummaryrefslogtreecommitdiff
path: root/p2p/examples/monitor/src/client.rs
diff options
context:
space:
mode:
Diffstat (limited to 'p2p/examples/monitor/src/client.rs')
-rw-r--r--p2p/examples/monitor/src/client.rs36
1 files changed, 33 insertions, 3 deletions
diff --git a/p2p/examples/monitor/src/client.rs b/p2p/examples/monitor/src/client.rs
index b81c286..2f4d00d 100644
--- a/p2p/examples/monitor/src/client.rs
+++ b/p2p/examples/monitor/src/client.rs
@@ -1,4 +1,5 @@
use clap::Parser;
+use serde::{Deserialize, Serialize};
use karyon_jsonrpc::Client;
use karyon_p2p::endpoint::Endpoint;
@@ -11,6 +12,9 @@ struct Cli {
rpc_endpoint: Endpoint,
}
+#[derive(Deserialize, Serialize)]
+struct Pong {}
+
fn main() {
smol::block_on(async {
env_logger::init();
@@ -32,18 +36,44 @@ fn main() {
.await
.expect("Subscribe to peer pool events");
+ let (_, sub3) = rpc
+ .subscribe("MonitorRPC.discovery_subscribe", ())
+ .await
+ .expect("Subscribe to discovery events");
+
+ smol::spawn(async move {
+ loop {
+ let event = sub.recv().await.expect("Receive connection event");
+ println!("Receive new connection event: {event}");
+ }
+ })
+ .detach();
+
smol::spawn(async move {
loop {
- let _event = sub.recv().await.expect("Receive connection event");
+ let event = sub2.recv().await.expect("Receive peer pool event");
+ println!("Receive new peerpool event: {event}");
}
})
.detach();
smol::spawn(async move {
loop {
- let _event = sub2.recv().await.expect("Receive peer pool event");
+ let event = sub3.recv().await.expect("Receive discovery event");
+ println!("Receive new discovery event: {event}");
}
})
- .await;
+ .detach();
+
+ // start ping-pong loop
+ loop {
+ smol::Timer::after(std::time::Duration::from_secs(1)).await;
+ let _: Pong = rpc
+ .call("MonitorRPC.ping", ())
+ .await
+ .expect("Receive pong message");
+
+ println!("Receive pong message");
+ }
});
}