Browse Source

libplugin: put method name into command struct.

We'll need this for the usage map coming in the next patch.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
fix-test_pay_direct-flake
Rusty Russell 6 years ago
parent
commit
c58a4b9b42
  1. 42
      plugins/libplugin.c

42
plugins/libplugin.c

@ -27,6 +27,7 @@ struct plugin_conn {
struct command { struct command {
u64 id; u64 id;
const char *methodname;
struct plugin_conn *rpc; struct plugin_conn *rpc;
}; };
@ -110,11 +111,10 @@ static int read_json(struct plugin_conn *conn)
static struct command *read_json_request(const tal_t *ctx, static struct command *read_json_request(const tal_t *ctx,
struct plugin_conn *conn, struct plugin_conn *conn,
struct plugin_conn *rpc, struct plugin_conn *rpc,
const jsmntok_t **method,
const jsmntok_t **params, const jsmntok_t **params,
int *reqlen) int *reqlen)
{ {
const jsmntok_t *toks, *id; const jsmntok_t *toks, *id, *method;
bool valid; bool valid;
struct command *cmd = tal(ctx, struct command); struct command *cmd = tal(ctx, struct command);
@ -128,7 +128,7 @@ static struct command *read_json_request(const tal_t *ctx,
plugin_err("Malformed JSON command '%*.s' is not an object", plugin_err("Malformed JSON command '%*.s' is not an object",
*reqlen, membuf_elems(&conn->mb)); *reqlen, membuf_elems(&conn->mb));
*method = json_get_member(membuf_elems(&conn->mb), toks, "method"); method = json_get_member(membuf_elems(&conn->mb), toks, "method");
*params = json_get_member(membuf_elems(&conn->mb), toks, "params"); *params = json_get_member(membuf_elems(&conn->mb), toks, "params");
/* FIXME: Notifications don't have id! */ /* FIXME: Notifications don't have id! */
id = json_get_member(membuf_elems(&conn->mb), toks, "id"); id = json_get_member(membuf_elems(&conn->mb), toks, "id");
@ -138,6 +138,7 @@ static struct command *read_json_request(const tal_t *ctx,
membuf_elems(&conn->mb) + id->start); membuf_elems(&conn->mb) + id->start);
/* Putting this in cmd avoids a global, or direct exposure to users */ /* Putting this in cmd avoids a global, or direct exposure to users */
cmd->rpc = rpc; cmd->rpc = rpc;
cmd->methodname = json_strdup(cmd, membuf_elems(&conn->mb), method);
return cmd; return cmd;
} }
@ -474,14 +475,12 @@ static void handle_new_command(const tal_t *ctx,
size_t num_commands) size_t num_commands)
{ {
struct command *cmd; struct command *cmd;
const jsmntok_t *method, *params; const jsmntok_t *params;
int reqlen; int reqlen;
cmd = read_json_request(ctx, request_conn, rpc_conn, cmd = read_json_request(ctx, request_conn, rpc_conn, &params, &reqlen);
&method, &params, &reqlen);
for (size_t i = 0; i < num_commands; i++) { for (size_t i = 0; i < num_commands; i++) {
if (json_tok_streq(membuf_elems(&request_conn->mb), method, if (streq(cmd->methodname, commands[i].name)) {
commands[i].name)) {
commands[i].handle(cmd, membuf_elems(&request_conn->mb), commands[i].handle(cmd, membuf_elems(&request_conn->mb),
params); params);
membuf_consume(&request_conn->mb, reqlen); membuf_consume(&request_conn->mb, reqlen);
@ -489,9 +488,7 @@ static void handle_new_command(const tal_t *ctx,
} }
} }
plugin_err("Unknown command '%.*s'", plugin_err("Unknown command '%s'", cmd->methodname);
method->end - method->start,
membuf_elems(&request_conn->mb) + method->start);
} }
void plugin_main(char *argv[], void plugin_main(char *argv[],
@ -502,7 +499,7 @@ void plugin_main(char *argv[],
struct plugin_conn request_conn, rpc_conn; struct plugin_conn request_conn, rpc_conn;
const tal_t *ctx = tal(NULL, char); const tal_t *ctx = tal(NULL, char);
struct command *cmd; struct command *cmd;
const jsmntok_t *method, *params; const jsmntok_t *params;
int reqlen; int reqlen;
struct pollfd fds[2]; struct pollfd fds[2];
@ -523,23 +520,18 @@ void plugin_main(char *argv[],
uintmap_init(&out_reqs); uintmap_init(&out_reqs);
cmd = read_json_request(tmpctx, &request_conn, NULL, cmd = read_json_request(tmpctx, &request_conn, NULL,
&method, &params, &reqlen); &params, &reqlen);
if (!json_tok_streq(membuf_elems(&request_conn.mb), method, if (!streq(cmd->methodname, "getmanifest"))
"getmanifest")) { plugin_err("Expected getmanifest not %s", cmd->methodname);
plugin_err("Expected getmanifest not '%.*s'",
method->end - method->start,
membuf_elems(&request_conn.mb) + method->start);
}
membuf_consume(&request_conn.mb, reqlen); membuf_consume(&request_conn.mb, reqlen);
handle_getmanifest(cmd, commands, num_commands); handle_getmanifest(cmd, commands, num_commands);
cmd = read_json_request(tmpctx, &request_conn, &rpc_conn, cmd = read_json_request(tmpctx, &request_conn, &rpc_conn,
&method, &params, &reqlen); &params, &reqlen);
if (!json_tok_streq(membuf_elems(&request_conn.mb), method, "init")) { if (!streq(cmd->methodname, "init"))
plugin_err("Expected init not '%.*s'", plugin_err("Expected init not %s", cmd->methodname);
method->end - method->start,
membuf_elems(&request_conn.mb) + method->start);
}
handle_init(cmd, membuf_elems(&request_conn.mb), handle_init(cmd, membuf_elems(&request_conn.mb),
params, init); params, init);
membuf_consume(&request_conn.mb, reqlen); membuf_consume(&request_conn.mb, reqlen);

Loading…
Cancel
Save