From cd8439b1c95c0c4172ed5eb1e18a333ef974b01f Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Thu, 13 Dec 2018 13:23:21 +0100 Subject: [PATCH] plugin: Dispatch notifications to subscribed plugins Signed-off-by: Christian Decker --- lightningd/plugin.c | 25 +++++++++++++++++++++++++ lightningd/plugin.h | 3 +++ 2 files changed, 28 insertions(+) diff --git a/lightningd/plugin.c b/lightningd/plugin.c index b0399ca54..9299c8765 100644 --- a/lightningd/plugin.c +++ b/lightningd/plugin.c @@ -1054,3 +1054,28 @@ void json_add_opt_plugins(struct json_stream *response, json_add_string(response, "plugin", p->cmd); } } + +/** + * Determine whether a plugin is subscribed to a given topic/method. + */ +static bool plugin_subscriptions_contains(struct plugin *plugin, + const char *method) +{ + for (size_t i = 0; i < tal_count(plugin->subscriptions); i++) + if (streq(method, plugin->subscriptions[i])) + return true; + + return false; +} + +void plugins_notify(struct plugins *plugins, + const struct jsonrpc_notification *n TAKES) +{ + struct plugin *p; + list_for_each(&plugins->plugins, p, list) { + if (plugin_subscriptions_contains(p, n->method)) + plugin_send(p, json_stream_dup(p, n->stream)); + } + if (taken(n)) + tal_free(n); +} diff --git a/lightningd/plugin.h b/lightningd/plugin.h index 40d64caf4..f47e5e6b4 100644 --- a/lightningd/plugin.h +++ b/lightningd/plugin.h @@ -79,4 +79,7 @@ char *add_plugin_dir(struct plugins *plugins, const char *dir, */ void clear_plugins(struct plugins *plugins); +void plugins_notify(struct plugins *plugins, + const struct jsonrpc_notification *n TAKES); + #endif /* LIGHTNING_LIGHTNINGD_PLUGIN_H */