diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-12-29 20:13:22 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-12-29 20:13:24 +0100 |
commit | 41154413d411048207cce3c22d68442d795e6a6f (patch) | |
tree | c176faac85a2f17b90a00bcce204f2989a327983 /lib/src/gossip/handler.rs | |
parent | 6265a4a9372e651732f53cd6a129dd50e491245e (diff) |
Remove the GossipHandler, handle in code directly
Use the iced idea of how to act on events rather than having a type for
handling gossip messages.
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'lib/src/gossip/handler.rs')
-rw-r--r-- | lib/src/gossip/handler.rs | 73 |
1 files changed, 0 insertions, 73 deletions
diff --git a/lib/src/gossip/handler.rs b/lib/src/gossip/handler.rs deleted file mode 100644 index e524da8..0000000 --- a/lib/src/gossip/handler.rs +++ /dev/null @@ -1,73 +0,0 @@ -//! Low-level module for gossip'ing code -//! -//! This module implements the low-level gossiping functionality that other modules use to -//! implement actual behaviours on -//! - -use std::sync::Arc; - -use anyhow::Result; -use futures::Stream; -use futures::StreamExt; -use tokio::sync::RwLock; - -use crate::profile::Profile; -use crate::gossip::GossipMessage; - -#[derive(Debug)] -pub struct GossipHandler<Strategy = LogStrategy> - where Strategy: GossipHandlingStrategy + Sync + Send -{ - profile: Arc<RwLock<Profile>>, - strategy: std::marker::PhantomData<Strategy>, -} - -impl<Strat> GossipHandler<Strat> - where Strat: GossipHandlingStrategy + Sync + Send -{ - pub fn new(profile: Arc<RwLock<Profile>>) -> Self { - Self { - profile, - strategy: std::marker::PhantomData, - } - } - - pub fn run<S>(self, input: S) -> impl Stream<Item = (GossipMessage, Result<()>)> - where S: Stream<Item = (ipfs::PeerId, GossipMessage)> - { - input.then(move |(source, msg)| { - let pr = self.profile.clone(); - async move { - log::trace!("Received gossip message from {}: {:?}", source, msg); - let res = Strat::handle_gossip_message(pr.clone(), &source, &msg).await; - (msg, res) - } - }) - } -} - -#[async_trait::async_trait] -pub trait GossipHandlingStrategy: Sync + Send { - async fn handle_gossip_message(profile: Arc<RwLock<Profile>>, source: &ipfs::PeerId, msg: &GossipMessage) -> Result<()>; -} - -pub struct LogStrategy; - -#[async_trait::async_trait] -impl GossipHandlingStrategy for LogStrategy { - async fn handle_gossip_message(_profile: Arc<RwLock<Profile>>, source: &ipfs::PeerId, msg: &GossipMessage) -> Result<()> { - use std::convert::TryFrom; - use std::ops::Deref; - - match msg { - GossipMessage::CurrentProfileState { peer_id, cid } => { - let peer_id = ipfs::PeerId::from_bytes(peer_id); - let cid = cid::Cid::try_from(cid.deref()); - - log::trace!("{:?} told me that {:?} is at {:?}", source, peer_id, cid); - } - } - - Ok(()) - } -} |