From 15dbe6284ec3a4edb6e02d871cf60c44cd4df938 Mon Sep 17 00:00:00 2001 From: Daniel Sanchez Quiros Date: Fri, 7 Oct 2022 11:26:55 +0200 Subject: [PATCH] Implemented filter methods wrappers --- waku/src/node/filter.rs | 70 +++++++++++++++++++++++++++++++++++++++++ waku/src/node/mod.rs | 1 + 2 files changed, 71 insertions(+) create mode 100644 waku/src/node/filter.rs diff --git a/waku/src/node/filter.rs b/waku/src/node/filter.rs new file mode 100644 index 0000000..7b763b5 --- /dev/null +++ b/waku/src/node/filter.rs @@ -0,0 +1,70 @@ +// std +use std::ffi::{CStr, CString}; +use std::time::Duration; +// crates + +// internal +use crate::general::Result; +use crate::general::{FilterSubscription, JsonResponse, MessageId, PeerId}; + +/// Creates a subscription in a lightnode for messages that matches a content filter and optionally a [`PubSubTopic`] +/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_filter_subscribechar-filterjson-char-peerid-int-timeoutms) +pub fn waku_filter_subscribe( + filter_subscription: &FilterSubscription, + peer_id: PeerId, + timeout: Duration, +) -> Result<()> { + let result = unsafe { + CStr::from_ptr(waku_sys::waku_filter_subscribe( + CString::new( + serde_json::to_string(filter_subscription) + .expect("FilterSubscription should always be able to be serialized"), + ) + .expect("CString should build properly from the serialized filter subscription") + .into_raw(), + CString::new(peer_id) + .expect("CString should build properly from peer id") + .into_raw(), + timeout + .as_millis() + .try_into() + .expect("Duration as milliseconds should fit in a i32"), + )) + } + .to_str() + .expect("Response should always succeed to load to a &str"); + + let response: JsonResponse = + serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); + + Result::from(response).map(|_| ()) +} + +/// Removes subscriptions in a light node matching a content filter and, optionally, a [`PubSubTopic`] +/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_filter_unsubscribechar-filterjson-int-timeoutms) +pub fn waku_filter_unsubscribe( + filter_subscription: &FilterSubscription, + timeout: Duration, +) -> Result<()> { + let result = unsafe { + CStr::from_ptr(waku_sys::waku_filter_unsubscribe( + CString::new( + serde_json::to_string(filter_subscription) + .expect("FilterSubscription should always be able to be serialized"), + ) + .expect("CString should build properly from the serialized filter subscription") + .into_raw(), + timeout + .as_millis() + .try_into() + .expect("Duration as milliseconds should fit in a i32"), + )) + } + .to_str() + .expect("Response should always succeed to load to a &str"); + + let response: JsonResponse = + serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); + + Result::from(response).map(|_| ()) +} diff --git a/waku/src/node/mod.rs b/waku/src/node/mod.rs index 3010900..4cb4b19 100644 --- a/waku/src/node/mod.rs +++ b/waku/src/node/mod.rs @@ -1,4 +1,5 @@ mod config; +mod filter; mod lightpush; mod management; mod peers;