2 * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
3 * Author "Fulup Ar Foll"
4 * Author José Bollo <jose.bollo@iot.bzh>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
27 #include <json-c/json.h>
28 #include <afb/afb-event-itf.h>
37 * Structure for event listeners
39 struct afb_evt_listener {
41 /* chaining listeners */
42 struct afb_evt_listener *next;
44 /* interface for callbacks */
45 const struct afb_evt_itf *itf;
47 /* closure for the callback */
50 /* head of the list of events listened */
51 struct afb_evt_watch *watchs;
53 /* mutex of the listener */
54 pthread_mutex_t mutex;
56 /* count of reference to the listener */
61 * Structure for describing events
63 struct afb_evt_event {
66 struct afb_evt_event *next;
68 /* head of the list of listeners watching the event */
69 struct afb_evt_watch *watchs;
77 /* mutex of the event */
78 pthread_mutex_t mutex;
80 /* name of the event */
85 * Structure for associating events and listeners
87 struct afb_evt_watch {
90 struct afb_evt_event *event;
92 /* link to the next listener for the same event */
93 struct afb_evt_watch *next_by_event;
96 struct afb_evt_listener *listener;
98 /* link to the next event for the same listener */
99 struct afb_evt_watch *next_by_listener;
105 /* declare functions */
106 static int evt_broadcast(struct afb_evt_event *evt, struct json_object *obj);
107 static int evt_push(struct afb_evt_event *evt, struct json_object *obj);
108 static void evt_destroy(struct afb_evt_event *evt);
109 static const char *evt_name(struct afb_evt_event *evt);
111 /* the interface for events */
112 static struct afb_event_itf afb_evt_event_itf = {
113 .broadcast = (void*)evt_broadcast,
114 .push = (void*)evt_push,
115 .drop = (void*)evt_destroy,
116 .name = (void*)evt_name
119 /* head of the list of listeners */
120 static pthread_mutex_t listeners_mutex = PTHREAD_MUTEX_INITIALIZER;
121 static struct afb_evt_listener *listeners = NULL;
123 /* handling id of events */
124 static pthread_mutex_t events_mutex = PTHREAD_MUTEX_INITIALIZER;
125 static struct afb_evt_event *events = NULL;
126 static int event_id_counter = 0;
127 static int event_id_wrapped = 0;
130 * Broadcasts the 'event' of 'id' with its 'obj'
131 * 'obj' is released (like json_object_put)
132 * calls hooks if hookflags isn't 0
133 * Returns the count of listener having receive the event.
135 static int broadcast(const char *event, struct json_object *obj, int id, int hookflags)
138 struct afb_evt_listener *listener;
140 if (hookflags & afb_hook_flag_evt_broadcast_before)
141 afb_hook_evt_broadcast_before(event, id, obj);
143 pthread_mutex_lock(&listeners_mutex);
144 listener = listeners;
146 if (listener->itf->broadcast != NULL) {
147 listener->itf->broadcast(listener->closure, event, id, json_object_get(obj));
150 listener = listener->next;
152 pthread_mutex_unlock(&listeners_mutex);
153 if (hookflags & afb_hook_flag_evt_broadcast_after)
154 afb_hook_evt_broadcast_after(event, id, obj, result);
155 json_object_put(obj);
160 * Broadcasts the event 'evt' with its 'object'
161 * 'object' is released (like json_object_put)
162 * Returns the count of listener that received the event.
164 static int evt_broadcast(struct afb_evt_event *evt, struct json_object *object)
166 return broadcast(evt->name, object, evt->id, evt->hookflags);
170 * Broadcasts the 'event' with its 'object'
171 * 'object' is released (like json_object_put)
172 * Returns the count of listener having receive the event.
174 int afb_evt_broadcast(const char *event, struct json_object *object)
176 return broadcast(event, object, 0, -1);
180 * Pushes the event 'evt' with 'obj' to its listeners
181 * 'obj' is released (like json_object_put)
182 * Returns the count of listener taht received the event.
184 static int evt_push(struct afb_evt_event *evt, struct json_object *obj)
187 struct afb_evt_watch *watch;
188 struct afb_evt_listener *listener;
191 pthread_mutex_lock(&evt->mutex);
192 if (evt->hookflags & afb_hook_flag_evt_push_before)
193 afb_hook_evt_push_before(evt->name, evt->id, obj);
196 listener = watch->listener;
197 assert(listener->itf->push != NULL);
198 if (watch->activity != 0) {
199 listener->itf->push(listener->closure, evt->name, evt->id, json_object_get(obj));
202 watch = watch->next_by_event;
204 if (evt->hookflags & afb_hook_flag_evt_push_after)
205 afb_hook_evt_push_after(evt->name, evt->id, obj, result);
206 pthread_mutex_unlock(&evt->mutex);
207 json_object_put(obj);
212 * Returns the name associated to the event 'evt'.
214 static const char *evt_name(struct afb_evt_event *evt)
216 const char *name = strchr(evt->name, '/');
217 name = name ? name + 1 : evt->name;
218 if (evt->hookflags & afb_hook_flag_evt_name)
219 afb_hook_evt_name(evt->name, evt->id);
226 static void remove_watch(struct afb_evt_watch *watch)
228 struct afb_evt_watch **prv;
229 struct afb_evt_event *evt;
230 struct afb_evt_listener *listener;
232 /* notify listener if needed */
234 listener = watch->listener;
235 if (watch->activity != 0 && listener->itf->remove != NULL)
236 listener->itf->remove(listener->closure, evt->name, evt->id);
238 /* unlink the watch for its event */
241 prv = &(*prv)->next_by_event;
242 *prv = watch->next_by_event;
244 /* unlink the watch for its listener */
245 prv = &listener->watchs;
247 prv = &(*prv)->next_by_listener;
248 *prv = watch->next_by_listener;
255 * Destroys the event 'evt'
257 static void evt_destroy(struct afb_evt_event *evt)
260 struct afb_evt_event **prv;
261 struct afb_evt_listener *listener;
264 /* unlinks the event if valid! */
265 pthread_mutex_lock(&events_mutex);
268 while (*prv && !(found = (*prv == evt)))
272 pthread_mutex_unlock(&events_mutex);
274 /* destroys the event */
276 /* removes all watchers */
277 while(evt->watchs != NULL) {
278 listener = evt->watchs->listener;
279 pthread_mutex_lock(&listener->mutex);
280 pthread_mutex_lock(&evt->mutex);
281 remove_watch(evt->watchs);
282 pthread_mutex_unlock(&evt->mutex);
283 pthread_mutex_unlock(&listener->mutex);
287 if (evt->hookflags & afb_hook_flag_evt_drop)
288 afb_hook_evt_drop(evt->name, evt->id);
291 pthread_mutex_destroy(&evt->mutex);
298 * Creates an event of 'name' and returns it.
299 * Returns an event with closure==NULL in case of error.
301 struct afb_event afb_evt_create_event(const char *name)
304 struct afb_evt_event *evt;
306 /* allocates the event */
308 evt = malloc(len + sizeof * evt);
312 /* initialize the event */
314 memcpy(evt->name, name, len + 1);
316 /* allocates the id */
317 pthread_mutex_lock(&events_mutex);
319 if (++event_id_counter < 0) {
320 event_id_wrapped = 1;
321 event_id_counter = 1024; /* heuristic: small numbers are not destroyed */
323 if (!event_id_wrapped)
326 while(evt != NULL && evt->id != event_id_counter)
328 } while (evt != NULL);
330 /* initialize the event */
331 memcpy(evt->name, name, len + 1);
334 evt->id = event_id_counter;
335 pthread_mutex_init(&evt->mutex, NULL);
337 evt->hookflags = afb_hook_flags_evt(evt->name);
338 if (evt->hookflags & afb_hook_flag_evt_create)
339 afb_hook_evt_create(evt->name, evt->id);
340 pthread_mutex_unlock(&events_mutex);
342 /* returns the event */
343 return (struct afb_event){ .itf = &afb_evt_event_itf, .closure = evt };
345 return (struct afb_event){ .itf = NULL, .closure = NULL };
349 * Returns the name of the 'event'
351 const char *afb_evt_event_name(struct afb_event event)
353 return (event.itf != &afb_evt_event_itf) ? NULL : ((struct afb_evt_event *)event.closure)->name;
357 * Returns the id of the 'event'
359 int afb_evt_event_id(struct afb_event event)
361 return (event.itf != &afb_evt_event_itf) ? 0 : ((struct afb_evt_event *)event.closure)->id;
365 * Returns an instance of the listener defined by the 'send' callback
367 * Returns NULL in case of memory depletion.
369 struct afb_evt_listener *afb_evt_listener_create(const struct afb_evt_itf *itf, void *closure)
371 struct afb_evt_listener *listener;
373 /* search if an instance already exists */
374 pthread_mutex_lock(&listeners_mutex);
375 listener = listeners;
376 while (listener != NULL) {
377 if (listener->itf == itf && listener->closure == closure) {
378 listener = afb_evt_listener_addref(listener);
381 listener = listener->next;
385 listener = calloc(1, sizeof *listener);
386 if (listener != NULL) {
389 listener->closure = closure;
390 listener->watchs = NULL;
391 listener->refcount = 1;
392 pthread_mutex_init(&listener->mutex, NULL);
393 listener->next = listeners;
394 listeners = listener;
397 pthread_mutex_unlock(&listeners_mutex);
402 * Increases the reference count of 'listener' and returns it
404 struct afb_evt_listener *afb_evt_listener_addref(struct afb_evt_listener *listener)
406 __atomic_add_fetch(&listener->refcount, 1, __ATOMIC_RELAXED);
411 * Decreases the reference count of the 'listener' and destroys it
414 void afb_evt_listener_unref(struct afb_evt_listener *listener)
416 struct afb_evt_listener **prv;
417 struct afb_evt_event *evt;
419 if (!__atomic_sub_fetch(&listener->refcount, 1, __ATOMIC_RELAXED)) {
421 /* unlink the listener */
422 pthread_mutex_lock(&listeners_mutex);
424 while (*prv != listener)
426 *prv = listener->next;
427 pthread_mutex_unlock(&listeners_mutex);
429 /* remove the watchers */
430 pthread_mutex_lock(&listener->mutex);
431 while (listener->watchs != NULL) {
432 evt = listener->watchs->event;
433 pthread_mutex_lock(&evt->mutex);
434 remove_watch(listener->watchs);
435 pthread_mutex_unlock(&evt->mutex);
437 pthread_mutex_unlock(&listener->mutex);
439 /* free the listener */
440 pthread_mutex_destroy(&listener->mutex);
446 * Makes the 'listener' watching 'event'
447 * Returns 0 in case of success or else -1.
449 int afb_evt_add_watch(struct afb_evt_listener *listener, struct afb_event event)
451 struct afb_evt_watch *watch;
452 struct afb_evt_event *evt;
454 /* check parameter */
455 if (event.itf != &afb_evt_event_itf || listener->itf->push == NULL) {
460 /* search the existing watch for the listener */
462 pthread_mutex_lock(&listener->mutex);
463 watch = listener->watchs;
464 while(watch != NULL) {
465 if (watch->event == evt)
467 watch = watch->next_by_listener;
470 /* not found, allocate a new */
471 watch = malloc(sizeof *watch);
473 pthread_mutex_unlock(&listener->mutex);
478 /* initialise and link */
481 watch->listener = listener;
482 watch->next_by_listener = listener->watchs;
483 listener->watchs = watch;
484 pthread_mutex_lock(&evt->mutex);
485 watch->next_by_event = evt->watchs;
487 pthread_mutex_unlock(&evt->mutex);
490 if (watch->activity == 0 && listener->itf->add != NULL)
491 listener->itf->add(listener->closure, evt->name, evt->id);
493 pthread_mutex_unlock(&listener->mutex);
499 * Avoids the 'listener' to watch 'event'
500 * Returns 0 in case of success or else -1.
502 int afb_evt_remove_watch(struct afb_evt_listener *listener, struct afb_event event)
504 struct afb_evt_watch *watch;
505 struct afb_evt_event *evt;
507 /* check parameter */
508 if (event.itf != &afb_evt_event_itf) {
513 /* search the existing watch */
515 pthread_mutex_lock(&listener->mutex);
516 watch = listener->watchs;
517 while(watch != NULL) {
518 if (watch->event == evt) {
519 if (watch->activity != 0) {
521 if (watch->activity == 0 && listener->itf->remove != NULL)
522 listener->itf->remove(listener->closure, evt->name, evt->id);
524 pthread_mutex_unlock(&listener->mutex);
527 watch = watch->next_by_listener;
529 pthread_mutex_unlock(&listener->mutex);
535 * update the hooks for events
537 void afb_evt_update_hooks()
539 struct afb_evt_event *evt;
541 pthread_mutex_lock(&events_mutex);
542 for (evt = events ; evt ; evt = evt->next)
543 evt->hookflags = afb_hook_flags_evt(evt->name);
544 pthread_mutex_unlock(&events_mutex);