event: add ability to get the event name
authorJosé Bollo <jose.bollo@iot.bzh>
Fri, 14 Oct 2016 13:13:43 +0000 (15:13 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Fri, 14 Oct 2016 13:13:43 +0000 (15:13 +0200)
Change-Id: Iecfeb4ab07c07715093d729710669abecee722de
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
include/afb/afb-event-itf.h
src/afb-evt.c

index 5f7b837..ad9206a 100644 (file)
@@ -32,6 +32,7 @@ struct afb_event_itf {
        int (*broadcast)(void *closure, struct json_object *obj);
        int (*push)(void *closure, struct json_object *obj);
        void (*drop)(void *closure);
+       const char *(*name)(void *closure);
 };
 
 /*
@@ -83,7 +84,7 @@ static inline int afb_event_push(struct afb_event event, struct json_object *obj
 }
 
 /*
- * Drops the data associated to the event
+ * Drops the data associated to the 'event'
  * After calling this function, the event
  * MUST NOT BE USED ANYMORE.
  */
@@ -92,3 +93,11 @@ static inline void afb_event_drop(struct afb_event event)
        event.itf->drop(event.closure);
 }
 
+/*
+ * Gets the name associated to the 'event'.
+ */
+static inline const char *afb_event_name(struct afb_event event)
+{
+       return event.itf->name(event.closure);
+}
+
index 4924b8c..d8853b9 100644 (file)
@@ -94,12 +94,14 @@ struct afb_evt_watch {
 static int evt_broadcast(struct afb_evt_event *evt, struct json_object *obj);
 static int evt_push(struct afb_evt_event *evt, struct json_object *obj);
 static void evt_destroy(struct afb_evt_event *evt);
+static const char *evt_name(struct afb_evt_event *evt);
 
 /* the interface for events */
 static struct afb_event_itf afb_evt_event_itf = {
        .broadcast = (void*)evt_broadcast,
        .push = (void*)evt_push,
-       .drop = (void*)evt_destroy
+       .drop = (void*)evt_destroy,
+       .name = (void*)evt_name
 };
 
 /* head of the list of listeners */
@@ -144,8 +146,8 @@ int afb_evt_broadcast(const char *event, struct json_object *object)
 }
 
 /*
- * Broadcasts the event 'evt' with its 'object'
- * 'object' is released (like json_object_put)
+ * Pushes the event 'evt' with 'obj' to its listeners
+ * 'obj' is released (like json_object_put)
  * Returns the count of listener taht received the event.
  */
 static int evt_push(struct afb_evt_event *evt, struct json_object *obj)
@@ -168,6 +170,14 @@ static int evt_push(struct afb_evt_event *evt, struct json_object *obj)
        return result;
 }
 
+/*
+ * Returns the name associated to the event 'evt'.
+ */
+static const char *evt_name(struct afb_evt_event *evt)
+{
+       return evt->name;
+}
+
 /*
  * remove the 'watch'
  */