Skip to content
Snippets Groups Projects
Commit fee7e5dd authored by Sébastien Blin's avatar Sébastien Blin Committed by Adrien Béraud
Browse files

rust: fix callback result and add DhtIdentity::new()

parent 4c2a9744
No related branches found
No related tags found
No related merge requests found
...@@ -20,7 +20,7 @@ extern crate opendht; ...@@ -20,7 +20,7 @@ extern crate opendht;
use std::{ thread, time }; use std::{ thread, time };
use opendht::{ InfoHash, DhtRunner, DhtRunnerConfig, Value }; use opendht::{ InfoHash, DhtRunner, DhtRunnerConfig, Value };
use opendht::crypto::*; // use opendht::crypto::*;
fn main() { fn main() {
println!("{}", InfoHash::random()); println!("{}", InfoHash::random());
...@@ -31,7 +31,7 @@ fn main() { ...@@ -31,7 +31,7 @@ fn main() {
let mut dht = DhtRunner::new(); let mut dht = DhtRunner::new();
let mut config = DhtRunnerConfig::new(); let /*mut*/ config = DhtRunnerConfig::new();
//// If you want to inject a certificate, uncomment the following lines and previous mut. //// If you want to inject a certificate, uncomment the following lines and previous mut.
//// Note: you can generate a certificate with //// 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 //// openssl req -x509 -newkey rsa:4096 -sha256 -days 3650 -nodes -keyout example.key -out example.crt -subj /CN=example.com
...@@ -46,6 +46,7 @@ fn main() { ...@@ -46,6 +46,7 @@ fn main() {
let mut get_cb = |v: Box<Value>| { let mut get_cb = |v: Box<Value>| {
//data += 1; //data += 1;
println!("GET: VALUE CB - data: {} - v: {}", data, v); println!("GET: VALUE CB - data: {} - v: {}", data, v);
true
}; };
let mut done_cb = |ok: bool| { let mut done_cb = |ok: bool| {
println!("GET: DONE CB - data: {} - ok: {}", data, ok); println!("GET: DONE CB - data: {} - ok: {}", data, ok);
...@@ -62,6 +63,7 @@ fn main() { ...@@ -62,6 +63,7 @@ fn main() {
println!("Start listening /foo"); println!("Start listening /foo");
let mut value_cb = |v, expired| { let mut value_cb = |v, expired| {
println!("LISTEN: DONE CB - data: {} - v: {} - expired: {}", data, v, expired); println!("LISTEN: DONE CB - data: {} - v: {} - expired: {}", data, v, expired);
true
}; };
let token = dht.listen(&InfoHash::get("foo"), &mut value_cb); let token = dht.listen(&InfoHash::get("foo"), &mut value_cb);
let one_min = time::Duration::from_secs(10); let one_min = time::Duration::from_secs(10);
......
...@@ -23,6 +23,7 @@ use std::ffi::CString; ...@@ -23,6 +23,7 @@ use std::ffi::CString;
use std::io; use std::io;
use std::io::prelude::*; use std::io::prelude::*;
use std::fs::File; use std::fs::File;
use std::ptr;
impl PublicKey { impl PublicKey {
pub fn new() -> Box<PublicKey> { pub fn new() -> Box<PublicKey> {
...@@ -154,6 +155,12 @@ impl Drop for DhtCertificate { ...@@ -154,6 +155,12 @@ impl Drop for DhtCertificate {
} }
impl DhtIdentity { impl DhtIdentity {
pub fn new(common_name: &str) -> DhtIdentity {
unsafe {
DhtIdentity::generate(common_name, Box::from_raw(ptr::null_mut()))
}
}
pub fn generate(common_name: &str, ca: Box<DhtIdentity>) -> DhtIdentity { pub fn generate(common_name: &str, ca: Box<DhtIdentity>) -> DhtIdentity {
let common_name = CString::new(common_name).unwrap(); let common_name = CString::new(common_name).unwrap();
unsafe { unsafe {
......
...@@ -89,13 +89,13 @@ impl DhtNodeConfig ...@@ -89,13 +89,13 @@ impl DhtNodeConfig
struct GetHandler<'a> struct GetHandler<'a>
{ {
get_cb: &'a mut(dyn FnMut(Box<Value>)), get_cb: &'a mut(dyn FnMut(Box<Value>) -> bool),
done_cb: &'a mut(dyn FnMut(bool)) done_cb: &'a mut(dyn FnMut(bool))
} }
impl<'a> GetHandler<'a> impl<'a> GetHandler<'a>
{ {
fn get_cb(&mut self, v: Box<Value>) { fn get_cb(&mut self, v: Box<Value>) -> bool{
(self.get_cb)(v) (self.get_cb)(v)
} }
...@@ -104,9 +104,9 @@ impl<'a> GetHandler<'a> ...@@ -104,9 +104,9 @@ impl<'a> GetHandler<'a>
} }
} }
extern fn get_handler_cb(v: *mut Value, ptr: *mut c_void) { extern fn get_handler_cb(v: *mut Value, ptr: *mut c_void) -> bool {
if ptr.is_null() { if ptr.is_null() {
return; return true;
} }
unsafe { unsafe {
let handler = ptr as *mut GetHandler; let handler = ptr as *mut GetHandler;
...@@ -142,17 +142,17 @@ extern fn put_handler_done(ok: bool, ptr: *mut c_void) { ...@@ -142,17 +142,17 @@ extern fn put_handler_done(ok: bool, ptr: *mut c_void) {
struct ListenHandler<'a> struct ListenHandler<'a>
{ {
cb: &'a mut(dyn FnMut(Box<Value>, bool)) cb: &'a mut(dyn FnMut(Box<Value>, bool) -> bool)
} }
impl<'a> ListenHandler<'a> impl<'a> ListenHandler<'a>
{ {
fn cb(&mut self, v: Box<Value>, expired: bool) { fn cb(&mut self, v: Box<Value>, expired: bool) -> bool {
(self.cb)(v, expired) (self.cb)(v, expired)
} }
} }
extern fn listen_handler(v: *mut Value, expired: bool, ptr: *mut c_void) { extern fn listen_handler(v: *mut Value, expired: bool, ptr: *mut c_void) -> bool {
unsafe { unsafe {
let handler = ptr as *mut ListenHandler; let handler = ptr as *mut ListenHandler;
(*handler).cb((*v).boxed(), expired) (*handler).cb((*v).boxed(), expired)
...@@ -205,7 +205,7 @@ impl DhtRunner { ...@@ -205,7 +205,7 @@ impl DhtRunner {
} }
pub fn get<'a>(&mut self, h: &InfoHash, pub fn get<'a>(&mut self, h: &InfoHash,
get_cb: &'a mut(dyn FnMut(Box<Value>)), get_cb: &'a mut(dyn FnMut(Box<Value>) -> bool),
done_cb: &'a mut(dyn FnMut(bool))) { done_cb: &'a mut(dyn FnMut(bool))) {
let handler = Box::new(GetHandler { let handler = Box::new(GetHandler {
get_cb, get_cb,
...@@ -246,7 +246,7 @@ impl DhtRunner { ...@@ -246,7 +246,7 @@ impl DhtRunner {
} }
pub fn listen<'a>(&mut self, h: &InfoHash, pub fn listen<'a>(&mut self, h: &InfoHash,
cb: &'a mut(dyn FnMut(Box<Value>, bool))) -> Box<OpToken> { cb: &'a mut(dyn FnMut(Box<Value>, bool) -> bool)) -> Box<OpToken> {
let handler = Box::new(ListenHandler { let handler = Box::new(ListenHandler {
cb, cb,
}); });
......
...@@ -185,7 +185,7 @@ extern { ...@@ -185,7 +185,7 @@ extern {
pub fn dht_runner_run_config(dht: *mut DhtRunner, port: in_port_t, config: *const DhtRunnerConfig); pub fn dht_runner_run_config(dht: *mut DhtRunner, port: in_port_t, config: *const DhtRunnerConfig);
pub fn dht_runner_bootstrap(dht: *mut DhtRunner, host: *const c_char, service: *const c_char); pub fn dht_runner_bootstrap(dht: *mut DhtRunner, host: *const c_char, service: *const c_char);
pub fn dht_runner_get(dht: *mut DhtRunner, h: *const InfoHash, pub fn dht_runner_get(dht: *mut DhtRunner, h: *const InfoHash,
get_cb: extern fn(*mut Value, *mut c_void), get_cb: extern fn(*mut Value, *mut c_void) -> bool,
done_cb: extern fn(bool, *mut c_void), done_cb: extern fn(bool, *mut c_void),
cb_user_data: *mut c_void); cb_user_data: *mut c_void);
pub fn dht_runner_put(dht: *mut DhtRunner, h: *const InfoHash, v: *const Value, pub fn dht_runner_put(dht: *mut DhtRunner, h: *const InfoHash, v: *const Value,
...@@ -196,7 +196,7 @@ extern { ...@@ -196,7 +196,7 @@ extern {
cb_user_data: *mut c_void); cb_user_data: *mut c_void);
pub fn dht_runner_cancel_put(dht: *mut DhtRunner, h: *const InfoHash, vid: u64); pub fn dht_runner_cancel_put(dht: *mut DhtRunner, h: *const InfoHash, vid: u64);
pub fn dht_runner_listen(dht: *mut DhtRunner, h: *const InfoHash, pub fn dht_runner_listen(dht: *mut DhtRunner, h: *const InfoHash,
cb: extern fn(*mut Value, bool, *mut c_void), cb: extern fn(*mut Value, bool, *mut c_void) -> bool,
done_cb: extern fn(*mut c_void), done_cb: extern fn(*mut c_void),
cb_user_data: *mut c_void) -> *mut OpToken; cb_user_data: *mut c_void) -> *mut OpToken;
pub fn dht_runner_cancel_listen(dht: *mut DhtRunner, h: *const InfoHash, pub fn dht_runner_cancel_listen(dht: *mut DhtRunner, h: *const InfoHash,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment