Crates.io | kademlia-dht |
lib.rs | kademlia-dht |
version | 1.2.0 |
source | src |
created_at | 2018-04-05 05:34:25.512152 |
updated_at | 2019-10-27 06:54:31.946455 |
description | A simple implementation of the Kademlia DHT. |
homepage | |
repository | https://gitlab.com/jeffrey-xiao/kademlia-dht-rs |
max_upload_size | |
id | 59026 |
size | 68,867 |
A flexible implementation of the Kademlia distributed hash table. This library crate was mainly created to better understand the Rust concurrency primitives. This implementation is fairly close to the spec described in the original Kademlia paper with the exception of a few design considerations.
extern crate kademlia_dht;
extern crate sha3;
use kademlia_dht::{Key, Node};
use sha3::{Digest, Sha3_256};
use std::thread;
use std::time::Duration;
fn clone_into_array<A, T>(slice: &[T]) -> A
where
A: Sized + Default + AsMut<[T]>,
T: Clone,
{
let mut a = Default::default();
<A as AsMut<[T]>>::as_mut(&mut a).clone_from_slice(slice);
a
}
fn get_key(key: &str) -> Key {
let mut hasher = Sha3_256::default();
hasher.input(key.as_bytes());
Key(clone_into_array(hasher.result().as_slice()))
}
fn main() {
let mut node = Node::new("localhost", "8080", None);
let key = get_key("Hello");
let value = "World";
node.insert(key, value);
// inserting is asynchronous, so sleep for a second
thread::sleep(Duration::from_millis(1000));
assert_eq!(node.get(&key).unwrap(), value);
}
Add this to your Cargo.toml
:
[dependencies]
kademlia-dht = "*"
and this to your crate root if you are using Rust 2015:
extern crate kademlia_dht;
See CHANGELOG for more details.
Maymounkov, Petar, and David Mazières. 2002. “Kademlia: A Peer-to-Peer Information System Based on the Xor Metric.” In Revised Papers from the First International Workshop on Peer-to-Peer Systems, 53–65. IPTPS ’01. London, UK, UK: Springer-Verlag. http://dl.acm.org/citation.cfm?id=646334.687801.
kademlia-dht-rs
is dual-licensed under the terms of either the MIT License or the Apache License
(Version 2.0).
See LICENSE-APACHE and LICENSE-MIT for more details.