Skip to content

Commit

Permalink
libplugin: support for sending notifications.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <[email protected]>
Changelog-Added: libplugin: routines to send notification updates and progress.
  • Loading branch information
rustyrussell committed Oct 23, 2020
1 parent 9f687d6 commit 1278117
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions plugins/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
59 changes: 59 additions & 0 deletions plugins/libplugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions plugins/libplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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), \
Expand Down

0 comments on commit 1278117

Please sign in to comment.