use crate::engine::choking_algorithm::{ChokingAlgorithm, IdentityChokeAction, PeerIdentity};
use crate::engine::peer_stats::PeerStats;
pub fn add_peer_to_tracking(
algo: &mut Option<ChokingAlgorithm>,
peer_id: [u8; 8],
addr: std::net::SocketAddr,
) -> usize {
if let Some(algo) = algo.as_mut() {
let mut full_id = [0u8; 20];
full_id[..8].copy_from_slice(&peer_id);
let idx = algo.len();
algo.add_peer(PeerStats::new(full_id, addr));
idx
} else {
0
}
}
pub fn check_snubbed_peers(algo: &mut Option<ChokingAlgorithm>) -> Vec<usize> {
algo.as_mut()
.map(|a| a.check_snubbed_peers())
.unwrap_or_default()
}
pub async fn handle_snubbed_peer_by_identity(
algo: &mut Option<ChokingAlgorithm>,
identity: PeerIdentity,
) -> crate::error::Result<()> {
if let Some(algo) = algo.as_mut()
&& let Some(index) = algo
.peers()
.iter()
.position(|peer| PeerIdentity::from(peer) == identity)
{
algo.mark_peer_snubbed(index);
}
Ok(())
}
pub async fn handle_snubbed_peer(
algo: &mut Option<ChokingAlgorithm>,
peer_idx: usize,
) -> crate::error::Result<()> {
if let Some(algo) = algo.as_mut() {
algo.mark_peer_snubbed(peer_idx);
}
Ok(())
}
pub fn on_data_received_from_peer_by_identity(
algo: &mut Option<ChokingAlgorithm>,
identity: PeerIdentity,
bytes: u64,
) {
if let Some(algo) = algo.as_mut() {
algo.on_data_received_by_identity(identity, bytes);
}
}
pub fn on_data_received_from_peer(
algo: &mut Option<ChokingAlgorithm>,
peer_idx: usize,
bytes: u64,
) {
if let Some(a) = algo.as_mut() {
a.on_data_received(peer_idx, bytes);
}
}
pub fn on_peer_choke(algo: &mut Option<ChokingAlgorithm>, peer_idx: usize) {
if let Some(algo) = algo.as_mut()
&& let Some(peer) = algo.get_peer_mut(peer_idx)
{
peer.peer_choking = true;
}
}
pub fn on_peer_unchoke(algo: &mut Option<ChokingAlgorithm>, peer_idx: usize) {
if let Some(algo) = algo.as_mut()
&& let Some(peer) = algo.get_peer_mut(peer_idx)
{
peer.peer_choking = false;
}
}
pub fn on_piece_received(algo: &mut Option<ChokingAlgorithm>, peer_idx: usize, bytes: u32) {
if let Some(algo) = algo.as_mut()
&& let Some(peer) = algo.get_peer_mut(peer_idx)
{
peer.on_data_received(bytes as u64);
}
}
pub fn rotate_choke_by_identity(algo: &mut Option<ChokingAlgorithm>) -> Vec<IdentityChokeAction> {
algo.as_mut()
.map(ChokingAlgorithm::rotate_choke_by_identity)
.unwrap_or_default()
}
pub fn optimistically_unchoke_by_identity(
algo: &mut Option<ChokingAlgorithm>,
) -> Option<PeerIdentity> {
algo.as_mut()
.and_then(ChokingAlgorithm::optimistically_unchoke_by_identity)
}
pub fn select_best_peer_for_request_by_identity(
algo: &Option<ChokingAlgorithm>,
) -> Option<PeerIdentity> {
select_best_peer_for_request(algo)
.and_then(|index| algo.as_ref()?.get_peer(index).map(PeerIdentity::from))
}
pub fn select_best_peer_for_request(algo: &Option<ChokingAlgorithm>) -> Option<usize> {
algo.as_ref().and_then(|a| {
let peers = a.peers();
if peers.is_empty() {
return None;
}
let best_unchoked = peers
.iter()
.enumerate()
.filter(|(_, p)| !p.peer_choking && !p.is_snubbed && p.is_eligible_for_selection())
.max_by(|(_, a), (_, b)| {
a.download_speed
.partial_cmp(&b.download_speed)
.unwrap_or(std::cmp::Ordering::Equal)
})
.map(|(i, _)| i);
if best_unchoked.is_some() {
return best_unchoked;
}
peers
.iter()
.enumerate()
.filter(|(_, p)| p.is_eligible_for_selection())
.max_by(|(_, a), (_, b)| {
a.download_speed
.partial_cmp(&b.download_speed)
.unwrap_or(std::cmp::Ordering::Equal)
})
.map(|(i, _)| i)
})
}