use std::net::SocketAddr;
use tracing::trace;
use super::super::node_id::NodeId;
use super::DhtTask;
pub struct DhtAnnounceTask {
info_hash: NodeId,
announce_targets: Vec<(SocketAddr, Vec<u8>)>,
next_target: usize,
started: bool,
done: bool,
}
impl DhtAnnounceTask {
pub fn new(info_hash: NodeId, tokens: Vec<(SocketAddr, Vec<u8>)>) -> Self {
Self {
info_hash,
announce_targets: tokens,
next_target: 0,
started: false,
done: false,
}
}
pub fn info_hash(&self) -> &NodeId {
&self.info_hash
}
pub fn next_announce_target(&mut self) -> Option<(SocketAddr, Vec<u8>)> {
if self.next_target >= self.announce_targets.len() {
return None;
}
let target = self.announce_targets[self.next_target].clone();
self.next_target += 1;
if self.next_target >= self.announce_targets.len() {
self.done = true;
}
Some(target)
}
#[allow(dead_code)]
pub fn total_targets(&self) -> usize {
self.announce_targets.len()
}
#[allow(dead_code)]
pub fn remaining_targets(&self) -> usize {
self.announce_targets.len().saturating_sub(self.next_target)
}
}
impl DhtTask for DhtAnnounceTask {
fn startup(&mut self) {
self.started = true;
trace!(
info_hash = %self.info_hash,
targets = self.announce_targets.len(),
"Starting DHT announce task"
);
if self.announce_targets.is_empty() {
self.done = true;
}
}
fn finished(&self) -> bool {
self.done
}
}