use crate::peer::info::PeerRelation;
use super::error::Error;
use libp2p::{Multiaddr, PeerId};
use tokio::sync::mpsc;
pub type CommandReceiver = mpsc::UnboundedReceiver<Command>;
pub type CommandSender = mpsc::UnboundedSender<Command>;
pub fn command_channel() -> (CommandSender, CommandReceiver) {
mpsc::unbounded_channel()
}
#[derive(Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Command {
AddPeer {
peer_id: PeerId,
multiaddr: Multiaddr,
alias: Option<String>,
relation: PeerRelation,
},
RemovePeer {
peer_id: PeerId,
},
DialPeer {
peer_id: PeerId,
},
DialAddress {
address: Multiaddr,
},
DisconnectPeer {
peer_id: PeerId,
},
BanPeer {
peer_id: PeerId,
},
UnbanPeer {
peer_id: PeerId,
},
BanAddress {
address: Multiaddr,
},
UnbanAddress {
address: Multiaddr,
},
ChangeRelation {
peer_id: PeerId,
to: PeerRelation,
},
}
#[derive(Clone, Debug)]
pub struct NetworkCommandSender(CommandSender);
impl NetworkCommandSender {
pub(crate) fn new(inner: CommandSender) -> Self {
Self(inner)
}
pub fn send(&self, command: Command) -> Result<(), Error> {
self.0.send(command).map_err(|_| Error::SendingCommandFailed)
}
}