use std::time::Duration;
use qrpc::{Ctx, QrpcInstance, QrpcResult, State};
#[path = "common/demo_support.rs"]
mod demo_support;
use demo_support::DemoEnvelope;
#[tokio::main]
async fn main() -> QrpcResult<()> {
let instance =
QrpcInstance::<(), DemoEnvelope, _>::builder(
|_state: State<()>,
ctx: Ctx<DemoEnvelope>,
source_peer_id: String,
msg: DemoEnvelope| async move {
println!("[ctx-20001] recv from={} msg={msg:?}", source_peer_id);
if matches!(msg, DemoEnvelope::Ping(_)) {
ctx.send_to(&source_peer_id, &DemoEnvelope::pong("ctx reply from 20001"))
.await?;
}
Ok(())
},
)
.with_id("node-ctx-20001")
.with_ca_cert("tests/certs/ca.crt")
.with_identity("tests/certs/server.crt", "tests/certs/server.key")
.with_port(20011)
.build()?;
instance.start().await;
println!("[ctx-20001] started, listening on 20011");
let start = tokio::time::Instant::now();
loop {
let peers = instance.peer_ids().await;
if peers.iter().any(|p| p == "node-ctx-20002") {
break;
}
if start.elapsed() > Duration::from_secs(60) {
println!("[ctx-20001] waiting for node-ctx-20002 timeout, keep serving...");
break;
}
tokio::time::sleep(Duration::from_millis(200)).await;
}
println!("[ctx-20001] ready; press Ctrl+C to stop");
tokio::signal::ctrl_c()
.await
.map_err(|e| qrpc::QrpcError::MessageDecode(format!("ctrl_c error: {e}")))?;
instance.shutdown().await;
Ok(())
}