use std::time::Duration;
use qrpc::{Ctx, PeerConfig, 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!("[20002] recv from={} msg={msg:?}", source_peer_id);
Ok(())
},
)
.with_id("node-20002")
.with_ca_cert("tests/certs/ca.crt")
.with_identity("tests/certs/server.crt", "tests/certs/server.key")
.with_client_identity("tests/certs/client.crt", "tests/certs/client.key")
.with_port(20002)
.add_peer(PeerConfig {
address: "127.0.0.1".to_string(),
port: 20001,
server_name: "localhost".to_string(),
ca_cert_path: Some("tests/certs/ca.crt".to_string()),
expected_id: Some("node-20001".to_string()),
})
.build()?;
instance.start().await;
println!("[20002] started, listening on 20002 and dialing 20001");
let start = tokio::time::Instant::now();
loop {
let peers = instance.peer_ids().await;
if peers.iter().any(|p| p == "node-20001") {
break;
}
if start.elapsed() > Duration::from_secs(60) {
println!("[20002] waiting for node-20001 timeout, keep retrying...");
break;
}
tokio::time::sleep(Duration::from_millis(200)).await;
}
let _ = instance
.send_to("node-20001", &DemoEnvelope::ping("ping from 20002"))
.await;
println!("[20002] 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(())
}