summaryrefslogtreecommitdiffstats
path: root/lib/src/reactor/gossip/strategy.rs
blob: b54dddc205068cfefa163a978987ead9b0053652 (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
use std::sync::Arc;

use anyhow::Result;
use tokio::sync::RwLock;

use crate::profile::Profile;
use crate::reactor::gossip::msg::GossipMessage;

#[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;
        match msg {
            GossipMessage::CurrentProfileState { peer_id, cid } => {
                let peer_id = ipfs::PeerId::from_bytes(&peer_id);
                let cid = cid::Cid::try_from(&*cid);

                log::trace!("{:?} told me that {:?} is at {:?}", source, peer_id, cid);
            }
        }

        Ok(())
    }
}