diff --git a/examples/tic-tac-toe-gui/src/main.rs b/examples/tic-tac-toe-gui/src/main.rs index abc5728..72bb5ca 100644 --- a/examples/tic-tac-toe-gui/src/main.rs +++ b/examples/tic-tac-toe-gui/src/main.rs @@ -86,7 +86,10 @@ impl TicTacToeApp { self.waku.ctx.waku_set_event_callback(my_closure).expect("set event call back working"); // Subscribe to desired topic - self.waku.relay_subscribe(&self.game_topic.to_string()).expect("waku should subscribe"); + // self.waku.relay_subscribe(&self.game_topic.to_string()).expect("waku should subscribe"); + + let content_topic = WakuContentTopic::new("waku", "2", "tictactoegame", Encoding::Proto); + self.waku.filter_subscribe(&self.game_topic.to_string(), &content_topic.to_string()).expect("waku should subscribe"); // Connect to hard-coded node // let target_node_multi_addr = @@ -117,9 +120,9 @@ impl TicTacToeApp { false, ); - // let waku_handle = self.waku.lock().unwrap(); - self.waku.relay_publish_message(&message, &self.game_topic.to_string(), None) - .expect("Failed to send message"); + // self.waku.relay_publish_message(&message, &self.game_topic.to_string(), None) + // .expect("Failed to send message"); + self.waku.lightpush_publish_message(&message, &self.game_topic.to_string()).expect("Failed to send message"); } fn make_move(&mut self, row: usize, col: usize) { diff --git a/waku-bindings/src/node/filter.rs b/waku-bindings/src/node/filter.rs new file mode 100644 index 0000000..6a16f56 --- /dev/null +++ b/waku-bindings/src/node/filter.rs @@ -0,0 +1,102 @@ +//! Waku filter protocol related methods + +// std +use std::ffi::CString; +// crates +use libc::*; +// internal +use crate::general::Result; +use crate::node::events::WakuNodeContext; +use crate::utils::{get_trampoline, handle_no_response, LibwakuResponse}; + +pub fn waku_filter_subscribe( + ctx: &WakuNodeContext, + pubsub_topic: &str, + content_topics: &str, // comma-separated list of content topics +) -> Result<()> { + let pubsub_topic = pubsub_topic.to_string(); + let content_topics = content_topics.to_string(); + + let pubsub_topic_ptr = CString::new(pubsub_topic) + .expect("CString should build properly from pubsub topic") + .into_raw(); + let content_topics_ptr = CString::new(content_topics) + .expect("CString should build properly from content topic") + .into_raw(); + + let mut result: LibwakuResponse = Default::default(); + let result_cb = |r: LibwakuResponse| result = r; + let code = unsafe { + let mut closure = result_cb; + let cb = get_trampoline(&closure); + let out = waku_sys::waku_filter_subscribe( + ctx.obj_ptr, + pubsub_topic_ptr, + content_topics_ptr, + cb, + &mut closure as *mut _ as *mut c_void, + ); + + drop(CString::from_raw(pubsub_topic_ptr)); + drop(CString::from_raw(content_topics_ptr)); + + out + }; + + handle_no_response(code, result) +} + +pub fn waku_filter_unsubscribe( + ctx: &WakuNodeContext, + pubsub_topic: &str, + content_topics_topics: &str, // comma-separated list of content topics +) -> Result<()> { + let pubsub_topic = pubsub_topic.to_string(); + let content_topics_topics = content_topics_topics.to_string(); + + let pubsub_topic_ptr = CString::new(pubsub_topic) + .expect("CString should build properly from pubsub topic") + .into_raw(); + let content_topics_topics_ptr = CString::new(content_topics_topics) + .expect("CString should build properly from content topic") + .into_raw(); + + let mut result: LibwakuResponse = Default::default(); + let result_cb = |r: LibwakuResponse| result = r; + let code = unsafe { + let mut closure = result_cb; + let cb = get_trampoline(&closure); + let out = waku_sys::waku_filter_unsubscribe( + ctx.obj_ptr, + pubsub_topic_ptr, + content_topics_topics_ptr, + cb, + &mut closure as *mut _ as *mut c_void, + ); + + drop(CString::from_raw(pubsub_topic_ptr)); + drop(CString::from_raw(content_topics_topics_ptr)); + + out + }; + + handle_no_response(code, result) +} + +pub fn waku_filter_unsubscribe_all(ctx: &WakuNodeContext) -> Result<()> { + let mut result: LibwakuResponse = Default::default(); + let result_cb = |r: LibwakuResponse| result = r; + let code = unsafe { + let mut closure = result_cb; + let cb = get_trampoline(&closure); + let out = waku_sys::waku_filter_unsubscribe_all( + ctx.obj_ptr, + cb, + &mut closure as *mut _ as *mut c_void, + ); + + out + }; + + handle_no_response(code, result) +} diff --git a/waku-bindings/src/node/lightpush.rs b/waku-bindings/src/node/lightpush.rs new file mode 100644 index 0000000..f1c7290 --- /dev/null +++ b/waku-bindings/src/node/lightpush.rs @@ -0,0 +1,49 @@ +//! Waku lightpush protocol related methods + +// std +use std::ffi::CString; +// crates +use libc::*; +// internal +use crate::general::{MessageHash, Result, WakuMessage}; +use crate::node::events::WakuNodeContext; +use crate::utils::{get_trampoline, handle_response, LibwakuResponse}; + +pub fn waku_lightpush_publish_message( + ctx: &WakuNodeContext, + message: &WakuMessage, + pubsub_topic: &str, +) -> Result { + let pubsub_topic = pubsub_topic.to_string(); + + let message_ptr = CString::new( + serde_json::to_string(&message) + .expect("WakuMessages should always be able to success serializing"), + ) + .expect("CString should build properly from the serialized waku message") + .into_raw(); + let pubsub_topic_ptr = CString::new(pubsub_topic) + .expect("CString should build properly from pubsub topic") + .into_raw(); + + let mut result: LibwakuResponse = Default::default(); + let result_cb = |r: LibwakuResponse| result = r; + let code = unsafe { + let mut closure = result_cb; + let cb = get_trampoline(&closure); + let out = waku_sys::waku_lightpush_publish( + ctx.obj_ptr, + pubsub_topic_ptr, + message_ptr, + cb, + &mut closure as *mut _ as *mut c_void, + ); + + drop(CString::from_raw(message_ptr)); + drop(CString::from_raw(pubsub_topic_ptr)); + + out + }; + + handle_response(code, result) +} diff --git a/waku-bindings/src/node/mod.rs b/waku-bindings/src/node/mod.rs index d53ce75..459caef 100644 --- a/waku-bindings/src/node/mod.rs +++ b/waku-bindings/src/node/mod.rs @@ -2,6 +2,8 @@ mod config; mod events; +mod filter; +mod lightpush; mod management; mod peers; mod relay; @@ -133,4 +135,24 @@ impl WakuNodeHandle { pub fn relay_unsubscribe(&self, pubsub_topic: &String) -> Result<()> { relay::waku_relay_unsubscribe(&self.ctx, pubsub_topic) } + + pub fn filter_subscribe(&self, pubsub_topic: &String, content_topics: &String) -> Result<()> { + filter::waku_filter_subscribe(&self.ctx, pubsub_topic, content_topics) + } + + pub fn filter_unsubscribe(&self, pubsub_topic: &String, content_topics: &String) -> Result<()> { + filter::waku_filter_unsubscribe(&self.ctx, pubsub_topic, content_topics) + } + + pub fn filter_unsubscribe_all(&self) -> Result<()> { + filter::waku_filter_unsubscribe_all(&self.ctx) + } + + pub fn lightpush_publish_message( + &self, + message: &WakuMessage, + pubsub_topic: &String, + ) -> Result { + lightpush::waku_lightpush_publish_message(&self.ctx, message, pubsub_topic) + } } diff --git a/waku-sys/vendor b/waku-sys/vendor index 8368ff0..d814519 160000 --- a/waku-sys/vendor +++ b/waku-sys/vendor @@ -1 +1 @@ -Subproject commit 8368ff006d386ea1597862acca5405e1edbfa9cf +Subproject commit d814519578380bf01398c29424a5fd1005ed3a29