mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-30 02:01:35 +00:00
Move the examples to the facade crate (#197)
This commit is contained in:
@ -64,9 +64,9 @@ jobs:
|
|||||||
- restore_cache:
|
- restore_cache:
|
||||||
key: integration-test-cache
|
key: integration-test-cache
|
||||||
- run:
|
- run:
|
||||||
command: cargo run -p example --example ping-client -- /ip4/127.0.0.1/tcp/4001
|
command: cargo run -p libp2p --example ping-client -- /ip4/127.0.0.1/tcp/4001
|
||||||
- run:
|
- run:
|
||||||
command: cargo run -p example --example echo-dialer -- /ip4/127.0.0.1/tcp/10333
|
command: cargo run -p libp2p --example echo-dialer -- /ip4/127.0.0.1/tcp/10333
|
||||||
- save_cache:
|
- save_cache:
|
||||||
key: integration-test-cache
|
key: integration-test-cache
|
||||||
paths:
|
paths:
|
||||||
|
@ -3,7 +3,6 @@ members = [
|
|||||||
"circular-buffer",
|
"circular-buffer",
|
||||||
"datastore",
|
"datastore",
|
||||||
"dns",
|
"dns",
|
||||||
"example",
|
|
||||||
"floodsub",
|
"floodsub",
|
||||||
"identify",
|
"identify",
|
||||||
"kad",
|
"kad",
|
||||||
|
@ -1,27 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "example"
|
|
||||||
version = "0.1.0"
|
|
||||||
authors = ["pierre <pierre.krieger1708@gmail.com>"]
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
bigint = "4.2"
|
|
||||||
bytes = "0.4"
|
|
||||||
env_logger = "0.5.4"
|
|
||||||
futures = "0.1"
|
|
||||||
multiaddr = "0.3"
|
|
||||||
libp2p-mplex = { path = "../mplex" }
|
|
||||||
libp2p-identify = { path = "../identify" }
|
|
||||||
libp2p-kad = { path = "../kad" }
|
|
||||||
libp2p-floodsub = { path = "../floodsub" }
|
|
||||||
libp2p-peerstore = { path = "../peerstore" }
|
|
||||||
libp2p-ping = { path = "../ping" }
|
|
||||||
libp2p-relay = { path = "../relay" }
|
|
||||||
libp2p-secio = { path = "../secio" }
|
|
||||||
libp2p-core = { path = "../core" }
|
|
||||||
libp2p-tcp-transport = { path = "../tcp-transport" }
|
|
||||||
libp2p-websocket = { path = "../websocket" }
|
|
||||||
rand = "0.4"
|
|
||||||
structopt = "0.2"
|
|
||||||
tokio-core = "0.1"
|
|
||||||
tokio-io = "0.1"
|
|
||||||
tokio-stdin = "0.1"
|
|
@ -1,28 +0,0 @@
|
|||||||
# Examples
|
|
||||||
|
|
||||||
Running one of the examples:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
cargo run --example <name-of-the-example>
|
|
||||||
```
|
|
||||||
|
|
||||||
The follow examples exist:
|
|
||||||
|
|
||||||
- `echo-dialer` will attempt to connect to `/ip4/127.0.0.1/tcp/10333`, negotiate the `/echo/1.0.0`
|
|
||||||
protocol, then send the `"hello world"` message. Compatible with the `echo-server` example.
|
|
||||||
- `echo-server` will listen on `/ip4/0.0.0.0/tcp/10333`, negotiate the `/echo/1.0.0` protocol with
|
|
||||||
each incoming connection, then send back any entering message. Compatible with the `echo-dialer`
|
|
||||||
example.
|
|
||||||
- `ping-client` will try to connect to `/ip4/127.0.0.1/tcp/4001`, which is the default address of
|
|
||||||
your local IPFS node if you're running one. It will then open a substream and ping the node.
|
|
||||||
|
|
||||||
## How the keys were generated
|
|
||||||
|
|
||||||
The keys used in the examples were generated like this:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
openssl genrsa -out private.pem 2048
|
|
||||||
openssl rsa -in private.pem -outform DER -pubout -out public.der
|
|
||||||
openssl pkcs8 -in private.pem -topk8 -nocrypt -out private.pk8
|
|
||||||
rm private.pem # optional
|
|
||||||
```
|
|
@ -1,59 +0,0 @@
|
|||||||
// Copyright 2018 Parity Technologies (UK) Ltd.
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
||||||
// copy of this software and associated documentation files (the "Software"),
|
|
||||||
// to deal in the Software without restriction, including without limitation
|
|
||||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
||||||
// and/or sell copies of the Software, and to permit persons to whom the
|
|
||||||
// Software is furnished to do so, subject to the following conditions:
|
|
||||||
//
|
|
||||||
// The above copyright notice and this permission notice shall be included in
|
|
||||||
// all copies or substantial portions of the Software.
|
|
||||||
//
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
||||||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
||||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
||||||
// DEALINGS IN THE SOFTWARE.
|
|
||||||
|
|
||||||
extern crate libp2p_peerstore;
|
|
||||||
extern crate libp2p_core;
|
|
||||||
extern crate multiaddr;
|
|
||||||
|
|
||||||
use libp2p_peerstore::{PeerAccess, PeerId, Peerstore};
|
|
||||||
use multiaddr::Multiaddr;
|
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
/// Stores initial addresses on the given peer store. Uses a very large timeout.
|
|
||||||
pub fn ipfs_bootstrap<P>(peer_store: P)
|
|
||||||
where
|
|
||||||
P: Peerstore + Clone,
|
|
||||||
{
|
|
||||||
const ADDRESSES: &[&str] = &[
|
|
||||||
"/ip4/127.0.0.1/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
|
|
||||||
// TODO: add some bootstrap nodes here
|
|
||||||
];
|
|
||||||
|
|
||||||
let ttl = Duration::from_secs(100 * 365 * 24 * 3600);
|
|
||||||
|
|
||||||
for address in ADDRESSES.iter() {
|
|
||||||
let mut multiaddr = address
|
|
||||||
.parse::<Multiaddr>()
|
|
||||||
.expect("failed to parse hard-coded multiaddr");
|
|
||||||
|
|
||||||
let ipfs_component = multiaddr.pop().expect("hard-coded multiaddr is empty");
|
|
||||||
let peer = match ipfs_component {
|
|
||||||
multiaddr::AddrComponent::IPFS(key) => {
|
|
||||||
PeerId::from_bytes(key).expect("invalid peer id")
|
|
||||||
}
|
|
||||||
_ => panic!("hard-coded multiaddr didn't end with /ipfs/"),
|
|
||||||
};
|
|
||||||
|
|
||||||
peer_store
|
|
||||||
.clone()
|
|
||||||
.peer_or_create(&peer)
|
|
||||||
.add_addr(multiaddr, ttl.clone());
|
|
||||||
}
|
|
||||||
}
|
|
@ -16,8 +16,17 @@ libp2p-peerstore = { path = "../peerstore" }
|
|||||||
libp2p-ping = { path = "../ping" }
|
libp2p-ping = { path = "../ping" }
|
||||||
libp2p-ratelimit = { path = "../ratelimit" }
|
libp2p-ratelimit = { path = "../ratelimit" }
|
||||||
libp2p-relay = { path = "../relay" }
|
libp2p-relay = { path = "../relay" }
|
||||||
#libp2p-secio = { path = "../secio" }
|
libp2p-secio = { path = "../secio" }
|
||||||
libp2p-core = { path = "../core" }
|
libp2p-core = { path = "../core" }
|
||||||
libp2p-tcp-transport = { path = "../tcp-transport" }
|
libp2p-tcp-transport = { path = "../tcp-transport" }
|
||||||
libp2p-websocket = { path = "../websocket" }
|
libp2p-websocket = { path = "../websocket" }
|
||||||
tokio-core = "0.1"
|
tokio-core = "0.1"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
bigint = "4.2"
|
||||||
|
env_logger = "0.5.4"
|
||||||
|
rand = "0.4"
|
||||||
|
structopt = "0.2"
|
||||||
|
tokio-core = "0.1"
|
||||||
|
tokio-io = "0.1"
|
||||||
|
tokio-stdin = "0.1"
|
||||||
|
10
libp2p/examples/README.md
Normal file
10
libp2p/examples/README.md
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
# How the keys were generated
|
||||||
|
|
||||||
|
The keys used in the examples were generated like this:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
openssl genrsa -out private.pem 2048
|
||||||
|
openssl rsa -in private.pem -outform DER -pubout -out public.der
|
||||||
|
openssl pkcs8 -in private.pem -topk8 -nocrypt -out private.pk8
|
||||||
|
rm private.pem # optional
|
||||||
|
```
|
@ -21,24 +21,20 @@
|
|||||||
extern crate bytes;
|
extern crate bytes;
|
||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
extern crate futures;
|
extern crate futures;
|
||||||
extern crate libp2p_mplex as multiplex;
|
extern crate libp2p;
|
||||||
extern crate libp2p_secio as secio;
|
|
||||||
extern crate libp2p_core as swarm;
|
|
||||||
extern crate libp2p_tcp_transport as tcp;
|
|
||||||
extern crate libp2p_websocket as websocket;
|
|
||||||
extern crate tokio_core;
|
extern crate tokio_core;
|
||||||
extern crate tokio_io;
|
extern crate tokio_io;
|
||||||
|
|
||||||
use futures::sync::oneshot;
|
use futures::sync::oneshot;
|
||||||
use futures::{Future, Sink, Stream};
|
use futures::{Future, Sink, Stream};
|
||||||
use std::env;
|
use std::env;
|
||||||
use swarm::Transport;
|
use libp2p::core::Transport;
|
||||||
use swarm::upgrade::{self, DeniedConnectionUpgrade, SimpleProtocol};
|
use libp2p::core::upgrade::{self, DeniedConnectionUpgrade, SimpleProtocol};
|
||||||
use tcp::TcpConfig;
|
use libp2p::tcp::TcpConfig;
|
||||||
use tokio_core::reactor::Core;
|
use tokio_core::reactor::Core;
|
||||||
use tokio_io::AsyncRead;
|
use tokio_io::AsyncRead;
|
||||||
use tokio_io::codec::BytesCodec;
|
use tokio_io::codec::BytesCodec;
|
||||||
use websocket::WsConfig;
|
use libp2p::websocket::WsConfig;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
@ -68,8 +64,8 @@ fn main() {
|
|||||||
let secio = {
|
let secio = {
|
||||||
let private_key = include_bytes!("test-private-key.pk8");
|
let private_key = include_bytes!("test-private-key.pk8");
|
||||||
let public_key = include_bytes!("test-public-key.der").to_vec();
|
let public_key = include_bytes!("test-public-key.der").to_vec();
|
||||||
secio::SecioConfig {
|
libp2p::secio::SecioConfig {
|
||||||
key: secio::SecioKeyPair::rsa_from_pkcs8(private_key, public_key).unwrap(),
|
key: libp2p::secio::SecioKeyPair::rsa_from_pkcs8(private_key, public_key).unwrap(),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -77,7 +73,7 @@ fn main() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// On top of plaintext or secio, we will use the multiplex protocol.
|
// On top of plaintext or secio, we will use the multiplex protocol.
|
||||||
.with_upgrade(multiplex::MultiplexConfig::new())
|
.with_upgrade(libp2p::mplex::MultiplexConfig::new())
|
||||||
// The object returned by the call to `with_upgrade(MultiplexConfig::new())` can't be used as a
|
// The object returned by the call to `with_upgrade(MultiplexConfig::new())` can't be used as a
|
||||||
// `Transport` because the output of the upgrade is not a stream but a controller for
|
// `Transport` because the output of the upgrade is not a stream but a controller for
|
||||||
// muxing. We have to explicitly call `into_connection_reuse()` in order to turn this into
|
// muxing. We have to explicitly call `into_connection_reuse()` in order to turn this into
|
||||||
@ -88,7 +84,7 @@ fn main() {
|
|||||||
// connections for us. The second parameter we pass is the connection upgrade that is accepted
|
// connections for us. The second parameter we pass is the connection upgrade that is accepted
|
||||||
// by the listening part. We don't want to accept anything, so we pass a dummy object that
|
// by the listening part. We don't want to accept anything, so we pass a dummy object that
|
||||||
// represents a connection that is always denied.
|
// represents a connection that is always denied.
|
||||||
let (swarm_controller, swarm_future) = swarm::swarm(
|
let (swarm_controller, swarm_future) = libp2p::core::swarm(
|
||||||
transport.clone().with_upgrade(DeniedConnectionUpgrade),
|
transport.clone().with_upgrade(DeniedConnectionUpgrade),
|
||||||
|_socket, _client_addr| -> Result<(), _> {
|
|_socket, _client_addr| -> Result<(), _> {
|
||||||
unreachable!("All incoming connections should have been denied")
|
unreachable!("All incoming connections should have been denied")
|
@ -21,24 +21,20 @@
|
|||||||
extern crate bytes;
|
extern crate bytes;
|
||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
extern crate futures;
|
extern crate futures;
|
||||||
extern crate libp2p_mplex as multiplex;
|
extern crate libp2p;
|
||||||
extern crate libp2p_secio as secio;
|
|
||||||
extern crate libp2p_core as swarm;
|
|
||||||
extern crate libp2p_tcp_transport as tcp;
|
|
||||||
extern crate libp2p_websocket as websocket;
|
|
||||||
extern crate tokio_core;
|
extern crate tokio_core;
|
||||||
extern crate tokio_io;
|
extern crate tokio_io;
|
||||||
|
|
||||||
use futures::future::{loop_fn, Future, IntoFuture, Loop};
|
use futures::future::{loop_fn, Future, IntoFuture, Loop};
|
||||||
use futures::{Sink, Stream};
|
use futures::{Sink, Stream};
|
||||||
use std::env;
|
use std::env;
|
||||||
use swarm::Transport;
|
use libp2p::core::Transport;
|
||||||
use swarm::upgrade::{self, SimpleProtocol};
|
use libp2p::core::upgrade::{self, SimpleProtocol};
|
||||||
use tcp::TcpConfig;
|
use libp2p::tcp::TcpConfig;
|
||||||
use tokio_core::reactor::Core;
|
use tokio_core::reactor::Core;
|
||||||
use tokio_io::AsyncRead;
|
use tokio_io::AsyncRead;
|
||||||
use tokio_io::codec::BytesCodec;
|
use tokio_io::codec::BytesCodec;
|
||||||
use websocket::WsConfig;
|
use libp2p::websocket::WsConfig;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
@ -67,8 +63,8 @@ fn main() {
|
|||||||
let secio = {
|
let secio = {
|
||||||
let private_key = include_bytes!("test-private-key.pk8");
|
let private_key = include_bytes!("test-private-key.pk8");
|
||||||
let public_key = include_bytes!("test-public-key.der").to_vec();
|
let public_key = include_bytes!("test-public-key.der").to_vec();
|
||||||
secio::SecioConfig {
|
libp2p::secio::SecioConfig {
|
||||||
key: secio::SecioKeyPair::rsa_from_pkcs8(private_key, public_key).unwrap(),
|
key: libp2p::secio::SecioKeyPair::rsa_from_pkcs8(private_key, public_key).unwrap(),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -76,7 +72,7 @@ fn main() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// On top of plaintext or secio, we will use the multiplex protocol.
|
// On top of plaintext or secio, we will use the multiplex protocol.
|
||||||
.with_upgrade(multiplex::MultiplexConfig::new())
|
.with_upgrade(libp2p::mplex::MultiplexConfig::new())
|
||||||
// The object returned by the call to `with_upgrade(MultiplexConfig::new())` can't be used as a
|
// The object returned by the call to `with_upgrade(MultiplexConfig::new())` can't be used as a
|
||||||
// `Transport` because the output of the upgrade is not a stream but a controller for
|
// `Transport` because the output of the upgrade is not a stream but a controller for
|
||||||
// muxing. We have to explicitly call `into_connection_reuse()` in order to turn this into
|
// muxing. We have to explicitly call `into_connection_reuse()` in order to turn this into
|
||||||
@ -99,7 +95,7 @@ fn main() {
|
|||||||
|
|
||||||
// Let's put this `transport` into a *swarm*. The swarm will handle all the incoming and
|
// Let's put this `transport` into a *swarm*. The swarm will handle all the incoming and
|
||||||
// outgoing connections for us.
|
// outgoing connections for us.
|
||||||
let (swarm_controller, swarm_future) = swarm::swarm(
|
let (swarm_controller, swarm_future) = libp2p::core::swarm(
|
||||||
transport.clone().with_upgrade(proto),
|
transport.clone().with_upgrade(proto),
|
||||||
|socket, client_addr| {
|
|socket, client_addr| {
|
||||||
println!("Successfully negotiated protocol with {}", client_addr);
|
println!("Successfully negotiated protocol with {}", client_addr);
|
@ -21,13 +21,7 @@
|
|||||||
extern crate bytes;
|
extern crate bytes;
|
||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
extern crate futures;
|
extern crate futures;
|
||||||
extern crate libp2p_floodsub as floodsub;
|
extern crate libp2p;
|
||||||
extern crate libp2p_mplex as multiplex;
|
|
||||||
extern crate libp2p_peerstore as peerstore;
|
|
||||||
extern crate libp2p_secio as secio;
|
|
||||||
extern crate libp2p_core as swarm;
|
|
||||||
extern crate libp2p_tcp_transport as tcp;
|
|
||||||
extern crate libp2p_websocket as websocket;
|
|
||||||
extern crate rand;
|
extern crate rand;
|
||||||
extern crate tokio_core;
|
extern crate tokio_core;
|
||||||
extern crate tokio_io;
|
extern crate tokio_io;
|
||||||
@ -35,13 +29,13 @@ extern crate tokio_stdin;
|
|||||||
|
|
||||||
use futures::Stream;
|
use futures::Stream;
|
||||||
use futures::future::Future;
|
use futures::future::Future;
|
||||||
use peerstore::PeerId;
|
|
||||||
use std::{env, mem};
|
use std::{env, mem};
|
||||||
use swarm::upgrade;
|
use libp2p::core::upgrade;
|
||||||
use swarm::{Multiaddr, Transport};
|
use libp2p::core::{Multiaddr, Transport};
|
||||||
use tcp::TcpConfig;
|
use libp2p::peerstore::PeerId;
|
||||||
|
use libp2p::tcp::TcpConfig;
|
||||||
use tokio_core::reactor::Core;
|
use tokio_core::reactor::Core;
|
||||||
use websocket::WsConfig;
|
use libp2p::websocket::WsConfig;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
@ -70,8 +64,8 @@ fn main() {
|
|||||||
let secio = {
|
let secio = {
|
||||||
let private_key = include_bytes!("test-private-key.pk8");
|
let private_key = include_bytes!("test-private-key.pk8");
|
||||||
let public_key = include_bytes!("test-public-key.der").to_vec();
|
let public_key = include_bytes!("test-public-key.der").to_vec();
|
||||||
secio::SecioConfig {
|
libp2p::secio::SecioConfig {
|
||||||
key: secio::SecioKeyPair::rsa_from_pkcs8(private_key, public_key).unwrap(),
|
key: libp2p::secio::SecioKeyPair::rsa_from_pkcs8(private_key, public_key).unwrap(),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -79,7 +73,7 @@ fn main() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// On top of plaintext or secio, we will use the multiplex protocol.
|
// On top of plaintext or secio, we will use the multiplex protocol.
|
||||||
.with_upgrade(multiplex::MultiplexConfig::new())
|
.with_upgrade(libp2p::mplex::MultiplexConfig::new())
|
||||||
// The object returned by the call to `with_upgrade(MultiplexConfig::new())` can't be used as a
|
// The object returned by the call to `with_upgrade(MultiplexConfig::new())` can't be used as a
|
||||||
// `Transport` because the output of the upgrade is not a stream but a controller for
|
// `Transport` because the output of the upgrade is not a stream but a controller for
|
||||||
// muxing. We have to explicitly call `into_connection_reuse()` in order to turn this into
|
// muxing. We have to explicitly call `into_connection_reuse()` in order to turn this into
|
||||||
@ -97,11 +91,11 @@ fn main() {
|
|||||||
PeerId::from_public_key(&key)
|
PeerId::from_public_key(&key)
|
||||||
};
|
};
|
||||||
|
|
||||||
let (floodsub_upgrade, floodsub_rx) = floodsub::FloodSubUpgrade::new(my_id);
|
let (floodsub_upgrade, floodsub_rx) = libp2p::floodsub::FloodSubUpgrade::new(my_id);
|
||||||
|
|
||||||
// Let's put this `transport` into a *swarm*. The swarm will handle all the incoming and
|
// Let's put this `transport` into a *swarm*. The swarm will handle all the incoming and
|
||||||
// outgoing connections for us.
|
// outgoing connections for us.
|
||||||
let (swarm_controller, swarm_future) = swarm::swarm(
|
let (swarm_controller, swarm_future) = libp2p::core::swarm(
|
||||||
transport.clone().with_upgrade(floodsub_upgrade.clone()),
|
transport.clone().with_upgrade(floodsub_upgrade.clone()),
|
||||||
|socket, client_addr| {
|
|socket, client_addr| {
|
||||||
println!("Successfully negotiated protocol with {}", client_addr);
|
println!("Successfully negotiated protocol with {}", client_addr);
|
||||||
@ -114,9 +108,9 @@ fn main() {
|
|||||||
.expect("unsupported multiaddr");
|
.expect("unsupported multiaddr");
|
||||||
println!("Now listening on {:?}", address);
|
println!("Now listening on {:?}", address);
|
||||||
|
|
||||||
let topic = floodsub::TopicBuilder::new("chat").build();
|
let topic = libp2p::floodsub::TopicBuilder::new("chat").build();
|
||||||
|
|
||||||
let floodsub_ctl = floodsub::FloodSubController::new(&floodsub_upgrade);
|
let floodsub_ctl = libp2p::floodsub::FloodSubController::new(&floodsub_upgrade);
|
||||||
floodsub_ctl.subscribe(&topic);
|
floodsub_ctl.subscribe(&topic);
|
||||||
|
|
||||||
let floodsub_rx = floodsub_rx.for_each(|msg| {
|
let floodsub_rx = floodsub_rx.for_each(|msg| {
|
@ -21,27 +21,21 @@
|
|||||||
extern crate bigint;
|
extern crate bigint;
|
||||||
extern crate bytes;
|
extern crate bytes;
|
||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
extern crate example;
|
|
||||||
extern crate futures;
|
extern crate futures;
|
||||||
extern crate libp2p_identify as identify;
|
extern crate libp2p;
|
||||||
extern crate libp2p_kad as kad;
|
|
||||||
extern crate libp2p_mplex as multiplex;
|
|
||||||
extern crate libp2p_peerstore as peerstore;
|
|
||||||
extern crate libp2p_secio as secio;
|
|
||||||
extern crate libp2p_core as swarm;
|
|
||||||
extern crate libp2p_tcp_transport as tcp;
|
|
||||||
extern crate tokio_core;
|
extern crate tokio_core;
|
||||||
extern crate tokio_io;
|
extern crate tokio_io;
|
||||||
|
|
||||||
use bigint::U512;
|
use bigint::U512;
|
||||||
use futures::future::Future;
|
use futures::future::Future;
|
||||||
use peerstore::PeerId;
|
use libp2p::peerstore::{PeerAccess, PeerId, Peerstore};
|
||||||
|
use libp2p::Multiaddr;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use swarm::Transport;
|
use libp2p::core::Transport;
|
||||||
use swarm::upgrade;
|
use libp2p::core::upgrade;
|
||||||
use tcp::TcpConfig;
|
use libp2p::tcp::TcpConfig;
|
||||||
use tokio_core::reactor::Core;
|
use tokio_core::reactor::Core;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -59,8 +53,8 @@ fn main() {
|
|||||||
// We start by building the tokio engine that will run all the sockets.
|
// We start by building the tokio engine that will run all the sockets.
|
||||||
let mut core = Core::new().unwrap();
|
let mut core = Core::new().unwrap();
|
||||||
|
|
||||||
let peer_store = Arc::new(peerstore::memory_peerstore::MemoryPeerstore::empty());
|
let peer_store = Arc::new(libp2p::peerstore::memory_peerstore::MemoryPeerstore::empty());
|
||||||
example::ipfs_bootstrap(&*peer_store);
|
ipfs_bootstrap(&*peer_store);
|
||||||
|
|
||||||
// Now let's build the transport stack.
|
// Now let's build the transport stack.
|
||||||
// We create a `TcpConfig` that indicates that we want TCP/IP.
|
// We create a `TcpConfig` that indicates that we want TCP/IP.
|
||||||
@ -74,8 +68,8 @@ fn main() {
|
|||||||
let secio = {
|
let secio = {
|
||||||
let private_key = include_bytes!("test-private-key.pk8");
|
let private_key = include_bytes!("test-private-key.pk8");
|
||||||
let public_key = include_bytes!("test-public-key.der").to_vec();
|
let public_key = include_bytes!("test-public-key.der").to_vec();
|
||||||
secio::SecioConfig {
|
libp2p::secio::SecioConfig {
|
||||||
key: secio::SecioKeyPair::rsa_from_pkcs8(private_key, public_key).unwrap(),
|
key: libp2p::secio::SecioKeyPair::rsa_from_pkcs8(private_key, public_key).unwrap(),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -83,14 +77,14 @@ fn main() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// On top of plaintext or secio, we will use the multiplex protocol.
|
// On top of plaintext or secio, we will use the multiplex protocol.
|
||||||
.with_upgrade(multiplex::MultiplexConfig::new())
|
.with_upgrade(libp2p::mplex::MultiplexConfig::new())
|
||||||
// The object returned by the call to `with_upgrade(MultiplexConfig::new())` can't be used as a
|
// The object returned by the call to `with_upgrade(MultiplexConfig::new())` can't be used as a
|
||||||
// `Transport` because the output of the upgrade is not a stream but a controller for
|
// `Transport` because the output of the upgrade is not a stream but a controller for
|
||||||
// muxing. We have to explicitly call `into_connection_reuse()` in order to turn this into
|
// muxing. We have to explicitly call `into_connection_reuse()` in order to turn this into
|
||||||
// a `Transport`.
|
// a `Transport`.
|
||||||
.into_connection_reuse();
|
.into_connection_reuse();
|
||||||
|
|
||||||
let transport = identify::IdentifyTransport::new(transport, peer_store.clone())
|
let transport = libp2p::identify::IdentifyTransport::new(transport, peer_store.clone())
|
||||||
.map(|id_out, _, _| {
|
.map(|id_out, _, _| {
|
||||||
id_out.socket
|
id_out.socket
|
||||||
});
|
});
|
||||||
@ -104,7 +98,7 @@ fn main() {
|
|||||||
|
|
||||||
// Let's put this `transport` into a Kademlia *swarm*. The swarm will handle all the incoming
|
// Let's put this `transport` into a Kademlia *swarm*. The swarm will handle all the incoming
|
||||||
// and outgoing connections for us.
|
// and outgoing connections for us.
|
||||||
let kad_config = kad::KademliaConfig {
|
let kad_config = libp2p::kad::KademliaConfig {
|
||||||
parallelism: 3,
|
parallelism: 3,
|
||||||
record_store: (),
|
record_store: (),
|
||||||
peer_store: peer_store,
|
peer_store: peer_store,
|
||||||
@ -112,13 +106,13 @@ fn main() {
|
|||||||
timeout: Duration::from_secs(2),
|
timeout: Duration::from_secs(2),
|
||||||
};
|
};
|
||||||
|
|
||||||
let kad_ctl_proto = kad::KademliaControllerPrototype::new(kad_config);
|
let kad_ctl_proto = libp2p::kad::KademliaControllerPrototype::new(kad_config);
|
||||||
|
|
||||||
let proto = kad::KademliaUpgrade::from_prototype(&kad_ctl_proto);
|
let proto = libp2p::kad::KademliaUpgrade::from_prototype(&kad_ctl_proto);
|
||||||
|
|
||||||
// Let's put this `transport` into a *swarm*. The swarm will handle all the incoming and
|
// Let's put this `transport` into a *swarm*. The swarm will handle all the incoming and
|
||||||
// outgoing connections for us.
|
// outgoing connections for us.
|
||||||
let (swarm_controller, swarm_future) = swarm::swarm(
|
let (swarm_controller, swarm_future) = libp2p::core::swarm(
|
||||||
transport.clone().with_upgrade(proto.clone()),
|
transport.clone().with_upgrade(proto.clone()),
|
||||||
|upgrade, _| upgrade,
|
|upgrade, _| upgrade,
|
||||||
);
|
);
|
||||||
@ -156,3 +150,35 @@ fn main() {
|
|||||||
.map_err(|(err, _)| err),
|
.map_err(|(err, _)| err),
|
||||||
).unwrap();
|
).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Stores initial addresses on the given peer store. Uses a very large timeout.
|
||||||
|
pub fn ipfs_bootstrap<P>(peer_store: P)
|
||||||
|
where
|
||||||
|
P: Peerstore + Clone,
|
||||||
|
{
|
||||||
|
const ADDRESSES: &[&str] = &[
|
||||||
|
"/ip4/127.0.0.1/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
|
||||||
|
// TODO: add some bootstrap nodes here
|
||||||
|
];
|
||||||
|
|
||||||
|
let ttl = Duration::from_secs(100 * 365 * 24 * 3600);
|
||||||
|
|
||||||
|
for address in ADDRESSES.iter() {
|
||||||
|
let mut multiaddr = address
|
||||||
|
.parse::<Multiaddr>()
|
||||||
|
.expect("failed to parse hard-coded multiaddr");
|
||||||
|
|
||||||
|
let ipfs_component = multiaddr.pop().expect("hard-coded multiaddr is empty");
|
||||||
|
let peer = match ipfs_component {
|
||||||
|
libp2p::multiaddr::AddrComponent::IPFS(key) => {
|
||||||
|
PeerId::from_bytes(key).expect("invalid peer id")
|
||||||
|
}
|
||||||
|
_ => panic!("hard-coded multiaddr didn't end with /ipfs/"),
|
||||||
|
};
|
||||||
|
|
||||||
|
peer_store
|
||||||
|
.clone()
|
||||||
|
.peer_or_create(&peer)
|
||||||
|
.add_addr(multiaddr, ttl.clone());
|
||||||
|
}
|
||||||
|
}
|
@ -21,20 +21,16 @@
|
|||||||
extern crate bytes;
|
extern crate bytes;
|
||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
extern crate futures;
|
extern crate futures;
|
||||||
extern crate libp2p_mplex as multiplex;
|
extern crate libp2p;
|
||||||
extern crate libp2p_ping as ping;
|
|
||||||
extern crate libp2p_secio as secio;
|
|
||||||
extern crate libp2p_core as swarm;
|
|
||||||
extern crate libp2p_tcp_transport as tcp;
|
|
||||||
extern crate tokio_core;
|
extern crate tokio_core;
|
||||||
extern crate tokio_io;
|
extern crate tokio_io;
|
||||||
|
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
use futures::sync::oneshot;
|
use futures::sync::oneshot;
|
||||||
use std::env;
|
use std::env;
|
||||||
use swarm::Transport;
|
use libp2p::core::Transport;
|
||||||
use swarm::upgrade::{self, DeniedConnectionUpgrade};
|
use libp2p::core::upgrade::{self, DeniedConnectionUpgrade};
|
||||||
use tcp::TcpConfig;
|
use libp2p::tcp::TcpConfig;
|
||||||
use tokio_core::reactor::Core;
|
use tokio_core::reactor::Core;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -60,8 +56,8 @@ fn main() {
|
|||||||
let secio = {
|
let secio = {
|
||||||
let private_key = include_bytes!("test-private-key.pk8");
|
let private_key = include_bytes!("test-private-key.pk8");
|
||||||
let public_key = include_bytes!("test-public-key.der").to_vec();
|
let public_key = include_bytes!("test-public-key.der").to_vec();
|
||||||
secio::SecioConfig {
|
libp2p::secio::SecioConfig {
|
||||||
key: secio::SecioKeyPair::rsa_from_pkcs8(private_key, public_key).unwrap(),
|
key: libp2p::secio::SecioKeyPair::rsa_from_pkcs8(private_key, public_key).unwrap(),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -69,7 +65,7 @@ fn main() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// On top of plaintext or secio, we will use the multiplex protocol.
|
// On top of plaintext or secio, we will use the multiplex protocol.
|
||||||
.with_upgrade(multiplex::MultiplexConfig::new())
|
.with_upgrade(libp2p::mplex::MultiplexConfig::new())
|
||||||
// The object returned by the call to `with_upgrade(MultiplexConfig::new())` can't be used as a
|
// The object returned by the call to `with_upgrade(MultiplexConfig::new())` can't be used as a
|
||||||
// `Transport` because the output of the upgrade is not a stream but a controller for
|
// `Transport` because the output of the upgrade is not a stream but a controller for
|
||||||
// muxing. We have to explicitly call `into_connection_reuse()` in order to turn this into
|
// muxing. We have to explicitly call `into_connection_reuse()` in order to turn this into
|
||||||
@ -80,7 +76,7 @@ fn main() {
|
|||||||
// connections for us. The second parameter we pass is the connection upgrade that is accepted
|
// connections for us. The second parameter we pass is the connection upgrade that is accepted
|
||||||
// by the listening part. We don't want to accept anything, so we pass a dummy object that
|
// by the listening part. We don't want to accept anything, so we pass a dummy object that
|
||||||
// represents a connection that is always denied.
|
// represents a connection that is always denied.
|
||||||
let (swarm_controller, swarm_future) = swarm::swarm(
|
let (swarm_controller, swarm_future) = libp2p::core::swarm(
|
||||||
transport.clone().with_upgrade(DeniedConnectionUpgrade),
|
transport.clone().with_upgrade(DeniedConnectionUpgrade),
|
||||||
|_socket, _client_addr| -> Result<(), _> {
|
|_socket, _client_addr| -> Result<(), _> {
|
||||||
unreachable!("All incoming connections should have been denied")
|
unreachable!("All incoming connections should have been denied")
|
||||||
@ -91,7 +87,7 @@ fn main() {
|
|||||||
let (tx, rx) = oneshot::channel();
|
let (tx, rx) = oneshot::channel();
|
||||||
swarm_controller
|
swarm_controller
|
||||||
.dial_custom_handler(target_addr.parse().expect("invalid multiaddr"),
|
.dial_custom_handler(target_addr.parse().expect("invalid multiaddr"),
|
||||||
transport.with_upgrade(ping::Ping),
|
transport.with_upgrade(libp2p::ping::Ping),
|
||||||
|(mut pinger, future), _| {
|
|(mut pinger, future), _| {
|
||||||
let ping = pinger.ping().map_err(|_| unreachable!()).inspect(|_| {
|
let ping = pinger.ping().map_err(|_| unreachable!()).inspect(|_| {
|
||||||
println!("Received pong from the remote");
|
println!("Received pong from the remote");
|
@ -18,10 +18,10 @@
|
|||||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
// DEALINGS IN THE SOFTWARE.
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
extern crate libp2p_peerstore;
|
extern crate libp2p;
|
||||||
extern crate rand;
|
extern crate rand;
|
||||||
|
|
||||||
use libp2p_peerstore::PeerId;
|
use libp2p::peerstore::PeerId;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let pid = {
|
let pid = {
|
@ -49,26 +49,22 @@
|
|||||||
extern crate bytes;
|
extern crate bytes;
|
||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
extern crate futures;
|
extern crate futures;
|
||||||
extern crate libp2p_mplex as multiplex;
|
extern crate libp2p;
|
||||||
extern crate libp2p_peerstore as peerstore;
|
|
||||||
extern crate libp2p_relay as relay;
|
|
||||||
extern crate libp2p_core as core;
|
|
||||||
extern crate libp2p_tcp_transport as tcp;
|
|
||||||
extern crate rand;
|
extern crate rand;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate structopt;
|
extern crate structopt;
|
||||||
extern crate tokio_core;
|
extern crate tokio_core;
|
||||||
extern crate tokio_io;
|
extern crate tokio_io;
|
||||||
|
|
||||||
use core::Multiaddr;
|
use libp2p::core::Multiaddr;
|
||||||
use core::transport::Transport;
|
use libp2p::core::transport::Transport;
|
||||||
use core::upgrade::{self, SimpleProtocol};
|
use libp2p::core::upgrade::{self, SimpleProtocol};
|
||||||
use futures::{future::{self, Either, Loop, loop_fn}, prelude::*};
|
use futures::{future::{self, Either, Loop, loop_fn}, prelude::*};
|
||||||
use peerstore::{PeerAccess, PeerId, Peerstore, memory_peerstore::MemoryPeerstore};
|
use libp2p::peerstore::{PeerAccess, PeerId, Peerstore, memory_peerstore::MemoryPeerstore};
|
||||||
use relay::{RelayConfig, RelayTransport};
|
use libp2p::relay::{RelayConfig, RelayTransport};
|
||||||
use std::{error::Error, iter, str::FromStr, sync::Arc, time::Duration};
|
use std::{error::Error, iter, str::FromStr, sync::Arc, time::Duration};
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
use tcp::TcpConfig;
|
use libp2p::tcp::TcpConfig;
|
||||||
use tokio_core::reactor::Core;
|
use tokio_core::reactor::Core;
|
||||||
use tokio_io::{AsyncRead, codec::BytesCodec};
|
use tokio_io::{AsyncRead, codec::BytesCodec};
|
||||||
|
|
||||||
@ -135,7 +131,7 @@ fn run_dialer(opts: DialerOpts) -> Result<(), Box<Error>> {
|
|||||||
RelayTransport::new(opts.me, tcp, store, iter::once(opts.relay)).with_dummy_muxing()
|
RelayTransport::new(opts.me, tcp, store, iter::once(opts.relay)).with_dummy_muxing()
|
||||||
};
|
};
|
||||||
|
|
||||||
let (control, future) = core::swarm(transport.clone(), |_, _| {
|
let (control, future) = libp2p::core::swarm(transport.clone(), |_, _| {
|
||||||
future::ok(())
|
future::ok(())
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -176,16 +172,16 @@ fn run_listener(opts: ListenerOpts) -> Result<(), Box<Error>> {
|
|||||||
let upgraded = transport.with_upgrade(relay)
|
let upgraded = transport.with_upgrade(relay)
|
||||||
.and_then(|out, endpoint, addr| {
|
.and_then(|out, endpoint, addr| {
|
||||||
match out {
|
match out {
|
||||||
relay::Output::Sealed(future) => {
|
libp2p::relay::Output::Sealed(future) => {
|
||||||
Either::A(future.map(Either::A))
|
Either::A(future.map(Either::A))
|
||||||
}
|
}
|
||||||
relay::Output::Stream(socket) => {
|
libp2p::relay::Output::Stream(socket) => {
|
||||||
Either::B(upgrade::apply(socket, echo, endpoint, addr).map(Either::B))
|
Either::B(upgrade::apply(socket, echo, endpoint, addr).map(Either::B))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let (control, future) = core::swarm(upgraded, |out, _| {
|
let (control, future) = libp2p::core::swarm(upgraded, |out, _| {
|
||||||
match out {
|
match out {
|
||||||
Either::A(()) => Either::A(future::ok(())),
|
Either::A(()) => Either::A(future::ok(())),
|
||||||
Either::B((socket, _)) => Either::B(loop_fn(socket, move |socket| {
|
Either::B((socket, _)) => Either::B(loop_fn(socket, move |socket| {
|
@ -33,7 +33,7 @@ pub extern crate libp2p_peerstore as peerstore;
|
|||||||
pub extern crate libp2p_ping as ping;
|
pub extern crate libp2p_ping as ping;
|
||||||
pub extern crate libp2p_ratelimit as ratelimit;
|
pub extern crate libp2p_ratelimit as ratelimit;
|
||||||
pub extern crate libp2p_relay as relay;
|
pub extern crate libp2p_relay as relay;
|
||||||
//pub extern crate libp2p_secio as secio;
|
pub extern crate libp2p_secio as secio;
|
||||||
pub extern crate libp2p_tcp_transport as tcp;
|
pub extern crate libp2p_tcp_transport as tcp;
|
||||||
pub extern crate libp2p_websocket as websocket;
|
pub extern crate libp2p_websocket as websocket;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user