evt: handles broadcasting and tracking
[src/app-framework-binder.git] / src / afb-evt.c
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  * Author José Bollo <jose.bollo@iot.bzh>
5  *
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
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 #define _GNU_SOURCE
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <assert.h>
24 #include <errno.h>
25
26 #include <json-c/json.h>
27 #include <afb/afb-event-itf.h>
28
29 #include "afb-evt.h"
30
31 struct afb_evt_watch;
32
33 /*
34  * Structure for event listeners
35  */
36 struct afb_evt_listener {
37
38         /* chaining listeners */
39         struct afb_evt_listener *next;
40
41         /* interface for callbacks */
42         const struct afb_evt_itf *itf;
43
44         /* closure for the callback */
45         void *closure;
46
47         /* head of the list of events listened */
48         struct afb_evt_watch *watchs;
49
50         /* count of reference to the listener */
51         int refcount;
52 };
53
54 /*
55  * Structure for describing events
56  */
57 struct afb_evt_event {
58
59         /* next event */
60         struct afb_evt_event *next;
61
62         /* head of the list of listeners watching the event */
63         struct afb_evt_watch *watchs;
64
65         /* id of the event */
66         int id;
67
68         /* name of the event */
69         char name[1];
70 };
71
72 /*
73  * Structure for associating events and listeners
74  */
75 struct afb_evt_watch {
76
77         /* the event */
78         struct afb_evt_event *event;
79
80         /* link to the next listener for the same event */
81         struct afb_evt_watch *next_by_event;
82
83         /* the listener */
84         struct afb_evt_listener *listener;
85
86         /* link to the next event for the same listener */
87         struct afb_evt_watch *next_by_listener;
88
89         /* activity */
90         unsigned activity;
91 };
92
93 /* declare functions */
94 static int evt_broadcast(struct afb_evt_event *evt, struct json_object *obj);
95 static int evt_push(struct afb_evt_event *evt, struct json_object *obj);
96 static void evt_destroy(struct afb_evt_event *evt);
97
98 /* the interface for events */
99 static struct afb_event_itf afb_evt_event_itf = {
100         .broadcast = (void*)evt_broadcast,
101         .push = (void*)evt_push,
102         .drop = (void*)evt_destroy
103 };
104
105 /* head of the list of listeners */
106 static struct afb_evt_listener *listeners = NULL;
107
108 /* handling id of events */
109 static struct afb_evt_event *events = NULL;
110 static int event_id_counter = 0;
111 static int event_id_wrapped = 0;
112
113 /*
114  * Broadcasts the event 'evt' with its 'object'
115  * 'object' is released (like json_object_put)
116  * Returns the count of listener that received the event.
117  */
118 static int evt_broadcast(struct afb_evt_event *evt, struct json_object *object)
119 {
120         return afb_evt_broadcast(evt->name, object);
121 }
122
123 /*
124  * Broadcasts the 'event' with its 'object'
125  * 'object' is released (like json_object_put)
126  * Returns the count of listener having receive the event.
127  */
128 int afb_evt_broadcast(const char *event, struct json_object *object)
129 {
130         int result;
131         struct afb_evt_listener *listener;
132
133         result = 0;
134         listener = listeners;
135         while(listener) {
136                 if (listener->itf->broadcast != NULL) {
137                         listener->itf->broadcast(listener->closure, event, 0, json_object_get(object));
138                         result++;
139                 }
140                 listener = listener->next;
141         }
142         json_object_put(object);
143         return result;
144 }
145
146 /*
147  * Broadcasts the event 'evt' with its 'object'
148  * 'object' is released (like json_object_put)
149  * Returns the count of listener taht received the event.
150  */
151 static int evt_push(struct afb_evt_event *evt, struct json_object *obj)
152 {
153         int result;
154         struct afb_evt_watch *watch;
155         struct afb_evt_listener *listener;
156
157         result = 0;
158         watch = evt->watchs;
159         while(watch) {
160                 listener = watch->listener;
161                 assert(listener->itf->push != NULL);
162                 if (watch->activity != 0)
163                         listener->itf->push(listener->closure, evt->name, evt->id, json_object_get(obj));
164                 watch = watch->next_by_event;
165                 result++;
166         }
167         json_object_put(obj);
168         return result;
169 }
170
171 /*
172  * remove the 'watch'
173  */
174 static void remove_watch(struct afb_evt_watch *watch)
175 {
176         struct afb_evt_watch **prv;
177         struct afb_evt_event *evt;
178         struct afb_evt_listener *listener;
179
180         /* notify listener if needed */
181         evt = watch->event;
182         listener = watch->listener;
183         if (watch->activity != 0 && listener->itf->remove != NULL)
184                 listener->itf->remove(listener->closure, evt->name, evt->id);
185
186         /* unlink the watch for its event */
187         prv = &evt->watchs;
188         while(*prv != watch)
189                 prv = &(*prv)->next_by_event;
190         *prv = watch->next_by_event;
191
192         /* unlink the watch for its listener */
193         prv = &listener->watchs;
194         while(*prv != watch)
195                 prv = &(*prv)->next_by_listener;
196         *prv = watch->next_by_listener;
197
198         /* recycle memory */
199         free(watch);
200 }
201
202 /*
203  * Destroys the event 'evt'
204  */
205 static void evt_destroy(struct afb_evt_event *evt)
206 {
207         struct afb_evt_event **prv;
208         if (evt != NULL) {
209                 /* removes the event if valid! */
210                 prv = &events;
211                 while (*prv != NULL) {
212                         if (*prv != evt)
213                                 prv = &(*prv)->next;
214                         else {
215                                 /* valid, unlink */
216                                 *prv = evt->next;
217
218                                 /* removes all watchers */
219                                 while(evt->watchs != NULL)
220                                         remove_watch(evt->watchs);
221
222                                 /* free */
223                                 free(evt);
224                                 break;
225                         }
226                 }
227         }
228 }
229
230 /*
231  * Creates an event of 'name' and returns it.
232  * Returns an event with closure==NULL in case of error.
233  */
234 struct afb_event afb_evt_create_event(const char *name)
235 {
236         size_t len;
237         struct afb_evt_event *evt;
238
239         /* allocates the id */
240         do {
241                 if (++event_id_counter < 0) {
242                         event_id_wrapped = 1;
243                         event_id_counter = 1024; /* heuristic: small numbers are not destroyed */
244                 }
245                 if (!event_id_wrapped)
246                         break;
247                 evt = events;
248                 while(evt != NULL && evt->id != event_id_counter)
249                         evt = evt->next;
250         } while (evt != NULL);
251
252         /* allocates the event */
253         len = strlen(name);
254         evt = malloc(len + sizeof * evt);
255         if (evt == NULL)
256                 goto error;
257
258         /* initialize the event */
259         evt->next = events;
260         evt->watchs = NULL;
261         evt->id = event_id_counter;
262         assert(evt->id > 0);
263         memcpy(evt->name, name, len + 1);
264         events = evt;
265
266         /* returns the event */
267         return (struct afb_event){ .itf = &afb_evt_event_itf, .closure = evt };
268 error:
269         return (struct afb_event){ .itf = NULL, .closure = NULL };
270 }
271
272 /*
273  * Returns the name of the 'event'
274  */
275 const char *afb_evt_event_name(struct afb_event event)
276 {
277         return (event.itf != &afb_evt_event_itf) ? NULL : ((struct afb_evt_event *)event.closure)->name;
278 }
279
280 /*
281  * Returns the id of the 'event'
282  */
283 int afb_evt_event_id(struct afb_event event)
284 {
285         return (event.itf != &afb_evt_event_itf) ? 0 : ((struct afb_evt_event *)event.closure)->id;
286 }
287
288 /*
289  * Returns an instance of the listener defined by the 'send' callback
290  * and the 'closure'.
291  * Returns NULL in case of memory depletion.
292  */
293 struct afb_evt_listener *afb_evt_listener_create(const struct afb_evt_itf *itf, void *closure)
294 {
295         struct afb_evt_listener *listener;
296
297         /* search if an instance already exists */
298         listener = listeners;
299         while (listener != NULL) {
300                 if (listener->itf == itf && listener->closure == closure)
301                         return afb_evt_listener_addref(listener);
302                 listener = listener->next;
303         }
304
305         /* allocates */
306         listener = calloc(1, sizeof *listener);
307         if (listener != NULL) {
308                 /* init */
309                 listener->next = listeners;
310                 listener->itf = itf;
311                 listener->closure = closure;
312                 listener->watchs = NULL;
313                 listener->refcount = 1;
314                 listeners = listener;
315         }
316         return listener;
317 }
318
319 /*
320  * Increases the reference count of 'listener' and returns it
321  */
322 struct afb_evt_listener *afb_evt_listener_addref(struct afb_evt_listener *listener)
323 {
324         listener->refcount++;
325         return listener;
326 }
327
328 /*
329  * Decreases the reference count of the 'listener' and destroys it
330  * when no more used.
331  */
332 void afb_evt_listener_unref(struct afb_evt_listener *listener)
333 {
334         if (0 == --listener->refcount) {
335                 struct afb_evt_listener **prv;
336
337                 /* remove the watchers */
338                 while (listener->watchs != NULL)
339                         remove_watch(listener->watchs);
340
341                 /* unlink the listener */
342                 prv = &listeners;
343                 while (*prv != listener)
344                         prv = &(*prv)->next;
345                 *prv = listener->next;
346
347                 /* free the listener */
348                 free(listener);
349         }
350 }
351
352 /*
353  * Makes the 'listener' watching 'event'
354  * Returns 0 in case of success or else -1.
355  */
356 int afb_evt_add_watch(struct afb_evt_listener *listener, struct afb_event event)
357 {
358         struct afb_evt_watch *watch;
359         struct afb_evt_event *evt;
360
361         /* check parameter */
362         if (event.itf != &afb_evt_event_itf || listener->itf->push == NULL) {
363                 errno = EINVAL;
364                 return -1;
365         }
366
367         /* search the existing watch for the listener */
368         evt = event.closure;
369         watch = listener->watchs;
370         while(watch != NULL) {
371                 if (watch->event == evt)
372                         goto found;
373                 watch = watch->next_by_listener;
374         }
375
376         /* not found, allocate a new */
377         watch = malloc(sizeof *watch);
378         if (watch == NULL) {
379                 errno = ENOMEM;
380                 return -1;
381         }
382
383         /* initialise and link */
384         watch->event = evt;
385         watch->next_by_event = evt->watchs;
386         watch->listener = listener;
387         watch->next_by_listener = listener->watchs;
388         watch->activity = 0;
389         evt->watchs = watch;
390         listener->watchs = watch;
391
392 found:
393         if (watch->activity == 0 && listener->itf->add != NULL)
394                 listener->itf->add(listener->closure, evt->name, evt->id);
395         watch->activity++;
396
397         return 0;
398 }
399
400 /*
401  * Avoids the 'listener' to watch 'event'
402  * Returns 0 in case of success or else -1.
403  */
404 int afb_evt_remove_watch(struct afb_evt_listener *listener, struct afb_event event)
405 {
406         struct afb_evt_watch *watch;
407         struct afb_evt_event *evt;
408
409         /* check parameter */
410         if (event.itf != &afb_evt_event_itf) {
411                 errno = EINVAL;
412                 return -1;
413         }
414
415         /* search the existing watch */
416         evt = event.closure;
417         watch = listener->watchs;
418         while(watch != NULL) {
419                 if (watch->event == evt) {
420                         /* found: remove it */
421                         if (watch->activity != 0) {
422                                 watch->activity--;
423                                 if (watch->activity == 0 && listener->itf->remove != NULL)
424                                         listener->itf->remove(listener->closure, evt->name, evt->id);
425                         }
426                         return 0;
427                 }
428                 watch = watch->next_by_listener;
429         }
430         errno = ENOENT;
431         return -1;
432 }
433