mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-02 02:35:31 +00:00
udp: in Peer, replace PeerStatus with is_seeder bool
This commit is contained in:
parent
c76d7442e2
commit
becf88c372
2 changed files with 50 additions and 32 deletions
|
|
@ -17,7 +17,7 @@ use aquatic_udp_protocol::*;
|
||||||
use crate::common::*;
|
use crate::common::*;
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
|
|
||||||
use storage::{Peer, TorrentMap, TorrentMaps};
|
use storage::{TorrentMap, TorrentMaps};
|
||||||
|
|
||||||
pub fn run_swarm_worker(
|
pub fn run_swarm_worker(
|
||||||
_sentinel: PanicSentinel,
|
_sentinel: PanicSentinel,
|
||||||
|
|
@ -145,16 +145,17 @@ fn handle_announce_request<I: Ip>(
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
let peer = Peer {
|
|
||||||
ip_address: peer_ip,
|
|
||||||
port: request.port,
|
|
||||||
status: PeerStatus::from_event_and_bytes_left(request.event, request.bytes_left),
|
|
||||||
valid_until: peer_valid_until,
|
|
||||||
};
|
|
||||||
|
|
||||||
let torrent_data = torrents.0.entry(request.info_hash).or_default();
|
let torrent_data = torrents.0.entry(request.info_hash).or_default();
|
||||||
|
|
||||||
torrent_data.update_peer(request.peer_id, peer);
|
let peer_status = PeerStatus::from_event_and_bytes_left(request.event, request.bytes_left);
|
||||||
|
|
||||||
|
torrent_data.update_peer(
|
||||||
|
request.peer_id,
|
||||||
|
peer_ip,
|
||||||
|
request.port,
|
||||||
|
peer_status,
|
||||||
|
peer_valid_until,
|
||||||
|
);
|
||||||
|
|
||||||
let response_peers =
|
let response_peers =
|
||||||
torrent_data.extract_response_peers(rng, request.peer_id, max_num_peers_to_take);
|
torrent_data.extract_response_peers(rng, request.peer_id, max_num_peers_to_take);
|
||||||
|
|
|
||||||
|
|
@ -20,15 +20,15 @@ use crate::config::Config;
|
||||||
use super::create_torrent_scrape_statistics;
|
use super::create_torrent_scrape_statistics;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Peer<I: Ip> {
|
struct Peer<I: Ip> {
|
||||||
pub ip_address: I,
|
ip_address: I,
|
||||||
pub port: Port,
|
port: Port,
|
||||||
pub status: PeerStatus,
|
is_seeder: bool,
|
||||||
pub valid_until: ValidUntil,
|
valid_until: ValidUntil,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I: Ip> Peer<I> {
|
impl<I: Ip> Peer<I> {
|
||||||
pub fn to_response_peer(&self) -> ResponsePeer<I> {
|
fn to_response_peer(&self) -> ResponsePeer<I> {
|
||||||
ResponsePeer {
|
ResponsePeer {
|
||||||
ip_address: self.ip_address,
|
ip_address: self.ip_address,
|
||||||
port: self.port,
|
port: self.port,
|
||||||
|
|
@ -36,7 +36,7 @@ impl<I: Ip> Peer<I> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type PeerMap<I> = IndexMap<PeerId, Peer<I>>;
|
type PeerMap<I> = IndexMap<PeerId, Peer<I>>;
|
||||||
|
|
||||||
pub struct TorrentData<I: Ip> {
|
pub struct TorrentData<I: Ip> {
|
||||||
peers: PeerMap<I>,
|
peers: PeerMap<I>,
|
||||||
|
|
@ -45,14 +45,35 @@ pub struct TorrentData<I: Ip> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I: Ip> TorrentData<I> {
|
impl<I: Ip> TorrentData<I> {
|
||||||
pub fn update_peer(&mut self, peer_id: PeerId, peer: Peer<I>) {
|
pub fn update_peer(
|
||||||
let opt_removed_peer = match peer.status {
|
&mut self,
|
||||||
|
peer_id: PeerId,
|
||||||
|
ip_address: I,
|
||||||
|
port: Port,
|
||||||
|
status: PeerStatus,
|
||||||
|
valid_until: ValidUntil,
|
||||||
|
) {
|
||||||
|
let opt_removed_peer = match status {
|
||||||
PeerStatus::Leeching => {
|
PeerStatus::Leeching => {
|
||||||
|
let peer = Peer {
|
||||||
|
ip_address,
|
||||||
|
port,
|
||||||
|
is_seeder: false,
|
||||||
|
valid_until,
|
||||||
|
};
|
||||||
|
|
||||||
self.num_leechers += 1;
|
self.num_leechers += 1;
|
||||||
|
|
||||||
self.peers.insert(peer_id, peer)
|
self.peers.insert(peer_id, peer)
|
||||||
}
|
}
|
||||||
PeerStatus::Seeding => {
|
PeerStatus::Seeding => {
|
||||||
|
let peer = Peer {
|
||||||
|
ip_address,
|
||||||
|
port,
|
||||||
|
is_seeder: true,
|
||||||
|
valid_until,
|
||||||
|
};
|
||||||
|
|
||||||
self.num_seeders += 1;
|
self.num_seeders += 1;
|
||||||
|
|
||||||
self.peers.insert(peer_id, peer)
|
self.peers.insert(peer_id, peer)
|
||||||
|
|
@ -60,14 +81,14 @@ impl<I: Ip> TorrentData<I> {
|
||||||
PeerStatus::Stopped => self.peers.remove(&peer_id),
|
PeerStatus::Stopped => self.peers.remove(&peer_id),
|
||||||
};
|
};
|
||||||
|
|
||||||
match opt_removed_peer.map(|peer| peer.status) {
|
match opt_removed_peer.map(|peer| peer.is_seeder) {
|
||||||
Some(PeerStatus::Leeching) => {
|
Some(true) => {
|
||||||
self.num_leechers -= 1;
|
self.num_leechers -= 1;
|
||||||
}
|
}
|
||||||
Some(PeerStatus::Seeding) => {
|
Some(false) => {
|
||||||
self.num_seeders -= 1;
|
self.num_seeders -= 1;
|
||||||
}
|
}
|
||||||
_ => {}
|
None => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -107,15 +128,11 @@ impl<I: Ip> TorrentData<I> {
|
||||||
if peer.valid_until.valid(now) {
|
if peer.valid_until.valid(now) {
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
match peer.status {
|
if peer.is_seeder {
|
||||||
PeerStatus::Seeding => {
|
self.num_seeders -= 1;
|
||||||
self.num_seeders -= 1;
|
} else {
|
||||||
}
|
self.num_leechers -= 1;
|
||||||
PeerStatus::Leeching => {
|
}
|
||||||
self.num_leechers -= 1;
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
};
|
|
||||||
|
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
@ -265,7 +282,7 @@ mod tests {
|
||||||
Peer {
|
Peer {
|
||||||
ip_address: Ipv4Addr::from(i.to_be_bytes()),
|
ip_address: Ipv4Addr::from(i.to_be_bytes()),
|
||||||
port: Port(1),
|
port: Port(1),
|
||||||
status: PeerStatus::Leeching,
|
is_seeder: false,
|
||||||
valid_until: ValidUntil::new(ServerStartInstant::new(), 0),
|
valid_until: ValidUntil::new(ServerStartInstant::new(), 0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue