Skip to content

Commit

Permalink
emit newexternaladdr with discovered
Browse files Browse the repository at this point in the history
  • Loading branch information
hopinheimer committed Dec 18, 2024
1 parent 6a6fa12 commit 35c9814
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion protocols/mdns/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use std::{
task::{Context, Poll},
time::Instant,
};

use std::collections::VecDeque;
use futures::{channel::mpsc, Stream, StreamExt};
use if_watch::IfEvent;
use libp2p_core::{transport::PortUse, Endpoint, Multiaddr};
Expand Down Expand Up @@ -188,6 +188,9 @@ where
listen_addresses: Arc<RwLock<ListenAddresses>>,

local_peer_id: PeerId,

/// Pending behaviour events to be emitted.
pending_events: VecDeque<Event>,
}

impl<P> Behaviour<P>
Expand All @@ -208,6 +211,7 @@ where
closest_expiration: Default::default(),
listen_addresses: Default::default(),
local_peer_id,
pending_events: Default::default(),
})
}

Expand Down Expand Up @@ -304,6 +308,11 @@ where
&mut self,
cx: &mut Context<'_>,
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
// Checking for pending events and emit them
if let Some(event) = self.pending_events.pop_front() {
return Poll::Ready(ToSwarm::GenerateEvent(event));
}

// Poll ifwatch.
while let Poll::Ready(Some(event)) = Pin::new(&mut self.if_watch).poll_next(cx) {
match event {
Expand Down Expand Up @@ -360,6 +369,8 @@ where
tracing::info!(%peer, address=%addr, "discovered peer on address");
self.discovered_nodes.push((peer, addr.clone(), expiration));
discovered.push((peer, addr));

self.pending_events.push_back(Event::NewExternalAddr(addr.clone()));
}
}

Expand Down Expand Up @@ -400,6 +411,9 @@ pub enum Event {
/// Discovered nodes through mDNS.
Discovered(Vec<(PeerId, Multiaddr)>),

/// The multiaddress is reachable externally.
NewExternalAddr(Multiaddr),

/// The given combinations of `PeerId` and `Multiaddr` have expired.
///
/// Each discovered record has a time-to-live. When this TTL expires and the address hasn't
Expand Down

0 comments on commit 35c9814

Please sign in to comment.