notification: Add registeration interface

This commit is contained in:
trueptolemy 2019-08-11 05:32:13 +08:00 committed by Christian Decker
parent d0fe0d658f
commit 25d79c5c3f
2 changed files with 39 additions and 0 deletions

View File

@ -13,8 +13,26 @@ const char *notification_topics[] = {
"forward_event"
};
static struct notification *find_notification_by_topic(const char* topic)
{
static struct notification **notilist = NULL;
static size_t num_notis;
if (!notilist)
notilist = autodata_get(notifications, &num_notis);
for (size_t i=0; i<num_notis; i++)
if (streq(notilist[i]->topic, topic))
return notilist[i];
return NULL;
}
bool notifications_have_topic(const char *topic)
{
struct notification *noti = find_notification_by_topic(topic);
if (noti)
return true;
/* TODO: Remove this block after making all notifications registered. */
for (size_t i=0; i<ARRAY_SIZE(notification_topics); i++)
if (streq(topic, notification_topics[i]))
return true;
@ -131,3 +149,7 @@ void notify_forward_event(struct lightningd *ld,
jsonrpc_notification_end(n);
plugins_notify(ld->plugins, take(n));
}
/* TODO: It's a dummy notification. Will be removed when we have a 'real' one
* in this file. */
REGISTER_JSON_INTERNAL_COMMAND(hello_notification, NULL, void *);

View File

@ -3,6 +3,7 @@
#include "config.h"
#include <bitcoin/short_channel_id.h>
#include <bitcoin/tx.h>
#include <ccan/autodata/autodata.h>
#include <ccan/json_escape/json_escape.h>
#include <ccan/time/time.h>
#include <common/amount.h>
@ -17,6 +18,22 @@
bool notifications_have_topic(const char *topic);
struct notification {
const char *topic;
/* the serialization interface */
void *serialize;
};
AUTODATA_TYPE(notifications, struct notification);
/* FIXME: Find a way to avoid back-to-back declaration and definition */
#define REGISTER_NOTIFICATION(topic, serialize) \
struct notification topic##_notification_gen = { \
stringify(topic), \
serialize, \
}; \
AUTODATA(notifications, &topic##_notification_gen);
void notify_connect(struct lightningd *ld, struct node_id *nodeid,
struct wireaddr_internal *addr);
void notify_disconnect(struct lightningd *ld, struct node_id *nodeid);