2021-02-05 22:24:28 +01:00
|
|
|
use crate::p2p::State;
|
2021-02-11 21:51:32 +01:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
|
use mio::net::TcpStream;
|
2021-02-05 22:24:28 +01:00
|
|
|
|
2021-02-11 21:51:32 +01:00
|
|
|
#[derive(Debug)]
|
2021-02-05 22:24:28 +01:00
|
|
|
pub struct Peer {
|
2021-02-11 21:51:32 +01:00
|
|
|
addr: SocketAddr,
|
|
|
|
|
stream: TcpStream,
|
2021-02-05 22:24:28 +01:00
|
|
|
state: State,
|
2021-02-11 21:51:32 +01:00
|
|
|
inbound: bool,
|
|
|
|
|
public: bool,
|
2021-02-05 22:24:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Peer {
|
2021-02-11 21:51:32 +01:00
|
|
|
pub fn new(addr: SocketAddr, stream: TcpStream, state: State, inbound: bool) -> Self {
|
|
|
|
|
Peer { addr, stream, state, inbound, public: false }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_addr(&self) -> SocketAddr {
|
|
|
|
|
self.addr.clone()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_stream(&mut self) -> &mut TcpStream {
|
|
|
|
|
&mut self.stream
|
2021-02-05 22:24:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_state(&self) -> &State {
|
|
|
|
|
&self.state
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_state(&mut self, state: State) {
|
|
|
|
|
self.state = state;
|
|
|
|
|
}
|
2021-02-11 21:51:32 +01:00
|
|
|
|
|
|
|
|
pub fn is_public(&self) -> bool {
|
|
|
|
|
self.public
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_public(&mut self, public: bool) {
|
|
|
|
|
self.public = public;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn active(&self) -> bool {
|
|
|
|
|
self.state.active()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn disabled(&self) -> bool {
|
|
|
|
|
self.state.disabled()
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 18:20:30 +01:00
|
|
|
/// If loopback address then we care about ip and port.
|
|
|
|
|
/// If regular address then we only care about the ip and ignore the port.
|
2021-02-11 21:51:32 +01:00
|
|
|
pub fn equals(&self, addr: &SocketAddr) -> bool {
|
|
|
|
|
if self.addr.ip().is_loopback() {
|
|
|
|
|
self.addr == *addr
|
|
|
|
|
} else {
|
|
|
|
|
self.addr.ip() == addr.ip()
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-05 22:24:28 +01:00
|
|
|
}
|