#[cfg(feature = "autonat")]
mod autonat;
mod blacklist;
#[cfg(feature = "kad")]
pub(crate) mod dht;
#[cfg(feature = "floodsub")]
pub(crate) mod floodsub;
#[cfg(feature = "gossipsub")]
pub(crate) mod gossipsub;
mod peer_store;
#[cfg(feature = "rendezvous")]
pub(crate) mod rendezvous;
#[cfg(feature = "request-response")]
pub(crate) mod request_response;
#[cfg(feature = "stream")]
pub(crate) mod stream;
pub(crate) mod swarm;
mod whitelist;
#[cfg(feature = "autonat")]
use crate::handle::autonat::ConnexaAutonat;
use crate::handle::blacklist::ConnexaBlacklist;
#[cfg(feature = "kad")]
use crate::handle::dht::ConnexaDht;
#[cfg(feature = "floodsub")]
use crate::handle::floodsub::ConnexaFloodsub;
#[cfg(feature = "gossipsub")]
use crate::handle::gossipsub::ConnexaGossipsub;
use crate::handle::peer_store::ConnexaPeerstore;
#[cfg(feature = "rendezvous")]
use crate::handle::rendezvous::ConnexaRendezvous;
#[cfg(feature = "request-response")]
use crate::handle::request_response::ConnexaRequestResponse;
#[cfg(feature = "stream")]
use crate::handle::stream::ConnexaStream;
use crate::handle::swarm::ConnexaSwarm;
use crate::handle::whitelist::ConnexaWhitelist;
use crate::types::Command;
use async_rt::CommunicationTask;
use libp2p::identity::Keypair;
use std::fmt::Debug;
use tracing::Span;
type Result<T> = std::io::Result<T>;
pub struct Connexa<T = ()> {
#[allow(dead_code)]
span: Span,
keypair: Keypair,
to_task: CommunicationTask<Command<T>>,
}
impl<T> Clone for Connexa<T> {
fn clone(&self) -> Self {
Self {
span: self.span.clone(),
keypair: self.keypair.clone(),
to_task: self.to_task.clone(),
}
}
}
impl<T> Connexa<T> {
pub(crate) fn new(
span: Span,
keypair: Keypair,
to_task: CommunicationTask<Command<T>>,
) -> Self {
Self {
span,
keypair,
to_task,
}
}
}
impl<T> Debug for Connexa<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Connexa")
.field("public_key", &self.keypair.public())
.finish()
}
}
impl<T> Connexa<T>
where
T: Send + Sync + 'static,
{
pub fn swarm(&self) -> ConnexaSwarm<'_, T> {
ConnexaSwarm::new(self)
}
#[cfg(feature = "autonat")]
pub fn autonat(&self) -> ConnexaAutonat<'_, T> {
ConnexaAutonat::new(self)
}
#[cfg(feature = "floodsub")]
pub fn floodsub(&self) -> ConnexaFloodsub<'_, T> {
ConnexaFloodsub::new(self)
}
#[cfg(feature = "gossipsub")]
pub fn gossipsub(&self) -> ConnexaGossipsub<'_, T> {
ConnexaGossipsub::new(self)
}
#[cfg(feature = "kad")]
pub fn dht(&self) -> ConnexaDht<'_, T> {
ConnexaDht::new(self)
}
#[cfg(feature = "request-response")]
pub fn request_response(&self) -> ConnexaRequestResponse<'_, T> {
ConnexaRequestResponse::new(self)
}
#[cfg(feature = "stream")]
pub fn stream(&self) -> ConnexaStream<'_, T> {
ConnexaStream::new(self)
}
#[cfg(feature = "rendezvous")]
pub fn rendezvous(&self) -> ConnexaRendezvous<'_, T> {
ConnexaRendezvous::new(self)
}
pub fn whitelist(&self) -> ConnexaWhitelist<'_, T> {
ConnexaWhitelist::new(self)
}
pub fn blacklist(&self) -> ConnexaBlacklist<'_, T> {
ConnexaBlacklist::new(self)
}
pub fn peer_store(&self) -> ConnexaPeerstore<'_, T> {
ConnexaPeerstore::new(self)
}
pub fn keypair(&self) -> &Keypair {
&self.keypair
}
pub fn shutdown(self) {
self.to_task.abort();
}
}
impl<T> Connexa<T> {
pub async fn send_custom_event(&self, event: T) -> Result<()>
where
T: Send + Sync + 'static,
{
self.to_task.clone().send(Command::Custom(event)).await
}
}