2020-10-15 18:14:39 -04:00
|
|
|
use futures::task::Context;
|
|
|
|
use libp2p::{
|
|
|
|
core::{connection::ConnectionId, ConnectedPoint},
|
|
|
|
swarm::{
|
|
|
|
protocols_handler::DummyProtocolsHandler, NetworkBehaviour, NetworkBehaviourAction,
|
|
|
|
PollParameters,
|
|
|
|
},
|
|
|
|
Multiaddr, PeerId,
|
|
|
|
};
|
2020-10-19 19:10:28 -04:00
|
|
|
use std::{collections::VecDeque, task::Poll};
|
2020-10-15 18:14:39 -04:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
2020-10-19 19:10:28 -04:00
|
|
|
pub enum OutEvent {
|
2020-10-15 18:14:39 -04:00
|
|
|
ConnectionEstablished(PeerId),
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:10:28 -04:00
|
|
|
/// A NetworkBehaviour that tracks connections to the counterparty. Although the
|
|
|
|
/// libp2p `NetworkBehaviour` abstraction encompasses connections to multiple
|
|
|
|
/// peers we only ever connect to a single counterparty. Peer Tracker tracks
|
|
|
|
/// that connection.
|
2020-10-15 18:14:39 -04:00
|
|
|
#[derive(Default, Debug)]
|
|
|
|
pub struct PeerTracker {
|
2020-10-19 19:10:28 -04:00
|
|
|
connected: Option<(PeerId, Multiaddr)>,
|
|
|
|
events: VecDeque<OutEvent>,
|
2020-10-15 18:14:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PeerTracker {
|
2020-10-21 22:30:07 -04:00
|
|
|
/// Returns the peer id of counterparty if we are connected.
|
2020-10-19 19:10:28 -04:00
|
|
|
pub fn counterparty_peer_id(&self) -> Option<PeerId> {
|
|
|
|
if let Some((id, _)) = &self.connected {
|
|
|
|
return Some(id.clone());
|
2020-10-15 18:14:39 -04:00
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2020-10-21 22:30:07 -04:00
|
|
|
/// Returns the multiaddr of counterparty if we are connected.
|
2020-10-19 19:10:28 -04:00
|
|
|
pub fn counterparty_addr(&self) -> Option<Multiaddr> {
|
|
|
|
if let Some((_, addr)) = &self.connected {
|
|
|
|
return Some(addr.clone());
|
2020-10-15 18:14:39 -04:00
|
|
|
}
|
2020-10-19 19:10:28 -04:00
|
|
|
None
|
2020-10-15 18:14:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NetworkBehaviour for PeerTracker {
|
|
|
|
type ProtocolsHandler = DummyProtocolsHandler;
|
2020-10-19 19:10:28 -04:00
|
|
|
type OutEvent = OutEvent;
|
2020-10-15 18:14:39 -04:00
|
|
|
|
|
|
|
fn new_handler(&mut self) -> Self::ProtocolsHandler {
|
|
|
|
DummyProtocolsHandler::default()
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:10:28 -04:00
|
|
|
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
|
2020-10-15 18:14:39 -04:00
|
|
|
let mut addresses: Vec<Multiaddr> = vec![];
|
|
|
|
|
2020-10-19 19:10:28 -04:00
|
|
|
if let Some(addr) = self.counterparty_addr() {
|
|
|
|
addresses.push(addr)
|
2020-10-15 18:14:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
addresses
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inject_connected(&mut self, _: &PeerId) {}
|
|
|
|
|
|
|
|
fn inject_disconnected(&mut self, _: &PeerId) {}
|
|
|
|
|
|
|
|
fn inject_connection_established(
|
|
|
|
&mut self,
|
|
|
|
peer: &PeerId,
|
|
|
|
_: &ConnectionId,
|
|
|
|
point: &ConnectedPoint,
|
|
|
|
) {
|
2020-10-19 19:10:28 -04:00
|
|
|
match point {
|
|
|
|
ConnectedPoint::Dialer { address } => {
|
|
|
|
self.connected = Some((peer.clone(), address.clone()));
|
|
|
|
}
|
|
|
|
ConnectedPoint::Listener {
|
|
|
|
local_addr: _,
|
|
|
|
send_back_addr,
|
|
|
|
} => {
|
|
|
|
self.connected = Some((peer.clone(), send_back_addr.clone()));
|
|
|
|
}
|
2020-10-15 18:14:39 -04:00
|
|
|
}
|
2020-10-19 19:10:28 -04:00
|
|
|
|
|
|
|
self.events
|
|
|
|
.push_back(OutEvent::ConnectionEstablished(peer.clone()));
|
2020-10-15 18:14:39 -04:00
|
|
|
}
|
|
|
|
|
2020-10-19 19:10:28 -04:00
|
|
|
fn inject_connection_closed(&mut self, _: &PeerId, _: &ConnectionId, _: &ConnectedPoint) {
|
|
|
|
self.connected = None;
|
2020-10-15 18:14:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn inject_event(&mut self, _: PeerId, _: ConnectionId, _: void::Void) {}
|
|
|
|
|
|
|
|
fn poll(
|
|
|
|
&mut self,
|
|
|
|
_: &mut Context<'_>,
|
|
|
|
_: &mut impl PollParameters,
|
|
|
|
) -> Poll<NetworkBehaviourAction<void::Void, Self::OutEvent>> {
|
|
|
|
if let Some(event) = self.events.pop_front() {
|
|
|
|
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
|
|
|
|
}
|
|
|
|
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
}
|