Skip to content

Commit 4524888

Browse files
committed
libplugin: add callback for command notifications.
Signed-off-by: Rusty Russell <[email protected]>
1 parent fff7dca commit 4524888

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

plugins/libplugin.c

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,27 @@ static void destroy_out_req(struct out_req *out_req, struct plugin *plugin)
392392
strmap_del(&plugin->out_reqs, out_req->id, NULL);
393393
}
394394

395+
void forward_notified(struct command *cmd,
396+
const char *method,
397+
const char *buf,
398+
const jsmntok_t *params,
399+
void *arg)
400+
{
401+
const jsmntok_t *msgtok;
402+
struct json_stream *js;
403+
404+
/* FIXME: There are also progress indicators, how to forward them? */
405+
msgtok = json_get_member(buf, params, "message");
406+
if (!msgtok)
407+
return;
408+
409+
js = plugin_notify_start(cmd, "message");
410+
json_add_tok(js, "level", json_get_member(buf, params, "level"), buf);
411+
json_add_str_fmt(js, "message", "%s: %s",
412+
method, json_strdup(tmpctx, buf, msgtok));
413+
plugin_notify_end(cmd, js);
414+
}
415+
395416
/* FIXME: Move lightningd/jsonrpc to common/ ? */
396417

397418
struct out_req *
@@ -409,6 +430,11 @@ jsonrpc_request_start_(struct command *cmd,
409430
const char *buf,
410431
const jsmntok_t *result,
411432
void *arg),
433+
void (*notified)(struct command *command,
434+
const char *method,
435+
const char *buf,
436+
const jsmntok_t *params,
437+
void *arg),
412438
void *arg)
413439
{
414440
struct out_req *out;
@@ -420,6 +446,7 @@ jsonrpc_request_start_(struct command *cmd,
420446
out->cmd = cmd;
421447
out->cb = cb;
422448
out->errcb = errcb;
449+
out->notified = notified;
423450
out->arg = arg;
424451
strmap_add(&cmd->plugin->out_reqs, out->id, out);
425452
tal_add_destructor2(out, destroy_out_req, cmd->plugin);
@@ -1085,9 +1112,19 @@ static void handle_rpc_reply(struct plugin *plugin, const char *buf, const jsmnt
10851112
bool cmd_freed;
10861113

10871114
idtok = json_get_member(buf, toks, "id");
1088-
if (!idtok)
1089-
/* FIXME: Don't simply ignore notifications! */
1115+
if (!idtok) {
1116+
const jsmntok_t *paramstok;
1117+
/* It's a notification about a command! */
1118+
paramstok = json_get_member(buf, toks, "params");
1119+
idtok = json_get_member(buf, paramstok, "id");
1120+
out = strmap_getn(&plugin->out_reqs,
1121+
json_tok_full(buf, idtok),
1122+
json_tok_full_len(idtok));
1123+
if (!out || !out->notified)
1124+
return;
1125+
out->notified(out->cmd, out->method, buf, paramstok, out->arg);
10901126
return;
1127+
}
10911128

10921129
out = strmap_getn(&plugin->out_reqs,
10931130
json_tok_full(buf, idtok),
@@ -1654,6 +1691,13 @@ static struct command_result *handle_init(struct command *cmd,
16541691
struct command *aux_cmd = aux_command(cmd);
16551692

16561693
io_new_conn(p, p->rpc_conn->fd, rpc_conn_init, p);
1694+
1695+
/* Enable notifications on all commands */
1696+
req = jsonrpc_request_start(aux_cmd, "notifications",
1697+
ignore_and_complete, plugin_broken_cb, NULL);
1698+
json_add_bool(req->js, "enable", true);
1699+
send_outreq(req);
1700+
16571701
/* In case they intercept rpc_command, we can't do this sync. */
16581702
req = jsonrpc_request_start(aux_cmd, "listconfigs",
16591703
get_beglist, plugin_broken_cb, NULL);

plugins/libplugin.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ struct out_req {
5050
const char *buf,
5151
const jsmntok_t *error,
5252
void *arg);
53+
/* The callback when we a notification about this command. */
54+
void (*notified)(struct command *command,
55+
const char *method,
56+
const char *buf,
57+
const jsmntok_t *params,
58+
void *arg);
5359
void *arg;
5460
};
5561

@@ -135,6 +141,13 @@ struct command_result *plugin_broken_cb(struct command *cmd,
135141
const jsmntok_t *result,
136142
void *arg);
137143

144+
/* Helper to forward notifications */
145+
void forward_notified(struct command *cmd,
146+
const char *method,
147+
const char *buf,
148+
const jsmntok_t *params,
149+
void *arg);
150+
138151
/* Helper to create a JSONRPC2 request stream. Send it with `send_outreq`. */
139152
struct out_req *jsonrpc_request_start_(struct command *cmd,
140153
const char *method,
@@ -150,6 +163,11 @@ struct out_req *jsonrpc_request_start_(struct command *cmd,
150163
const char *buf,
151164
const jsmntok_t *result,
152165
void *arg),
166+
void (*notified)(struct command *command,
167+
const char *method,
168+
const char *buf,
169+
const jsmntok_t *params,
170+
void *arg),
153171
void *arg)
154172
NON_NULL_ARGS(1, 2, 5);
155173

@@ -167,6 +185,7 @@ struct out_req *jsonrpc_request_start_(struct command *cmd,
167185
const char *mthod, \
168186
const char *buf, \
169187
const jsmntok_t *result), \
188+
NULL, \
170189
(arg))
171190

172191
#define jsonrpc_request_with_filter_start(cmd, method, filter, cb, errcb, arg) \
@@ -183,6 +202,7 @@ struct out_req *jsonrpc_request_start_(struct command *cmd,
183202
const char *mthod, \
184203
const char *buf, \
185204
const jsmntok_t *result), \
205+
NULL, \
186206
(arg))
187207

188208
/* This variant has callbacks received whole obj, not "result" or
@@ -196,8 +216,32 @@ struct out_req *jsonrpc_request_start_(struct command *cmd,
196216
const char *buf, \
197217
const jsmntok_t *result), \
198218
NULL, \
219+
NULL, \
199220
(arg))
200221

222+
/* This one handles notifications as well */
223+
#define jsonrpc_request_notified_start(cmd, method, cb, errcb, notcb, arg) \
224+
jsonrpc_request_start_((cmd), (method), NULL, NULL, \
225+
typesafe_cb_preargs(struct command_result *, void *, \
226+
(cb), (arg), \
227+
struct command *command, \
228+
const char *mthod, \
229+
const char *buf, \
230+
const jsmntok_t *result), \
231+
typesafe_cb_preargs(struct command_result *, void *, \
232+
(errcb), (arg), \
233+
struct command *command, \
234+
const char *mthod, \
235+
const char *buf, \
236+
const jsmntok_t *result), \
237+
typesafe_cb_preargs(void, void *, \
238+
(notcb), (arg), \
239+
struct command *command, \
240+
const char *mthod, \
241+
const char *buf, \
242+
const jsmntok_t *parms), \
243+
(arg))
244+
201245
/* Batch of requests: cb and errcb are optional, finalcb is called when all complete. */
202246
struct request_batch *request_batch_new_(const tal_t *ctx,
203247
struct command_result *(*cb)(struct command *,

0 commit comments

Comments
 (0)