Skip to content

Commit

Permalink
add filter and lightpush
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivansete-status committed Nov 20, 2024
1 parent 4cfe285 commit b707942
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 5 deletions.
11 changes: 7 additions & 4 deletions examples/tic-tac-toe-gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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) {
Expand Down
102 changes: 102 additions & 0 deletions waku-bindings/src/node/filter.rs
Original file line number Diff line number Diff line change
@@ -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)
}
49 changes: 49 additions & 0 deletions waku-bindings/src/node/lightpush.rs
Original file line number Diff line number Diff line change
@@ -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<MessageHash> {
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)
}
22 changes: 22 additions & 0 deletions waku-bindings/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

mod config;
mod events;
mod filter;
mod lightpush;
mod management;
mod peers;
mod relay;
Expand Down Expand Up @@ -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<MessageHash> {
lightpush::waku_lightpush_publish_message(&self.ctx, message, pubsub_topic)
}
}

0 comments on commit b707942

Please sign in to comment.