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

rust: add put bindings

parent 431da16b
No related branches found
No related tags found
No related merge requests found
...@@ -28,10 +28,11 @@ struct Handler { ...@@ -28,10 +28,11 @@ struct Handler {
fn main() { fn main() {
println!("{}", InfoHash::random()); println!("{}", InfoHash::random());
println!("{}", InfoHash::new()); println!("{}", InfoHash::new());
println!("{}", InfoHash::get("toto")); println!("{}", InfoHash::get("alice"));
let mut dht = DhtRunner::new(); let mut dht = DhtRunner::new();
dht.run(1412); dht.run(1412);
// TODO take slice in boostrap
dht.bootstrap( dht.bootstrap(
&CString::new("bootstrap.jami.net").unwrap(), &CString::new("bootstrap.jami.net").unwrap(),
&CString::new("4222").unwrap() &CString::new("4222").unwrap()
...@@ -41,9 +42,11 @@ fn main() { ...@@ -41,9 +42,11 @@ fn main() {
_data: 8, _data: 8,
}; };
loop { loop {
println!("Get /toto"); println!("Get /alice");
let ptr = &mut handler as *mut _ as *mut c_void; let ptr = &mut handler as *mut _ as *mut c_void;
dht.get(&InfoHash::get("toto"), get_cb, done_cb, ptr); dht.get(&InfoHash::get("alice"), get_cb, done_cb, ptr);
let v = Value::new("hi!");
dht.put(&InfoHash::get("bob"), Box::into_raw(v), done_cb, ptr);
thread::sleep(ten_secs); thread::sleep(ten_secs);
} }
} }
\ No newline at end of file
...@@ -42,6 +42,8 @@ extern { ...@@ -42,6 +42,8 @@ extern {
fn dht_infohash_get(h: *mut InfoHash, dat: *mut uint8_t, dat_size: size_t); fn dht_infohash_get(h: *mut InfoHash, dat: *mut uint8_t, dat_size: size_t);
fn dht_value_get_data(data: *const Value) -> DataView; fn dht_value_get_data(data: *const Value) -> DataView;
fn dht_value_unref(data: *mut Value);
fn dht_value_new(data: *const uint8_t, size: size_t) -> *mut Value;
fn dht_runner_new() -> *mut DhtRunner; fn dht_runner_new() -> *mut DhtRunner;
fn dht_runner_delete(dht: *mut DhtRunner); fn dht_runner_delete(dht: *mut DhtRunner);
...@@ -51,6 +53,9 @@ extern { ...@@ -51,6 +53,9 @@ extern {
get_cb: extern fn(*mut Value, *mut c_void), get_cb: extern fn(*mut Value, *mut c_void),
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);
fn dht_runner_put(dht: *mut DhtRunner, h: *const InfoHash, v: *const Value,
done_cb: extern fn(bool, *mut c_void),
cb_user_data: *mut c_void);
} }
impl InfoHash { impl InfoHash {
...@@ -118,6 +123,15 @@ impl DhtRunner { ...@@ -118,6 +123,15 @@ impl DhtRunner {
dht_runner_get(&mut *self, h, get_cb, done_cb, cb_user_data) dht_runner_get(&mut *self, h, get_cb, done_cb, cb_user_data)
} }
} }
pub fn put(&mut self, h: &InfoHash, v: *const Value,
done_cb: extern fn(bool, *mut c_void),
cb_user_data: *mut c_void) {
unsafe {
dht_runner_put(&mut *self, h, v, done_cb, cb_user_data)
}
}
} }
impl Drop for DhtRunner { impl Drop for DhtRunner {
...@@ -129,6 +143,13 @@ impl Drop for DhtRunner { ...@@ -129,6 +143,13 @@ impl Drop for DhtRunner {
} }
impl Value { impl Value {
pub fn new(data: &str) -> Box<Value> {
unsafe {
Box::from_raw(dht_value_new(data.as_bytes().as_ptr(),
data.as_bytes().len()))
}
}
fn dataview(&self) -> DataView { fn dataview(&self) -> DataView {
unsafe { unsafe {
dht_value_get_data(self) dht_value_get_data(self)
...@@ -136,6 +157,14 @@ impl Value { ...@@ -136,6 +157,14 @@ impl Value {
} }
} }
impl Drop for Value {
fn drop(&mut self) {
unsafe {
dht_value_unref(&mut *self)
}
}
}
impl fmt::Display for Value { impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
unsafe { unsafe {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment