caretta-sync/examples/core/src/server.rs

39 lines
1.4 KiB
Rust
Raw Normal View History

2025-08-20 06:50:35 +09:00
use caretta::{config::P2pConfig, server::ServerTrait};
2025-08-21 07:40:33 +09:00
use libp2p::{futures::StreamExt, noise, swarm::SwarmEvent, tcp, yamux};
pub struct Server{}
2025-08-20 06:50:35 +09:00
impl ServerTrait for Server {
2025-08-21 07:40:33 +09:00
async fn serve_p2p<T>(config: &T) -> Result<(), caretta::error::Error>
where
T: AsRef<P2pConfig>
{
let mut swarm = libp2p::SwarmBuilder::with_existing_identity(config.as_ref().secret.clone())
2025-08-20 06:50:35 +09:00
.with_tokio()
.with_tcp(
tcp::Config::default(),
noise::Config::new,
yamux::Config::default,
)?
2025-08-21 07:40:33 +09:00
.with_behaviour(|keypair| caretta::p2p::Behaviour::try_from(keypair).unwrap())?
2025-08-20 06:50:35 +09:00
.build();
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
loop{
let swarm_event = swarm.select_next_some().await;
tokio::spawn(async move{
match swarm_event {
SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {address:?}"),
SwarmEvent::Behaviour(event) => {
println!("{event:?}");
event.run().await;
},
_ => {}
}
});
}
}
2025-08-21 07:40:33 +09:00
async fn serve_rpc<T>(config: &T) -> Result<(), caretta::error::Error>
where T: AsRef<caretta::config::RpcConfig> {
todo!()
}
2025-08-20 06:50:35 +09:00
}