aquatic_http: use new trait Ip for ip generic parameters

This commit is contained in:
Joakim Frostegård 2020-07-08 14:22:30 +02:00
parent 2386dd0e8b
commit 8ddccb20aa
3 changed files with 25 additions and 19 deletions

View file

@ -16,6 +16,12 @@ use crate::protocol::request::Request;
use crate::protocol::response::{Response, ResponsePeer}; use crate::protocol::response::{Response, ResponsePeer};
pub trait Ip: Copy + Eq + ::std::hash::Hash {}
impl Ip for Ipv4Addr {}
impl Ip for Ipv6Addr {}
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub struct ConnectionMeta { pub struct ConnectionMeta {
/// Index of socket worker responsible for this connection. Required for /// Index of socket worker responsible for this connection. Required for
@ -27,10 +33,10 @@ pub struct ConnectionMeta {
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub struct PeerConnectionMeta<P> { pub struct PeerConnectionMeta<I: Ip> {
pub worker_index: usize, pub worker_index: usize,
pub poll_token: Token, pub poll_token: Token,
pub peer_ip_address: P, pub peer_ip_address: I,
} }
@ -63,16 +69,16 @@ impl PeerStatus {
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct Peer<P> { pub struct Peer<I: Ip> {
pub connection_meta: PeerConnectionMeta<P>, pub connection_meta: PeerConnectionMeta<I>,
pub port: u16, pub port: u16,
pub status: PeerStatus, pub status: PeerStatus,
pub valid_until: ValidUntil, pub valid_until: ValidUntil,
} }
impl <S: Copy>Peer<S> { impl <I: Ip>Peer<I> {
pub fn to_response_peer(&self) -> ResponsePeer<S> { pub fn to_response_peer(&self) -> ResponsePeer<I> {
ResponsePeer { ResponsePeer {
ip_address: self.connection_meta.peer_ip_address, ip_address: self.connection_meta.peer_ip_address,
port: self.port port: self.port
@ -82,23 +88,23 @@ impl <S: Copy>Peer<S> {
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PeerMapKey<P: Eq + ::std::hash::Hash> { pub struct PeerMapKey<I: Ip> {
pub peer_id: PeerId, pub peer_id: PeerId,
pub ip_or_key: Either<P, String> pub ip_or_key: Either<I, String>
} }
pub type PeerMap<P> = IndexMap<PeerMapKey<P>, Peer<P>>; pub type PeerMap<I> = IndexMap<PeerMapKey<I>, Peer<I>>;
pub struct TorrentData<P: Eq + ::std::hash::Hash> { pub struct TorrentData<I: Ip> {
pub peers: PeerMap<P>, pub peers: PeerMap<I>,
pub num_seeders: usize, pub num_seeders: usize,
pub num_leechers: usize, pub num_leechers: usize,
} }
impl <P: Eq + ::std::hash::Hash> Default for TorrentData<P> { impl <I: Ip> Default for TorrentData<I> {
#[inline] #[inline]
fn default() -> Self { fn default() -> Self {
Self { Self {
@ -110,7 +116,7 @@ impl <P: Eq + ::std::hash::Hash> Default for TorrentData<P> {
} }
pub type TorrentMap<P> = HashMap<InfoHash, TorrentData<P>>; pub type TorrentMap<I> = HashMap<InfoHash, TorrentData<I>>;
#[derive(Default)] #[derive(Default)]

View file

@ -170,14 +170,14 @@ pub fn handle_announce_requests(
/// Insert/update peer. Return num_seeders, num_leechers and response peers /// Insert/update peer. Return num_seeders, num_leechers and response peers
fn upsert_peer_and_get_response_peers<P: Copy + Eq + ::std::hash::Hash>( fn upsert_peer_and_get_response_peers<I: Ip>(
config: &Config, config: &Config,
rng: &mut impl Rng, rng: &mut impl Rng,
request_sender_meta: PeerConnectionMeta<P>, request_sender_meta: PeerConnectionMeta<I>,
torrent_data: &mut TorrentData<P>, torrent_data: &mut TorrentData<I>,
request: AnnounceRequest, request: AnnounceRequest,
valid_until: ValidUntil, valid_until: ValidUntil,
) -> (usize, usize, Vec<ResponsePeer<P>>) { ) -> (usize, usize, Vec<ResponsePeer<I>>) {
// Insert/update/remove peer who sent this request // Insert/update/remove peer who sent this request
{ {
let peer_status = PeerStatus::from_event_and_bytes_left( let peer_status = PeerStatus::from_event_and_bytes_left(
@ -235,7 +235,7 @@ fn upsert_peer_and_get_response_peers<P: Copy + Eq + ::std::hash::Hash>(
Some(numwant) => numwant.min(config.protocol.max_peers), Some(numwant) => numwant.min(config.protocol.max_peers),
}; };
let response_peers: Vec<ResponsePeer<P>> = extract_response_peers( let response_peers: Vec<ResponsePeer<I>> = extract_response_peers(
rng, rng,
&torrent_data.peers, &torrent_data.peers,
max_num_peers_to_take, max_num_peers_to_take,

View file

@ -12,7 +12,7 @@ pub fn clean_torrents(state: &State){
} }
fn clean_torrent_map<I: Eq + ::std::hash::Hash>( fn clean_torrent_map<I: Ip>(
torrent_map: &mut TorrentMap<I>, torrent_map: &mut TorrentMap<I>,
){ ){
let now = Instant::now(); let now = Instant::now();