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
This commit is contained in:
draoi
2024-02-28 13:19:22 +01:00
parent 532168fea8
commit ecf81d9f93

View File

@@ -436,6 +436,25 @@ impl Slot {
}
}
/// TODO: doc
#[async_trait]
pub trait PeerDiscoveryBase {
async fn start(self: Arc<Self>);
async fn stop(self: Arc<Self>);
async fn run(self: Arc<Self>);
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<Self>) {
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()
}