summaryrefslogtreecommitdiff
path: root/client/src/download.rs
blob: e65ef2528225e46a31380820d9631cd3f958735a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
    wearechat - generic multiplayer game with voip
    Copyright (C) 2025 metamuffin

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, version 3 of the License only.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/
use crate::network::Network;
use anyhow::Result;
use log::debug;
use std::{collections::HashSet, marker::PhantomData};
use weareshared::{
    packets::{Packet, ReadWrite, Resource},
    store::{ResourceStore, sha256},
};

pub struct Downloader {
    have: HashSet<Resource>,
    need: HashSet<Resource>,
    pending: HashSet<Resource>,
    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<T: ReadWrite>(&mut self, hash: Resource<T>) -> Result<Option<T>> {
        self.try_get_raw(Resource(hash.0, PhantomData))?
            .map(|x| T::read(&mut x.as_slice()))
            .transpose()
    }
    pub fn try_get_raw(&mut self, hash: Resource) -> Result<Option<Vec<u8>>> {
        if self.have.contains(&hash) {
            self.store.get_raw(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.0), PhantomData);
                self.store.set_raw(&d.0)?;
                self.need.remove(&key);
                self.pending.remove(&key);
                if self.have.insert(key) {
                    debug!("have {key}");
                }
            }
            _ => (),
        }
        Ok(())
    }
    pub fn update(&mut self, network: &mut Network) -> Result<()> {
        let mut new_pending = Vec::new();
        for n in self.need.difference(&self.pending) {
            network
                .packet_send
                .send(Packet::RequestResource(*n))
                .unwrap();
            debug!("need {n}");
            new_pending.push(*n);
        }
        self.pending.extend(new_pending);
        Ok(())
    }
}