From ecf81d9f937cd974edc810dff80f70454efa2c6b Mon Sep 17 00:00:00 2001 From: draoi Date: Wed, 28 Feb 2024 13:19:22 +0100 Subject: [PATCH] net: make a generic base class for PeerDiscovery Making PeerDiscovery generic means that in the future we can have different types of PeerDiscovery. Specifically, swarming requires us to have 2 kinds of Peer Discovery, a an overlay network PeerDiscovery (like the one we have currency), and a PeerDiscovery for subnets that looks for subnets within the overlay network. This paves the way for future changes of this nature --- src/net/session/outbound_session.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/net/session/outbound_session.rs b/src/net/session/outbound_session.rs index 26ccf12be..5c8ae7d3a 100644 --- a/src/net/session/outbound_session.rs +++ b/src/net/session/outbound_session.rs @@ -436,6 +436,25 @@ impl Slot { } } +/// TODO: doc +#[async_trait] +pub trait PeerDiscoveryBase { + async fn start(self: Arc); + + async fn stop(self: Arc); + + async fn run(self: Arc); + + async fn wait(&self) -> bool; + + fn notify(&self); + + fn session(&self) -> OutboundSessionPtr; + + fn p2p(&self) -> P2pPtr; +} + +/// TODO: doc struct PeerDiscovery { process: StoppableTaskPtr, wakeup_self: CondVar, @@ -450,7 +469,10 @@ impl PeerDiscovery { session: LazyWeak::new(), }) } +} +#[async_trait] +impl PeerDiscoveryBase for PeerDiscovery { async fn start(self: Arc) { let ex = self.p2p().executor(); self.process.clone().start( @@ -610,10 +632,10 @@ impl PeerDiscovery { fn notify(&self) { self.wakeup_self.notify() } - fn session(&self) -> OutboundSessionPtr { self.session.upgrade() } + fn p2p(&self) -> P2pPtr { self.session().p2p() }