mirror of
https://github.com/logos-messaging/logos-messaging-rust-bindings.git
synced 2026-01-07 00:13:10 +00:00
add new context.rs to separate the code a little
This commit is contained in:
parent
e86b325bc9
commit
14aca7f403
48
waku-bindings/src/node/context.rs
Normal file
48
waku-bindings/src/node/context.rs
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
use std::ffi::c_void;
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
|
use crate::utils::{get_trampoline, LibwakuResponse};
|
||||||
|
|
||||||
|
pub struct WakuNodeContext {
|
||||||
|
obj_ptr: *mut c_void,
|
||||||
|
msg_observer: Arc<Mutex<Box<dyn FnMut(LibwakuResponse) + Send + Sync>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WakuNodeContext {
|
||||||
|
pub fn new(obj_ptr: *mut c_void) -> Self {
|
||||||
|
Self {
|
||||||
|
obj_ptr: obj_ptr,
|
||||||
|
msg_observer: Arc::new(Mutex::new(Box::new(|_response| {
|
||||||
|
println!("msg observer not set")
|
||||||
|
}))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_ptr(&self) -> *mut c_void {
|
||||||
|
self.obj_ptr
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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(LibwakuResponse) + 'static + Sync + Send>(
|
||||||
|
&self,
|
||||||
|
closure: F,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
if let Ok(mut boxed_closure) = self.msg_observer.lock() {
|
||||||
|
*boxed_closure = Box::new(closure);
|
||||||
|
unsafe {
|
||||||
|
let cb = get_trampoline(&(*boxed_closure));
|
||||||
|
waku_sys::waku_set_event_callback(
|
||||||
|
self.obj_ptr,
|
||||||
|
cb,
|
||||||
|
&mut (*boxed_closure) as *mut _ as *mut c_void,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(format!(
|
||||||
|
"Failed to acquire lock in waku_set_event_callback!"
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -4,24 +4,14 @@
|
|||||||
//! An example of an asynchronous event that might be emitted is receiving a message.
|
//! An example of an asynchronous event that might be emitted is receiving a message.
|
||||||
//! When an event is emitted, this callback will be triggered receiving an [`Event`]
|
//! When an event is emitted, this callback will be triggered receiving an [`Event`]
|
||||||
|
|
||||||
// std
|
|
||||||
use std::ffi::c_void;
|
|
||||||
// crates
|
// crates
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
// internal
|
// internal
|
||||||
use crate::general::WakuMessage;
|
use crate::general::WakuMessage;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
use crate::utils::{get_trampoline, LibwakuResponse};
|
|
||||||
use crate::MessageHash;
|
use crate::MessageHash;
|
||||||
|
|
||||||
use std::sync::{Arc, Mutex};
|
|
||||||
|
|
||||||
pub struct WakuNodeContext {
|
|
||||||
obj_ptr: *mut c_void,
|
|
||||||
msg_observer: Arc<Mutex<Box<dyn FnMut(LibwakuResponse) + Send + Sync>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Waku event
|
/// Waku event
|
||||||
/// For now just WakuMessage is supported
|
/// For now just WakuMessage is supported
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
@ -45,45 +35,6 @@ pub struct WakuMessageEvent {
|
|||||||
pub waku_message: WakuMessage,
|
pub waku_message: WakuMessage,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WakuNodeContext {
|
|
||||||
pub fn new(obj_ptr: *mut c_void) -> Self {
|
|
||||||
Self {
|
|
||||||
obj_ptr: obj_ptr,
|
|
||||||
msg_observer: Arc::new(Mutex::new(Box::new(|_response| {
|
|
||||||
println!("msg observer not set")
|
|
||||||
}))),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_ptr(&self) -> *mut c_void {
|
|
||||||
self.obj_ptr
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 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(LibwakuResponse) + 'static + Sync + Send>(
|
|
||||||
&self,
|
|
||||||
closure: F,
|
|
||||||
) -> Result<(), String> {
|
|
||||||
if let Ok(mut boxed_closure) = self.msg_observer.lock() {
|
|
||||||
*boxed_closure = Box::new(closure);
|
|
||||||
unsafe {
|
|
||||||
let cb = get_trampoline(&(*boxed_closure));
|
|
||||||
waku_sys::waku_set_event_callback(
|
|
||||||
self.obj_ptr,
|
|
||||||
cb,
|
|
||||||
&mut (*boxed_closure) as *mut _ as *mut c_void,
|
|
||||||
)
|
|
||||||
};
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(format!(
|
|
||||||
"Failed to acquire lock in waku_set_event_callback!"
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::Event;
|
use crate::Event;
|
||||||
|
|||||||
@ -6,7 +6,7 @@ use std::ffi::CString;
|
|||||||
use libc::*;
|
use libc::*;
|
||||||
// internal
|
// internal
|
||||||
use crate::general::Result;
|
use crate::general::Result;
|
||||||
use crate::node::events::WakuNodeContext;
|
use crate::node::context::WakuNodeContext;
|
||||||
use crate::utils::{get_trampoline, handle_no_response, LibwakuResponse};
|
use crate::utils::{get_trampoline, handle_no_response, LibwakuResponse};
|
||||||
|
|
||||||
pub fn waku_filter_subscribe(
|
pub fn waku_filter_subscribe(
|
||||||
|
|||||||
@ -6,7 +6,7 @@ use std::ffi::CString;
|
|||||||
use libc::*;
|
use libc::*;
|
||||||
// internal
|
// internal
|
||||||
use crate::general::{MessageHash, Result, WakuMessage};
|
use crate::general::{MessageHash, Result, WakuMessage};
|
||||||
use crate::node::events::WakuNodeContext;
|
use crate::node::context::WakuNodeContext;
|
||||||
use crate::utils::{get_trampoline, handle_response, LibwakuResponse};
|
use crate::utils::{get_trampoline, handle_response, LibwakuResponse};
|
||||||
|
|
||||||
pub fn waku_lightpush_publish_message(
|
pub fn waku_lightpush_publish_message(
|
||||||
|
|||||||
@ -8,7 +8,7 @@ use multiaddr::Multiaddr;
|
|||||||
// internal
|
// internal
|
||||||
use super::config::WakuNodeConfig;
|
use super::config::WakuNodeConfig;
|
||||||
use crate::general::Result;
|
use crate::general::Result;
|
||||||
use crate::node::events::WakuNodeContext;
|
use crate::node::context::WakuNodeContext;
|
||||||
use crate::utils::LibwakuResponse;
|
use crate::utils::LibwakuResponse;
|
||||||
use crate::utils::WakuDecode;
|
use crate::utils::WakuDecode;
|
||||||
use crate::utils::{get_trampoline, handle_json_response, handle_no_response, handle_response};
|
use crate::utils::{get_trampoline, handle_json_response, handle_no_response, handle_response};
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
//! Waku node implementation
|
//! Waku node implementation
|
||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
|
mod context;
|
||||||
mod events;
|
mod events;
|
||||||
mod filter;
|
mod filter;
|
||||||
mod lightpush;
|
mod lightpush;
|
||||||
@ -18,7 +19,8 @@ use crate::general::{MessageHash, Result, WakuMessage};
|
|||||||
|
|
||||||
pub use config::RLNConfig;
|
pub use config::RLNConfig;
|
||||||
pub use config::WakuNodeConfig;
|
pub use config::WakuNodeConfig;
|
||||||
pub use events::{Event, WakuMessageEvent, WakuNodeContext};
|
pub use context::WakuNodeContext;
|
||||||
|
pub use events::{Event, WakuMessageEvent};
|
||||||
pub use relay::waku_create_content_topic;
|
pub use relay::waku_create_content_topic;
|
||||||
|
|
||||||
use crate::Encoding;
|
use crate::Encoding;
|
||||||
|
|||||||
@ -8,7 +8,7 @@ use libc::*;
|
|||||||
use multiaddr::Multiaddr;
|
use multiaddr::Multiaddr;
|
||||||
// internal
|
// internal
|
||||||
use crate::general::Result;
|
use crate::general::Result;
|
||||||
use crate::node::events::WakuNodeContext;
|
use crate::node::context::WakuNodeContext;
|
||||||
use crate::utils::LibwakuResponse;
|
use crate::utils::LibwakuResponse;
|
||||||
use crate::utils::{get_trampoline, handle_no_response};
|
use crate::utils::{get_trampoline, handle_no_response};
|
||||||
|
|
||||||
|
|||||||
@ -7,7 +7,7 @@ use std::time::Duration;
|
|||||||
use libc::*;
|
use libc::*;
|
||||||
// internal
|
// internal
|
||||||
use crate::general::{Encoding, MessageHash, Result, WakuContentTopic, WakuMessage};
|
use crate::general::{Encoding, MessageHash, Result, WakuContentTopic, WakuMessage};
|
||||||
use crate::node::events::WakuNodeContext;
|
use crate::node::context::WakuNodeContext;
|
||||||
use crate::utils::{get_trampoline, handle_no_response, handle_response, LibwakuResponse};
|
use crate::utils::{get_trampoline, handle_no_response, handle_response, LibwakuResponse};
|
||||||
|
|
||||||
/// Create a content topic according to [RFC 23](https://rfc.vac.dev/spec/23/)
|
/// Create a content topic according to [RFC 23](https://rfc.vac.dev/spec/23/)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user