Browse Source

libplugin: generalize the plugin_timer callback type

We don't take the callback result into account, so it can better be void.
Having a general callback parameter is handy, because for bcli we want
to pass it the struct bcli.
travis-debug
darosior 5 years ago
committed by Rusty Russell
parent
commit
3eb0f56f87
  1. 14
      plugins/autoclean.c
  2. 9
      plugins/libplugin.c
  3. 3
      plugins/libplugin.h

14
plugins/autoclean.c

@ -9,7 +9,7 @@
static u64 cycle_seconds = 0, expired_by = 86400; static u64 cycle_seconds = 0, expired_by = 86400;
static struct plugin_timer *cleantimer; static struct plugin_timer *cleantimer;
static struct command_result *do_clean(struct plugin *p); static void do_clean(void *cb_arg);
static struct command_result *ignore(struct command *timer, static struct command_result *ignore(struct command *timer,
const char *buf, const char *buf,
@ -17,19 +17,20 @@ static struct command_result *ignore(struct command *timer,
void *arg) void *arg)
{ {
struct plugin *p = arg; struct plugin *p = arg;
cleantimer = plugin_timer(p, time_from_sec(cycle_seconds), do_clean); cleantimer = plugin_timer(p, time_from_sec(cycle_seconds), do_clean, p);
return timer_complete(p); return timer_complete(p);
} }
static struct command_result *do_clean(struct plugin *p) static void do_clean(void *cb_arg)
{ {
struct plugin *p = cb_arg;
/* FIXME: delexpiredinvoice should be in our plugin too! */ /* FIXME: delexpiredinvoice should be in our plugin too! */
struct out_req *req = jsonrpc_request_start(p, NULL, "delexpiredinvoice", struct out_req *req = jsonrpc_request_start(p, NULL, "delexpiredinvoice",
ignore, ignore, p); ignore, ignore, p);
json_add_u64(req->js, "maxexpirytime", json_add_u64(req->js, "maxexpirytime",
time_now().ts.tv_sec - expired_by); time_now().ts.tv_sec - expired_by);
return send_outreq(p, req); send_outreq(p, req);
} }
static struct command_result *json_autocleaninvoice(struct command *cmd, static struct command_result *json_autocleaninvoice(struct command *cmd,
@ -53,7 +54,8 @@ static struct command_result *json_autocleaninvoice(struct command *cmd,
return command_success_str(cmd, "Autoclean timer disabled"); return command_success_str(cmd, "Autoclean timer disabled");
} }
tal_free(cleantimer); tal_free(cleantimer);
cleantimer = plugin_timer(cmd->plugin, time_from_sec(cycle_seconds), do_clean); cleantimer = plugin_timer(cmd->plugin, time_from_sec(cycle_seconds),
do_clean, cmd->plugin);
return command_success_str(cmd, return command_success_str(cmd,
tal_fmt(cmd, "Autocleaning %"PRIu64 tal_fmt(cmd, "Autocleaning %"PRIu64
@ -68,7 +70,7 @@ static void init(struct plugin *p,
if (cycle_seconds) { if (cycle_seconds) {
plugin_log(p, LOG_INFORM, "autocleaning every %"PRIu64" seconds", cycle_seconds); plugin_log(p, LOG_INFORM, "autocleaning every %"PRIu64" seconds", cycle_seconds);
cleantimer = plugin_timer(p, time_from_sec(cycle_seconds), cleantimer = plugin_timer(p, time_from_sec(cycle_seconds),
do_clean); do_clean, p);
} else } else
plugin_log(p, LOG_DBG, "autocleaning not active"); plugin_log(p, LOG_DBG, "autocleaning not active");
} }

9
plugins/libplugin.c

@ -25,7 +25,8 @@ bool deprecated_apis;
struct plugin_timer { struct plugin_timer {
struct timer timer; struct timer timer;
struct command_result *(*cb)(struct plugin *p); void (*cb)(void *cb_arg);
void *cb_arg;
}; };
struct rpc_conn { struct rpc_conn {
@ -820,7 +821,7 @@ static void call_plugin_timer(struct plugin *p, struct timer *timer)
p->in_timer++; p->in_timer++;
/* Free this if they don't. */ /* Free this if they don't. */
tal_steal(tmpctx, t); tal_steal(tmpctx, t);
t->cb(p); t->cb(t->cb_arg);
} }
static void destroy_plugin_timer(struct plugin_timer *timer, struct plugin *p) static void destroy_plugin_timer(struct plugin_timer *timer, struct plugin *p)
@ -829,10 +830,12 @@ static void destroy_plugin_timer(struct plugin_timer *timer, struct plugin *p)
} }
struct plugin_timer *plugin_timer(struct plugin *p, struct timerel t, struct plugin_timer *plugin_timer(struct plugin *p, struct timerel t,
struct command_result *(*cb)(struct plugin *p)) void (*cb)(void *cb_arg),
void *cb_arg)
{ {
struct plugin_timer *timer = tal(NULL, struct plugin_timer); struct plugin_timer *timer = tal(NULL, struct plugin_timer);
timer->cb = cb; timer->cb = cb;
timer->cb_arg = cb_arg;
timer_init(&timer->timer); timer_init(&timer->timer);
timer_addrel(&p->timers, &timer->timer, t); timer_addrel(&p->timers, &timer->timer, t);
tal_add_destructor2(timer, destroy_plugin_timer, p); tal_add_destructor2(timer, destroy_plugin_timer, p);

3
plugins/libplugin.h

@ -212,7 +212,8 @@ struct command_result *timer_complete(struct plugin *p);
*/ */
struct plugin_timer *plugin_timer(struct plugin *p, struct plugin_timer *plugin_timer(struct plugin *p,
struct timerel t, struct timerel t,
struct command_result *(*cb)(struct plugin *p)); void (*cb)(void *cb_arg),
void *cb_arg);
/* Log something */ /* Log something */
void plugin_log(struct plugin *p, enum log_level l, const char *fmt, ...) PRINTF_FMT(3, 4); void plugin_log(struct plugin *p, enum log_level l, const char *fmt, ...) PRINTF_FMT(3, 4);

Loading…
Cancel
Save