2024-11-28 10:35:41 +01:00
|
|
|
//! Waku lightpush protocol related methods
|
|
|
|
|
|
|
|
|
|
// std
|
|
|
|
|
use std::ffi::CString;
|
|
|
|
|
// crates
|
|
|
|
|
use libc::*;
|
2024-12-19 23:05:39 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
use tokio::sync::Notify;
|
2024-11-28 10:35:41 +01:00
|
|
|
// internal
|
2024-12-15 21:29:44 +01:00
|
|
|
use crate::general::{messagehash::MessageHash, Result, WakuMessage};
|
2024-11-28 10:35:41 +01:00
|
|
|
use crate::node::context::WakuNodeContext;
|
|
|
|
|
use crate::utils::{get_trampoline, handle_response, LibwakuResponse};
|
|
|
|
|
|
|
|
|
|
use crate::general::pubsubtopic::PubsubTopic;
|
|
|
|
|
|
2024-12-19 23:05:39 +01:00
|
|
|
pub async fn waku_lightpush_publish_message(
|
2024-11-28 10:35:41 +01:00
|
|
|
ctx: &WakuNodeContext,
|
|
|
|
|
message: &WakuMessage,
|
|
|
|
|
pubsub_topic: &PubsubTopic,
|
|
|
|
|
) -> Result<MessageHash> {
|
2024-12-19 23:05:39 +01:00
|
|
|
let message = CString::new(
|
2024-11-28 10:35:41 +01:00
|
|
|
serde_json::to_string(&message)
|
|
|
|
|
.expect("WakuMessages should always be able to success serializing"),
|
|
|
|
|
)
|
2024-12-19 23:05:39 +01:00
|
|
|
.expect("CString should build properly from the serialized waku message");
|
|
|
|
|
let message_ptr = message.as_ptr();
|
|
|
|
|
|
|
|
|
|
let pubsub_topic = CString::new(String::from(pubsub_topic))
|
|
|
|
|
.expect("CString should build properly from pubsub topic");
|
|
|
|
|
let pubsub_topic_ptr = pubsub_topic.as_ptr();
|
|
|
|
|
|
|
|
|
|
let mut result = LibwakuResponse::default();
|
|
|
|
|
let notify = Arc::new(Notify::new());
|
|
|
|
|
let notify_clone = notify.clone();
|
|
|
|
|
let result_cb = |r: LibwakuResponse| {
|
|
|
|
|
result = r;
|
|
|
|
|
notify_clone.notify_one(); // Notify that the value has been updated
|
|
|
|
|
};
|
|
|
|
|
|
2024-11-28 10:35:41 +01:00
|
|
|
let code = unsafe {
|
|
|
|
|
let mut closure = result_cb;
|
|
|
|
|
let cb = get_trampoline(&closure);
|
|
|
|
|
let out = waku_sys::waku_lightpush_publish(
|
|
|
|
|
ctx.get_ptr(),
|
|
|
|
|
pubsub_topic_ptr,
|
|
|
|
|
message_ptr,
|
|
|
|
|
cb,
|
|
|
|
|
&mut closure as *mut _ as *mut c_void,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
out
|
|
|
|
|
};
|
|
|
|
|
|
2024-12-19 23:05:39 +01:00
|
|
|
notify.notified().await; // Wait until a result is received
|
2024-11-28 10:35:41 +01:00
|
|
|
handle_response(code, result)
|
|
|
|
|
}
|