Skip to content
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

refactor: hide *mut c_void behind WakunodeContext #96

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions waku-bindings/src/node/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// crates
use libc::c_void;

pub struct WakuNodeContext {
pub obj_ptr: *mut c_void,
}
5 changes: 3 additions & 2 deletions waku-bindings/src/node/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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)
};
}

Expand Down
31 changes: 16 additions & 15 deletions waku-bindings/src/node/management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
Expand All @@ -37,61 +38,61 @@ 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)
}

/// 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)
}

/// 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)
}

/// 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)
Expand All @@ -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());
}
}
27 changes: 13 additions & 14 deletions waku-bindings/src/node/mod.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,52 @@
//! Waku node implementation

mod config;
mod context;
mod events;
mod management;
mod peers;
mod relay;

// std
pub use aes_gcm::{Aes256Gcm, Key};
pub use aes_gcm::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};
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
Expand All @@ -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.
Expand All @@ -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)
}
}

Expand Down
5 changes: 3 additions & 2 deletions waku-bindings/src/node/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<()> {
Expand All @@ -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))
Expand Down
17 changes: 9 additions & 8 deletions waku-bindings/src/node/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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>,
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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,
Expand All @@ -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")
Expand All @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions waku-bindings/tests/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,9 @@ async fn default_echo() -> Result<(), String> {

assert!(got_all);


node2.stop()?;
node1.stop()?;

Ok(())
}

Expand Down
Loading
Loading