From 5abd9cc94358409c8a806f1bcae75b652d5251cb Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Thu, 15 Feb 2024 19:16:16 -0400 Subject: [PATCH] refactor: callback --- Cargo.lock | 307 ++++++++++++------------------- Cargo.toml | 3 +- waku-bindings/Cargo.toml | 2 +- waku-bindings/src/node/events.rs | 27 +-- waku-bindings/src/node/mod.rs | 37 +++- waku-bindings/src/utils.rs | 7 +- 6 files changed, 158 insertions(+), 225 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1cea34a..b260183 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -93,12 +93,6 @@ dependencies = [ "libc", ] -[[package]] -name = "anyhow" -version = "1.0.66" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6" - [[package]] name = "ark-bn254" version = "0.4.0" @@ -319,7 +313,7 @@ version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" dependencies = [ - "hermit-abi", + "hermit-abi 0.1.19", "libc", "winapi", ] @@ -485,12 +479,6 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec8a7b6a70fde80372154c65702f00a0f56f3e1c36abbc6c440484be248856db" -[[package]] -name = "cassowary" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df8670b8c7b9dae1793364eafadf7239c40d669904660c5960d74cfd80b46a53" - [[package]] name = "cast" version = "0.3.0" @@ -525,11 +513,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bfd4d1b31faaa3a89d7934dbded3111da0d2ef28e3ebccdb4f0179f5929d1ef1" dependencies = [ "iana-time-zone", - "js-sys", "num-integer", "num-traits", - "time", - "wasm-bindgen", "winapi", ] @@ -794,31 +779,6 @@ version = "0.8.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345" -[[package]] -name = "crossterm" -version = "0.25.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e64e6c0fbe2c17357405f7c758c1ef960fce08bdfb2c03d88d2a18d7e09c4b67" -dependencies = [ - "bitflags 1.3.2", - "crossterm_winapi", - "libc", - "mio", - "parking_lot 0.12.1", - "signal-hook", - "signal-hook-mio", - "winapi", -] - -[[package]] -name = "crossterm_winapi" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ae1b35a484aa10e07fe0638d02301c5ad24de82d310ccbd2f3693da5f09bf1c" -dependencies = [ - "winapi", -] - [[package]] name = "crunchy" version = "0.2.2" @@ -1315,7 +1275,7 @@ checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" dependencies = [ "cfg-if", "libc", - "wasi 0.11.0+wasi-snapshot-preview1", + "wasi", ] [[package]] @@ -1404,6 +1364,12 @@ dependencies = [ "libc", ] +[[package]] +name = "hermit-abi" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd5256b483761cd23699d0da46cc6fd2ee3be420bbe6d020ae4a091e70b7e9fd" + [[package]] name = "hex" version = "0.4.3" @@ -1697,14 +1663,13 @@ dependencies = [ [[package]] name = "mio" -version = "0.8.4" +version = "0.8.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57ee1c23c7c63b0c9250c339ffdc69255f110b298b901b9f6c82547b7b87caaf" +checksum = "8f3d0b296e374a4e6f3c7b0a1f5a51d748a0d34c85e7dc48fc3fa9a87657fe09" dependencies = [ "libc", - "log", - "wasi 0.11.0+wasi-snapshot-preview1", - "windows-sys 0.36.1", + "wasi", + "windows-sys 0.48.0", ] [[package]] @@ -1854,6 +1819,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num_cpus" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +dependencies = [ + "hermit-abi 0.3.6", + "libc", +] + [[package]] name = "object" version = "0.28.4" @@ -1967,9 +1942,9 @@ checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" [[package]] name = "pin-project-lite" -version = "0.2.9" +version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" +checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" [[package]] name = "pin-utils" @@ -2086,29 +2061,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "prost" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "399c3c31cdec40583bb68f0b18403400d01ec4289c383aa047560439952c4dd7" -dependencies = [ - "bytes", - "prost-derive", -] - -[[package]] -name = "prost-derive" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7345d5f0e08c0536d7ac7229952590239e77abf0a0100a1b1d890add6ea96364" -dependencies = [ - "anyhow", - "itertools", - "proc-macro2", - "quote", - "syn 1.0.100", -] - [[package]] name = "ptr_meta" version = "0.1.4" @@ -2590,27 +2542,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3" -[[package]] -name = "signal-hook" -version = "0.3.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a253b5e89e2698464fc26b545c9edceb338e18a89effeeecfea192c3025be29d" -dependencies = [ - "libc", - "signal-hook-registry", -] - -[[package]] -name = "signal-hook-mio" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29ad2e15f37ec9a6cc544097b78a1ec90001e9f71b81338ca39f430adaca99af" -dependencies = [ - "libc", - "mio", - "signal-hook", -] - [[package]] name = "signal-hook-registry" version = "1.4.0" @@ -2678,6 +2609,16 @@ dependencies = [ "syn 1.0.100", ] +[[package]] +name = "socket2" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b5fac59a5cb5dd637972e5fca70daf0523c9067fcdc4842f053dae04a18f8e9" +dependencies = [ + "libc", + "windows-sys 0.48.0", +] + [[package]] name = "spki" version = "0.6.0" @@ -2844,17 +2785,6 @@ dependencies = [ "once_cell", ] -[[package]] -name = "time" -version = "0.1.44" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" -dependencies = [ - "libc", - "wasi 0.10.0+wasi-snapshot-preview1", - "winapi", -] - [[package]] name = "tiny-keccak" version = "2.0.2" @@ -2891,25 +2821,32 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" [[package]] name = "tokio" -version = "1.25.0" +version = "1.36.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8e00990ebabbe4c14c08aca901caed183ecd5c09562a12c824bb53d3c3fd3af" +checksum = "61285f6515fa018fb2d1e46eb21223fff441ee8db5d0f1435e8ab4f5cdb80931" dependencies = [ - "autocfg", + "backtrace", + "bytes", + "libc", + "mio", + "num_cpus", + "parking_lot 0.12.1", "pin-project-lite", + "signal-hook-registry", + "socket2", "tokio-macros", - "windows-sys 0.42.0", + "windows-sys 0.48.0", ] [[package]] name = "tokio-macros" -version = "1.8.2" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d266c00fde287f55d3f1c3e96c500c362a2b8c695076ec180f27918820bc6df8" +checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" dependencies = [ "proc-macro2", "quote", - "syn 1.0.100", + "syn 2.0.48", ] [[package]] @@ -2921,18 +2858,6 @@ dependencies = [ "serde", ] -[[package]] -name = "toy-chat" -version = "0.1.0" -dependencies = [ - "chrono", - "crossterm", - "prost", - "tui", - "unicode-width", - "waku-bindings", -] - [[package]] name = "tracing" version = "0.1.40" @@ -2996,19 +2921,6 @@ dependencies = [ "tracing-core", ] -[[package]] -name = "tui" -version = "0.19.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ccdd26cbd674007e649a272da4475fb666d3aa0ad0531da7136db6fab0e5bad1" -dependencies = [ - "bitflags 1.3.2", - "cassowary", - "crossterm", - "unicode-segmentation", - "unicode-width", -] - [[package]] name = "typenum" version = "1.15.0" @@ -3036,12 +2948,6 @@ dependencies = [ "tinyvec", ] -[[package]] -name = "unicode-segmentation" -version = "1.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fdbf052a0783de01e944a6ce7a8cb939e295b1e7be835a1112c3b9a7f047a5a" - [[package]] name = "unicode-width" version = "0.1.10" @@ -3143,12 +3049,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "wasi" -version = "0.10.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" - [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -3541,19 +3441,6 @@ dependencies = [ "windows_x86_64_msvc 0.33.0", ] -[[package]] -name = "windows-sys" -version = "0.36.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2" -dependencies = [ - "windows_aarch64_msvc 0.36.1", - "windows_i686_gnu 0.36.1", - "windows_i686_msvc 0.36.1", - "windows_x86_64_gnu 0.36.1", - "windows_x86_64_msvc 0.36.1", -] - [[package]] name = "windows-sys" version = "0.42.0" @@ -3569,13 +3456,37 @@ dependencies = [ "windows_x86_64_msvc 0.42.0", ] +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + [[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets", + "windows-targets 0.52.0", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", ] [[package]] @@ -3599,6 +3510,12 @@ version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.52.0" @@ -3611,18 +3528,18 @@ version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd761fd3eb9ab8cc1ed81e56e567f02dd82c4c837e48ac3b2181b9ffc5060807" -[[package]] -name = "windows_aarch64_msvc" -version = "0.36.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47" - [[package]] name = "windows_aarch64_msvc" version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4" +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + [[package]] name = "windows_aarch64_msvc" version = "0.52.0" @@ -3635,18 +3552,18 @@ version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cab0cf703a96bab2dc0c02c0fa748491294bf9b7feb27e1f4f96340f208ada0e" -[[package]] -name = "windows_i686_gnu" -version = "0.36.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6" - [[package]] name = "windows_i686_gnu" version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7" +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + [[package]] name = "windows_i686_gnu" version = "0.52.0" @@ -3659,18 +3576,18 @@ version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8cfdbe89cc9ad7ce618ba34abc34bbb6c36d99e96cae2245b7943cd75ee773d0" -[[package]] -name = "windows_i686_msvc" -version = "0.36.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024" - [[package]] name = "windows_i686_msvc" version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246" +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + [[package]] name = "windows_i686_msvc" version = "0.52.0" @@ -3683,18 +3600,18 @@ version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4dd9b0c0e9ece7bb22e84d70d01b71c6d6248b81a3c60d11869451b4cb24784" -[[package]] -name = "windows_x86_64_gnu" -version = "0.36.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1" - [[package]] name = "windows_x86_64_gnu" version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed" +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + [[package]] name = "windows_x86_64_gnu" version = "0.52.0" @@ -3707,6 +3624,12 @@ version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + [[package]] name = "windows_x86_64_gnullvm" version = "0.52.0" @@ -3719,18 +3642,18 @@ version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff1e4aa646495048ec7f3ffddc411e1d829c026a2ec62b39da15c1055e406eaa" -[[package]] -name = "windows_x86_64_msvc" -version = "0.36.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" - [[package]] name = "windows_x86_64_msvc" version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + [[package]] name = "windows_x86_64_msvc" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index e424c95..5d16f28 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,5 +3,4 @@ members = [ "waku-bindings", "waku-sys", - "examples/toy-chat" -] \ No newline at end of file + ] \ No newline at end of file diff --git a/waku-bindings/Cargo.toml b/waku-bindings/Cargo.toml index cd21ff1..3aab4d5 100644 --- a/waku-bindings/Cargo.toml +++ b/waku-bindings/Cargo.toml @@ -34,4 +34,4 @@ rln = "0.3.4" [dev-dependencies] futures = "0.3.25" serial_test = "1.0.0" -tokio = { version = "1.24.2", features = ["macros", "rt", "sync", "time"] } +tokio = { version = "1.36.0", features = ["full"] } diff --git a/waku-bindings/src/node/events.rs b/waku-bindings/src/node/events.rs index e9fdb46..d8c1f6d 100644 --- a/waku-bindings/src/node/events.rs +++ b/waku-bindings/src/node/events.rs @@ -6,7 +6,9 @@ // std use std::ffi::c_void; +use once_cell::sync::Lazy; // crates +use std::ops::Deref; use serde::{Deserialize, Serialize}; // internal use crate::general::WakuMessage; @@ -14,6 +16,8 @@ use crate::utils::get_trampoline; use crate::MessageId; use waku_sys::WakuCallBack; +use std::sync::Mutex; + /// Waku event /// For now just WakuMessage is supported #[non_exhaustive] @@ -51,34 +55,21 @@ impl WakuMessageEvent { } } -/// Wrapper callback, it transformst the `*const c_char` into an [`Event`] -fn callback(mut f: F) -> WakuCallBack { - let cb = move |v: &str| { - let data: Event = serde_json::from_str(v).expect("Parsing event to succeed"); - println!("EXEC CALLBACK"); - f(data); - println!("SUCCESS!"); - }; - - get_trampoline(&cb) -} - /// 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(ctx: *mut c_void, f: F) { - unsafe { waku_sys::waku_set_event_callback(ctx, callback(f), std::ptr::null_mut()) }; +pub fn waku_set_event_callback(ctx: *mut c_void, b: &Lazy>>) { + // TODO: call somehow `unsafe { waku_sys::waku_set_event_callback(ctx, cb) }`; } #[cfg(test)] mod tests { - use crate::node::events::callback; use crate::Event; // TODO: how to actually send an event and check if the callback is run? - #[test] - fn set_callback() { + //#[test] + /*fn set_callback() { callback(|_event| {}); - } + }*/ #[test] fn deserialize_message_event() { diff --git a/waku-bindings/src/node/mod.rs b/waku-bindings/src/node/mod.rs index cd27dd7..c45e1ee 100644 --- a/waku-bindings/src/node/mod.rs +++ b/waku-bindings/src/node/mod.rs @@ -10,7 +10,9 @@ mod relay; pub use aes_gcm::{Aes256Gcm, Key}; pub use multiaddr::Multiaddr; pub use secp256k1::{PublicKey, SecretKey}; +use std::sync::{Arc, Mutex}; use std::time::Duration; +use once_cell::sync::Lazy; // crates use libc::c_void; // internal @@ -21,22 +23,31 @@ pub use config::WakuNodeConfig; pub use events::{Event, WakuMessageEvent}; pub use relay::{waku_create_content_topic, waku_default_pubsub_topic}; +pub struct OpaqueWakunodePtr { + obj_ptr: *mut c_void, +} + +unsafe impl Send for OpaqueWakunodePtr {} + /// Handle to the underliying waku node pub struct WakuNodeHandle { - ctx: *mut c_void, + ctx: Arc>, + callback: Lazy>>, } impl WakuNodeHandle { /// Start a Waku node mounting all the protocols that were enabled during the Waku node instantiation. /// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_start) pub fn start(&self) -> Result<()> { - management::waku_start(self.ctx) + let mut ctx = self.ctx.lock().unwrap(); + management::waku_start(ctx.obj_ptr) } /// Stops a Waku node /// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_stop) pub fn stop(&self) -> Result<()> { - management::waku_stop(self.ctx) + let mut ctx = self.ctx.lock().unwrap(); + management::waku_stop(ctx.obj_ptr) } /// Dial peer using a multiaddress @@ -45,7 +56,8 @@ impl WakuNodeHandle { /// Use 0 for no timeout /// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_connect_peerchar-address-int-timeoutms) pub fn connect(&self, address: &Multiaddr, timeout: Option) -> Result<()> { - peers::waku_connect(self.ctx, address, timeout) + let mut ctx = self.ctx.lock().unwrap(); + peers::waku_connect(ctx.obj_ptr, address, timeout) } /// Publish a message using Waku Relay. @@ -57,28 +69,35 @@ impl WakuNodeHandle { pubsub_topic: &String, timeout: Option, ) -> Result { - relay::waku_relay_publish_message(self.ctx, message, pubsub_topic, timeout) + let mut ctx = self.ctx.lock().unwrap(); + relay::waku_relay_publish_message(ctx.obj_ptr, message, pubsub_topic, timeout) } /// Subscribe to WakuRelay to receive messages matching a content filter. pub fn relay_subscribe(&self, pubsub_topic: &String) -> Result<()> { - relay::waku_relay_subscribe(self.ctx, pubsub_topic) + let mut ctx = self.ctx.lock().unwrap(); + relay::waku_relay_subscribe(ctx.obj_ptr, pubsub_topic) } /// Closes the pubsub subscription to stop receiving messages matching a content filter. No more messages will be received from this pubsub topic pub fn relay_unsubscribe(&self, pubsub_topic: &String) -> Result<()> { - relay::waku_relay_unsubscribe(self.ctx, pubsub_topic) + let mut ctx = self.ctx.lock().unwrap(); + relay::waku_relay_unsubscribe(ctx.obj_ptr, pubsub_topic) } pub fn set_event_callback(&self, f: F) { - events::waku_set_event_callback(self.ctx, f) + let mut ctx = self.ctx.lock().unwrap(); + *self.callback.lock().unwrap() = Box::new(f); + events::waku_set_event_callback(ctx.obj_ptr, &self.callback) } } /// Spawn a new Waku node with the given configuration (default configuration if `None` provided) /// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_newchar-jsonconfig) pub fn waku_new(config: Option) -> Result { + let obj_ptr = management::waku_new(config)?; Ok(WakuNodeHandle { - ctx: management::waku_new(config)?, + ctx: Arc::new(Mutex::new(OpaqueWakunodePtr { obj_ptr })), + callback: Lazy::new(|| Mutex::new(Box::new(|_| {}))), }) } diff --git a/waku-bindings/src/utils.rs b/waku-bindings/src/utils.rs index bc2df6a..7a5bcde 100644 --- a/waku-bindings/src/utils.rs +++ b/waku-bindings/src/utils.rs @@ -18,8 +18,6 @@ unsafe extern "C" fn trampoline( ) where F: FnMut(&str), { - let user_data = &mut *(user_data as *mut F); - let response = if data.is_null() { "" } else { @@ -27,7 +25,10 @@ unsafe extern "C" fn trampoline( .expect("could not retrieve response") }; - user_data(response); + if !user_data.is_null() { + let user_data = &mut *(user_data as *mut F); + user_data(response); + } } pub fn get_trampoline(_closure: &F) -> WakuCallBack