Skip to content

Commit

Permalink
test.c hooks and notifications with libplugin.c
Browse files Browse the repository at this point in the history
Signed-off-by: Saibato <saibato.naga@pm.me>
  • Loading branch information
Saibato committed Dec 6, 2019
1 parent d3f266e commit 83a66e2
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ PKGLIBEXEC_PROGRAMS = \
lightningd/lightning_hsmd \
lightningd/lightning_onchaind \
lightningd/lightning_openingd
PLUGINS=plugins/pay plugins/autoclean plugins/fundchannel
PLUGINS=plugins/pay plugins/autoclean plugins/fundchannel plugins/test

install-program: installdirs $(BIN_PROGRAMS) $(PKGLIBEXEC_PROGRAMS) $(PLUGINS)
@$(NORMAL_INSTALL)
Expand Down
14 changes: 10 additions & 4 deletions plugins/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ PLUGIN_AUTOCLEAN_OBJS := $(PLUGIN_AUTOCLEAN_SRC:.c=.o)
PLUGIN_FUNDCHANNEL_SRC := plugins/fundchannel.c
PLUGIN_FUNDCHANNEL_OBJS := $(PLUGIN_FUNDCHANNEL_SRC:.c=.o)

PLUGIN_TEST_SRC := plugins/test.c
PLUGIN_TEST_OBJS := $(PLUGIN_TEST_SRC:.c=.o)


PLUGIN_LIB_SRC := plugins/libplugin.c
PLUGIN_LIB_HEADER := plugins/libplugin.h
PLUGIN_LIB_OBJS := $(PLUGIN_LIB_SRC:.c=.o)
Expand Down Expand Up @@ -48,11 +52,13 @@ plugins/autoclean: bitcoin/chainparams.o $(PLUGIN_AUTOCLEAN_OBJS) $(PLUGIN_LIB_O

plugins/fundchannel: common/addr.o $(PLUGIN_FUNDCHANNEL_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_COMMON_OBJS) $(JSMN_OBJS) $(CCAN_OBJS)

$(PLUGIN_PAY_OBJS) $(PLUGIN_AUTOCLEAN_OBJS) $(PLUGIN_FUNDCHANNEL_OBJS) $(PLUGIN_LIB_OBJS): $(PLUGIN_LIB_HEADER)
plugins/test: $(PLUGIN_TEST_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_COMMON_OBJS) $(JSMN_OBJS) $(CCAN_OBJS)

$(PLUGIN_PAY_OBJS) $(PLUGIN_AUTOCLEAN_OBJS) $(PLUGIN_FUNDCHANNEL_OBJS) $(PLUGIN_TESt_OBJS) $(PLUGIN_LIB_OBJS): $(PLUGIN_LIB_HEADER)

# Make sure these depend on everything.
ALL_PROGRAMS += plugins/pay plugins/autoclean plugins/fundchannel
ALL_OBJS += $(PLUGIN_PAY_OBJS) $(PLUGIN_AUTOCLEAN_OBJS) $(PLUGIN_FUNDCHANNEL_OBJS) $(PLUGIN_LIB_OBJS)
ALL_PROGRAMS += plugins/pay plugins/autoclean plugins/fundchannel plugins/test
ALL_OBJS += $(PLUGIN_PAY_OBJS) $(PLUGIN_AUTOCLEAN_OBJS) $(PLUGIN_FUNDCHANNEL_OBJS) $(PLUGIN_TEST_OBJS) $(PLUGIN_LIB_OBJS)

check-source: $(PLUGIN_PAY_SRC:%=check-src-include-order/%) $(PLUGIN_AUTOCLEAN_SRC:%=check-src-include-order/%) $(PLUGIN_FUNDCHANNEL_SRC:%=check-src-include-order/%)
check-source-bolt: $(PLUGIN_PAY_SRC:%=bolt-check/%) $(PLUGIN_AUTOCLEAN_SRC:%=bolt-check/%) $(PLUGIN_FUNDCHANNEL_SRC:%=bolt-check/%)
Expand All @@ -61,4 +67,4 @@ check-whitespace: $(PLUGIN_PAY_SRC:%=check-whitespace/%) $(PLUGIN_AUTOCLEAN_SRC:
clean: plugin-clean

plugin-clean:
$(RM) $(PLUGIN_PAY_OBJS) $(PLUGIN_AUTOCLEAN_OBJS) $(PLUGIN_FUNDCHANNEL_OBJS) $(PLUGIN_LIB_OBJS)
$(RM) $(PLUGIN_PAY_OBJS) $(PLUGIN_AUTOCLEAN_OBJS) $(PLUGIN_FUNDCHANNEL_OBJS) $(PLUGIN_TEST_OBJS) $(PLUGIN_LIB_OBJS)
6 changes: 3 additions & 3 deletions plugins/libplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ struct plugin_notification {
/* Create an array of these, one for each hook you subscribe to. */
struct plugin_hook {
const char *name;
struct command_result (*handle)(struct command *cmd,
const char *buf,
const jsmntok_t *params);
struct command_result *(*handle)(struct command *cmd,
const char *buf,
const jsmntok_t *params);
};

/* Helper to create a zero or single-value JSON object; if @str is NULL,
Expand Down
Binary file added plugins/test
Binary file not shown.
58 changes: 58 additions & 0 deletions plugins/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <bitcoin/chainparams.c>
#include <ccan/array_size/array_size.h>
#include <ccan/json_out/json_out.h>
#include <plugins/libplugin.h>

/* test hook and notification plugins */

static void init(struct plugin_conn *rpc,
const char *buf UNUSED, const jsmntok_t *config UNUSED)
{
}

static struct command_result *json_hook(struct command *cmd,
const char *buf,
const jsmntok_t *params)
{
struct json_out *ret;

plugin_log(LOG_DBG, "hook: '%s'", buf);

ret = json_out_new(NULL);
json_out_start(ret, NULL, '{');
json_out_addstr(ret, "result", "continue");
json_out_end(ret, '}');

return command_success(cmd, ret);
}

static void json_notificate(struct command *cmd,
const char *buf,
const jsmntok_t *params)
{
plugin_log(LOG_DBG, "notification: '%s'", buf);
return;
}

const struct plugin_notification command_notification[] = { {
"connect",
json_notificate
},
};


const struct plugin_hook command_hook[] = { {
"peer_connected",
json_hook
},
};


int main(int argc, char *argv[])
{
setup_locale();
plugin_main(argv, init, PLUGIN_RESTARTABLE, NULL, 0,
command_notification, ARRAY_SIZE(command_notification),
command_hook, ARRAY_SIZE(command_hook)
, NULL);
}

0 comments on commit 83a66e2

Please sign in to comment.