Skip to main content

rings_core/message/handlers/
subring.rs

1#![warn(missing_docs)]
2use async_trait::async_trait;
3
4use super::storage::handle_storage_store_act;
5use crate::dht::ChordStorage;
6use crate::dht::PeerRing;
7use crate::error::Result;
8use crate::prelude::entry::EntryOperation;
9use crate::swarm::Swarm;
10
11/// SubringInterface should imply necessary operator for DHT Subring
12#[cfg_attr(feature = "wasm", async_trait(?Send))]
13#[cfg_attr(not(feature = "wasm"), async_trait)]
14pub trait SubringInterface<const REDUNDANT: u16> {
15    /// join a subring
16    async fn subring_join(&self, name: &str) -> Result<()>;
17}
18
19#[cfg_attr(feature = "wasm", async_trait(?Send))]
20#[cfg_attr(not(feature = "wasm"), async_trait)]
21impl<const REDUNDANT: u16> SubringInterface<REDUNDANT> for Swarm {
22    /// add did into current chord subring.
23    /// send direct message with `JoinSubring` type, which will handled by `next` node.
24    async fn subring_join(&self, name: &str) -> Result<()> {
25        let op = EntryOperation::JoinSubring(name.to_string(), self.dht.did);
26        let act = <PeerRing as ChordStorage<_, REDUNDANT>>::entry_operate(&self.dht, op).await?;
27        handle_storage_store_act(self.transport.clone(), act).await?;
28        Ok(())
29    }
30}