-
Notifications
You must be signed in to change notification settings - Fork 174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add rust async functions #483
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ edition = "2018" | |
|
||
[dependencies] | ||
libc="0.2.0" | ||
os_socketaddr="0.1.0" | ||
os_socketaddr="0.1.0" | ||
futures="0.3.4" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright (C) 2014-2020 Savoir-faire Linux Inc. | ||
* Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com> | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
extern crate opendht; | ||
use std::{thread, time}; | ||
|
||
use opendht::{ InfoHash, DhtRunner, DhtRunnerConfig, Value }; | ||
// use opendht::crypto::*; | ||
use futures::prelude::*; | ||
|
||
fn main() { | ||
println!("{}", InfoHash::random()); | ||
println!("{}", InfoHash::new()); | ||
println!("{}", InfoHash::new().is_zero()); | ||
println!("{}", InfoHash::get("alice")); | ||
println!("{}", InfoHash::get("alice").is_zero()); | ||
|
||
|
||
let mut dht = DhtRunner::new(); | ||
let /*mut*/ config = DhtRunnerConfig::new(); | ||
//// If you want to inject a certificate, uncomment the following lines and previous mut. | ||
//// Note: you can generate a certificate with | ||
//// openssl req -x509 -newkey rsa:4096 -sha256 -days 3650 -nodes -keyout example.key -out example.crt -subj /CN=example.com | ||
//let cert = DhtCertificate::import("example.crt").ok().expect("Invalid cert file"); | ||
//let pk = PrivateKey::import("example.key", ""); | ||
//config.set_identity(cert, pk); | ||
dht.run_config(1412, config); | ||
use std::net::ToSocketAddrs; | ||
let addrs = "bootstrap.jami.net:4222".to_socket_addrs().unwrap(); | ||
|
||
futures::executor::block_on(async { | ||
let r = dht.bootstrap_async(addrs).await; | ||
|
||
println!("Current node id: {}", dht.node_id()); | ||
|
||
let mut stream = dht.get_async(&InfoHash::get("bob")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Imho, we can do like reqwest crate. Async by default and a sync Dht builder if someone needs to do some sync operations. So we can have DhtRunner::new(); and DhtRunner::blocking::new(), then .get() for both versions (https://docs.rs/reqwest/0.10.1/reqwest/blocking/index.html vs https://docs.rs/reqwest/0.10.1/reqwest/) If you want I can take some time to modify this :). I think adding _async everywhere is not the best way |
||
|
||
while let Ok(Some(value)) = stream.try_next().await { | ||
println!("GOT: VALUE - value: {}", value); | ||
} | ||
|
||
dht.put_async(&InfoHash::get("bob"), Value::new("hi!"), false).await; | ||
|
||
println!("Start listening /foo (sleep 10s)"); | ||
let mut stream = dht.listen_async(&InfoHash::get("foo")); | ||
let one_min = time::Duration::from_secs(10); | ||
thread::sleep(one_min); | ||
while let Some((v, expired)) = stream.next().await { | ||
println!("LISTEN: DONE CB - v: {} - expired: {}", v, expired); | ||
} | ||
}); | ||
|
||
println!("Public ips: {:#?}", dht.public_addresses()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
emplace_back