Skip to content
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

Add a custom derive for NetworkBehaviour #619

Merged
merged 10 commits into from
Nov 12, 2018

Conversation

tomaka
Copy link
Member

@tomaka tomaka commented Nov 8, 2018

Adds the possibility to do this:

#[derive(NetworkBehaviour)]
struct MyBehaviour<TSubstream> {
    ping: PingListenerBehaviour<TSubstream>,
    identify: PeriodicIdentifyBehaviour<TSubstream>,
    floodsub: FloodsubBehaviour<TSubstream>,
}

And this automatically implements NetworkBehaviour on MyBehaviour.

Note that the events generated by the fields are ignored by default. To handle them, the user can do this:

#[derive(NetworkBehaviour)]
struct MyBehaviour<TSubstream> {
    ping: PingListenerBehaviour<TSubstream>,
    identify: PeriodicIdentifyBehaviour<TSubstream>,
    #[behaviour(handler = "foo")]
    floodsub: FloodsubBehaviour<TSubstream>,
}

impl<TSubstream> MyBehaviour<TSubstream> {
    // Called whenever the floodsub layer generates a message.
    fn foo(&mut self, event: FloodsubMessage) {
        // ...
    }
}

The one thing that's missing is the situation where the user wants to generate events from their behaviour. This needs thoughts as to how to do it.

@tomaka tomaka mentioned this pull request Nov 8, 2018
24 tasks
@tomaka tomaka changed the title [WIP] Add a custom derive for NetworkBehaviour Add a custom derive for NetworkBehaviour Nov 9, 2018
@tomaka tomaka removed the in progress label Nov 9, 2018
@tomaka tomaka force-pushed the derive-net-behaviour branch from d4091f5 to b4d0b04 Compare November 10, 2018 14:19
Copy link
Contributor

@twittner twittner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Impressive!

@tomaka tomaka merged commit 623728b into libp2p:master Nov 12, 2018
@tomaka tomaka deleted the derive-net-behaviour branch November 12, 2018 16:12
dvdplm added a commit to dvdplm/rust-libp2p that referenced this pull request Nov 14, 2018
…e-handled_node_tasks

* upstream/master:
  Tweaks, spelling and grammar (libp2p#629)
  Add a badge with a link to deps.rs (libp2p#630)
  Rewrite floodsub to use the ProtocolsHandler (libp2p#603)
  Add an IdentifyListen behaviour (libp2p#626)
  Add a custom derive for NetworkBehaviour (libp2p#619)
  Set the maximum size of Mplex messages to 1Mb (libp2p#622)
  Use expect rather than unwrap (libp2p#625)
  Make libp2p-websocket optional (libp2p#624)
  Add From<IpAddr> for Multiaddr (libp2p#623)
  Add implementations of NetworkBehaviour for ping (libp2p#618)
  Add a PeriodicIdentifyBehaviour (libp2p#617)
  Use upstream rust-secp256k1 (libp2p#616)
  Use yamux and aio-limited from crates.io (libp2p#621)
dvdplm added a commit to dvdplm/rust-libp2p that referenced this pull request Nov 14, 2018
…st-collection_stream

* dp/chore/test-core-handled_node_tasks: (24 commits)
  Revert changes to Debug impl for HandledNodesTasks
  whitespace
  Don't organise tests in submodules
  Rename test builders
  Tweaks, spelling and grammar (libp2p#629)
  Add a badge with a link to deps.rs (libp2p#630)
  Rewrite floodsub to use the ProtocolsHandler (libp2p#603)
  Add an IdentifyListen behaviour (libp2p#626)
  Add a custom derive for NetworkBehaviour (libp2p#619)
  Set the maximum size of Mplex messages to 1Mb (libp2p#622)
  Use expect rather than unwrap (libp2p#625)
  Make libp2p-websocket optional (libp2p#624)
  Add From<IpAddr> for Multiaddr (libp2p#623)
  Add implementations of NetworkBehaviour for ping (libp2p#618)
  Add a PeriodicIdentifyBehaviour (libp2p#617)
  Use upstream rust-secp256k1 (libp2p#616)
  Use yamux and aio-limited from crates.io (libp2p#621)
  Remove tests for Task we don't need Test Task.send_event() and id() using a HandledNodesTasks
  Better debug impl for HandledNodesTasks
  Address grumbles
  ...
dvdplm added a commit to dvdplm/rust-libp2p that referenced this pull request Nov 16, 2018
…_swarm

* upstream/master: (33 commits)
  Behavior -> Behaviour (libp2p#650)
  Remove Send/Sync/'static reqs from ping (libp2p#652)
  Tests for CollectionStream (libp2p#588)
  Document the muxing module (libp2p#647)
  Document the core::upgrade module (libp2p#644)
  Send Close when shutting down gracefully, as per spec (libp2p#631)
  Remove the Send + 'static requirements for floodsub (libp2p#637)
  Move protocols_handler to the root (libp2p#643)
  Use a random sequence number in floodsub (libp2p#634)
  Split `ConnectionUpgrade`. (libp2p#642)
  Add an impl Debug for HandledNode (libp2p#628)
  minor typo in docs (libp2p#640)
  Update connected peers' topics on NodeEvent (libp2p#638)
  Fix the concerns of libp2p#603 (libp2p#635)
  Tests for HandledNodesTasks (libp2p#584)
  Tweaks, spelling and grammar (libp2p#629)
  Add a badge with a link to deps.rs (libp2p#630)
  Rewrite floodsub to use the ProtocolsHandler (libp2p#603)
  Add an IdentifyListen behaviour (libp2p#626)
  Add a custom derive for NetworkBehaviour (libp2p#619)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants