diff options
Diffstat (limited to 'src/swarm/speaking.rs')
| -rw-r--r-- | src/swarm/speaking.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/swarm/speaking.rs b/src/swarm/speaking.rs new file mode 100644 index 0000000..dcbfac4 --- /dev/null +++ b/src/swarm/speaking.rs @@ -0,0 +1,46 @@ +//! What the peers' builds speak and know, as each says with its handshakes (SPEAKS, +//! `transport::Speaks`): the wire versions it speaks and the newest revision of the +//! federation it knows. A peer that sends none runs a build from before SPEAKS, or isn't +//! heard from yet. + +use std::collections::BTreeMap; + +use crate::locks::MutexExt; +use crate::transport::{Speaks, wire}; + +/// How many peers' SPEAKS are kept: anyone can send one. +const MOST: usize = 4096; + +/// What this build speaks and knows. +pub(crate) fn own() -> Speaks { + Speaks { + oldest: wire::OLDEST, + newest: wire::VERSION, + revision: newest_revision(), + } +} + +#[cfg(feature = "federation")] +fn newest_revision() -> Option<[u8; 32]> { + Some(crate::federation::revision::Revision::newest().id().0) +} + +#[cfg(not(feature = "federation"))] +fn newest_revision() -> Option<[u8; 32]> { + None +} + +/// The SPEAKS each peer sent last, by node ID. +#[derive(Default)] +pub(crate) struct Speaking(std::sync::Mutex<BTreeMap<String, Speaks>>); + +impl Speaking { + /// `peer` said `speaks`. + pub(crate) fn heard(&self, peer: &str, speaks: Speaks) { + let mut heard = self.0.lock_or_recover(); + if heard.len() >= MOST && !heard.contains_key(peer) { + heard.clear(); + } + heard.insert(peer.to_owned(), speaks); + } +} |
