-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(swarm): allow NetworkBehaviours to create and remove listeners
#3292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
4f3b233
feat/networkbehaviour: Added ListenOn and RemoveListener
dariusc93 d7cc720
Merge branch 'master' into networkbehaviour-listen
dariusc93 14d0c49
Merge branch 'master' into networkbehaviour-listen
dariusc93 518295d
Merge branch 'master' into networkbehaviour-listen
dariusc93 f9f50f8
Merge branch 'networkbehaviour-listen' of github.com:dariusc93/rust-l…
dariusc93 ed11db4
Merge branch 'master' into networkbehaviour-listen
dariusc93 eaff6f0
Merge branch 'master' into networkbehaviour-listen
dariusc93 d104545
Merge branch 'master' into networkbehaviour-listen
dariusc93 1892fd8
Merge branch 'master' into networkbehaviour-listen
dariusc93 ee4cf30
chore: Use ListenerId in `ToSwarm::ListenOn`
dariusc93 a5331cb
Update swarm-derive/src/lib.rs
dariusc93 ced5bb0
Update swarm/src/behaviour.rs
dariusc93 9d2db40
Update swarm/src/behaviour.rs
dariusc93 b26f4fc
Merge branch 'master' into networkbehaviour-listen
dariusc93 84895ef
Merge branch 'master' into networkbehaviour-listen
dariusc93 ad8856e
chore: Send ListenerError to behaviour
dariusc93 df1a19c
Merge branch 'master' into networkbehaviour-listen
dariusc93 ec128ac
Update swarm/src/behaviour.rs
thomaseizinger 97be0c6
chore: Update CHANGELOG.md
dariusc93 8cbf386
chore: Update comments
dariusc93 6c163a6
Merge branch 'master' into networkbehaviour-listen
dariusc93 b326e32
Merge branch 'master' into networkbehaviour-listen
dariusc93 0b0f04e
Merge branch 'master' into networkbehaviour-listen
dariusc93 ae1089f
Merge branch 'master' into networkbehaviour-listen
dariusc93 590f198
Merge branch 'master' into networkbehaviour-listen
dariusc93 97ec46d
chore: Return listener error to behaviour
dariusc93 48523bf
Merge branch 'master' into networkbehaviour-listen
dariusc93 fa64bca
chore: emit event when calling ToSwarm::ListenOn
dariusc93 4c9b192
Merge branch 'networkbehaviour-listen' of github.com:dariusc93/rust-l…
dariusc93 124a150
chore: Add Swarm::add_listener
dariusc93 1f6a798
chore: Add test
dariusc93 15fd6e4
chore: Rename test function
dariusc93 c8968e0
chore: Formatted code
dariusc93 1e40992
chore: Updated test
dariusc93 82a9898
Merge branch 'master' into networkbehaviour-listen
dariusc93 4d2946d
Merge branch 'master' into networkbehaviour-listen
dariusc93 d5e36f5
Merge branch 'master' into networkbehaviour-listen
dariusc93 3e09dc0
Merge branch 'master' into networkbehaviour-listen
dariusc93 2666615
Merge branch 'master' into networkbehaviour-listen
dariusc93 95e61ab
chore: Update test
dariusc93 f880ba2
chore: Move code into add_listener
dariusc93 7068f12
fix: Move listen on into add_listener
dariusc93 0bd41ab
chore: Added ListenOpts
dariusc93 f4c196f
chore: Formatted code
dariusc93 bc789b2
chore: re-export ListenOpts
dariusc93 5ea58d1
chore: Updated code
dariusc93 603933c
chore: Added comment
dariusc93 08d7659
chore: Formatted code
dariusc93 92bf3aa
Merge branch 'master' into networkbehaviour-listen
dariusc93 64d0ad4
Merge branch 'master' into networkbehaviour-listen
dariusc93 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| use crate::ListenerId; | ||
| use libp2p_core::Multiaddr; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct ListenOpts { | ||
| id: ListenerId, | ||
| address: Multiaddr, | ||
| } | ||
|
|
||
| impl ListenOpts { | ||
| pub fn new(address: Multiaddr) -> ListenOpts { | ||
| ListenOpts { | ||
| id: ListenerId::next(), | ||
| address, | ||
| } | ||
| } | ||
|
|
||
| /// Get the [`ListenerId`] of this listen attempt | ||
| pub fn listener_id(&self) -> ListenerId { | ||
| self.id | ||
| } | ||
|
|
||
| /// Get the [`Multiaddr`] that is being listened on | ||
| pub fn address(&self) -> &Multiaddr { | ||
| &self.address | ||
| } | ||
| } | ||
|
|
||
| impl From<Multiaddr> for ListenOpts { | ||
| fn from(addr: Multiaddr) -> Self { | ||
| ListenOpts::new(addr) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| use std::{ | ||
| collections::{HashSet, VecDeque}, | ||
| task::{Context, Poll}, | ||
| }; | ||
|
|
||
| use libp2p_core::{multiaddr::Protocol, transport::ListenerId, Endpoint, Multiaddr}; | ||
| use libp2p_identity::PeerId; | ||
| use libp2p_swarm::{ | ||
| derive_prelude::NewListener, dummy, ConnectionDenied, ConnectionId, FromSwarm, ListenOpts, | ||
| ListenerClosed, ListenerError, NetworkBehaviour, NewListenAddr, PollParameters, Swarm, | ||
| SwarmEvent, THandler, THandlerInEvent, THandlerOutEvent, ToSwarm, | ||
| }; | ||
|
|
||
| use libp2p_swarm_test::SwarmExt; | ||
|
|
||
| #[async_std::test] | ||
| async fn behaviour_listener() { | ||
| let mut swarm = Swarm::new_ephemeral(|_| Behaviour::default()); | ||
| let addr: Multiaddr = Protocol::Memory(0).into(); | ||
| let id = swarm.behaviour_mut().listen(addr.clone()); | ||
|
|
||
| let address = swarm | ||
| .wait(|e| match e { | ||
| SwarmEvent::NewListenAddr { | ||
| listener_id, | ||
| address, | ||
| } => { | ||
| assert_eq!(listener_id, id); | ||
| Some(address) | ||
| } | ||
| _ => None, | ||
| }) | ||
| .await; | ||
|
|
||
| swarm.behaviour_mut().stop_listening(id); | ||
|
|
||
| swarm | ||
| .wait(|e| match e { | ||
| SwarmEvent::ListenerClosed { | ||
| listener_id, | ||
| addresses, | ||
| reason, | ||
| } => { | ||
| assert_eq!(listener_id, id); | ||
| assert!(addresses.contains(&address)); | ||
| assert!(reason.is_ok()); | ||
| Some(()) | ||
| } | ||
| _ => None, | ||
| }) | ||
| .await; | ||
| } | ||
|
|
||
| #[derive(Default)] | ||
| struct Behaviour { | ||
| events: VecDeque<ToSwarm<<Self as NetworkBehaviour>::ToSwarm, THandlerInEvent<Self>>>, | ||
| listeners: HashSet<ListenerId>, | ||
| } | ||
|
|
||
| impl Behaviour { | ||
| pub(crate) fn listen(&mut self, addr: Multiaddr) -> ListenerId { | ||
| let opts = ListenOpts::new(addr); | ||
| let listener_id = opts.listener_id(); | ||
| assert!(!self.listeners.contains(&listener_id)); | ||
| self.events.push_back(ToSwarm::ListenOn { opts }); | ||
| self.listeners.insert(listener_id); | ||
|
|
||
| listener_id | ||
| } | ||
|
|
||
| pub(crate) fn stop_listening(&mut self, id: ListenerId) { | ||
| self.events.push_back(ToSwarm::RemoveListener { id }); | ||
| } | ||
| } | ||
|
|
||
| impl NetworkBehaviour for Behaviour { | ||
| type ConnectionHandler = dummy::ConnectionHandler; | ||
| type ToSwarm = void::Void; | ||
|
|
||
| fn handle_established_inbound_connection( | ||
| &mut self, | ||
| _: ConnectionId, | ||
| _: PeerId, | ||
| _: &Multiaddr, | ||
| _: &Multiaddr, | ||
| ) -> Result<libp2p_swarm::THandler<Self>, ConnectionDenied> { | ||
| Ok(dummy::ConnectionHandler) | ||
| } | ||
|
|
||
| fn handle_established_outbound_connection( | ||
| &mut self, | ||
| _: ConnectionId, | ||
| _: PeerId, | ||
| _: &Multiaddr, | ||
| _: Endpoint, | ||
| ) -> Result<THandler<Self>, ConnectionDenied> { | ||
| Ok(dummy::ConnectionHandler) | ||
| } | ||
|
|
||
| fn on_connection_handler_event( | ||
| &mut self, | ||
| _: PeerId, | ||
| _: ConnectionId, | ||
| _: THandlerOutEvent<Self>, | ||
| ) { | ||
| } | ||
|
|
||
| fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) { | ||
| match event { | ||
| FromSwarm::NewListener(NewListener { listener_id }) => { | ||
| assert!(self.listeners.contains(&listener_id)); | ||
| } | ||
| FromSwarm::NewListenAddr(NewListenAddr { listener_id, .. }) => { | ||
| assert!(self.listeners.contains(&listener_id)); | ||
| } | ||
| FromSwarm::ListenerError(ListenerError { listener_id, err }) => { | ||
| panic!("Error for listener {listener_id:?}: {err}"); | ||
| } | ||
| FromSwarm::ListenerClosed(ListenerClosed { | ||
| listener_id, | ||
| reason, | ||
| }) => { | ||
| assert!(self.listeners.contains(&listener_id)); | ||
| assert!(reason.is_ok()); | ||
| self.listeners.remove(&listener_id); | ||
| assert!(!self.listeners.contains(&listener_id)); | ||
| } | ||
| _ => {} | ||
| } | ||
| } | ||
|
|
||
| fn poll( | ||
| &mut self, | ||
| _: &mut Context<'_>, | ||
| _: &mut impl PollParameters, | ||
| ) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> { | ||
| if let Some(event) = self.events.pop_front() { | ||
| return Poll::Ready(event); | ||
| } | ||
|
|
||
| Poll::Pending | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.