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>
35 * Structure for event listeners
37 struct afb_evt_listener {
39 /* chaining listeners */
40 struct afb_evt_listener *next;
42 /* interface for callbacks */
43 const struct afb_evt_itf *itf;
45 /* closure for the callback */
48 /* head of the list of events listened */
49 struct afb_evt_watch *watchs;
51 /* mutex of the listener */
52 pthread_mutex_t mutex;
54 /* count of reference to the listener */
59 * Structure for describing events
61 struct afb_evt_event {
64 struct afb_evt_event *next;
66 /* head of the list of listeners watching the event */
67 struct afb_evt_watch *watchs;
72 /* mutex of the event */
73 pthread_mutex_t mutex;
75 /* name of the event */
80 * Structure for associating events and listeners
82 struct afb_evt_watch {
85 struct afb_evt_event *event;
87 /* link to the next listener for the same event */
88 struct afb_evt_watch *next_by_event;
91 struct afb_evt_listener *listener;
93 /* link to the next event for the same listener */
94 struct afb_evt_watch *next_by_listener;
100 /* declare functions */
101 static int evt_broadcast(struct afb_evt_event *evt, struct json_object *obj);
102 static int evt_push(struct afb_evt_event *evt, struct json_object *obj);
103 static void evt_destroy(struct afb_evt_event *evt);
104 static const char *evt_name(struct afb_evt_event *evt);
106 /* the interface for events */
107 static struct afb_event_itf afb_evt_event_itf = {
108 .broadcast = (void*)evt_broadcast,
109 .push = (void*)evt_push,
110 .drop = (void*)evt_destroy,
111 .name = (void*)evt_name
114 /* head of the list of listeners */
115 static pthread_mutex_t listeners_mutex = PTHREAD_MUTEX_INITIALIZER;
116 static struct afb_evt_listener *listeners = NULL;
118 /* handling id of events */
119 static pthread_mutex_t events_mutex = PTHREAD_MUTEX_INITIALIZER;
120 static struct afb_evt_event *events = NULL;
121 static int event_id_counter = 0;
122 static int event_id_wrapped = 0;
125 * Broadcasts the event 'evt' with its 'object'
126 * 'object' is released (like json_object_put)
127 * Returns the count of listener that received the event.
129 static int evt_broadcast(struct afb_evt_event *evt, struct json_object *object)
131 return afb_evt_broadcast(evt->name, object);
135 * Broadcasts the 'event' with its 'object'
136 * 'object' is released (like json_object_put)
137 * Returns the count of listener having receive the event.
139 int afb_evt_broadcast(const char *event, struct json_object *object)
142 struct afb_evt_listener *listener;
145 pthread_mutex_lock(&listeners_mutex);
146 listener = listeners;
148 if (listener->itf->broadcast != NULL) {
149 listener->itf->broadcast(listener->closure, event, 0, json_object_get(object));
152 listener = listener->next;
154 pthread_mutex_unlock(&listeners_mutex);
155 json_object_put(object);
160 * Pushes the event 'evt' with 'obj' to its listeners
161 * 'obj' is released (like json_object_put)
162 * Returns the count of listener taht received the event.
164 static int evt_push(struct afb_evt_event *evt, struct json_object *obj)
167 struct afb_evt_watch *watch;
168 struct afb_evt_listener *listener;
171 pthread_mutex_lock(&evt->mutex);
174 listener = watch->listener;
175 assert(listener->itf->push != NULL);
176 if (watch->activity != 0)
177 listener->itf->push(listener->closure, evt->name, evt->id, json_object_get(obj));
178 watch = watch->next_by_event;
181 pthread_mutex_unlock(&evt->mutex);
182 json_object_put(obj);
187 * Returns the name associated to the event 'evt'.
189 static const char *evt_name(struct afb_evt_event *evt)
197 static void remove_watch(struct afb_evt_watch *watch)
199 struct afb_evt_watch **prv;
200 struct afb_evt_event *evt;
201 struct afb_evt_listener *listener;
203 /* notify listener if needed */
205 listener = watch->listener;
206 if (watch->activity != 0 && listener->itf->remove != NULL)
207 listener->itf->remove(listener->closure, evt->name, evt->id);
209 /* unlink the watch for its event */
212 prv = &(*prv)->next_by_event;
213 *prv = watch->next_by_event;
215 /* unlink the watch for its listener */
216 prv = &listener->watchs;
218 prv = &(*prv)->next_by_listener;
219 *prv = watch->next_by_listener;
226 * Destroys the event 'evt'
228 static void evt_destroy(struct afb_evt_event *evt)
231 struct afb_evt_event **prv;
232 struct afb_evt_listener *listener;
235 /* unlinks the event if valid! */
236 pthread_mutex_lock(&events_mutex);
238 while (*prv && !(found = (*prv == evt)))
242 pthread_mutex_unlock(&events_mutex);
244 /* destroys the event */
246 /* removes all watchers */
247 while(evt->watchs != NULL) {
248 listener = evt->watchs->listener;
249 pthread_mutex_lock(&listener->mutex);
250 pthread_mutex_lock(&evt->mutex);
251 remove_watch(evt->watchs);
252 pthread_mutex_unlock(&evt->mutex);
253 pthread_mutex_unlock(&listener->mutex);
257 pthread_mutex_destroy(&evt->mutex);
264 * Creates an event of 'name' and returns it.
265 * Returns an event with closure==NULL in case of error.
267 struct afb_event afb_evt_create_event(const char *name)
270 struct afb_evt_event *evt;
272 /* allocates the event */
274 evt = malloc(len + sizeof * evt);
278 /* initialize the event */
280 memcpy(evt->name, name, len + 1);
282 /* allocates the id */
283 pthread_mutex_lock(&events_mutex);
285 if (++event_id_counter < 0) {
286 event_id_wrapped = 1;
287 event_id_counter = 1024; /* heuristic: small numbers are not destroyed */
289 if (!event_id_wrapped)
292 while(evt != NULL && evt->id != event_id_counter)
294 } while (evt != NULL);
296 /* initialize the event */
297 memcpy(evt->name, name, len + 1);
300 evt->id = event_id_counter;
301 pthread_mutex_init(&evt->mutex, NULL);
303 pthread_mutex_unlock(&events_mutex);
305 /* returns the event */
306 return (struct afb_event){ .itf = &afb_evt_event_itf, .closure = evt };
308 return (struct afb_event){ .itf = NULL, .closure = NULL };
312 * Returns the name of the 'event'
314 const char *afb_evt_event_name(struct afb_event event)
316 return (event.itf != &afb_evt_event_itf) ? NULL : ((struct afb_evt_event *)event.closure)->name;
320 * Returns the id of the 'event'
322 int afb_evt_event_id(struct afb_event event)
324 return (event.itf != &afb_evt_event_itf) ? 0 : ((struct afb_evt_event *)event.closure)->id;
328 * Returns an instance of the listener defined by the 'send' callback
330 * Returns NULL in case of memory depletion.
332 struct afb_evt_listener *afb_evt_listener_create(const struct afb_evt_itf *itf, void *closure)
334 struct afb_evt_listener *listener;
336 /* search if an instance already exists */
337 pthread_mutex_lock(&listeners_mutex);
338 listener = listeners;
339 while (listener != NULL) {
340 if (listener->itf == itf && listener->closure == closure) {
341 listener = afb_evt_listener_addref(listener);
344 listener = listener->next;
348 listener = calloc(1, sizeof *listener);
349 if (listener != NULL) {
352 listener->closure = closure;
353 listener->watchs = NULL;
354 listener->refcount = 1;
355 pthread_mutex_init(&listener->mutex, NULL);
356 listener->next = listeners;
357 listeners = listener;
360 pthread_mutex_unlock(&listeners_mutex);
365 * Increases the reference count of 'listener' and returns it
367 struct afb_evt_listener *afb_evt_listener_addref(struct afb_evt_listener *listener)
369 __atomic_add_fetch(&listener->refcount, 1, __ATOMIC_RELAXED);
374 * Decreases the reference count of the 'listener' and destroys it
377 void afb_evt_listener_unref(struct afb_evt_listener *listener)
379 struct afb_evt_listener **prv;
380 struct afb_evt_event *evt;
382 if (!__atomic_sub_fetch(&listener->refcount, 1, __ATOMIC_RELAXED)) {
384 /* unlink the listener */
385 pthread_mutex_lock(&listeners_mutex);
387 while (*prv != listener)
389 *prv = listener->next;
390 pthread_mutex_unlock(&listeners_mutex);
392 /* remove the watchers */
393 pthread_mutex_lock(&listener->mutex);
394 while (listener->watchs != NULL) {
395 evt = listener->watchs->event;
396 pthread_mutex_lock(&evt->mutex);
397 remove_watch(listener->watchs);
398 pthread_mutex_unlock(&evt->mutex);
400 pthread_mutex_unlock(&listener->mutex);
402 /* free the listener */
403 pthread_mutex_destroy(&listener->mutex);
409 * Makes the 'listener' watching 'event'
410 * Returns 0 in case of success or else -1.
412 int afb_evt_add_watch(struct afb_evt_listener *listener, struct afb_event event)
414 struct afb_evt_watch *watch;
415 struct afb_evt_event *evt;
417 /* check parameter */
418 if (event.itf != &afb_evt_event_itf || listener->itf->push == NULL) {
423 /* search the existing watch for the listener */
425 pthread_mutex_lock(&listener->mutex);
426 watch = listener->watchs;
427 while(watch != NULL) {
428 if (watch->event == evt)
430 watch = watch->next_by_listener;
433 /* not found, allocate a new */
434 watch = malloc(sizeof *watch);
436 pthread_mutex_unlock(&listener->mutex);
441 /* initialise and link */
444 watch->listener = listener;
445 watch->next_by_listener = listener->watchs;
446 listener->watchs = watch;
447 pthread_mutex_lock(&evt->mutex);
448 watch->next_by_event = evt->watchs;
450 pthread_mutex_unlock(&evt->mutex);
453 if (watch->activity == 0 && listener->itf->add != NULL)
454 listener->itf->add(listener->closure, evt->name, evt->id);
456 pthread_mutex_unlock(&listener->mutex);
462 * Avoids the 'listener' to watch 'event'
463 * Returns 0 in case of success or else -1.
465 int afb_evt_remove_watch(struct afb_evt_listener *listener, struct afb_event event)
467 struct afb_evt_watch *watch;
468 struct afb_evt_event *evt;
470 /* check parameter */
471 if (event.itf != &afb_evt_event_itf) {
476 /* search the existing watch */
478 pthread_mutex_lock(&listener->mutex);
479 watch = listener->watchs;
480 while(watch != NULL) {
481 if (watch->event == evt) {
482 /* found: remove it */
483 if (watch->activity != 0) {
485 if (watch->activity == 0 && listener->itf->remove != NULL)
486 listener->itf->remove(listener->closure, evt->name, evt->id);
488 pthread_mutex_unlock(&listener->mutex);
491 watch = watch->next_by_listener;
493 pthread_mutex_unlock(&listener->mutex);