use crate::peer::{ConnectionDirection, ConnectionProtocol, Metrics, PeerId, PeerState};
use derive_more::Display;
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::sync::RwLock;
#[derive(Debug, Display, Clone)]
#[display("{}[{}:{}]", inner.id, inner.protocol, inner.addr)]
pub struct PeerContext {
inner: Arc<InnerContext>,
}
impl PeerContext {
pub fn builder() -> PeerContextBuilder {
PeerContextBuilder::new()
}
pub fn id(&self) -> &PeerId {
&self.inner.id
}
pub fn addr(&self) -> &SocketAddr {
&self.inner.addr
}
pub fn connection_type(&self) -> &ConnectionDirection {
&self.inner.connection_type
}
pub fn protocol(&self) -> &ConnectionProtocol {
&self.inner.protocol
}
pub async fn state(&self) -> PeerState {
*self.inner.state.read().await
}
pub async fn set_state(&self, state: PeerState) {
*self.inner.state.write().await = state;
}
pub fn metrics(&self) -> &Metrics {
&self.inner.metrics
}
}
impl PartialEq for PeerContext {
fn eq(&self, other: &Self) -> bool {
self.id() == other.id()
&& self.addr() == other.addr()
&& self.protocol() == other.protocol()
}
}
#[derive(Debug)]
struct InnerContext {
id: PeerId,
addr: SocketAddr,
connection_type: ConnectionDirection,
protocol: ConnectionProtocol,
state: RwLock<PeerState>,
metrics: Metrics,
}
#[derive(Debug, Default)]
pub struct PeerContextBuilder {
id: Option<PeerId>,
addr: Option<SocketAddr>,
connection_type: Option<ConnectionDirection>,
connection_protocol: Option<ConnectionProtocol>,
state: Option<PeerState>,
metrics: Option<Metrics>,
}
impl PeerContextBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn id(&mut self, id: PeerId) -> &mut Self {
self.id = Some(id);
self
}
pub fn addr(&mut self, addr: SocketAddr) -> &mut Self {
self.addr = Some(addr);
self
}
pub fn connection_type(&mut self, connection_type: ConnectionDirection) -> &mut Self {
self.connection_type = Some(connection_type);
self
}
pub fn protocol(&mut self, protocol: ConnectionProtocol) -> &mut Self {
self.connection_protocol = Some(protocol);
self
}
pub fn state(&mut self, state: PeerState) -> &mut Self {
self.state = Some(state);
self
}
pub fn metrics(&mut self, metrics: Metrics) -> &mut Self {
self.metrics = Some(metrics);
self
}
pub fn build(&mut self) -> PeerContext {
let id = self.id.take().expect("id has not been set");
let addr = self.addr.take().expect("addr has not been set");
let connection_type = self
.connection_type
.take()
.expect("connection_type has not been set");
let protocol = self
.connection_protocol
.take()
.expect("connection_protocol has not been set");
let state = self.state.take().unwrap_or(PeerState::Handshake);
let metrics = self.metrics.take().unwrap_or_default();
PeerContext {
inner: Arc::new(InnerContext {
id,
addr,
connection_type,
protocol,
state: RwLock::new(state),
metrics,
}),
}
}
}