Browse Source

plugin: Remove plugin_request_new and expose plugin_request_send

plugin_request_new did nothing special aside from registering the
request ID with the dispatch code. This duty has now been moved to
plugin_request_send instead, which is also exposed so we can use that
code in plugin_hook.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
plugin-timeout-inc
Christian Decker 6 years ago
committed by Rusty Russell
parent
commit
a2fa0788fc
  1. 54
      lightningd/plugin.c
  2. 6
      lightningd/plugin.h

54
lightningd/plugin.c

@ -176,34 +176,6 @@ static void PRINTF_FMT(2,3) plugin_kill(struct plugin *plugin, char *fmt, ...)
list_del(&plugin->list);
}
/**
* Create the header of a JSON-RPC request and return open stream.
*
* The caller needs to add the request to req->stream.
*/
static struct jsonrpc_request *
plugin_request_new_(struct plugin *plugin,
const char *method,
void (*cb)(const char *buffer,
const jsmntok_t *toks,
const jsmntok_t *idtok,
void *),
void *arg)
{
struct jsonrpc_request *req = jsonrpc_request_start(plugin, method, cb, arg);
/* Add to map so we can find it later when routing the response */
uintmap_add(&plugin->plugins->pending_requests, req->id, req);
return req;
}
#define plugin_request_new(plugin, method, response_cb, response_cb_arg) \
plugin_request_new_((plugin), (method), \
typesafe_cb_preargs(void, void *, (response_cb), \
(response_cb_arg), \
const char *buffer, \
const jsmntok_t *toks, \
const jsmntok_t *idtok), \
(response_cb_arg))
/**
* Send a JSON-RPC message (request or notification) to the plugin.
*/
@ -660,11 +632,11 @@ static struct command_result *plugin_rpcmethod_dispatch(struct command *cmd,
idtok = json_get_member(buffer, toks, "id");
assert(idtok != NULL);
req = plugin_request_new(plugin, NULL, plugin_rpcmethod_cb, cmd);
req = jsonrpc_request_start(plugin, NULL, plugin_rpcmethod_cb, cmd);
snprintf(id, ARRAY_SIZE(id), "%"PRIu64, req->id);
json_stream_forward_change_id(req->stream, buffer, toks, idtok, id);
plugin_send(plugin, req->stream);
plugin_request_send(plugin, req);
req->stream = NULL;
return command_still_pending(cmd);
@ -956,9 +928,9 @@ void plugins_init(struct plugins *plugins, const char *dev_plugin_debug)
* write-only on p->stdout */
io_new_conn(p, stdout, plugin_stdout_conn_init, p);
io_new_conn(p, stdin, plugin_stdin_conn_init, p);
req = plugin_request_new(p, "getmanifest", plugin_manifest_cb, p);
req = jsonrpc_request_start(p, "getmanifest", plugin_manifest_cb, p);
jsonrpc_request_end(req);
plugin_send(p, req->stream);
plugin_request_send(p, req);
plugins->pending_manifests++;
/* Don't timeout if they're running a debugger. */
@ -999,9 +971,7 @@ static void plugin_config(struct plugin *plugin)
const char *name;
struct jsonrpc_request *req;
struct lightningd *ld = plugin->plugins->ld;
/* No writer since we don't flush concurrently. */
req = plugin_request_new(plugin, "init", plugin_config_cb, plugin);
req = jsonrpc_request_start(plugin, "init", plugin_config_cb, plugin);
/* Add .params.options */
json_object_start(req->stream, "options");
@ -1019,7 +989,7 @@ static void plugin_config(struct plugin *plugin)
json_object_end(req->stream);
jsonrpc_request_end(req);
plugin_send(plugin, req->stream);
plugin_request_send(plugin, req);
}
void plugins_config(struct plugins *plugins)
@ -1063,3 +1033,15 @@ void plugins_notify(struct plugins *plugins,
if (taken(n))
tal_free(n);
}
void plugin_request_send(struct plugin *plugin,
struct jsonrpc_request *req TAKES)
{
/* Add to map so we can find it later when routing the response */
tal_steal(plugin, req);
uintmap_add(&plugin->plugins->pending_requests, req->id, req);
plugin_send(plugin, req->stream);
/* plugin_send steals the stream, so remove the dangling
* pointer here */
req->stream = NULL;
}

6
lightningd/plugin.h

@ -87,4 +87,10 @@ void clear_plugins(struct plugins *plugins);
void plugins_notify(struct plugins *plugins,
const struct jsonrpc_notification *n TAKES);
/**
* Send a jsonrpc_request to the specified plugin
*/
void plugin_request_send(struct plugin *plugin,
struct jsonrpc_request *req TAKES);
#endif /* LIGHTNING_LIGHTNINGD_PLUGIN_H */

Loading…
Cancel
Save