clawft_kernel/
mesh_artifact_peers.rs1use crate::mesh::MeshStream;
6use crate::mesh_artifact::ExchangeError;
7use crate::mesh_artifact_wire::{ArtifactDescriptor, ArtifactId, Bitfield};
8
9pub struct PeerLink {
11 pub peer_id: String,
13 pub stream: Box<dyn MeshStream>,
15 pub(crate) dead: bool,
16}
17
18impl PeerLink {
19 pub fn new(peer_id: impl Into<String>, stream: Box<dyn MeshStream>) -> Self {
21 Self {
22 peer_id: peer_id.into(),
23 stream,
24 dead: false,
25 }
26 }
27
28 pub fn is_dead(&self) -> bool {
30 self.dead
31 }
32}
33
34#[derive(Default)]
36pub struct PeerSet {
37 pub(crate) links: Vec<PeerLink>,
38}
39
40impl PeerSet {
41 pub fn new() -> Self {
43 Self::default()
44 }
45
46 pub fn single(link: PeerLink) -> Self {
48 Self { links: vec![link] }
49 }
50
51 pub fn push(&mut self, link: PeerLink) {
53 self.links.push(link);
54 }
55
56 pub fn len(&self) -> usize {
58 self.links.len()
59 }
60
61 pub fn is_empty(&self) -> bool {
63 self.links.is_empty()
64 }
65
66 pub fn links_mut(&mut self) -> &mut [PeerLink] {
68 &mut self.links
69 }
70}
71
72pub trait PieceScheduler: Send {
74 fn next_batch(&mut self, need: &Bitfield, peer_has: &Bitfield, max: usize) -> Vec<u32>;
77}
78
79#[derive(Debug, Default, Clone, Copy)]
81pub struct SequentialScheduler;
82
83impl PieceScheduler for SequentialScheduler {
84 fn next_batch(&mut self, need: &Bitfield, peer_has: &Bitfield, max: usize) -> Vec<u32> {
85 need.missing()
86 .filter(|&i| peer_has.get(i))
87 .take(max)
88 .collect()
89 }
90}
91
92#[derive(Debug, Clone, PartialEq, Eq)]
94pub struct FetchOutcome {
95 pub id: ArtifactId,
97 pub descriptor: ArtifactDescriptor,
99 pub pieces_fetched: u32,
101 pub bytes_fetched: u64,
103 pub pieces_rejected: u32,
105 pub sources: Vec<String>,
107}
108
109#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
111pub enum FetchError {
112 #[error("fetch incomplete: {missing} pieces missing after all peers ({last_error})")]
114 Incomplete {
115 missing: u32,
117 last_error: String,
119 },
120 #[error("fetched content failed verification: {0}")]
122 Verification(String),
123 #[error(transparent)]
125 Local(#[from] ExchangeError),
126}
127
128#[derive(Debug, Clone, Default, PartialEq, Eq)]
130pub struct ServeStats {
131 pub pieces_served: Vec<u32>,
133 pub bytes_served: u64,
135}