opendht

Crates.ioopendht
lib.rsopendht
version0.5.2
sourcesrc
created_at2019-01-03 16:08:45.264793
updated_at2021-03-05 17:04:37.881264
descriptionA Rust API for OpenDHT
homepage
repositoryhttps://github.com/manuels/opendht
max_upload_size
id105248
size35,048
(manuels)

documentation

https://docs.rs/opendht

README

A Rust API for OpenDHT.

#![feature(await_macro, async_await, futures_api)]

extern crate opendht;
extern crate futures;
#[macro_use] extern crate tokio;

use std::net::ToSocketAddrs;
use tokio::prelude::*;

use opendht::OpenDht;

async fn run(dht: OpenDht) {
    println!("Bootstrapping...");
    let addrs: Vec<_> = "bootstrap.ring.cx:4222".to_socket_addrs().unwrap().collect();
    let f = dht.bootstrap(&addrs);
    await!(f).unwrap();

    let key = &b"foo"[..];

    println!("Storing...");
    let f = dht.put(key, &[9,9,9]);
    await!(f).unwrap();

    let mut f = dht.get(key); // dht.listen(key) is also available

    while let Some(item) = await!(f.next()) {
        println!("Found {:?}", item);
    }

    dht.join();
    println!("Done: All threads joined.");
}

fn main() {
    tokio::run_async(async {
        let dht = OpenDht::new(4222).unwrap();

        let dht2 = dht.clone();
        tokio::spawn_async(async move {
            while let Some(next) = dht2.tick() {
                let f = tokio::timer::Delay::new(next);
                let _ = await!(f);
            }
        });

        await!(run(dht));
    });
}
Commit count: 13

cargo fmt