mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-03-03 18:57:06 +01:00
jsonrpc: Create a struct for notifications that we send
Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
parent
c57e63b31d
commit
9ad2f57e46
2 changed files with 41 additions and 0 deletions
|
@ -1004,6 +1004,26 @@ static struct command_result *param_command(struct command *cmd,
|
|||
tok->end - tok->start, buffer + tok->start);
|
||||
}
|
||||
|
||||
struct jsonrpc_notification *jsonrpc_notification_start(const tal_t *ctx, const char *method)
|
||||
{
|
||||
struct jsonrpc_notification *n = tal(ctx, struct jsonrpc_notification);
|
||||
n->method = tal_strdup(n, method);
|
||||
n->stream = new_json_stream(n, NULL);
|
||||
json_object_start(n->stream, NULL);
|
||||
json_add_string(n->stream, "jsonrpc", "2.0");
|
||||
json_add_string(n->stream, "method", method);
|
||||
json_object_start(n->stream, "params");
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
void jsonrpc_notification_end(struct jsonrpc_notification *n)
|
||||
{
|
||||
json_object_end(n->stream); /* closes '.params' */
|
||||
json_object_end(n->stream); /* closes '.' */
|
||||
json_stream_append(n->stream, "\n\n");
|
||||
}
|
||||
|
||||
/* We add this destructor as a canary to detect cmd failing. */
|
||||
static void destroy_command_canary(struct command *cmd, bool *failed)
|
||||
{
|
||||
|
|
|
@ -58,6 +58,14 @@ struct json_command {
|
|||
const char *verbose;
|
||||
};
|
||||
|
||||
struct jsonrpc_notification {
|
||||
/* The topic that this notification is for. Internally this
|
||||
* will be serialized as "method", hence the different name
|
||||
* here */
|
||||
const char *method;
|
||||
struct json_stream *stream;
|
||||
};
|
||||
|
||||
/**
|
||||
* json_stream_success - start streaming a successful json result.
|
||||
* @cmd: the command we're running.
|
||||
|
@ -162,5 +170,18 @@ bool jsonrpc_command_add(struct jsonrpc *rpc, struct json_command *command);
|
|||
*/
|
||||
void jsonrpc_command_remove(struct jsonrpc *rpc, const char *method);
|
||||
|
||||
/**
|
||||
* Begin a JSON-RPC notification with the specified topic.
|
||||
*
|
||||
* Automatically starts the `params` object, hence only key-value
|
||||
* based params are supported at the moment.
|
||||
*/
|
||||
struct jsonrpc_notification *jsonrpc_notification_start(const tal_t *ctx, const char *topic);
|
||||
|
||||
/**
|
||||
* Counterpart to jsonrpc_notification_start.
|
||||
*/
|
||||
void jsonrpc_notification_end(struct jsonrpc_notification *n);
|
||||
|
||||
AUTODATA_TYPE(json_command, struct json_command);
|
||||
#endif /* LIGHTNING_LIGHTNINGD_JSONRPC_H */
|
||||
|
|
Loading…
Add table
Reference in a new issue