mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-22 14:21:33 +00:00
Merge branch 'rust_master' into libp2p_0_20
# Conflicts: # .github/workflows/ci.yml # Cargo.toml # core/Cargo.toml # examples/distributed-key-value-store.rs # misc/multistream-select/Cargo.toml # muxers/mplex/Cargo.toml # protocols/deflate/Cargo.toml # protocols/gossipsub/Cargo.toml # protocols/identify/Cargo.toml # protocols/kad/src/behaviour.rs # protocols/kad/src/behaviour/test.rs # protocols/kad/src/kbucket.rs # protocols/kad/src/kbucket/bucket.rs # protocols/kad/src/lib.rs # protocols/kad/src/query.rs # protocols/kad/src/query/peers/closest.rs # protocols/mdns/Cargo.toml # protocols/ping/Cargo.toml # protocols/secio/Cargo.toml # transports/tcp/Cargo.toml # transports/uds/Cargo.toml
This commit is contained in:
@ -1,210 +1,209 @@
|
||||
// // Copyright 20l9 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.
|
||||
// Copyright 20l9 Parity Technologies (UK) Ltd.
|
||||
//
|
||||
// //! A basic key value store demonstrating libp2p and the mDNS and Kademlia protocols.
|
||||
// //!
|
||||
// //! 1. Using two terminal windows, start two instances. If you local network
|
||||
// //! allows mDNS, they will automatically connect.
|
||||
// //!
|
||||
// //! 2. Type `PUT my-key my-value` in terminal one and hit return.
|
||||
// //!
|
||||
// //! 3. Type `GET my-key` in terminal two and hit return.
|
||||
// //!
|
||||
// //! 4. Close with Ctrl-c.
|
||||
// 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:
|
||||
//
|
||||
// use async_std::{io, task};
|
||||
// use futures::prelude::*;
|
||||
// use libp2p::kad::record::store::MemoryStore;
|
||||
// use libp2p::kad::{
|
||||
// record::Key,
|
||||
// Kademlia,
|
||||
// KademliaEvent,
|
||||
// PutRecordOk,
|
||||
// QueryResult,
|
||||
// Quorum,
|
||||
// Record
|
||||
// };
|
||||
// use libp2p::{
|
||||
// NetworkBehaviour,
|
||||
// PeerId,
|
||||
// Swarm,
|
||||
// build_development_transport,
|
||||
// identity,
|
||||
// mdns::{Mdns, MdnsEvent},
|
||||
// swarm::NetworkBehaviourEventProcess
|
||||
// };
|
||||
// use std::{error::Error, task::{Context, Poll}};
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// fn main() -> Result<(), Box<dyn Error>> {
|
||||
// env_logger::init();
|
||||
//
|
||||
// // Create a random key for ourselves.
|
||||
// let local_key = identity::Keypair::generate_ed25519();
|
||||
// let local_peer_id = PeerId::from(local_key.public());
|
||||
//
|
||||
// // Set up a an encrypted DNS-enabled TCP Transport over the Mplex protocol.
|
||||
// let transport = build_development_transport(local_key)?;
|
||||
//
|
||||
// // We create a custom network behaviour that combines Kademlia and mDNS.
|
||||
// #[derive(NetworkBehaviour)]
|
||||
// struct MyBehaviour {
|
||||
// kademlia: Kademlia<MemoryStore>,
|
||||
// mdns: Mdns
|
||||
// }
|
||||
//
|
||||
// impl NetworkBehaviourEventProcess<MdnsEvent> for MyBehaviour {
|
||||
// // Called when `mdns` produces an event.
|
||||
// fn inject_event(&mut self, event: MdnsEvent) {
|
||||
// if let MdnsEvent::Discovered(list) = event {
|
||||
// for (peer_id, multiaddr) in list {
|
||||
// self.kademlia.add_address(&peer_id, multiaddr);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// impl NetworkBehaviourEventProcess<KademliaEvent> for MyBehaviour {
|
||||
// // Called when `kademlia` produces an event.
|
||||
// fn inject_event(&mut self, message: KademliaEvent) {
|
||||
// match message {
|
||||
// KademliaEvent::QueryResult { result, .. } => match result {
|
||||
// QueryResult::GetRecord(Ok(ok)) => {
|
||||
// for Record { key, value, .. } in ok.records {
|
||||
// println!(
|
||||
// "Got record {:?} {:?}",
|
||||
// std::str::from_utf8(key.as_ref()).unwrap(),
|
||||
// std::str::from_utf8(&value).unwrap(),
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
// QueryResult::GetRecord(Err(err)) => {
|
||||
// eprintln!("Failed to get record: {:?}", err);
|
||||
// }
|
||||
// QueryResult::PutRecord(Ok(PutRecordOk { key })) => {
|
||||
// println!(
|
||||
// "Successfully put record {:?}",
|
||||
// std::str::from_utf8(key.as_ref()).unwrap()
|
||||
// );
|
||||
// }
|
||||
// QueryResult::PutRecord(Err(err)) => {
|
||||
// eprintln!("Failed to put record: {:?}", err);
|
||||
// }
|
||||
// _ => {}
|
||||
// }
|
||||
// _ => {}
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Create a swarm to manage peers and events.
|
||||
// let mut swarm = {
|
||||
// // Create a Kademlia behaviour.
|
||||
// let store = MemoryStore::new(local_peer_id.clone());
|
||||
// let kademlia = Kademlia::new(local_peer_id.clone(), store);
|
||||
// let mdns = Mdns::new()?;
|
||||
// let behaviour = MyBehaviour { kademlia, mdns };
|
||||
// Swarm::new(transport, behaviour, local_peer_id)
|
||||
// };
|
||||
//
|
||||
// // Read full lines from stdin
|
||||
// let mut stdin = io::BufReader::new(io::stdin()).lines();
|
||||
//
|
||||
// // Listen on all interfaces and whatever port the OS assigns.
|
||||
// Swarm::listen_on(&mut swarm, "/ip4/0.0.0.0/tcp/0".parse()?)?;
|
||||
//
|
||||
// // Kick it off.
|
||||
// let mut listening = false;
|
||||
// task::block_on(future::poll_fn(move |cx: &mut Context| {
|
||||
// loop {
|
||||
// match stdin.try_poll_next_unpin(cx)? {
|
||||
// Poll::Ready(Some(line)) => handle_input_line(&mut swarm.kademlia, line),
|
||||
// Poll::Ready(None) => panic!("Stdin closed"),
|
||||
// Poll::Pending => break
|
||||
// }
|
||||
// }
|
||||
// loop {
|
||||
// match swarm.poll_next_unpin(cx) {
|
||||
// Poll::Ready(Some(event)) => println!("{:?}", event),
|
||||
// Poll::Ready(None) => return Poll::Ready(Ok(())),
|
||||
// Poll::Pending => {
|
||||
// if !listening {
|
||||
// if let Some(a) = Swarm::listeners(&swarm).next() {
|
||||
// println!("Listening on {:?}", a);
|
||||
// listening = true;
|
||||
// }
|
||||
// }
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// Poll::Pending
|
||||
// }))
|
||||
// }
|
||||
//
|
||||
// fn handle_input_line(kademlia: &mut Kademlia<MemoryStore>, line: String) {
|
||||
// let mut args = line.split(" ");
|
||||
//
|
||||
// match args.next() {
|
||||
// Some("GET") => {
|
||||
// let key = {
|
||||
// match args.next() {
|
||||
// Some(key) => Key::new(&key),
|
||||
// None => {
|
||||
// eprintln!("Expected key");
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
// };
|
||||
// kademlia.get_record(&key, Quorum::One);
|
||||
// }
|
||||
// Some("PUT") => {
|
||||
// let key = {
|
||||
// match args.next() {
|
||||
// Some(key) => Key::new(&key),
|
||||
// None => {
|
||||
// eprintln!("Expected key");
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
// };
|
||||
// let value = {
|
||||
// match args.next() {
|
||||
// Some(value) => value.as_bytes().to_vec(),
|
||||
// None => {
|
||||
// eprintln!("Expected value");
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
// };
|
||||
// let record = Record {
|
||||
// key,
|
||||
// value,
|
||||
// publisher: None,
|
||||
// expires: None,
|
||||
// };
|
||||
// kademlia.put_record(record, Quorum::One).expect("Failed to store record locally.");
|
||||
// }
|
||||
// _ => {
|
||||
// eprintln!("expected GET or PUT");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// 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.
|
||||
|
||||
fn main() {}
|
||||
//! A basic key value store demonstrating libp2p and the mDNS and Kademlia protocols.
|
||||
//!
|
||||
//! 1. Using two terminal windows, start two instances. If you local network
|
||||
//! allows mDNS, they will automatically connect.
|
||||
//!
|
||||
//! 2. Type `PUT my-key my-value` in terminal one and hit return.
|
||||
//!
|
||||
//! 3. Type `GET my-key` in terminal two and hit return.
|
||||
//!
|
||||
//! 4. Close with Ctrl-c.
|
||||
|
||||
use async_std::{io, task};
|
||||
use futures::prelude::*;
|
||||
use libp2p::kad::record::store::MemoryStore;
|
||||
use libp2p::kad::{
|
||||
Kademlia,
|
||||
KademliaEvent,
|
||||
PeerRecord,
|
||||
PutRecordOk,
|
||||
QueryResult,
|
||||
Quorum,
|
||||
Record,
|
||||
record::Key,
|
||||
};
|
||||
use libp2p::{
|
||||
NetworkBehaviour,
|
||||
PeerId,
|
||||
Swarm,
|
||||
build_development_transport,
|
||||
identity,
|
||||
mdns::{Mdns, MdnsEvent},
|
||||
swarm::NetworkBehaviourEventProcess
|
||||
};
|
||||
use std::{error::Error, task::{Context, Poll}};
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
env_logger::init();
|
||||
|
||||
// Create a random key for ourselves.
|
||||
let local_key = identity::Keypair::generate_ed25519();
|
||||
let local_peer_id = PeerId::from(local_key.public());
|
||||
|
||||
// Set up a an encrypted DNS-enabled TCP Transport over the Mplex protocol.
|
||||
let transport = build_development_transport(local_key)?;
|
||||
|
||||
// We create a custom network behaviour that combines Kademlia and mDNS.
|
||||
#[derive(NetworkBehaviour)]
|
||||
struct MyBehaviour {
|
||||
kademlia: Kademlia<MemoryStore>,
|
||||
mdns: Mdns
|
||||
}
|
||||
|
||||
impl NetworkBehaviourEventProcess<MdnsEvent> for MyBehaviour {
|
||||
// Called when `mdns` produces an event.
|
||||
fn inject_event(&mut self, event: MdnsEvent) {
|
||||
if let MdnsEvent::Discovered(list) = event {
|
||||
for (peer_id, multiaddr) in list {
|
||||
self.kademlia.add_address(&peer_id, multiaddr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl NetworkBehaviourEventProcess<KademliaEvent> for MyBehaviour {
|
||||
// Called when `kademlia` produces an event.
|
||||
fn inject_event(&mut self, message: KademliaEvent) {
|
||||
match message {
|
||||
KademliaEvent::QueryResult { result, .. } => match result {
|
||||
QueryResult::GetRecord(Ok(ok)) => {
|
||||
for PeerRecord { record: Record { key, value, .. }, ..} in ok.records {
|
||||
println!(
|
||||
"Got record {:?} {:?}",
|
||||
std::str::from_utf8(key.as_ref()).unwrap(),
|
||||
std::str::from_utf8(&value).unwrap(),
|
||||
);
|
||||
}
|
||||
}
|
||||
QueryResult::GetRecord(Err(err)) => {
|
||||
eprintln!("Failed to get record: {:?}", err);
|
||||
}
|
||||
QueryResult::PutRecord(Ok(PutRecordOk { key })) => {
|
||||
println!(
|
||||
"Successfully put record {:?}",
|
||||
std::str::from_utf8(key.as_ref()).unwrap()
|
||||
);
|
||||
}
|
||||
QueryResult::PutRecord(Err(err)) => {
|
||||
eprintln!("Failed to put record: {:?}", err);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create a swarm to manage peers and events.
|
||||
let mut swarm = {
|
||||
// Create a Kademlia behaviour.
|
||||
let store = MemoryStore::new(local_peer_id.clone());
|
||||
let kademlia = Kademlia::new(local_peer_id.clone(), store);
|
||||
let mdns = Mdns::new()?;
|
||||
let behaviour = MyBehaviour { kademlia, mdns };
|
||||
Swarm::new(transport, behaviour, local_peer_id)
|
||||
};
|
||||
|
||||
// Read full lines from stdin
|
||||
let mut stdin = io::BufReader::new(io::stdin()).lines();
|
||||
|
||||
// Listen on all interfaces and whatever port the OS assigns.
|
||||
Swarm::listen_on(&mut swarm, "/ip4/0.0.0.0/tcp/0".parse()?)?;
|
||||
|
||||
// Kick it off.
|
||||
let mut listening = false;
|
||||
task::block_on(future::poll_fn(move |cx: &mut Context| {
|
||||
loop {
|
||||
match stdin.try_poll_next_unpin(cx)? {
|
||||
Poll::Ready(Some(line)) => handle_input_line(&mut swarm.kademlia, line),
|
||||
Poll::Ready(None) => panic!("Stdin closed"),
|
||||
Poll::Pending => break
|
||||
}
|
||||
}
|
||||
loop {
|
||||
match swarm.poll_next_unpin(cx) {
|
||||
Poll::Ready(Some(event)) => println!("{:?}", event),
|
||||
Poll::Ready(None) => return Poll::Ready(Ok(())),
|
||||
Poll::Pending => {
|
||||
if !listening {
|
||||
if let Some(a) = Swarm::listeners(&swarm).next() {
|
||||
println!("Listening on {:?}", a);
|
||||
listening = true;
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
Poll::Pending
|
||||
}))
|
||||
}
|
||||
|
||||
fn handle_input_line(kademlia: &mut Kademlia<MemoryStore>, line: String) {
|
||||
let mut args = line.split(" ");
|
||||
|
||||
match args.next() {
|
||||
Some("GET") => {
|
||||
let key = {
|
||||
match args.next() {
|
||||
Some(key) => Key::new(&key),
|
||||
None => {
|
||||
eprintln!("Expected key");
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
kademlia.get_record(&key, Quorum::One);
|
||||
}
|
||||
Some("PUT") => {
|
||||
let key = {
|
||||
match args.next() {
|
||||
Some(key) => Key::new(&key),
|
||||
None => {
|
||||
eprintln!("Expected key");
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
let value = {
|
||||
match args.next() {
|
||||
Some(value) => value.as_bytes().to_vec(),
|
||||
None => {
|
||||
eprintln!("Expected value");
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
let record = Record {
|
||||
key,
|
||||
value,
|
||||
publisher: None,
|
||||
expires: None,
|
||||
};
|
||||
kademlia.put_record(record, Quorum::One).expect("Failed to store record locally.");
|
||||
}
|
||||
_ => {
|
||||
eprintln!("expected GET or PUT");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user