mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-02 02:35:31 +00:00
aquatic: remove unnecessary fields from Peer; general clean up
This commit is contained in:
parent
31c8864658
commit
ad68dda9f5
2 changed files with 12 additions and 13 deletions
|
|
@ -22,6 +22,7 @@ pub struct ConnectionKey {
|
||||||
pub socket_addr: SocketAddr
|
pub socket_addr: SocketAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub type ConnectionMap = DashMap<ConnectionKey, Time>;
|
pub type ConnectionMap = DashMap<ConnectionKey, Time>;
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -32,13 +33,14 @@ pub enum PeerStatus {
|
||||||
Stopped
|
Stopped
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl PeerStatus {
|
impl PeerStatus {
|
||||||
/// Determine peer status from announce event and number of bytes left.
|
/// Determine peer status from announce event and number of bytes left.
|
||||||
///
|
///
|
||||||
/// Likely, the last branch will be taken most of the time.
|
/// Likely, the last branch will be taken most of the time.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn from_event_and_bytes_left(
|
pub fn from_event_and_bytes_left(
|
||||||
event: AnnounceEvent,
|
event: AnnounceEvent,
|
||||||
bytes_left: NumberOfBytes
|
bytes_left: NumberOfBytes
|
||||||
) -> Self {
|
) -> Self {
|
||||||
if event == AnnounceEvent::Stopped {
|
if event == AnnounceEvent::Stopped {
|
||||||
|
|
@ -51,10 +53,9 @@ impl PeerStatus {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Peer {
|
pub struct Peer {
|
||||||
pub id: PeerId,
|
|
||||||
pub connection_id: ConnectionId,
|
|
||||||
pub ip_address: IpAddr,
|
pub ip_address: IpAddr,
|
||||||
pub port: Port,
|
pub port: Port,
|
||||||
pub status: PeerStatus,
|
pub status: PeerStatus,
|
||||||
|
|
@ -75,20 +76,21 @@ impl Peer {
|
||||||
announce_request: &AnnounceRequest,
|
announce_request: &AnnounceRequest,
|
||||||
ip_address: IpAddr
|
ip_address: IpAddr
|
||||||
) -> Self {
|
) -> Self {
|
||||||
|
let status = PeerStatus::from_event_and_bytes_left(
|
||||||
|
announce_request.event,
|
||||||
|
announce_request.bytes_left
|
||||||
|
);
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
id: announce_request.peer_id,
|
|
||||||
connection_id: announce_request.connection_id,
|
|
||||||
ip_address,
|
ip_address,
|
||||||
port: announce_request.port,
|
port: announce_request.port,
|
||||||
status: PeerStatus::from_event_and_bytes_left(
|
status,
|
||||||
announce_request.event,
|
|
||||||
announce_request.bytes_left
|
|
||||||
),
|
|
||||||
last_announce: Time(Instant::now())
|
last_announce: Time(Instant::now())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Hash, Clone)]
|
#[derive(PartialEq, Eq, Hash, Clone)]
|
||||||
pub struct PeerMapKey {
|
pub struct PeerMapKey {
|
||||||
pub ip: IpAddr,
|
pub ip: IpAddr,
|
||||||
|
|
@ -98,6 +100,7 @@ pub struct PeerMapKey {
|
||||||
|
|
||||||
pub type PeerMap = IndexMap<PeerMapKey, Peer>;
|
pub type PeerMap = IndexMap<PeerMapKey, Peer>;
|
||||||
|
|
||||||
|
|
||||||
pub struct TorrentData {
|
pub struct TorrentData {
|
||||||
pub peers: PeerMap,
|
pub peers: PeerMap,
|
||||||
pub num_seeders: AtomicUsize,
|
pub num_seeders: AtomicUsize,
|
||||||
|
|
@ -129,8 +132,6 @@ pub struct Statistics {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct State {
|
pub struct State {
|
||||||
pub connections: Arc<ConnectionMap>,
|
pub connections: Arc<ConnectionMap>,
|
||||||
|
|
|
||||||
|
|
@ -259,9 +259,7 @@ mod tests {
|
||||||
peer_id,
|
peer_id,
|
||||||
};
|
};
|
||||||
let value = Peer {
|
let value = Peer {
|
||||||
connection_id: ConnectionId(0),
|
|
||||||
ip_address,
|
ip_address,
|
||||||
id: peer_id,
|
|
||||||
port: Port(1),
|
port: Port(1),
|
||||||
status: PeerStatus::Leeching,
|
status: PeerStatus::Leeching,
|
||||||
last_announce: Time(Instant::now()),
|
last_announce: Time(Instant::now()),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue