diff --git a/Cargo.lock b/Cargo.lock index 73d9b169080..257d03dc90e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -445,6 +445,17 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "autonat-example" +version = "0.1.0" +dependencies = [ + "async-std", + "clap 4.1.11", + "env_logger 0.10.0", + "futures", + "libp2p", +] + [[package]] name = "base-x" version = "0.2.11" @@ -2213,20 +2224,15 @@ version = "0.10.1" dependencies = [ "async-std", "async-trait", - "clap 4.1.11", "env_logger 0.10.0", "futures", "futures-timer", "instant", "libp2p-core", - "libp2p-identify", "libp2p-identity", - "libp2p-noise", "libp2p-request-response", "libp2p-swarm", "libp2p-swarm-test", - "libp2p-tcp", - "libp2p-yamux", "log", "quick-protobuf", "rand 0.8.5", diff --git a/Cargo.toml b/Cargo.toml index fcb6ccc0d41..dc28b91b3e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "core", "examples/chat-example", + "examples/autonat", "examples/dcutr", "examples/distributed-key-value-store", "examples/file-sharing", diff --git a/examples/README.md b/examples/README.md index a8a07be0cbf..3b347c4c7a2 100644 --- a/examples/README.md +++ b/examples/README.md @@ -7,7 +7,7 @@ A set of examples showcasing how to use rust-libp2p. ## Individual libp2p features -- [Chat](./chat-example) A basic chat application demonstrating libp2p and the mDNS and Gossipsub protocols. +- [Chat](./autonat) A basic chat application demonstrating libp2p and the mDNS and Gossipsub protocols. - [Distributed key-value store](./distributed-key-value-store) A basic key value store demonstrating libp2p and the mDNS and Kademlia protocol. - [File sharing application](./file-sharing) Basic file sharing application with peers either providing or locating and getting files by name. diff --git a/examples/autonat/Cargo.toml b/examples/autonat/Cargo.toml new file mode 100644 index 00000000000..655a39fb8d9 --- /dev/null +++ b/examples/autonat/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "autonat-example" +version = "0.1.0" +edition = "2021" +publish = false +license = "MIT" + +[dependencies] +async-std = { version = "1.12", features = ["attributes"] } +clap = { version = "4.1.11", features = ["derive"] } +env_logger = "0.10.0" +futures = "0.3.27" +libp2p = { path = "../../libp2p", features = ["async-std", "tcp", "noise", "yamux", "autonat", "identify", "macros"] } diff --git a/protocols/autonat/examples/autonat_client.rs b/examples/autonat/src/bin/autonat_client.rs similarity index 88% rename from protocols/autonat/examples/autonat_client.rs rename to examples/autonat/src/bin/autonat_client.rs index 9e09a6d556b..e934e76e521 100644 --- a/protocols/autonat/examples/autonat_client.rs +++ b/examples/autonat/src/bin/autonat_client.rs @@ -25,22 +25,16 @@ //! //! To run this example, follow the instructions in `examples/server` to start a server, then run in a new terminal: //! ```sh -//! cargo run --example client -- --server-address --server-peer-id --listen-port +//! cargo run --bin autonat_client -- --server-address --server-peer-id --listen-port //! ``` //! The `listen-port` parameter is optional and allows to set a fixed port at which the local client should listen. use clap::Parser; use futures::prelude::*; -use libp2p_autonat as autonat; -use libp2p_core::multiaddr::Protocol; -use libp2p_core::{upgrade::Version, Multiaddr, Transport}; -use libp2p_identify as identify; -use libp2p_identity as identity; -use libp2p_identity::PeerId; -use libp2p_noise as noise; -use libp2p_swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent}; -use libp2p_tcp as tcp; -use libp2p_yamux as yamux; +use libp2p::core::multiaddr::Protocol; +use libp2p::core::{upgrade::Version, Multiaddr, Transport}; +use libp2p::swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent}; +use libp2p::{autonat, identify, identity, noise, tcp, yamux, PeerId}; use std::error::Error; use std::net::Ipv4Addr; use std::time::Duration; @@ -99,7 +93,6 @@ async fn main() -> Result<(), Box> { } #[derive(NetworkBehaviour)] -#[behaviour(out_event = "Event", prelude = "libp2p_swarm::derive_prelude")] struct Behaviour { identify: identify::Behaviour, auto_nat: autonat::Behaviour, diff --git a/protocols/autonat/examples/autonat_server.rs b/examples/autonat/src/bin/autonat_server.rs similarity index 88% rename from protocols/autonat/examples/autonat_server.rs rename to examples/autonat/src/bin/autonat_server.rs index 22da0e8ee4f..7189420e467 100644 --- a/protocols/autonat/examples/autonat_server.rs +++ b/examples/autonat/src/bin/autonat_server.rs @@ -22,21 +22,15 @@ //! //! To start the server run: //! ```sh -//! cargo run --example server -- --listen-port +//! cargo run --bin autonat_server -- --listen-port //! ``` //! The `listen-port` parameter is optional and allows to set a fixed port at which the local peer should listen. use clap::Parser; use futures::prelude::*; -use libp2p_autonat as autonat; -use libp2p_core::{multiaddr::Protocol, upgrade::Version, Multiaddr, Transport}; -use libp2p_identify as identify; -use libp2p_identity as identity; -use libp2p_identity::PeerId; -use libp2p_noise as noise; -use libp2p_swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent}; -use libp2p_tcp as tcp; -use libp2p_yamux as yamux; +use libp2p::core::{multiaddr::Protocol, upgrade::Version, Multiaddr, Transport}; +use libp2p::swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent}; +use libp2p::{autonat, identify, identity, noise, tcp, yamux, PeerId}; use std::error::Error; use std::net::Ipv4Addr; @@ -83,7 +77,6 @@ async fn main() -> Result<(), Box> { } #[derive(NetworkBehaviour)] -#[behaviour(out_event = "Event", prelude = "libp2p_swarm::derive_prelude")] struct Behaviour { identify: identify::Behaviour, auto_nat: autonat::Behaviour, diff --git a/protocols/autonat/Cargo.toml b/protocols/autonat/Cargo.toml index 8fc4f15c1f4..150a791e648 100644 --- a/protocols/autonat/Cargo.toml +++ b/protocols/autonat/Cargo.toml @@ -25,13 +25,7 @@ quick-protobuf = "0.8" [dev-dependencies] async-std = { version = "1.10", features = ["attributes"] } -clap = { version = "4.1.11", features = ["derive"] } env_logger = "0.10" -libp2p-identify = { path = "../identify" } -libp2p-noise = { path = "../../transports/noise" } -libp2p-swarm = { path = "../../swarm", features = ["async-std", "macros"] } -libp2p-tcp = { path = "../../transports/tcp", features = ["async-io"] } -libp2p-yamux = { path = "../../muxers/yamux" } libp2p-swarm-test = { path = "../../swarm-test" } # Passing arguments to the docsrs builder in order to properly document cfg's. @@ -40,11 +34,3 @@ libp2p-swarm-test = { path = "../../swarm-test" } all-features = true rustdoc-args = ["--cfg", "docsrs"] rustc-args = ["--cfg", "docsrs"] - -[[example]] -name = "client" -path = "examples/autonat_client.rs" - -[[example]] -name = "server" -path = "examples/autonat_server.rs"