From 95b98cf0ee61f3be49b6c3d8f63d89a02ddbeb9b Mon Sep 17 00:00:00 2001 From: Erik De Smedt Date: Fri, 23 Feb 2024 09:29:03 +0100 Subject: [PATCH] cln_plugin: Example package subscribing to "*" Creates an example package that subscribes to all notifications and logs them. This is useful for testing the behavior of subscribing to "*". I've also edited the Makefile to ensure that `make` builds the example and that `make clean` removes the example --- plugins/Makefile | 6 +++- plugins/examples/cln-subscribe-wildcard.rs | 35 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 plugins/examples/cln-subscribe-wildcard.rs diff --git a/plugins/Makefile b/plugins/Makefile index 959d7eda7..1cf7452e5 100644 --- a/plugins/Makefile +++ b/plugins/Makefile @@ -254,10 +254,14 @@ PLUGIN_BASES := $(PLUGINS:plugins/%=%) $(PY_PLUGINS:plugins/%=%) plugins/list_of_builtin_plugins_gen.h: plugins/Makefile Makefile config.vars @$(call VERBOSE,GEN $@,echo "static const char *list_of_builtin_plugins[] = { $(PLUGIN_BASES:%=\"%\",) NULL };" > $@) +target/${RUST_PROFILE}/examples/cln-subscribe-wildcard: ${CLN_PLUGIN_SRC} plugins/examples/cln-subscribe-wildcard.rs + cargo build ${CARGO_OPTS} --example cln-subscribe-wildcard + CLN_PLUGIN_EXAMPLES := \ target/${RUST_PROFILE}/examples/cln-plugin-startup \ target/${RUST_PROFILE}/examples/cln-plugin-reentrant \ - target/${RUST_PROFILE}/examples/cln-rpc-getinfo + target/${RUST_PROFILE}/examples/cln-rpc-getinfo \ + target/${RUST_PROFILE}/examples/cln-subscribe-wildcard CLN_PLUGIN_SRC = $(shell find plugins/src -name "*.rs") diff --git a/plugins/examples/cln-subscribe-wildcard.rs b/plugins/examples/cln-subscribe-wildcard.rs new file mode 100644 index 000000000..4942f3533 --- /dev/null +++ b/plugins/examples/cln-subscribe-wildcard.rs @@ -0,0 +1,35 @@ +/// This plug-in subscribes to the wildcard-notifications +/// and creates a corresponding log-entry + +use anyhow::Result; +use cln_plugin::{Builder, Plugin}; + +#[tokio::main] +async fn main() -> Result<()> { + let state = (); + + let configured = Builder::new(tokio::io::stdin(), tokio::io::stdout()) + .subscribe("*", handle_wildcard_notification) + .start(state) + .await?; + + match configured { + Some(p) => p.join().await?, + None => return Ok(()) // cln was started with --help + }; + + Ok(()) +} + +async fn handle_wildcard_notification(_plugin: Plugin<()>, value : serde_json::Value) -> Result<()> { + let notification_type : String = value + .as_object() + .unwrap() + .keys() + .next() + .unwrap() + .into(); + + log::info!("Received notification {}", notification_type); + Ok(()) +}