Browse Source

libplugin: support for sending notifications.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Added: libplugin: routines to send notification updates and progress.
travis-experimental
Rusty Russell 4 years ago
parent
commit
127811757f
  1. 1
      plugins/Makefile
  2. 59
      plugins/libplugin.c
  3. 15
      plugins/libplugin.h

1
plugins/Makefile

@ -92,6 +92,7 @@ PLUGIN_COMMON_OBJS := \
common/pseudorand.o \
common/random_select.o \
common/setup.o \
common/status_levels.o \
common/type_to_string.o \
common/utils.o \
common/version.o \

59
plugins/libplugin.c

@ -985,6 +985,65 @@ static void plugin_logv(struct plugin *p, enum log_level l,
jsonrpc_finish_and_send(p, js);
}
struct json_stream *plugin_notify_start(struct command *cmd, const char *method)
{
struct json_stream *js = new_json_stream(cmd, NULL, NULL);
json_object_start(js, NULL);
json_add_string(js, "jsonrpc", "2.0");
json_add_string(js, "method", method);
json_object_start(js, "params");
json_add_u64(js, "id", *cmd->id);
return js;
}
void plugin_notify_end(struct command *cmd, struct json_stream *js)
{
json_object_end(js);
jsonrpc_finish_and_send(cmd->plugin, js);
}
/* Convenience wrapper for notify with "message" */
void plugin_notify_message(struct command *cmd,
enum log_level level,
const char *fmt, ...)
{
va_list ap;
struct json_stream *js = plugin_notify_start(cmd, "message");
va_start(ap, fmt);
json_add_string(js, "level", log_level_name(level));
/* In case we're OOM */
if (js->jout)
json_out_addv(js->jout, "message", true, fmt, ap);
va_end(ap);
plugin_notify_end(cmd, js);
}
void plugin_notify_progress(struct command *cmd,
u32 num_stages, u32 stage,
u32 num_progress, u32 progress)
{
struct json_stream *js = plugin_notify_start(cmd, "progress");
assert(progress < num_progress);
json_add_u32(js, "num", progress);
json_add_u32(js, "total", num_progress);
if (num_stages > 0) {
assert(stage < num_stages);
json_object_start(js, "stage");
json_add_u32(js, "num", stage);
json_add_u32(js, "total", num_stages);
json_object_end(js);
}
plugin_notify_end(cmd, js);
}
void NORETURN plugin_err(struct plugin *p, const char *fmt, ...)
{
va_list ap;

15
plugins/libplugin.h

@ -232,6 +232,21 @@ struct plugin_timer *plugin_timer_(struct plugin *p,
/* Log something */
void plugin_log(struct plugin *p, enum log_level l, const char *fmt, ...) PRINTF_FMT(3, 4);
/* Notify the caller of something. */
struct json_stream *plugin_notify_start(struct command *cmd, const char *method);
void plugin_notify_end(struct command *cmd, struct json_stream *js);
/* Convenience wrapper for notify "message" */
void plugin_notify_message(struct command *cmd,
enum log_level level,
const char *fmt, ...)
PRINTF_FMT(3, 4);
/* Convenience wrapper for progress: num_stages is normally 0. */
void plugin_notify_progress(struct command *cmd,
u32 num_stages, u32 stage,
u32 num_progress, u32 progress);
/* Macro to define arguments */
#define plugin_option_(name, type, description, set, arg, deprecated) \
(name), \

Loading…
Cancel
Save