mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 23:24:27 +01:00
plugin: Add hooks that a plugin might register
This is the first use of the `hooks` autodata field, and it required a dummy element in order for the section not to be dropped, it'll be removed once we have actual hooks.
This commit is contained in:
committed by
Rusty Russell
parent
ff897f8788
commit
b8584a744b
@@ -19,6 +19,7 @@
|
|||||||
#include <lightningd/json.h>
|
#include <lightningd/json.h>
|
||||||
#include <lightningd/lightningd.h>
|
#include <lightningd/lightningd.h>
|
||||||
#include <lightningd/notification.h>
|
#include <lightningd/notification.h>
|
||||||
|
#include <lightningd/plugin_hook.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
@@ -787,6 +788,30 @@ static bool plugin_subscriptions_add(struct plugin *plugin, const char *buffer,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool plugin_hooks_add(struct plugin *plugin, const char *buffer,
|
||||||
|
const jsmntok_t *resulttok)
|
||||||
|
{
|
||||||
|
const jsmntok_t *hookstok = json_get_member(buffer, resulttok, "hooks");
|
||||||
|
if (!hookstok)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
for (int i = 0; i < hookstok->size; i++) {
|
||||||
|
char *name = json_strdup(NULL, plugin->buffer,
|
||||||
|
json_get_arr(hookstok, i));
|
||||||
|
if (!plugin_hook_register(plugin, name)) {
|
||||||
|
plugin_kill(plugin,
|
||||||
|
"could not register hook '%s', either the "
|
||||||
|
"name doesn't exist or another plugin "
|
||||||
|
"already registered it.",
|
||||||
|
name);
|
||||||
|
tal_free(name);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
tal_free(name);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
static void plugin_manifest_timeout(struct plugin *plugin)
|
static void plugin_manifest_timeout(struct plugin *plugin)
|
||||||
{
|
{
|
||||||
log_broken(plugin->log, "The plugin failed to respond to \"getmanifest\" in time, terminating.");
|
log_broken(plugin->log, "The plugin failed to respond to \"getmanifest\" in time, terminating.");
|
||||||
@@ -816,12 +841,13 @@ static void plugin_manifest_cb(const char *buffer,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!plugin_opts_add(plugin, buffer, resulttok)
|
if (!plugin_opts_add(plugin, buffer, resulttok) ||
|
||||||
|| !plugin_rpcmethods_add(plugin, buffer, resulttok)
|
!plugin_rpcmethods_add(plugin, buffer, resulttok) ||
|
||||||
|| !plugin_subscriptions_add(plugin, buffer, resulttok))
|
!plugin_subscriptions_add(plugin, buffer, resulttok) ||
|
||||||
|
!plugin_hooks_add(plugin, buffer, resulttok))
|
||||||
plugin_kill(
|
plugin_kill(
|
||||||
plugin,
|
plugin,
|
||||||
"Failed to register options, methods, or subscriptions.");
|
"Failed to register options, methods, hooks, or subscriptions.");
|
||||||
/* Reset timer, it'd kill us otherwise. */
|
/* Reset timer, it'd kill us otherwise. */
|
||||||
tal_free(plugin->timeout_timer);
|
tal_free(plugin->timeout_timer);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,35 @@
|
|||||||
#include <lightningd/plugin_hook.h>
|
#include <lightningd/plugin_hook.h>
|
||||||
|
|
||||||
|
static struct plugin_hook *plugin_hook_by_name(const char *name)
|
||||||
|
{
|
||||||
|
static struct plugin_hook **hooks = NULL;
|
||||||
|
static size_t num_hooks;
|
||||||
|
if (!hooks)
|
||||||
|
hooks = autodata_get(hooks, &num_hooks);
|
||||||
|
|
||||||
|
for (size_t i=0; i<num_hooks; i++)
|
||||||
|
if (streq(hooks[i]->name, name))
|
||||||
|
return hooks[i];
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool plugin_hook_register(struct plugin *plugin, const char *method)
|
||||||
|
{
|
||||||
|
struct plugin_hook *hook = plugin_hook_by_name(method);
|
||||||
|
if (!hook) {
|
||||||
|
/* No such hook name registered */
|
||||||
|
return false;
|
||||||
|
} else if (hook->plugin != NULL) {
|
||||||
|
/* Another plugin already registered for this name */
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
hook->plugin = plugin;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* FIXME(cdecker): Remove dummy hook, once we have a real one */
|
||||||
|
REGISTER_PLUGIN_HOOK(hello, NULL, void *, NULL, void *, NULL, void *);
|
||||||
|
|
||||||
void plugin_hook_call_(struct lightningd *ld, const struct plugin_hook *hook,
|
void plugin_hook_call_(struct lightningd *ld, const struct plugin_hook *hook,
|
||||||
void *payload, void *cb_arg)
|
void *payload, void *cb_arg)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ void plugin_hook_call_(struct lightningd *ld, const struct plugin_hook *hook,
|
|||||||
*/
|
*/
|
||||||
/* FIXME: Find a way to avoid back-to-back declaration and definition */
|
/* FIXME: Find a way to avoid back-to-back declaration and definition */
|
||||||
#define PLUGIN_HOOK_CALL_DEF(name, payload_type, response_cb_arg_type) \
|
#define PLUGIN_HOOK_CALL_DEF(name, payload_type, response_cb_arg_type) \
|
||||||
static inline void plugin_hook_call_##name( \
|
UNNEEDED static inline void plugin_hook_call_##name( \
|
||||||
struct lightningd *ld, payload_type payload, \
|
struct lightningd *ld, payload_type payload, \
|
||||||
response_cb_arg_type cb_arg) \
|
response_cb_arg_type cb_arg) \
|
||||||
{ \
|
{ \
|
||||||
|
|||||||
Reference in New Issue
Block a user