From 127811757f4d555dc5b291d5de3b294fc128c010 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 12 Oct 2020 16:03:50 +1030 Subject: [PATCH] libplugin: support for sending notifications. Signed-off-by: Rusty Russell Changelog-Added: libplugin: routines to send notification updates and progress. --- plugins/Makefile | 1 + plugins/libplugin.c | 59 +++++++++++++++++++++++++++++++++++++++++++++ plugins/libplugin.h | 15 ++++++++++++ 3 files changed, 75 insertions(+) diff --git a/plugins/Makefile b/plugins/Makefile index 46606d1ba..fd7b05c13 100644 --- a/plugins/Makefile +++ b/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 \ diff --git a/plugins/libplugin.c b/plugins/libplugin.c index 49bdb3edf..ec4d1b5de 100644 --- a/plugins/libplugin.c +++ b/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; diff --git a/plugins/libplugin.h b/plugins/libplugin.h index cada4de4b..9ecb68065 100644 --- a/plugins/libplugin.h +++ b/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), \