Skip to content

Commit

Permalink
plugins/libplugin: hook support
Browse files Browse the repository at this point in the history
  • Loading branch information
darosior committed Dec 5, 2019
1 parent 7353a74 commit 6b7b3d4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion plugins/autoclean.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ int main(int argc, char *argv[])
{
setup_locale();
plugin_main(argv, init, PLUGIN_RESTARTABLE, commands, ARRAY_SIZE(commands),
NULL, 0,
NULL, 0, NULL, 0,
plugin_option("autocleaninvoice-cycle",
"string",
"Perform cleanup of expired invoices every"
Expand Down
3 changes: 2 additions & 1 deletion plugins/fundchannel.c
Original file line number Diff line number Diff line change
Expand Up @@ -512,5 +512,6 @@ static const struct plugin_command commands[] = { {
int main(int argc, char *argv[])
{
setup_locale();
plugin_main(argv, init, PLUGIN_RESTARTABLE, commands, ARRAY_SIZE(commands), NULL, 0, NULL);
plugin_main(argv, init, PLUGIN_RESTARTABLE, commands, ARRAY_SIZE(commands),
NULL, 0, NULL, 0, NULL);
}
31 changes: 26 additions & 5 deletions plugins/libplugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,8 @@ handle_getmanifest(struct command *getmanifest_cmd,
size_t num_commands,
const struct plugin_notification *notif_subs,
size_t num_notif_subs,
const struct plugin_hook *hook_subs,
size_t num_hook_subs,
const struct plugin_option *opts,
const enum plugin_restartability restartability)
{
Expand Down Expand Up @@ -523,6 +525,11 @@ handle_getmanifest(struct command *getmanifest_cmd,
json_out_addstr(params, NULL, notif_subs[i].name);
json_out_end(params, ']');

json_out_start(params, "hooks", '[');
for (size_t i = 0; i < num_hook_subs; i++)
json_out_addstr(params, NULL, hook_subs[i].name);
json_out_end(params, ']');

json_out_addstr(params, "dynamic", restartability == PLUGIN_RESTARTABLE ? "true" : "false");
json_out_end(params, '}');
json_out_finished(params);
Expand Down Expand Up @@ -625,7 +632,9 @@ static void handle_new_command(const tal_t *ctx,
const struct plugin_command *commands,
size_t num_commands,
const struct plugin_notification *notif_subs,
size_t num_notif_subs)
size_t num_notif_subs,
const struct plugin_hook *hook_subs,
size_t num_hook_subs)
{
struct command *cmd;
const jsmntok_t *params;
Expand All @@ -643,6 +652,14 @@ static void handle_new_command(const tal_t *ctx,
}
return;
}
for (size_t i = 0; i < num_hook_subs; i++) {
if (streq(cmd->methodname, hook_subs[i].name)) {
hook_subs[i].handle(cmd, membuf_elems(&request_conn->mb),
params);
membuf_consume(&request_conn->mb, reqlen);
return;
}
}
for (size_t i = 0; i < num_commands; i++) {
if (streq(cmd->methodname, commands[i].name)) {
commands[i].handle(cmd, membuf_elems(&request_conn->mb),
Expand Down Expand Up @@ -748,6 +765,8 @@ void plugin_main(char *argv[],
size_t num_commands,
const struct plugin_notification *notif_subs,
size_t num_notif_subs,
const struct plugin_hook *hook_subs,
size_t num_hook_subs,
...)
{
struct plugin_conn request_conn;
Expand Down Expand Up @@ -779,7 +798,7 @@ void plugin_main(char *argv[],
membuf_tal_realloc);
uintmap_init(&out_reqs);

va_start(ap, num_notif_subs);
va_start(ap, num_hook_subs);
while ((optname = va_arg(ap, const char *)) != NULL) {
struct plugin_option o;
o.name = optname;
Expand All @@ -798,7 +817,7 @@ void plugin_main(char *argv[],

membuf_consume(&request_conn.mb, reqlen);
handle_getmanifest(cmd, commands, num_commands, notif_subs, num_notif_subs,
opts, restartability);
hook_subs, num_hook_subs, opts, restartability);

cmd = read_json_request(tmpctx, &request_conn, &rpc_conn,
&params, &reqlen);
Expand All @@ -825,7 +844,8 @@ void plugin_main(char *argv[],
/* If we already have some input, process now. */
if (membuf_num_elems(&request_conn.mb) != 0) {
handle_new_command(ctx, &request_conn, &rpc_conn,
commands, num_commands, notif_subs, num_notif_subs);
commands, num_commands, notif_subs, num_notif_subs,
hook_subs, num_hook_subs);
continue;
}
if (membuf_num_elems(&rpc_conn.mb) != 0) {
Expand All @@ -852,7 +872,8 @@ void plugin_main(char *argv[],

if (fds[0].revents & POLLIN)
handle_new_command(ctx, &request_conn, &rpc_conn,
commands, num_commands, notif_subs, num_notif_subs);
commands, num_commands, notif_subs, num_notif_subs,
hook_subs, num_hook_subs);
if (fds[1].revents & POLLIN)
handle_rpc_reply(&rpc_conn);
}
Expand Down
10 changes: 10 additions & 0 deletions plugins/libplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ struct plugin_notification {
const jsmntok_t *params);
};

/* Create an array of these, one for each hook you subscribe to. */
struct plugin_hook {
const char *name;
struct command_result (*handle)(struct command *cmd,
const char *buf,
const jsmntok_t *params);
};

/* Helper to create a zero or single-value JSON object; if @str is NULL,
* object is empty. */
struct json_out *json_out_obj(const tal_t *ctx,
Expand Down Expand Up @@ -173,5 +181,7 @@ void NORETURN LAST_ARG_NULL plugin_main(char *argv[],
size_t num_commands,
const struct plugin_notification *notif_subs,
size_t num_notif_subs,
const struct plugin_hook *hook_subs,
size_t num_hook_subs,
...);
#endif /* LIGHTNING_PLUGINS_LIBPLUGIN_H */
3 changes: 2 additions & 1 deletion plugins/pay.c
Original file line number Diff line number Diff line change
Expand Up @@ -1395,5 +1395,6 @@ static const struct plugin_command commands[] = { {
int main(int argc, char *argv[])
{
setup_locale();
plugin_main(argv, init, PLUGIN_RESTARTABLE, commands, ARRAY_SIZE(commands), NULL, 0, NULL);
plugin_main(argv, init, PLUGIN_RESTARTABLE, commands, ARRAY_SIZE(commands),
NULL, 0, NULL, 0, NULL);
}

0 comments on commit 6b7b3d4

Please sign in to comment.