Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
opendht
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
savoirfairelinux
opendht
Commits
a567ca1d
Commit
a567ca1d
authored
Oct 17, 2019
by
Sébastien Blin
Committed by
Adrien Béraud
Oct 22, 2019
Browse files
Options
Downloads
Patches
Plain Diff
rust: add support for listen/cancel_listen
parent
97b20705
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
rust/examples/dhtnode.rs
+18
-2
18 additions, 2 deletions
rust/examples/dhtnode.rs
rust/src/ffi.rs
+63
-0
63 additions, 0 deletions
rust/src/ffi.rs
rust/src/lib.rs
+23
-55
23 additions, 55 deletions
rust/src/lib.rs
with
104 additions
and
57 deletions
rust/examples/dhtnode.rs
+
18
−
2
View file @
a567ca1d
...
@@ -3,7 +3,7 @@ use std::ffi::CString;
...
@@ -3,7 +3,7 @@ use std::ffi::CString;
use
std
::{
thread
,
time
};
use
std
::{
thread
,
time
};
use
libc
::
c_void
;
use
libc
::
c_void
;
use
opendht
::
*
;
use
opendht
::
{
InfoHash
,
DhtRunner
,
Value
}
;
extern
fn
get_cb
(
v
:
*
mut
Value
,
ptr
:
*
mut
c_void
)
{
extern
fn
get_cb
(
v
:
*
mut
Value
,
ptr
:
*
mut
c_void
)
{
if
ptr
.is_null
()
{
if
ptr
.is_null
()
{
...
@@ -15,6 +15,16 @@ extern fn get_cb(v: *mut Value, ptr: *mut c_void) {
...
@@ -15,6 +15,16 @@ extern fn get_cb(v: *mut Value, ptr: *mut c_void) {
}
}
}
}
extern
fn
value_cb
(
v
:
*
mut
Value
,
expired
:
bool
,
ptr
:
*
mut
c_void
)
{
if
ptr
.is_null
()
{
return
;
}
let
_handler
:
&
mut
Handler
=
unsafe
{
&
mut
*
(
ptr
as
*
mut
Handler
)
};
unsafe
{
println!
(
"Got data: {} - expired: {}"
,
*
v
,
expired
);
}
}
extern
fn
done_cb
(
ok
:
bool
,
ptr
:
*
mut
c_void
)
{
extern
fn
done_cb
(
ok
:
bool
,
ptr
:
*
mut
c_void
)
{
let
_handler
:
&
mut
Handler
=
unsafe
{
&
mut
*
(
ptr
as
*
mut
Handler
)
};
let
_handler
:
&
mut
Handler
=
unsafe
{
&
mut
*
(
ptr
as
*
mut
Handler
)
};
println!
(
"In done - {}"
,
ok
);
println!
(
"In done - {}"
,
ok
);
...
@@ -41,9 +51,15 @@ fn main() {
...
@@ -41,9 +51,15 @@ fn main() {
let
mut
handler
=
Handler
{
let
mut
handler
=
Handler
{
_data
:
8
,
_data
:
8
,
};
};
let
ptr
=
&
mut
handler
as
*
mut
_
as
*
mut
c_void
;
println!
(
"Start listening /foo"
);
let
token
=
dht
.listen
(
&
InfoHash
::
get
(
"foo"
),
value_cb
,
ptr
);
thread
::
sleep
(
ten_secs
);
println!
(
"Stop listening /foo"
);
dht
.cancel_listen
(
&
InfoHash
::
get
(
"foo"
),
token
);
loop
{
loop
{
println!
(
"Get /alice"
);
println!
(
"Get /alice"
);
let
ptr
=
&
mut
handler
as
*
mut
_
as
*
mut
c_void
;
dht
.get
(
&
InfoHash
::
get
(
"alice"
),
get_cb
,
done_cb
,
ptr
);
dht
.get
(
&
InfoHash
::
get
(
"alice"
),
get_cb
,
done_cb
,
ptr
);
let
v
=
Value
::
new
(
"hi!"
);
let
v
=
Value
::
new
(
"hi!"
);
dht
.put
(
&
InfoHash
::
get
(
"bob"
),
Box
::
into_raw
(
v
),
done_cb
,
ptr
);
dht
.put
(
&
InfoHash
::
get
(
"bob"
),
Box
::
into_raw
(
v
),
done_cb
,
ptr
);
...
...
This diff is collapsed.
Click to expand it.
rust/src/ffi.rs
0 → 100644
+
63
−
0
View file @
a567ca1d
use
libc
::{
c_char
,
c_void
,
in_port_t
,
size_t
};
const
HASH_LEN
:
usize
=
20
;
#[repr(C)]
pub
struct
InfoHash
{
pub
d
:
[
u8
;
HASH_LEN
],
}
#[repr(C)]
pub
struct
DhtRunner
{
_opaque
:
[
u8
;
0
]
}
#[repr(C)]
pub
struct
Value
{
_opaque
:
[
u8
;
0
]
}
#[repr(C)]
pub
struct
DataView
{
pub
data
:
*
const
u8
,
pub
size
:
size_t
}
#[repr(C)]
pub
struct
OpToken
{
_opaque
:
[
u8
;
0
]
}
#[link(name
=
"opendht-c"
)]
extern
{
pub
fn
dht_infohash_print
(
h
:
*
const
InfoHash
)
->
*
mut
c_char
;
pub
fn
dht_infohash_random
(
h
:
*
mut
InfoHash
);
pub
fn
dht_infohash_get
(
h
:
*
mut
InfoHash
,
dat
:
*
mut
u8
,
dat_size
:
size_t
);
pub
fn
dht_value_get_data
(
data
:
*
const
Value
)
->
DataView
;
pub
fn
dht_value_unref
(
data
:
*
mut
Value
);
pub
fn
dht_value_new
(
data
:
*
const
u8
,
size
:
size_t
)
->
*
mut
Value
;
pub
fn
dht_runner_new
()
->
*
mut
DhtRunner
;
pub
fn
dht_runner_delete
(
dht
:
*
mut
DhtRunner
);
pub
fn
dht_runner_run
(
dht
:
*
mut
DhtRunner
,
port
:
in_port_t
);
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
,
get_cb
:
extern
fn
(
*
mut
Value
,
*
mut
c_void
),
done_cb
:
extern
fn
(
bool
,
*
mut
c_void
),
cb_user_data
:
*
mut
c_void
);
pub
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
);
pub
fn
dht_runner_listen
(
dht
:
*
mut
DhtRunner
,
h
:
*
const
InfoHash
,
cb
:
extern
fn
(
*
mut
Value
,
bool
,
*
mut
c_void
),
cb_user_data
:
*
mut
c_void
)
->
*
const
OpToken
;
pub
fn
dht_runner_cancel_listen
(
dht
:
*
mut
DhtRunner
,
h
:
*
const
InfoHash
,
token
:
*
const
OpToken
);
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
rust/src/lib.rs
+
23
−
55
View file @
a567ca1d
extern
crate
libc
;
extern
crate
libc
;
mod
ffi
;
use
ffi
::
*
;
pub
use
ffi
::{
DhtRunner
,
InfoHash
,
Value
};
use
std
::
fmt
;
use
std
::
fmt
;
use
std
::
ffi
::
CStr
;
use
std
::
ffi
::
CStr
;
use
std
::
ffi
::
CString
;
use
std
::
ffi
::
CString
;
use
std
::
ptr
;
use
std
::
str
;
use
std
::
str
;
use
std
::
slice
;
use
std
::
slice
;
use
libc
::{
c_char
,
c_void
,
in_port_t
,
size_t
,
uint8_t
};
use
libc
::
c_void
;
const
HASH_LEN
:
usize
=
20
;
#[repr(C)]
pub
struct
InfoHash
{
d
:
[
u8
;
HASH_LEN
],
}
#[repr(C)]
pub
struct
DhtRunner
{
_opaque
:
[
u8
;
0
]
}
#[repr(C)]
pub
struct
Value
{
_opaque
:
[
u8
;
0
]
}
#[repr(C)]
pub
struct
DataView
{
data
:
*
const
uint8_t
,
size
:
size_t
}
#[link(name
=
"opendht-c"
)]
extern
{
fn
dht_infohash_print
(
h
:
*
const
InfoHash
)
->
*
mut
c_char
;
fn
dht_infohash_random
(
h
:
*
mut
InfoHash
);
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_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_delete
(
dht
:
*
mut
DhtRunner
);
fn
dht_runner_run
(
dht
:
*
mut
DhtRunner
,
port
:
in_port_t
);
fn
dht_runner_bootstrap
(
dht
:
*
mut
DhtRunner
,
host
:
*
const
c_char
,
service
:
*
const
c_char
);
fn
dht_runner_get
(
dht
:
*
mut
DhtRunner
,
h
:
*
const
InfoHash
,
get_cb
:
extern
fn
(
*
mut
Value
,
*
mut
c_void
),
done_cb
:
extern
fn
(
bool
,
*
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
{
pub
fn
new
()
->
InfoHash
{
pub
fn
new
()
->
InfoHash
{
...
@@ -132,6 +85,21 @@ impl DhtRunner {
...
@@ -132,6 +85,21 @@ impl DhtRunner {
dht_runner_put
(
&
mut
*
self
,
h
,
v
,
done_cb
,
cb_user_data
)
dht_runner_put
(
&
mut
*
self
,
h
,
v
,
done_cb
,
cb_user_data
)
}
}
}
}
pub
fn
listen
(
&
mut
self
,
h
:
&
InfoHash
,
cb
:
extern
fn
(
*
mut
Value
,
bool
,
*
mut
c_void
),
cb_user_data
:
*
mut
c_void
)
->
*
const
OpToken
{
unsafe
{
dht_runner_listen
(
&
mut
*
self
,
h
,
cb
,
cb_user_data
)
}
}
pub
fn
cancel_listen
(
&
mut
self
,
h
:
&
InfoHash
,
token
:
*
const
OpToken
)
{
unsafe
{
dht_runner_cancel_listen
(
&
mut
*
self
,
h
,
token
)
}
}
}
}
impl
Drop
for
DhtRunner
{
impl
Drop
for
DhtRunner
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment