X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-evt.c;h=1c81fd53bee206ff6ef9c9218325185d72668a47;hb=15059fa1ed5f94c3b5d96357c9be6adfa5ea37b0;hp=7b7ce4f3ce31b5105055fcbb13a4f718e6c13fbb;hpb=9eb56fc592c6e2c305a0fbcc69499271ac034236;p=src%2Fapp-framework-binder.git diff --git a/src/afb-evt.c b/src/afb-evt.c index 7b7ce4f3..1c81fd53 100644 --- a/src/afb-evt.c +++ b/src/afb-evt.c @@ -28,6 +28,7 @@ #include #include "afb-evt.h" +#include "afb-hook.h" #include "verbose.h" struct afb_evt_watch; @@ -70,6 +71,9 @@ struct afb_evt_event { /* id of the event */ int id; + /* hooking */ + int hookflags; + /* mutex of the event */ pthread_mutex_t mutex; @@ -123,46 +127,62 @@ static int event_id_counter = 0; static int event_id_wrapped = 0; /* - * Broadcasts the event 'evt' with its 'object' - * 'object' is released (like json_object_put) - * Returns the count of listener that received the event. - */ -static int evt_broadcast(struct afb_evt_event *evt, struct json_object *object) -{ - return afb_evt_broadcast(evt->name, object); -} - -/* - * Broadcasts the 'event' with its 'object' - * 'object' is released (like json_object_put) + * Broadcasts the 'event' of 'id' with its 'obj' + * 'obj' is released (like json_object_put) + * calls hooks if hookflags isn't 0 * Returns the count of listener having receive the event. */ -int afb_evt_broadcast(const char *event, struct json_object *object) +static int broadcast(const char *event, struct json_object *obj, int id, int hookflags) { int result; struct afb_evt_listener *listener; + if (hookflags & afb_hook_flag_evt_broadcast_before) + afb_hook_evt_broadcast_before(event, id, obj); result = 0; pthread_mutex_lock(&listeners_mutex); listener = listeners; while(listener) { if (listener->itf->broadcast != NULL) { - listener->itf->broadcast(listener->closure, event, 0, json_object_get(object)); + listener->itf->broadcast(listener->closure, event, id, json_object_get(obj)); result++; } listener = listener->next; } pthread_mutex_unlock(&listeners_mutex); - json_object_put(object); + if (hookflags & afb_hook_flag_evt_broadcast_after) + afb_hook_evt_broadcast_after(event, id, obj, result); + json_object_put(obj); return result; } +/* + * Broadcasts the event 'evt' with its 'object' + * 'object' is released (like json_object_put) + * Returns the count of listener that received the event. + */ +static int evt_broadcast(struct afb_evt_event *evt, struct json_object *object) +{ + return broadcast(evt->name, object, evt->id, evt->hookflags); +} + +/* + * Broadcasts the 'event' with its 'object' + * 'object' is released (like json_object_put) + * Returns the count of listener having receive the event. + */ +int afb_evt_broadcast(const char *event, struct json_object *object) +{ + return broadcast(event, object, 0, -1); +} + /* * Pushes the event 'evt' with 'obj' to its listeners * 'obj' is released (like json_object_put) + * calls hooks if hookflags isn't 0 * Returns the count of listener taht received the event. */ -static int evt_push(struct afb_evt_event *evt, struct json_object *obj) +static int push(struct afb_evt_event *evt, struct json_object *obj, int hookflags) { int result; struct afb_evt_watch *watch; @@ -170,6 +190,8 @@ static int evt_push(struct afb_evt_event *evt, struct json_object *obj) result = 0; pthread_mutex_lock(&evt->mutex); + if (hookflags & afb_hook_flag_evt_push_before) + afb_hook_evt_push_before(evt->name, evt->id, obj); watch = evt->watchs; while(watch) { listener = watch->listener; @@ -180,17 +202,33 @@ static int evt_push(struct afb_evt_event *evt, struct json_object *obj) } watch = watch->next_by_event; } + if (hookflags & afb_hook_flag_evt_push_after) + afb_hook_evt_push_after(evt->name, evt->id, obj, result); pthread_mutex_unlock(&evt->mutex); json_object_put(obj); return result; } +/* + * 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) +{ + return push(evt, obj, evt->hookflags); +} + /* * Returns the name associated to the event 'evt'. */ static const char *evt_name(struct afb_evt_event *evt) { - return evt->name; + const char *name = strchr(evt->name, '/'); + name = name ? name + 1 : evt->name; + if (evt->hookflags & afb_hook_flag_evt_name) + afb_hook_evt_name(evt->name, evt->id); + return name; } /* @@ -256,6 +294,10 @@ static void evt_destroy(struct afb_evt_event *evt) pthread_mutex_unlock(&listener->mutex); } + /* hook */ + if (evt->hookflags & afb_hook_flag_evt_drop) + afb_hook_evt_drop(evt->name, evt->id); + /* free */ pthread_mutex_destroy(&evt->mutex); free(evt); @@ -270,7 +312,7 @@ static void evt_destroy(struct afb_evt_event *evt) struct afb_event afb_evt_create_event(const char *name) { size_t len; - struct afb_evt_event *evt; + struct afb_evt_event *evt, *oevt; /* allocates the event */ len = strlen(name); @@ -278,10 +320,6 @@ struct afb_event afb_evt_create_event(const char *name) if (evt == NULL) goto error; - /* initialize the event */ - evt->watchs = NULL; - memcpy(evt->name, name, len + 1); - /* allocates the id */ pthread_mutex_lock(&events_mutex); do { @@ -291,10 +329,10 @@ struct afb_event afb_evt_create_event(const char *name) } if (!event_id_wrapped) break; - evt = events; - while(evt != NULL && evt->id != event_id_counter) - evt = evt->next; - } while (evt != NULL); + oevt = events; + while(oevt != NULL && oevt->id != event_id_counter) + oevt = oevt->next; + } while (oevt != NULL); /* initialize the event */ memcpy(evt->name, name, len + 1); @@ -303,6 +341,9 @@ struct afb_event afb_evt_create_event(const char *name) evt->id = event_id_counter; pthread_mutex_init(&evt->mutex, NULL); events = evt; + evt->hookflags = afb_hook_flags_evt(evt->name); + if (evt->hookflags & afb_hook_flag_evt_create) + afb_hook_evt_create(evt->name, evt->id); pthread_mutex_unlock(&events_mutex); /* returns the event */ @@ -497,3 +538,32 @@ int afb_evt_remove_watch(struct afb_evt_listener *listener, struct afb_event eve return -1; } +/* + * update the hooks for events + */ +void afb_evt_update_hooks() +{ + struct afb_evt_event *evt; + + pthread_mutex_lock(&events_mutex); + for (evt = events ; evt ; evt = evt->next) + evt->hookflags = afb_hook_flags_evt(evt->name); + pthread_mutex_unlock(&events_mutex); +} + +int afb_evt_push(struct afb_event event, struct json_object *object) +{ + if (event.itf == &afb_evt_event_itf) + return evt_push((struct afb_evt_event *)event.closure, object); + json_object_put(object); + return 0; +} + +int afb_evt_unhooked_push(struct afb_event event, struct json_object *object) +{ + if (event.itf == &afb_evt_event_itf) + return push((struct afb_evt_event *)event.closure, object, 0); + json_object_put(object); + return 0; +} +