add filter and lightpush

This commit is contained in:
Ivan Folgueira Bande 2024-11-20 10:36:23 +01:00
parent 4cfe285eff
commit b707942bb6
No known key found for this signature in database
GPG Key ID: 3C117481F89E24A7
5 changed files with 181 additions and 5 deletions

View File

@ -86,7 +86,10 @@ impl TicTacToeApp {
self.waku.ctx.waku_set_event_callback(my_closure).expect("set event call back working"); self.waku.ctx.waku_set_event_callback(my_closure).expect("set event call back working");
// Subscribe to desired topic // 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 // Connect to hard-coded node
// let target_node_multi_addr = // let target_node_multi_addr =
@ -117,9 +120,9 @@ impl TicTacToeApp {
false, false,
); );
// let waku_handle = self.waku.lock().unwrap(); // self.waku.relay_publish_message(&message, &self.game_topic.to_string(), None)
self.waku.relay_publish_message(&message, &self.game_topic.to_string(), None) // .expect("Failed to send message");
.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) { fn make_move(&mut self, row: usize, col: usize) {

View File

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

View File

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

View File

@ -2,6 +2,8 @@
mod config; mod config;
mod events; mod events;
mod filter;
mod lightpush;
mod management; mod management;
mod peers; mod peers;
mod relay; mod relay;
@ -133,4 +135,24 @@ impl WakuNodeHandle {
pub fn relay_unsubscribe(&self, pubsub_topic: &String) -> Result<()> { 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 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)
}
} }

@ -1 +1 @@
Subproject commit 8368ff006d386ea1597862acca5405e1edbfa9cf Subproject commit d814519578380bf01398c29424a5fd1005ed3a29