Skip to content

Commit

Permalink
added behavior event when local protocols change
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Huseby <dwh@linuxprogrammer.org>
  • Loading branch information
dhuseby committed Aug 18, 2023
1 parent cbdbaa8 commit a7ae23c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
13 changes: 12 additions & 1 deletion protocols/identify/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use libp2p_identity::PublicKey;
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm};
use libp2p_swarm::{
ConnectionDenied, DialError, ExternalAddresses, ListenAddresses, NetworkBehaviour,
NotifyHandler, PollParameters, StreamUpgradeError, THandlerInEvent, ToSwarm,
NotifyHandler, PollParameters, StreamProtocol, StreamUpgradeError, THandlerInEvent, ToSwarm,
};
use libp2p_swarm::{ConnectionId, THandler, THandlerOutEvent};
use lru::LruCache;
Expand Down Expand Up @@ -309,6 +309,10 @@ impl NetworkBehaviour for Behaviour {
self.events
.push_back(ToSwarm::GenerateEvent(Event::Error { peer_id, error }));
}

Check failure on line 311 in protocols/identify/src/behaviour.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/protocols/identify/src/behaviour.rs
handler::Event::LocalProtocolsChanged(protocols) => {
self.events
.push_back(ToSwarm::GenerateEvent(Event::LocalProtocolsChanged { peer_id, protocols }));
}
}
}

Expand Down Expand Up @@ -439,6 +443,13 @@ pub enum Event {
/// The error that occurred.
error: StreamUpgradeError<UpgradeError>,
},
/// Local protocols have changed.
LocalProtocolsChanged {
/// The peer we are advertising protocols to.
peer_id: PeerId,
/// The list of local supported protocols we are advertising to the peer.
protocols: Vec<StreamProtocol>,
},
}

/// If there is a given peer_id in the multiaddr, make sure it is the same as
Expand Down
35 changes: 22 additions & 13 deletions protocols/identify/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ pub enum Event {
IdentificationPushed,
/// Failed to identify the remote, or to reply to an identification request.
IdentificationError(StreamUpgradeError<UpgradeError>),
/// Local supported protocols changed
LocalProtocolsChanged(Vec<StreamProtocol>),
}

impl Handler {
Expand Down Expand Up @@ -370,20 +372,27 @@ impl ConnectionHandler for Handler {
.then(|| self.local_protocols_to_string())
.unwrap_or_default();

Check failure on line 373 in protocols/identify/src/handler.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/protocols/identify/src/handler.rs

if protocols_changed && self.exchanged_one_periodic_identify {
log::debug!(
"Supported listen protocols changed from [{before}] to [{after}], pushing to {}",
self.remote_peer_id
);

let info = self.build_info();
if protocols_changed {
self.events
.push(ConnectionHandlerEvent::OutboundSubstreamRequest {
protocol: SubstreamProtocol::new(
Either::Right(Push::outbound(info)),
(),
),
});
.push(ConnectionHandlerEvent::NotifyBehaviour(Event::LocalProtocolsChanged(
Vec::from_iter(self.local_supported_protocols.iter().cloned())
)));

if self.exchanged_one_periodic_identify {
log::debug!(
"Supported listen protocols changed from [{before}] to [{after}], pushing to {}",
self.remote_peer_id
);

let info = self.build_info();
self.events
.push(ConnectionHandlerEvent::OutboundSubstreamRequest {
protocol: SubstreamProtocol::new(
Either::Right(Push::outbound(info)),
(),
),
});
}
}
}
}
Expand Down

0 comments on commit a7ae23c

Please sign in to comment.