From e15b39b2a9cf028b12cbe98f56674e58c5a6bd4c Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sun, 5 Jan 2025 22:27:53 +0100 Subject: a --- client/src/download.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 client/src/download.rs (limited to 'client/src/download.rs') diff --git a/client/src/download.rs b/client/src/download.rs new file mode 100644 index 0000000..256c25c --- /dev/null +++ b/client/src/download.rs @@ -0,0 +1,58 @@ +use anyhow::Result; +use std::collections::HashSet; +use weareshared::{ + packets::{Packet, Resource}, + store::{ResourceStore, sha256}, +}; + +use crate::network::Network; + +pub struct Downloader { + have: HashSet, + need: HashSet, + pending: HashSet, + store: ResourceStore, +} + +impl Downloader { + pub fn new(store: ResourceStore) -> Self { + Self { + have: HashSet::new(), + need: HashSet::new(), + pending: HashSet::new(), + store, + } + } + pub fn try_get(&mut self, hash: Resource) -> Result>> { + if self.have.contains(&hash) { + self.store.get(hash) + } else { + self.need.insert(hash); + Ok(None) + } + } + pub fn packet(&mut self, p: &Packet) -> Result<()> { + match p { + Packet::RespondResource(d) => { + let key = Resource(sha256(&d)); + self.store.set(&d)?; + self.need.remove(&key); + self.pending.remove(&key); + self.have.insert(key); + } + _ => (), + } + Ok(()) + } + pub fn update(&mut self, network: &mut Network) { + let mut new_pending = Vec::new(); + for n in self.need.difference(&self.pending) { + network + .packet_send + .send(Packet::RequestResource(*n)) + .unwrap(); + new_pending.push(*n); + } + self.pending.extend(new_pending); + } +} -- cgit v1.2.3-70-g09d2