Browse Source

daemon: dev-output command.

Useful for controlling conversations between two nodes, by
blocking one's output.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 9 years ago
parent
commit
b9d4f7c0ab
  1. 17
      daemon/json.c
  2. 3
      daemon/json.h
  3. 1
      daemon/jsonrpc.c
  4. 1
      daemon/jsonrpc.h
  5. 5
      daemon/lightning-cli.c
  6. 51
      daemon/peer.c
  7. 1
      daemon/peer.h

17
daemon/json.c

@ -114,6 +114,23 @@ bool json_tok_is_null(const char *buffer, const jsmntok_t *tok)
return buffer[tok->start] == 'n';
}
bool json_tok_bool(const char *buffer, const jsmntok_t *tok, bool *b)
{
if (tok->type != JSMN_PRIMITIVE)
return false;
if (tok->end - tok->start == strlen("true")
&& memcmp(buffer + tok->start, "true", strlen("true")) == 0) {
*b = true;
return true;
}
if (tok->end - tok->start == strlen("false")
&& memcmp(buffer + tok->start, "false", strlen("false")) == 0) {
*b = false;
return true;
}
return false;
}
const jsmntok_t *json_next(const jsmntok_t *tok)
{
const jsmntok_t *t;

3
daemon/json.h

@ -32,6 +32,9 @@ bool json_tok_u64(const char *buffer, const jsmntok_t *tok,
bool json_tok_bitcoin_amount(const char *buffer, const jsmntok_t *tok,
uint64_t *satoshi);
/* Extract boolean this (must be a true or false) */
bool json_tok_bool(const char *buffer, const jsmntok_t *tok, bool *b);
/* Is this the null primitive? */
bool json_tok_is_null(const char *buffer, const jsmntok_t *tok);

1
daemon/jsonrpc.c

@ -262,6 +262,7 @@ static const struct json_command *cmdlist[] = {
&crash_command,
&disconnect_command,
&signcommit_command,
&output_command,
};
static void json_help(struct command *cmd,

1
daemon/jsonrpc.h

@ -67,4 +67,5 @@ extern const struct json_command close_command;
extern const struct json_command newaddr_command;
extern const struct json_command disconnect_command;
extern const struct json_command signcommit_command;
extern const struct json_command output_command;
#endif /* LIGHTNING_DAEMON_JSONRPC_H */

5
daemon/lightning-cli.c

@ -96,8 +96,11 @@ int main(int argc, char *argv[])
method, idstr);
for (i = 2; i < argc; i++) {
/* Numbers are left unquoted, and quoted things left alone. */
/* Numbers and bools are left unquoted,
* and quoted things left alone. */
if (strspn(argv[i], "0123456789") == strlen(argv[i])
|| streq(argv[i], "true")
|| streq(argv[i], "false")
|| argv[i][0] == '"')
tal_append_fmt(&cmd, "%s", argv[i]);
else

51
daemon/peer.c

@ -675,7 +675,7 @@ static struct io_plan *pkt_out(struct io_conn *conn, struct peer *peer)
Pkt *out;
size_t n = tal_count(peer->outpkt);
if (peer->fake_close)
if (peer->fake_close || !peer->output_enabled)
return io_out_wait(conn, peer, pkt_out, peer);
if (n == 0) {
@ -861,6 +861,7 @@ static struct peer *new_peer(struct lightningd_state *dstate,
* immediately so don't make peer a parent. */
peer->conn = conn;
peer->fake_close = false;
peer->output_enabled = true;
io_set_finish(conn, peer_disconnect, peer);
peer->local.offer_anchor = offer_anchor;
@ -2911,6 +2912,54 @@ static void json_signcommit(struct command *cmd,
command_success(cmd, response);
}
static void json_output(struct command *cmd,
const char *buffer, const jsmntok_t *params)
{
struct peer *peer;
jsmntok_t *peeridtok, *enabletok;
bool enable;
if (!json_get_params(buffer, params,
"peerid", &peeridtok,
"enable", &enabletok,
NULL)) {
command_fail(cmd, "Need peerid and enable");
return;
}
peer = find_peer(cmd->dstate, buffer, peeridtok);
if (!peer) {
command_fail(cmd, "Could not find peer with that peerid");
return;
}
if (!peer->conn) {
command_fail(cmd, "Peer is already disconnected");
return;
}
if (!json_tok_bool(buffer, enabletok, &enable)) {
command_fail(cmd, "enable must be true or false");
return;
}
log_debug(peer->log, "dev-output: output %s",
enable ? "enabled" : "disabled");
peer->output_enabled = enable;
/* Flush any outstanding output */
if (peer->output_enabled)
io_wake(peer);
command_success(cmd, null_response(cmd));
}
const struct json_command output_command = {
"dev-output",
json_output,
"Enable/disable any messages to peer {peerid} depending on {enable}",
"Returns an empty result on success"
};
const struct json_command disconnect_command = {
"dev-disconnect",
json_disconnect,

1
daemon/peer.h

@ -223,6 +223,7 @@ struct peer {
/* For testing. */
bool fake_close;
bool output_enabled;
/* Stuff we have in common. */
struct peer_visible_state local, remote;

Loading…
Cancel
Save