event: add ability to get the event name
[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 static const char *evt_name(struct afb_evt_event *evt);
98
99 /* the interface for events */
100 static struct afb_event_itf afb_evt_event_itf = {
101         .broadcast = (void*)evt_broadcast,
102         .push = (void*)evt_push,
103         .drop = (void*)evt_destroy,
104         .name = (void*)evt_name
105 };
106
107 /* head of the list of listeners */
108 static struct afb_evt_listener *listeners = NULL;
109
110 /* handling id of events */
111 static struct afb_evt_event *events = NULL;
112 static int event_id_counter = 0;
113 static int event_id_wrapped = 0;
114
115 /*
116  * Broadcasts the event 'evt' with its 'object'
117  * 'object' is released (like json_object_put)
118  * Returns the count of listener that received the event.
119  */
120 static int evt_broadcast(struct afb_evt_event *evt, struct json_object *object)
121 {
122         return afb_evt_broadcast(evt->name, object);
123 }
124
125 /*
126  * Broadcasts the 'event' with its 'object'
127  * 'object' is released (like json_object_put)
128  * Returns the count of listener having receive the event.
129  */
130 int afb_evt_broadcast(const char *event, struct json_object *object)
131 {
132         int result;
133         struct afb_evt_listener *listener;
134
135         result = 0;
136         listener = listeners;
137         while(listener) {
138                 if (listener->itf->broadcast != NULL) {
139                         listener->itf->broadcast(listener->closure, event, 0, json_object_get(object));
140                         result++;
141                 }
142                 listener = listener->next;
143         }
144         json_object_put(object);
145         return result;
146 }
147
148 /*
149  * Pushes the event 'evt' with 'obj' to its listeners
150  * 'obj' is released (like json_object_put)
151  * Returns the count of listener taht received the event.
152  */
153 static int evt_push(struct afb_evt_event *evt, struct json_object *obj)
154 {
155         int result;
156         struct afb_evt_watch *watch;
157         struct afb_evt_listener *listener;
158
159         result = 0;
160         watch = evt->watchs;
161         while(watch) {
162                 listener = watch->listener;
163                 assert(listener->itf->push != NULL);
164                 if (watch->activity != 0)
165                         listener->itf->push(listener->closure, evt->name, evt->id, json_object_get(obj));
166                 watch = watch->next_by_event;
167                 result++;
168         }
169         json_object_put(obj);
170         return result;
171 }
172
173 /*
174  * Returns the name associated to the event 'evt'.
175  */
176 static const char *evt_name(struct afb_evt_event *evt)
177 {
178         return evt->name;
179 }
180
181 /*
182  * remove the 'watch'
183  */
184 static void remove_watch(struct afb_evt_watch *watch)
185 {
186         struct afb_evt_watch **prv;
187         struct afb_evt_event *evt;
188         struct afb_evt_listener *listener;
189
190         /* notify listener if needed */
191         evt = watch->event;
192         listener = watch->listener;
193         if (watch->activity != 0 && listener->itf->remove != NULL)
194                 listener->itf->remove(listener->closure, evt->name, evt->id);
195
196         /* unlink the watch for its event */
197         prv = &evt->watchs;
198         while(*prv != watch)
199                 prv = &(*prv)->next_by_event;
200         *prv = watch->next_by_event;
201
202         /* unlink the watch for its listener */
203         prv = &listener->watchs;
204         while(*prv != watch)
205                 prv = &(*prv)->next_by_listener;
206         *prv = watch->next_by_listener;
207
208         /* recycle memory */
209         free(watch);
210 }
211
212 /*
213  * Destroys the event 'evt'
214  */
215 static void evt_destroy(struct afb_evt_event *evt)
216 {
217         struct afb_evt_event **prv;
218         if (evt != NULL) {
219                 /* removes the event if valid! */
220                 prv = &events;
221                 while (*prv != NULL) {
222                         if (*prv != evt)
223                                 prv = &(*prv)->next;
224                         else {
225                                 /* valid, unlink */
226                                 *prv = evt->next;
227
228                                 /* removes all watchers */
229                                 while(evt->watchs != NULL)
230                                         remove_watch(evt->watchs);
231
232                                 /* free */
233                                 free(evt);
234                                 break;
235                         }
236                 }
237         }
238 }
239
240 /*
241  * Creates an event of 'name' and returns it.
242  * Returns an event with closure==NULL in case of error.
243  */
244 struct afb_event afb_evt_create_event(const char *name)
245 {
246         size_t len;
247         struct afb_evt_event *evt;
248
249         /* allocates the id */
250         do {
251                 if (++event_id_counter < 0) {
252                         event_id_wrapped = 1;
253                         event_id_counter = 1024; /* heuristic: small numbers are not destroyed */
254                 }
255                 if (!event_id_wrapped)
256                         break;
257                 evt = events;
258                 while(evt != NULL && evt->id != event_id_counter)
259                         evt = evt->next;
260         } while (evt != NULL);
261
262         /* allocates the event */
263         len = strlen(name);
264         evt = malloc(len + sizeof * evt);
265         if (evt == NULL)
266                 goto error;
267
268         /* initialize the event */
269         evt->next = events;
270         evt->watchs = NULL;
271         evt->id = event_id_counter;
272         assert(evt->id > 0);
273         memcpy(evt->name, name, len + 1);
274         events = evt;
275
276         /* returns the event */
277         return (struct afb_event){ .itf = &afb_evt_event_itf, .closure = evt };
278 error:
279         return (struct afb_event){ .itf = NULL, .closure = NULL };
280 }
281
282 /*
283  * Returns the name of the 'event'
284  */
285 const char *afb_evt_event_name(struct afb_event event)
286 {
287         return (event.itf != &afb_evt_event_itf) ? NULL : ((struct afb_evt_event *)event.closure)->name;
288 }
289
290 /*
291  * Returns the id of the 'event'
292  */
293 int afb_evt_event_id(struct afb_event event)
294 {
295         return (event.itf != &afb_evt_event_itf) ? 0 : ((struct afb_evt_event *)event.closure)->id;
296 }
297
298 /*
299  * Returns an instance of the listener defined by the 'send' callback
300  * and the 'closure'.
301  * Returns NULL in case of memory depletion.
302  */
303 struct afb_evt_listener *afb_evt_listener_create(const struct afb_evt_itf *itf, void *closure)
304 {
305         struct afb_evt_listener *listener;
306
307         /* search if an instance already exists */
308         listener = listeners;
309         while (listener != NULL) {
310                 if (listener->itf == itf && listener->closure == closure)
311                         return afb_evt_listener_addref(listener);
312                 listener = listener->next;
313         }
314
315         /* allocates */
316         listener = calloc(1, sizeof *listener);
317         if (listener != NULL) {
318                 /* init */
319                 listener->next = listeners;
320                 listener->itf = itf;
321                 listener->closure = closure;
322                 listener->watchs = NULL;
323                 listener->refcount = 1;
324                 listeners = listener;
325         }
326         return listener;
327 }
328
329 /*
330  * Increases the reference count of 'listener' and returns it
331  */
332 struct afb_evt_listener *afb_evt_listener_addref(struct afb_evt_listener *listener)
333 {
334         listener->refcount++;
335         return listener;
336 }
337
338 /*
339  * Decreases the reference count of the 'listener' and destroys it
340  * when no more used.
341  */
342 void afb_evt_listener_unref(struct afb_evt_listener *listener)
343 {
344         if (0 == --listener->refcount) {
345                 struct afb_evt_listener **prv;
346
347                 /* remove the watchers */
348                 while (listener->watchs != NULL)
349                         remove_watch(listener->watchs);
350
351                 /* unlink the listener */
352                 prv = &listeners;
353                 while (*prv != listener)
354                         prv = &(*prv)->next;
355                 *prv = listener->next;
356
357                 /* free the listener */
358                 free(listener);
359         }
360 }
361
362 /*
363  * Makes the 'listener' watching 'event'
364  * Returns 0 in case of success or else -1.
365  */
366 int afb_evt_add_watch(struct afb_evt_listener *listener, struct afb_event event)
367 {
368         struct afb_evt_watch *watch;
369         struct afb_evt_event *evt;
370
371         /* check parameter */
372         if (event.itf != &afb_evt_event_itf || listener->itf->push == NULL) {
373                 errno = EINVAL;
374                 return -1;
375         }
376
377         /* search the existing watch for the listener */
378         evt = event.closure;
379         watch = listener->watchs;
380         while(watch != NULL) {
381                 if (watch->event == evt)
382                         goto found;
383                 watch = watch->next_by_listener;
384         }
385
386         /* not found, allocate a new */
387         watch = malloc(sizeof *watch);
388         if (watch == NULL) {
389                 errno = ENOMEM;
390                 return -1;
391         }
392
393         /* initialise and link */
394         watch->event = evt;
395         watch->next_by_event = evt->watchs;
396         watch->listener = listener;
397         watch->next_by_listener = listener->watchs;
398         watch->activity = 0;
399         evt->watchs = watch;
400         listener->watchs = watch;
401
402 found:
403         if (watch->activity == 0 && listener->itf->add != NULL)
404                 listener->itf->add(listener->closure, evt->name, evt->id);
405         watch->activity++;
406
407         return 0;
408 }
409
410 /*
411  * Avoids the 'listener' to watch 'event'
412  * Returns 0 in case of success or else -1.
413  */
414 int afb_evt_remove_watch(struct afb_evt_listener *listener, struct afb_event event)
415 {
416         struct afb_evt_watch *watch;
417         struct afb_evt_event *evt;
418
419         /* check parameter */
420         if (event.itf != &afb_evt_event_itf) {
421                 errno = EINVAL;
422                 return -1;
423         }
424
425         /* search the existing watch */
426         evt = event.closure;
427         watch = listener->watchs;
428         while(watch != NULL) {
429                 if (watch->event == evt) {
430                         /* found: remove it */
431                         if (watch->activity != 0) {
432                                 watch->activity--;
433                                 if (watch->activity == 0 && listener->itf->remove != NULL)
434                                         listener->itf->remove(listener->closure, evt->name, evt->id);
435                         }
436                         return 0;
437                 }
438                 watch = watch->next_by_listener;
439         }
440         errno = ENOENT;
441         return -1;
442 }
443