X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-evt.c;h=d80af04524675437d9db67980fd57e16556943b0;hb=846e952260278225b79de4062fff1f8f2145a544;hp=2abbc819f716f1683448b5ad9bd059528901fe7c;hpb=ca9807f73646f536ac58c002d963a8bb8d245f5d;p=src%2Fapp-framework-binder.git diff --git a/src/afb-evt.c b/src/afb-evt.c index 2abbc819..d80af045 100644 --- a/src/afb-evt.c +++ b/src/afb-evt.c @@ -28,6 +28,8 @@ #include #include "afb-evt.h" +#include "afb-hook.h" +#include "verbose.h" struct afb_evt_watch; @@ -69,6 +71,9 @@ struct afb_evt_event { /* id of the event */ int id; + /* hooking */ + int hookflags; + /* mutex of the event */ pthread_mutex_t mutex; @@ -122,40 +127,55 @@ 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) @@ -169,15 +189,20 @@ static int evt_push(struct afb_evt_event *evt, struct json_object *obj) result = 0; pthread_mutex_lock(&evt->mutex); + if (evt->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; assert(listener->itf->push != NULL); - if (watch->activity != 0) + if (watch->activity != 0) { listener->itf->push(listener->closure, evt->name, evt->id, json_object_get(obj)); + result++; + } watch = watch->next_by_event; - result++; } + if (evt->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; @@ -188,6 +213,8 @@ static int evt_push(struct afb_evt_event *evt, struct json_object *obj) */ static const char *evt_name(struct afb_evt_event *evt) { + if (evt->hookflags & afb_hook_flag_evt_name) + afb_hook_evt_name(evt->name, evt->id); return evt->name; } @@ -234,6 +261,7 @@ static void evt_destroy(struct afb_evt_event *evt) if (evt != NULL) { /* unlinks the event if valid! */ pthread_mutex_lock(&events_mutex); + found = 0; prv = &events; while (*prv && !(found = (*prv == evt))) prv = &(*prv)->next; @@ -253,6 +281,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); @@ -300,6 +332,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 */ @@ -479,7 +514,6 @@ int afb_evt_remove_watch(struct afb_evt_listener *listener, struct afb_event eve watch = listener->watchs; while(watch != NULL) { if (watch->event == evt) { - /* found: remove it */ if (watch->activity != 0) { watch->activity--; if (watch->activity == 0 && listener->itf->remove != NULL) @@ -495,3 +529,16 @@ 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); +} +