|
| 1 | +#include "config.h" |
| 2 | +#include <plugins/libplugin.h> |
| 3 | +#include <plugins/renepay/utils.h> |
| 4 | + |
| 5 | +struct rpcbatch { |
| 6 | + size_t num_remaining; |
| 7 | + struct command *cmd; |
| 8 | + struct command_result *(*finalcb)(struct command *, void *); |
| 9 | + void *arg; |
| 10 | +}; |
| 11 | + |
| 12 | +struct rpcbatch_aux { |
| 13 | + struct rpcbatch *batch; |
| 14 | + void *arg; |
| 15 | + struct command_result *(*cb)(struct command *cmd, const char *, |
| 16 | + const char *, const jsmntok_t *, void *); |
| 17 | + struct command_result *(*errcb)(struct command *cmd, const char *, |
| 18 | + const char *, const jsmntok_t *, |
| 19 | + void *); |
| 20 | +}; |
| 21 | + |
| 22 | +struct rpcbatch * |
| 23 | +rpcbatch_new_(struct command *cmd, |
| 24 | + struct command_result *(*finalcb)(struct command *, void *), |
| 25 | + void *arg) |
| 26 | +{ |
| 27 | + struct rpcbatch *batch = tal(cmd, struct rpcbatch); |
| 28 | + batch->num_remaining = 0; |
| 29 | + batch->cmd = cmd; |
| 30 | + batch->finalcb = finalcb; |
| 31 | + batch->arg = arg; |
| 32 | + return batch; |
| 33 | +} |
| 34 | + |
| 35 | +static struct command_result *batch_one_complete(struct rpcbatch *batch) |
| 36 | +{ |
| 37 | + assert(batch->num_remaining); |
| 38 | + if (--batch->num_remaining != 0) |
| 39 | + return command_still_pending(batch->cmd); |
| 40 | + struct command *cmd = batch->cmd; |
| 41 | + void *arg = batch->arg; |
| 42 | + struct command_result *(*finalcb)(struct command *, void *) = |
| 43 | + batch->finalcb; |
| 44 | + tal_free(batch); |
| 45 | + return finalcb(cmd, arg); |
| 46 | +} |
| 47 | + |
| 48 | +static struct command_result * |
| 49 | +batch_one_success(struct command *cmd, const char *method, const char *buf, |
| 50 | + const jsmntok_t *result, struct rpcbatch_aux *aux) |
| 51 | +{ |
| 52 | + /* Little hack to get the value of "complete" from libplugin. */ |
| 53 | + struct command_result *complete = command_param_failed(); |
| 54 | + /* If this frees stuff (e.g. fails), just return */ |
| 55 | + if (aux->cb && aux->cb(cmd, method, buf, result, aux->arg) == complete) |
| 56 | + return complete; |
| 57 | + struct rpcbatch *batch = aux->batch; |
| 58 | + tal_free(aux); |
| 59 | + return batch_one_complete(batch); |
| 60 | +} |
| 61 | + |
| 62 | +static struct command_result * |
| 63 | +batch_one_failed(struct command *cmd, const char *method, const char *buf, |
| 64 | + const jsmntok_t *result, struct rpcbatch_aux *aux) |
| 65 | +{ |
| 66 | + /* Little hack to get the value of "complete" from libplugin. */ |
| 67 | + struct command_result *complete = command_param_failed(); |
| 68 | + /* If this frees stuff (e.g. fails), just return */ |
| 69 | + if (aux->errcb && |
| 70 | + aux->errcb(cmd, method, buf, result, aux->arg) == complete) |
| 71 | + return complete; |
| 72 | + struct rpcbatch *batch = aux->batch; |
| 73 | + tal_free(aux); |
| 74 | + return batch_one_complete(batch); |
| 75 | +} |
| 76 | + |
| 77 | +struct out_req *add_to_rpcbatch_( |
| 78 | + struct rpcbatch *batch, const char *cmdname, |
| 79 | + struct command_result *(*cb)(struct command *, const char *, const char *, |
| 80 | + const jsmntok_t *, void *arg), |
| 81 | + struct command_result *(*errcb)(struct command *, const char *, |
| 82 | + const char *, const jsmntok_t *, void *arg), |
| 83 | + void *arg) |
| 84 | +{ |
| 85 | + batch->num_remaining++; |
| 86 | + struct rpcbatch_aux *aux = tal(batch, struct rpcbatch_aux); |
| 87 | + aux->arg = arg; |
| 88 | + aux->batch = batch; |
| 89 | + aux->cb = cb; |
| 90 | + aux->errcb = errcb; |
| 91 | + return jsonrpc_request_start(batch->cmd, cmdname, batch_one_success, |
| 92 | + batch_one_failed, aux); |
| 93 | +} |
| 94 | + |
| 95 | +/* Runs finalcb immediately if batch is empty. */ |
| 96 | +struct command_result *rpcbatch_done(struct rpcbatch *batch) |
| 97 | +{ |
| 98 | + /* Same path as completion */ |
| 99 | + batch->num_remaining++; |
| 100 | + return batch_one_complete(batch); |
| 101 | +} |
0 commit comments