mirror of
https://github.com/logos-messaging/logos-messaging-rust-bindings.git
synced 2026-01-02 14:03:12 +00:00
refactor: hide *mut c_void behind WakunodeContext
This commit is contained in:
parent
c434e9ebf5
commit
f80702e5dd
6
waku-bindings/src/node/context.rs
Normal file
6
waku-bindings/src/node/context.rs
Normal file
@ -0,0 +1,6 @@
|
||||
// crates
|
||||
use libc::c_void;
|
||||
|
||||
pub struct WakuNodeContext {
|
||||
pub obj_ptr: *mut c_void,
|
||||
}
|
||||
@ -10,6 +10,7 @@ use std::ffi::c_void;
|
||||
use serde::{Deserialize, Serialize};
|
||||
// internal
|
||||
use crate::general::WakuMessage;
|
||||
use crate::node::context::WakuNodeContext;
|
||||
use crate::utils::get_trampoline;
|
||||
use crate::MessageId;
|
||||
|
||||
@ -38,7 +39,7 @@ pub struct WakuMessageEvent {
|
||||
|
||||
/// Register callback to act as event handler and receive application events,
|
||||
/// which are used to react to asynchronous events in Waku
|
||||
pub fn waku_set_event_callback<F: FnMut(Event) + Send + Sync>(ctx: *mut c_void, mut f: F) {
|
||||
pub fn waku_set_event_callback<F: FnMut(Event) + Send + Sync>(ctx: &WakuNodeContext, mut f: F) {
|
||||
let cb = |v: &str| {
|
||||
let data: Event = serde_json::from_str(v).expect("Parsing event to succeed");
|
||||
f(data);
|
||||
@ -48,7 +49,7 @@ pub fn waku_set_event_callback<F: FnMut(Event) + Send + Sync>(ctx: *mut c_void,
|
||||
let mut closure = cb;
|
||||
let cb = get_trampoline(&closure);
|
||||
|
||||
waku_sys::waku_set_event_callback(ctx, cb, &mut closure as *mut _ as *mut c_void)
|
||||
waku_sys::waku_set_event_callback(ctx.obj_ptr, cb, &mut closure as *mut _ as *mut c_void)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@ -8,11 +8,12 @@ use multiaddr::Multiaddr;
|
||||
// internal
|
||||
use super::config::WakuNodeConfig;
|
||||
use crate::general::Result;
|
||||
use crate::node::context::WakuNodeContext;
|
||||
use crate::utils::{get_trampoline, handle_json_response, handle_no_response, handle_response};
|
||||
|
||||
/// Instantiates a Waku node
|
||||
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_newchar-jsonconfig)
|
||||
pub fn waku_new(config: Option<WakuNodeConfig>) -> Result<*mut c_void> {
|
||||
pub fn waku_new(config: Option<WakuNodeConfig>) -> Result<WakuNodeContext> {
|
||||
let config = config.unwrap_or_default();
|
||||
|
||||
let config_ptr = CString::new(
|
||||
@ -24,7 +25,7 @@ pub fn waku_new(config: Option<WakuNodeConfig>) -> Result<*mut c_void> {
|
||||
|
||||
let mut error: String = Default::default();
|
||||
let error_cb = |v: &str| error = v.to_string();
|
||||
let node_ptr = unsafe {
|
||||
let obj_ptr = unsafe {
|
||||
let mut closure = error_cb;
|
||||
let cb = get_trampoline(&closure);
|
||||
let out = waku_sys::waku_new(config_ptr, cb, &mut closure as *mut _ as *mut c_void);
|
||||
@ -37,19 +38,19 @@ pub fn waku_new(config: Option<WakuNodeConfig>) -> Result<*mut c_void> {
|
||||
if !error.is_empty() {
|
||||
Err(error)
|
||||
} else {
|
||||
Ok(node_ptr)
|
||||
Ok(WakuNodeContext { obj_ptr })
|
||||
}
|
||||
}
|
||||
|
||||
/// Start a Waku node mounting all the protocols that were enabled during the Waku node instantiation.
|
||||
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_start)
|
||||
pub fn waku_start(ctx: *mut c_void) -> Result<()> {
|
||||
pub fn waku_start(ctx: &WakuNodeContext) -> Result<()> {
|
||||
let mut error: String = Default::default();
|
||||
let error_cb = |v: &str| error = v.to_string();
|
||||
let code = unsafe {
|
||||
let mut closure = error_cb;
|
||||
let cb = get_trampoline(&closure);
|
||||
waku_sys::waku_start(ctx, cb, &mut closure as *mut _ as *mut c_void)
|
||||
waku_sys::waku_start(ctx.obj_ptr, cb, &mut closure as *mut _ as *mut c_void)
|
||||
};
|
||||
|
||||
handle_no_response(code, &error)
|
||||
@ -57,13 +58,13 @@ pub fn waku_start(ctx: *mut c_void) -> Result<()> {
|
||||
|
||||
/// Stops a Waku node
|
||||
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_stop)
|
||||
pub fn waku_stop(ctx: *mut c_void) -> Result<()> {
|
||||
pub fn waku_stop(ctx: &WakuNodeContext) -> Result<()> {
|
||||
let mut error: String = Default::default();
|
||||
let error_cb = |v: &str| error = v.to_string();
|
||||
let code = unsafe {
|
||||
let mut closure = error_cb;
|
||||
let cb = get_trampoline(&closure);
|
||||
waku_sys::waku_stop(ctx, cb, &mut closure as *mut _ as *mut c_void)
|
||||
waku_sys::waku_stop(ctx.obj_ptr, cb, &mut closure as *mut _ as *mut c_void)
|
||||
};
|
||||
|
||||
handle_no_response(code, &error)
|
||||
@ -71,13 +72,13 @@ pub fn waku_stop(ctx: *mut c_void) -> Result<()> {
|
||||
|
||||
/// nwaku version
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
pub fn waku_version(ctx: *mut c_void) -> Result<String> {
|
||||
pub fn waku_version(ctx: &WakuNodeContext) -> Result<String> {
|
||||
let mut result: String = Default::default();
|
||||
let result_cb = |v: &str| result = v.to_string();
|
||||
let code = unsafe {
|
||||
let mut closure = result_cb;
|
||||
let cb = get_trampoline(&closure);
|
||||
waku_sys::waku_version(ctx, cb, &mut closure as *mut _ as *mut c_void)
|
||||
waku_sys::waku_version(ctx.obj_ptr, cb, &mut closure as *mut _ as *mut c_void)
|
||||
};
|
||||
|
||||
handle_response(code, &result)
|
||||
@ -85,13 +86,13 @@ pub fn waku_version(ctx: *mut c_void) -> Result<String> {
|
||||
|
||||
/// Get the multiaddresses the Waku node is listening to
|
||||
/// as per [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_listen_addresses)
|
||||
pub fn waku_listen_addresses(ctx: *mut c_void) -> Result<Vec<Multiaddr>> {
|
||||
pub fn waku_listen_addresses(ctx: &WakuNodeContext) -> Result<Vec<Multiaddr>> {
|
||||
let mut result: String = Default::default();
|
||||
let result_cb = |v: &str| result = v.to_string();
|
||||
let code = unsafe {
|
||||
let mut closure = result_cb;
|
||||
let cb = get_trampoline(&closure);
|
||||
waku_sys::waku_listen_addresses(ctx, cb, &mut closure as *mut _ as *mut c_void)
|
||||
waku_sys::waku_listen_addresses(ctx.obj_ptr, cb, &mut closure as *mut _ as *mut c_void)
|
||||
};
|
||||
|
||||
handle_json_response(code, &result)
|
||||
@ -108,21 +109,21 @@ mod test {
|
||||
fn waku_flow() {
|
||||
let node = waku_new(None).unwrap();
|
||||
|
||||
waku_start(node).unwrap();
|
||||
waku_start(&node).unwrap();
|
||||
|
||||
// test addresses
|
||||
let addresses = waku_listen_addresses(node).unwrap();
|
||||
let addresses = waku_listen_addresses(&node).unwrap();
|
||||
dbg!(&addresses);
|
||||
assert!(!addresses.is_empty());
|
||||
|
||||
waku_stop(node).unwrap();
|
||||
waku_stop(&node).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn nwaku_version() {
|
||||
let node = waku_new(None).unwrap();
|
||||
let version = waku_version(node).expect("should return the version");
|
||||
let version = waku_version(&node).expect("should return the version");
|
||||
assert!(!version.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
//! Waku node implementation
|
||||
|
||||
mod config;
|
||||
mod context;
|
||||
mod events;
|
||||
mod management;
|
||||
mod peers;
|
||||
@ -11,11 +12,9 @@ pub use aes_gcm::{Aes256Gcm, Key};
|
||||
pub use multiaddr::Multiaddr;
|
||||
pub use secp256k1::{PublicKey, SecretKey};
|
||||
use std::time::Duration;
|
||||
// crates
|
||||
use libc::c_void;
|
||||
// internal
|
||||
|
||||
use crate::general::{MessageId, Result, WakuMessage};
|
||||
use context::WakuNodeContext;
|
||||
|
||||
pub use config::WakuNodeConfig;
|
||||
pub use events::{Event, WakuMessageEvent};
|
||||
@ -23,31 +22,31 @@ pub use relay::waku_create_content_topic;
|
||||
|
||||
/// Handle to the underliying waku node
|
||||
pub struct WakuNodeHandle {
|
||||
ctx: *mut c_void,
|
||||
ctx: WakuNodeContext,
|
||||
}
|
||||
|
||||
impl WakuNodeHandle {
|
||||
/// Start a Waku node mounting all the protocols that were enabled during the Waku node instantiation.
|
||||
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_start)
|
||||
pub fn start(&self) -> Result<()> {
|
||||
management::waku_start(self.ctx)
|
||||
management::waku_start(&self.ctx)
|
||||
}
|
||||
|
||||
/// Stops a Waku node
|
||||
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_stop)
|
||||
pub fn stop(&self) -> Result<()> {
|
||||
management::waku_stop(self.ctx)
|
||||
management::waku_stop(&self.ctx)
|
||||
}
|
||||
|
||||
/// Get the multiaddresses the Waku node is listening to
|
||||
/// as per [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_listen_addresses)
|
||||
pub fn listen_addresses(&self) -> Result<Vec<Multiaddr>> {
|
||||
management::waku_listen_addresses(self.ctx)
|
||||
management::waku_listen_addresses(&self.ctx)
|
||||
}
|
||||
|
||||
/// Get the nwaku version
|
||||
pub fn version(&self) -> Result<String> {
|
||||
management::waku_version(self.ctx)
|
||||
management::waku_version(&self.ctx)
|
||||
}
|
||||
|
||||
/// Dial peer using a multiaddress
|
||||
@ -56,7 +55,7 @@ impl WakuNodeHandle {
|
||||
/// Use 0 for no timeout
|
||||
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_connect_peerchar-address-int-timeoutms)
|
||||
pub fn connect(&self, address: &Multiaddr, timeout: Option<Duration>) -> Result<()> {
|
||||
peers::waku_connect(self.ctx, address, timeout)
|
||||
peers::waku_connect(&self.ctx, address, timeout)
|
||||
}
|
||||
|
||||
/// Publish a message using Waku Relay.
|
||||
@ -68,21 +67,21 @@ impl WakuNodeHandle {
|
||||
pubsub_topic: &String,
|
||||
timeout: Option<Duration>,
|
||||
) -> Result<MessageId> {
|
||||
relay::waku_relay_publish_message(self.ctx, message, pubsub_topic, timeout)
|
||||
relay::waku_relay_publish_message(&self.ctx, message, pubsub_topic, timeout)
|
||||
}
|
||||
|
||||
/// Subscribe to WakuRelay to receive messages matching a content filter.
|
||||
pub fn relay_subscribe(&self, pubsub_topic: &String) -> Result<()> {
|
||||
relay::waku_relay_subscribe(self.ctx, pubsub_topic)
|
||||
relay::waku_relay_subscribe(&self.ctx, pubsub_topic)
|
||||
}
|
||||
|
||||
/// Closes the pubsub subscription to stop receiving messages matching a content filter. No more messages will be received from this pubsub topic
|
||||
pub fn relay_unsubscribe(&self, pubsub_topic: &String) -> Result<()> {
|
||||
relay::waku_relay_unsubscribe(self.ctx, pubsub_topic)
|
||||
relay::waku_relay_unsubscribe(&self.ctx, pubsub_topic)
|
||||
}
|
||||
|
||||
pub fn set_event_callback<F: FnMut(Event) + Send + Sync + 'static>(&self, f: F) {
|
||||
events::waku_set_event_callback(self.ctx, f)
|
||||
events::waku_set_event_callback(&self.ctx, f)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -8,6 +8,7 @@ use libc::*;
|
||||
use multiaddr::Multiaddr;
|
||||
// internal
|
||||
use crate::general::Result;
|
||||
use crate::node::context::WakuNodeContext;
|
||||
use crate::utils::{get_trampoline, handle_no_response};
|
||||
|
||||
/// Dial peer using a multiaddress
|
||||
@ -16,7 +17,7 @@ use crate::utils::{get_trampoline, handle_no_response};
|
||||
/// Use 0 for no timeout
|
||||
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_connect_peerchar-address-int-timeoutms)
|
||||
pub fn waku_connect(
|
||||
ctx: *mut c_void,
|
||||
ctx: &WakuNodeContext,
|
||||
address: &Multiaddr,
|
||||
timeout: Option<Duration>,
|
||||
) -> Result<()> {
|
||||
@ -30,7 +31,7 @@ pub fn waku_connect(
|
||||
let mut closure = error_cb;
|
||||
let cb = get_trampoline(&closure);
|
||||
let out = waku_sys::waku_connect(
|
||||
ctx,
|
||||
ctx.obj_ptr,
|
||||
address_ptr,
|
||||
timeout
|
||||
.map(|duration| duration.as_millis().try_into().unwrap_or(u32::MAX))
|
||||
|
||||
@ -7,13 +7,14 @@ use std::time::Duration;
|
||||
use libc::*;
|
||||
// internal
|
||||
use crate::general::{Encoding, MessageId, Result, WakuContentTopic, WakuMessage};
|
||||
use crate::node::context::WakuNodeContext;
|
||||
use crate::utils::{get_trampoline, handle_no_response, handle_response};
|
||||
|
||||
/// Create a content topic according to [RFC 23](https://rfc.vac.dev/spec/23/)
|
||||
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_content_topicchar-applicationname-unsigned-int-applicationversion-char-contenttopicname-char-encoding)
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
pub fn waku_create_content_topic(
|
||||
ctx: *mut c_void,
|
||||
ctx: &WakuNodeContext,
|
||||
application_name: &str,
|
||||
application_version: u32,
|
||||
content_topic_name: &str,
|
||||
@ -35,7 +36,7 @@ pub fn waku_create_content_topic(
|
||||
let mut closure = result_cb;
|
||||
let cb = get_trampoline(&closure);
|
||||
let out = waku_sys::waku_content_topic(
|
||||
ctx,
|
||||
ctx.obj_ptr,
|
||||
application_name_ptr,
|
||||
application_version,
|
||||
content_topic_name_ptr,
|
||||
@ -58,7 +59,7 @@ pub fn waku_create_content_topic(
|
||||
/// Publish a message using Waku Relay
|
||||
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_relay_publishchar-messagejson-char-pubsubtopic-int-timeoutms)
|
||||
pub fn waku_relay_publish_message(
|
||||
ctx: *mut c_void,
|
||||
ctx: &WakuNodeContext,
|
||||
message: &WakuMessage,
|
||||
pubsub_topic: &String,
|
||||
timeout: Option<Duration>,
|
||||
@ -81,7 +82,7 @@ pub fn waku_relay_publish_message(
|
||||
let mut closure = result_cb;
|
||||
let cb = get_trampoline(&closure);
|
||||
let out = waku_sys::waku_relay_publish(
|
||||
ctx,
|
||||
ctx.obj_ptr,
|
||||
pubsub_topic_ptr,
|
||||
message_ptr,
|
||||
timeout
|
||||
@ -105,7 +106,7 @@ pub fn waku_relay_publish_message(
|
||||
handle_response(code, &result)
|
||||
}
|
||||
|
||||
pub fn waku_relay_subscribe(ctx: *mut c_void, pubsub_topic: &String) -> Result<()> {
|
||||
pub fn waku_relay_subscribe(ctx: &WakuNodeContext, pubsub_topic: &String) -> Result<()> {
|
||||
let pubsub_topic = pubsub_topic.to_string();
|
||||
let pubsub_topic_ptr = CString::new(pubsub_topic)
|
||||
.expect("CString should build properly from pubsub topic")
|
||||
@ -117,7 +118,7 @@ pub fn waku_relay_subscribe(ctx: *mut c_void, pubsub_topic: &String) -> Result<(
|
||||
let mut closure = error_cb;
|
||||
let cb = get_trampoline(&closure);
|
||||
let out = waku_sys::waku_relay_subscribe(
|
||||
ctx,
|
||||
ctx.obj_ptr,
|
||||
pubsub_topic_ptr,
|
||||
cb,
|
||||
&mut closure as *mut _ as *mut c_void,
|
||||
@ -131,7 +132,7 @@ pub fn waku_relay_subscribe(ctx: *mut c_void, pubsub_topic: &String) -> Result<(
|
||||
handle_no_response(code, &error)
|
||||
}
|
||||
|
||||
pub fn waku_relay_unsubscribe(ctx: *mut c_void, pubsub_topic: &String) -> Result<()> {
|
||||
pub fn waku_relay_unsubscribe(ctx: &WakuNodeContext, pubsub_topic: &String) -> Result<()> {
|
||||
let pubsub_topic = pubsub_topic.to_string();
|
||||
let pubsub_topic_ptr = CString::new(pubsub_topic)
|
||||
.expect("CString should build properly from pubsub topic")
|
||||
@ -143,7 +144,7 @@ pub fn waku_relay_unsubscribe(ctx: *mut c_void, pubsub_topic: &String) -> Result
|
||||
let mut closure = error_cb;
|
||||
let cb = get_trampoline(&closure);
|
||||
let out = waku_sys::waku_relay_subscribe(
|
||||
ctx,
|
||||
ctx.obj_ptr,
|
||||
pubsub_topic_ptr,
|
||||
cb,
|
||||
&mut closure as *mut _ as *mut c_void,
|
||||
|
||||
@ -124,10 +124,9 @@ async fn default_echo() -> Result<(), String> {
|
||||
|
||||
assert!(got_all);
|
||||
|
||||
|
||||
node2.stop()?;
|
||||
node1.stop()?;
|
||||
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user