mirror of
https://github.com/logos-messaging/logos-messaging-rust-bindings.git
synced 2026-01-02 14:03:12 +00:00
* update nwaku vendor to v0.33.1 * build.rs: add negentropy dependency and cmdCount cmdLine dependencies * fix: call waku_setup when instantiating waku_new * Properly decode a Vec<Multiaddr> * First commit tic-tac-toe * adding some simple game logic to coordinate the turns a little * some logic to panic if a proper event callback hasn't been set * restoring back the type state pattern introduced by Richard * new PubsubTopic type * fix clippy issues --------- Co-authored-by: Richard Ramos <info@richardramos.me>
157 lines
5.2 KiB
Rust
157 lines
5.2 KiB
Rust
//! Waku [relay](https://rfc.vac.dev/spec/36/#waku-relay) protocol related methods
|
|
|
|
// std
|
|
use std::ffi::CString;
|
|
use std::time::Duration;
|
|
// crates
|
|
use libc::*;
|
|
// internal
|
|
use crate::general::contenttopic::{Encoding, WakuContentTopic};
|
|
use crate::general::pubsubtopic::PubsubTopic;
|
|
use crate::general::{MessageHash, Result, WakuMessage};
|
|
use crate::node::context::WakuNodeContext;
|
|
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/)
|
|
/// 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: &WakuNodeContext,
|
|
application_name: &str,
|
|
application_version: u32,
|
|
content_topic_name: &str,
|
|
encoding: Encoding,
|
|
) -> WakuContentTopic {
|
|
let application_name_ptr = CString::new(application_name)
|
|
.expect("Application name should always transform to CString")
|
|
.into_raw();
|
|
let content_topic_name_ptr = CString::new(content_topic_name)
|
|
.expect("Content topic should always transform to CString")
|
|
.into_raw();
|
|
let encoding_ptr = CString::new(encoding.to_string())
|
|
.expect("Encoding should always transform to CString")
|
|
.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_content_topic(
|
|
ctx.get_ptr(),
|
|
application_name_ptr,
|
|
application_version,
|
|
content_topic_name_ptr,
|
|
encoding_ptr,
|
|
cb,
|
|
&mut closure as *mut _ as *mut c_void,
|
|
);
|
|
|
|
drop(CString::from_raw(application_name_ptr));
|
|
drop(CString::from_raw(content_topic_name_ptr));
|
|
drop(CString::from_raw(encoding_ptr));
|
|
|
|
out
|
|
};
|
|
|
|
handle_response(code, result).expect("&str from result should always be extracted")
|
|
}
|
|
|
|
/// 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: &WakuNodeContext,
|
|
message: &WakuMessage,
|
|
pubsub_topic: &PubsubTopic,
|
|
timeout: Option<Duration>,
|
|
) -> Result<MessageHash> {
|
|
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(String::from(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_relay_publish(
|
|
ctx.get_ptr(),
|
|
pubsub_topic_ptr,
|
|
message_ptr,
|
|
timeout
|
|
.map(|duration| {
|
|
duration
|
|
.as_millis()
|
|
.try_into()
|
|
.expect("Duration as milliseconds should fit in a u32")
|
|
})
|
|
.unwrap_or(0),
|
|
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)
|
|
}
|
|
|
|
pub fn waku_relay_subscribe(ctx: &WakuNodeContext, pubsub_topic: &PubsubTopic) -> Result<()> {
|
|
let pubsub_topic_ptr = CString::new(String::from(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_relay_subscribe(
|
|
ctx.get_ptr(),
|
|
pubsub_topic_ptr,
|
|
cb,
|
|
&mut closure as *mut _ as *mut c_void,
|
|
);
|
|
|
|
drop(CString::from_raw(pubsub_topic_ptr));
|
|
|
|
out
|
|
};
|
|
|
|
handle_no_response(code, result)
|
|
}
|
|
|
|
pub fn waku_relay_unsubscribe(ctx: &WakuNodeContext, pubsub_topic: &PubsubTopic) -> Result<()> {
|
|
let pubsub_topic_ptr = CString::new(String::from(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_relay_subscribe(
|
|
ctx.get_ptr(),
|
|
pubsub_topic_ptr,
|
|
cb,
|
|
&mut closure as *mut _ as *mut c_void,
|
|
);
|
|
|
|
drop(CString::from_raw(pubsub_topic_ptr));
|
|
|
|
out
|
|
};
|
|
|
|
handle_no_response(code, result)
|
|
}
|