aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2016-05-11 14:36:14 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2016-05-11 14:36:14 +0200
commit3b0d4962f64f546474fa033ffa4e3d067194c888 (patch)
tree56d8d47236f7913776b0ceb67007d9f2552cfad8
parent423d86c6032c0a27f790a3766abd0c855726eab1 (diff)
allows to filter events before to receive it
Change-Id: Ia25d6a6269d7dd1af900b63d54cf615a69dfc4d7 Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r--src/afb-ws-json.c12
-rw-r--r--src/session.c6
-rw-r--r--src/session.h1
3 files changed, 14 insertions, 5 deletions
diff --git a/src/afb-ws-json.c b/src/afb-ws-json.c
index 2e68d277..7475b263 100644
--- a/src/afb-ws-json.c
+++ b/src/afb-ws-json.c
@@ -56,9 +56,15 @@ struct afb_ws_json
static void aws_send_event(struct afb_ws_json *ws, const char *event, struct json_object *object);
static const struct afb_event_listener_itf event_listener_itf = {
- .send = (void*)aws_send_event
+ .send = (void*)aws_send_event,
+ .expects = NULL
};
+static inline struct afb_event_listener listener_for(struct afb_ws_json *aws)
+{
+ return (struct afb_event_listener){ .itf = &event_listener_itf, .closure = aws };
+}
+
struct afb_ws_json *afb_ws_json_create(int fd, struct AFB_clientCtx *session, void (*cleanup)(void*), void *cleanup_closure)
{
struct afb_ws_json *result;
@@ -85,7 +91,7 @@ struct afb_ws_json *afb_ws_json_create(int fd, struct AFB_clientCtx *session, vo
if (result->ws == NULL)
goto error4;
- if (0 > ctxClientEventListenerAdd(result->session, (struct afb_event_listener){ .itf = &event_listener_itf, .closure = result }))
+ if (0 > ctxClientEventListenerAdd(result->session, listener_for(result)))
goto error5;
return result;
@@ -105,7 +111,7 @@ error:
static void aws_on_hangup(struct afb_ws_json *ws)
{
- ctxClientEventListenerRemove(ws->session, (struct afb_event_listener){ .itf = &event_listener_itf, .closure = ws });
+ ctxClientEventListenerRemove(ws->session, listener_for(ws));
afb_ws_destroy(ws->ws);
json_tokener_free(ws->tokener);
if (ws->cleanup != NULL)
diff --git a/src/session.c b/src/session.c
index 336dfd70..87e92376 100644
--- a/src/session.c
+++ b/src/session.c
@@ -378,8 +378,10 @@ static int send(struct afb_event_listener_list *head, const char *event, struct
result = 0;
iter = head;
while (iter != NULL) {
- iter->listener.itf->send(iter->listener.closure, event, json_object_get(object));
- result++;
+ if (iter->listener.itf->expects == NULL || iter->listener.itf->expects(iter->listener.closure, event)) {
+ iter->listener.itf->send(iter->listener.closure, event, json_object_get(object));
+ result++;
+ }
iter = iter->next;
}
diff --git a/src/session.h b/src/session.h
index c2722455..a4c05222 100644
--- a/src/session.h
+++ b/src/session.h
@@ -23,6 +23,7 @@ struct AFB_clientCtx;
struct afb_event_listener_itf
{
void (*send)(void *closure, const char *event, struct json_object *object);
+ int (*expects)(void *closure, const char *event);
};
struct afb_event_listener