plugin: Call next plugin that registered hook if result is continue

This commit is contained in:
Christian Decker 2020-02-05 15:21:48 +01:00 committed by Rusty Russell
parent 71e67ba47f
commit a3ab3d2990

View File

@ -87,6 +87,9 @@ void plugin_hook_unregister_all(struct plugin *plugin)
plugin_hook_unregister(plugin, hooks[i]->name); plugin_hook_unregister(plugin, hooks[i]->name);
} }
/* Mutual recursion */
static void plugin_hook_call_next(struct plugin_hook_request *ph_req);
/** /**
* Callback to be passed to the jsonrpc_request. * Callback to be passed to the jsonrpc_request.
* *
@ -97,15 +100,26 @@ static void plugin_hook_callback(const char *buffer, const jsmntok_t *toks,
const jsmntok_t *idtok, const jsmntok_t *idtok,
struct plugin_hook_request *r) struct plugin_hook_request *r)
{ {
const jsmntok_t *resulttok = json_get_member(buffer, toks, "result"); const jsmntok_t *resulttok, *resrestok;
struct db *db = r->db; struct db *db = r->db;
struct plugin_destroyed *pd; struct plugin_destroyed *pd;
bool more_plugins = r->current_plugin + 1 < tal_count(r->hook->plugins);
resulttok = json_get_member(buffer, toks, "result");
if (!resulttok) if (!resulttok)
fatal("Plugin for %s returned non-result response %.*s", fatal("Plugin for %s returned non-result response %.*s",
r->hook->name, r->hook->name, toks->end - toks->start,
toks->end - toks->start, buffer + toks->start); buffer + toks->start);
resrestok = json_get_member(buffer, resulttok, "result");
/* If this is a hook response containing a `continue` and we have more
* plugins queue the next call. */
if (resrestok && json_tok_streq(buffer, resrestok, "continue") &&
more_plugins) {
plugin_hook_call_next(r);
} else {
/* If command is "plugin stop", this can free r! */ /* If command is "plugin stop", this can free r! */
pd = plugin_detect_destruction(r->plugin); pd = plugin_detect_destruction(r->plugin);
db_begin_transaction(db); db_begin_transaction(db);
@ -114,6 +128,7 @@ static void plugin_hook_callback(const char *buffer, const jsmntok_t *toks,
if (!was_plugin_destroyed(pd)) if (!was_plugin_destroyed(pd))
tal_free(r); tal_free(r);
} }
}
static void plugin_hook_call_next(struct plugin_hook_request *ph_req) static void plugin_hook_call_next(struct plugin_hook_request *ph_req)
{ {