use libp2p::{identity::Keypair, swarm::SwarmBuilder, PeerId};
pub mod behaviour;
pub mod event;
mod transport;
use self::behaviour::SwarmBehaviour;
use crate::errors::TatamiError;
pub struct Swarm(pub libp2p::Swarm<SwarmBehaviour>);
impl Swarm {
pub async fn create(keypair: Keypair, bootstrap: bool) -> Result<Self, TatamiError> {
let local_peer_id = PeerId::from(keypair.public());
let transport = Self::transport(&keypair)?;
let behaviour = SwarmBehaviour::new(keypair, bootstrap).await?;
let swarm = SwarmBuilder::new(transport, behaviour, local_peer_id)
.executor(Box::new(|fut| {
tokio::spawn(fut);
}))
.build();
let swarm = Self(swarm);
Ok(swarm)
}
}