2022-10-09 16:50:40 +02:00
//! Waku [relay](https://rfc.vac.dev/spec/36/#waku-relay) protocol related methods
2022-10-06 15:51:00 +02:00
// std
2023-11-02 13:59:41 -04:00
use std ::ffi ::CString ;
2022-10-06 15:51:00 +02:00
use std ::time ::Duration ;
2023-11-02 13:59:41 -04:00
// crates
use libc ::* ;
2022-10-06 15:51:00 +02:00
// internal
2024-11-28 10:35:41 +01:00
use crate ::general ::contenttopic ::{ Encoding , WakuContentTopic } ;
use crate ::general ::pubsubtopic ::PubsubTopic ;
use crate ::general ::{ MessageHash , Result , WakuMessage } ;
2024-02-26 11:13:30 -04:00
use crate ::node ::context ::WakuNodeContext ;
2024-03-01 11:25:02 -04:00
use crate ::utils ::{ get_trampoline , handle_no_response , handle_response , LibwakuResponse } ;
2022-10-06 15:51:00 +02:00
/// 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)
2024-02-13 18:03:34 -04:00
#[ allow(clippy::not_unsafe_ptr_arg_deref) ]
2022-10-06 15:51:00 +02:00
pub fn waku_create_content_topic (
2024-02-26 11:13:30 -04:00
ctx : & WakuNodeContext ,
2022-10-06 15:51:00 +02:00
application_name : & str ,
2024-02-12 20:13:49 -04:00
application_version : u32 ,
2022-10-06 15:51:00 +02:00
content_topic_name : & str ,
encoding : Encoding ,
) -> WakuContentTopic {
2023-02-14 18:30:08 +01:00
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 ( ) ;
2024-03-01 11:25:02 -04:00
let mut result : LibwakuResponse = Default ::default ( ) ;
let result_cb = | r : LibwakuResponse | result = r ;
2023-11-02 13:59:41 -04:00
let code = unsafe {
let mut closure = result_cb ;
let cb = get_trampoline ( & closure ) ;
let out = waku_sys ::waku_content_topic (
2024-11-28 10:35:41 +01:00
ctx . get_ptr ( ) ,
2023-02-14 18:30:08 +01:00
application_name_ptr ,
2024-02-12 20:13:49 -04:00
application_version ,
2023-02-14 18:30:08 +01:00
content_topic_name_ptr ,
encoding_ptr ,
2023-11-02 13:59:41 -04:00
cb ,
& mut closure as * mut _ as * mut c_void ,
2023-02-14 18:30:08 +01:00
) ;
2023-11-02 13:59:41 -04:00
2023-02-14 18:30:08 +01:00
drop ( CString ::from_raw ( application_name_ptr ) ) ;
drop ( CString ::from_raw ( content_topic_name_ptr ) ) ;
drop ( CString ::from_raw ( encoding_ptr ) ) ;
2023-11-02 13:59:41 -04:00
out
} ;
2023-02-14 18:30:08 +01:00
2024-03-01 11:25:02 -04:00
handle_response ( code , result ) . expect ( " &str from result should always be extracted " )
2022-10-06 15:51:00 +02:00
}
/// 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 (
2024-02-26 11:13:30 -04:00
ctx : & WakuNodeContext ,
2022-10-06 15:51:00 +02:00
message : & WakuMessage ,
2024-11-28 10:35:41 +01:00
pubsub_topic : & PubsubTopic ,
2022-10-17 19:30:07 +02:00
timeout : Option < Duration > ,
2024-03-01 11:31:00 -04:00
) -> Result < MessageHash > {
2023-02-14 18:30:08 +01:00
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 ( ) ;
2024-11-28 10:35:41 +01:00
let pubsub_topic_ptr = CString ::new ( String ::from ( pubsub_topic ) )
2023-02-14 18:30:08 +01:00
. expect ( " CString should build properly from pubsub topic " )
. into_raw ( ) ;
2024-03-01 11:25:02 -04:00
let mut result : LibwakuResponse = Default ::default ( ) ;
let result_cb = | r : LibwakuResponse | result = r ;
2023-11-02 13:59:41 -04:00
let code = unsafe {
let mut closure = result_cb ;
let cb = get_trampoline ( & closure ) ;
let out = waku_sys ::waku_relay_publish (
2024-11-28 10:35:41 +01:00
ctx . get_ptr ( ) ,
2023-02-14 18:30:08 +01:00
pubsub_topic_ptr ,
2024-02-14 16:52:21 -04:00
message_ptr ,
2022-10-06 15:51:00 +02:00
timeout
2022-10-17 19:30:07 +02:00
. map ( | duration | {
duration
. as_millis ( )
. try_into ( )
2024-02-12 20:13:49 -04:00
. expect ( " Duration as milliseconds should fit in a u32 " )
2022-10-17 19:30:07 +02:00
} )
. unwrap_or ( 0 ) ,
2023-11-02 13:59:41 -04:00
cb ,
& mut closure as * mut _ as * mut c_void ,
2023-02-14 18:30:08 +01:00
) ;
2023-11-02 13:59:41 -04:00
2023-02-14 18:30:08 +01:00
drop ( CString ::from_raw ( message_ptr ) ) ;
drop ( CString ::from_raw ( pubsub_topic_ptr ) ) ;
2023-11-02 13:59:41 -04:00
out
2023-02-14 18:30:08 +01:00
} ;
2024-03-01 11:31:00 -04:00
handle_response ( code , result )
2022-10-06 15:51:00 +02:00
}
2024-11-28 10:35:41 +01:00
pub fn waku_relay_subscribe ( ctx : & WakuNodeContext , pubsub_topic : & PubsubTopic ) -> Result < ( ) > {
let pubsub_topic_ptr = CString ::new ( String ::from ( pubsub_topic ) )
2023-02-14 18:30:08 +01:00
. expect ( " CString should build properly from pubsub topic " )
. into_raw ( ) ;
2024-03-01 11:25:02 -04:00
let mut result : LibwakuResponse = Default ::default ( ) ;
let result_cb = | r : LibwakuResponse | result = r ;
2023-11-02 13:59:41 -04:00
let code = unsafe {
2024-03-01 11:25:02 -04:00
let mut closure = result_cb ;
2023-11-02 13:59:41 -04:00
let cb = get_trampoline ( & closure ) ;
let out = waku_sys ::waku_relay_subscribe (
2024-11-28 10:35:41 +01:00
ctx . get_ptr ( ) ,
2024-02-08 17:16:22 -04:00
pubsub_topic_ptr ,
2023-11-02 13:59:41 -04:00
cb ,
& mut closure as * mut _ as * mut c_void ,
) ;
2024-02-08 17:16:22 -04:00
drop ( CString ::from_raw ( pubsub_topic_ptr ) ) ;
2023-11-02 13:59:41 -04:00
out
2023-02-14 18:30:08 +01:00
} ;
2024-03-01 11:25:02 -04:00
handle_no_response ( code , result )
2022-10-06 15:51:00 +02:00
}
2024-11-28 10:35:41 +01:00
pub fn waku_relay_unsubscribe ( ctx : & WakuNodeContext , pubsub_topic : & PubsubTopic ) -> Result < ( ) > {
let pubsub_topic_ptr = CString ::new ( String ::from ( pubsub_topic ) )
2024-02-08 17:16:22 -04:00
. expect ( " CString should build properly from pubsub topic " )
. into_raw ( ) ;
2024-03-01 11:25:02 -04:00
let mut result : LibwakuResponse = Default ::default ( ) ;
let result_cb = | r : LibwakuResponse | result = r ;
2023-11-02 13:59:41 -04:00
let code = unsafe {
2024-03-01 11:25:02 -04:00
let mut closure = result_cb ;
2023-11-02 13:59:41 -04:00
let cb = get_trampoline ( & closure ) ;
let out = waku_sys ::waku_relay_subscribe (
2024-11-28 10:35:41 +01:00
ctx . get_ptr ( ) ,
2024-02-08 17:16:22 -04:00
pubsub_topic_ptr ,
2023-11-02 13:59:41 -04:00
cb ,
& mut closure as * mut _ as * mut c_void ,
) ;
2024-02-08 17:16:22 -04:00
drop ( CString ::from_raw ( pubsub_topic_ptr ) ) ;
2023-11-02 13:59:41 -04:00
out
2023-02-14 18:30:08 +01:00
} ;
2024-03-01 11:25:02 -04:00
handle_no_response ( code , result )
2022-10-06 15:51:00 +02:00
}