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.
26 #include <json-c/json.h>
27 #include <afb/afb-event-itf.h>
34 * Structure for event listeners
36 struct afb_evt_listener {
38 /* chaining listeners */
39 struct afb_evt_listener *next;
41 /* interface for callbacks */
42 const struct afb_evt_itf *itf;
44 /* closure for the callback */
47 /* head of the list of events listened */
48 struct afb_evt_watch *watchs;
50 /* count of reference to the listener */
55 * Structure for describing events
57 struct afb_evt_event {
60 struct afb_evt_event *next;
62 /* head of the list of listeners watching the event */
63 struct afb_evt_watch *watchs;
68 /* name of the event */
73 * Structure for associating events and listeners
75 struct afb_evt_watch {
78 struct afb_evt_event *event;
80 /* link to the next listener for the same event */
81 struct afb_evt_watch *next_by_event;
84 struct afb_evt_listener *listener;
86 /* link to the next event for the same listener */
87 struct afb_evt_watch *next_by_listener;
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);
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
107 /* head of the list of listeners */
108 static struct afb_evt_listener *listeners = NULL;
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;
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.
120 static int evt_broadcast(struct afb_evt_event *evt, struct json_object *object)
122 return afb_evt_broadcast(evt->name, object);
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.
130 int afb_evt_broadcast(const char *event, struct json_object *object)
133 struct afb_evt_listener *listener;
136 listener = listeners;
138 if (listener->itf->broadcast != NULL) {
139 listener->itf->broadcast(listener->closure, event, 0, json_object_get(object));
142 listener = listener->next;
144 json_object_put(object);
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.
153 static int evt_push(struct afb_evt_event *evt, struct json_object *obj)
156 struct afb_evt_watch *watch;
157 struct afb_evt_listener *listener;
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;
169 json_object_put(obj);
174 * Returns the name associated to the event 'evt'.
176 static const char *evt_name(struct afb_evt_event *evt)
184 static void remove_watch(struct afb_evt_watch *watch)
186 struct afb_evt_watch **prv;
187 struct afb_evt_event *evt;
188 struct afb_evt_listener *listener;
190 /* notify listener if needed */
192 listener = watch->listener;
193 if (watch->activity != 0 && listener->itf->remove != NULL)
194 listener->itf->remove(listener->closure, evt->name, evt->id);
196 /* unlink the watch for its event */
199 prv = &(*prv)->next_by_event;
200 *prv = watch->next_by_event;
202 /* unlink the watch for its listener */
203 prv = &listener->watchs;
205 prv = &(*prv)->next_by_listener;
206 *prv = watch->next_by_listener;
213 * Destroys the event 'evt'
215 static void evt_destroy(struct afb_evt_event *evt)
217 struct afb_evt_event **prv;
219 /* removes the event if valid! */
221 while (*prv != NULL) {
228 /* removes all watchers */
229 while(evt->watchs != NULL)
230 remove_watch(evt->watchs);
241 * Creates an event of 'name' and returns it.
242 * Returns an event with closure==NULL in case of error.
244 struct afb_event afb_evt_create_event(const char *name)
247 struct afb_evt_event *evt;
249 /* allocates the id */
251 if (++event_id_counter < 0) {
252 event_id_wrapped = 1;
253 event_id_counter = 1024; /* heuristic: small numbers are not destroyed */
255 if (!event_id_wrapped)
258 while(evt != NULL && evt->id != event_id_counter)
260 } while (evt != NULL);
262 /* allocates the event */
264 evt = malloc(len + sizeof * evt);
268 /* initialize the event */
271 evt->id = event_id_counter;
273 memcpy(evt->name, name, len + 1);
276 /* returns the event */
277 return (struct afb_event){ .itf = &afb_evt_event_itf, .closure = evt };
279 return (struct afb_event){ .itf = NULL, .closure = NULL };
283 * Returns the name of the 'event'
285 const char *afb_evt_event_name(struct afb_event event)
287 return (event.itf != &afb_evt_event_itf) ? NULL : ((struct afb_evt_event *)event.closure)->name;
291 * Returns the id of the 'event'
293 int afb_evt_event_id(struct afb_event event)
295 return (event.itf != &afb_evt_event_itf) ? 0 : ((struct afb_evt_event *)event.closure)->id;
299 * Returns an instance of the listener defined by the 'send' callback
301 * Returns NULL in case of memory depletion.
303 struct afb_evt_listener *afb_evt_listener_create(const struct afb_evt_itf *itf, void *closure)
305 struct afb_evt_listener *listener;
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;
316 listener = calloc(1, sizeof *listener);
317 if (listener != NULL) {
319 listener->next = listeners;
321 listener->closure = closure;
322 listener->watchs = NULL;
323 listener->refcount = 1;
324 listeners = listener;
330 * Increases the reference count of 'listener' and returns it
332 struct afb_evt_listener *afb_evt_listener_addref(struct afb_evt_listener *listener)
334 listener->refcount++;
339 * Decreases the reference count of the 'listener' and destroys it
342 void afb_evt_listener_unref(struct afb_evt_listener *listener)
344 if (0 == --listener->refcount) {
345 struct afb_evt_listener **prv;
347 /* remove the watchers */
348 while (listener->watchs != NULL)
349 remove_watch(listener->watchs);
351 /* unlink the listener */
353 while (*prv != listener)
355 *prv = listener->next;
357 /* free the listener */
363 * Makes the 'listener' watching 'event'
364 * Returns 0 in case of success or else -1.
366 int afb_evt_add_watch(struct afb_evt_listener *listener, struct afb_event event)
368 struct afb_evt_watch *watch;
369 struct afb_evt_event *evt;
371 /* check parameter */
372 if (event.itf != &afb_evt_event_itf || listener->itf->push == NULL) {
377 /* search the existing watch for the listener */
379 watch = listener->watchs;
380 while(watch != NULL) {
381 if (watch->event == evt)
383 watch = watch->next_by_listener;
386 /* not found, allocate a new */
387 watch = malloc(sizeof *watch);
393 /* initialise and link */
395 watch->next_by_event = evt->watchs;
396 watch->listener = listener;
397 watch->next_by_listener = listener->watchs;
400 listener->watchs = watch;
403 if (watch->activity == 0 && listener->itf->add != NULL)
404 listener->itf->add(listener->closure, evt->name, evt->id);
411 * Avoids the 'listener' to watch 'event'
412 * Returns 0 in case of success or else -1.
414 int afb_evt_remove_watch(struct afb_evt_listener *listener, struct afb_event event)
416 struct afb_evt_watch *watch;
417 struct afb_evt_event *evt;
419 /* check parameter */
420 if (event.itf != &afb_evt_event_itf) {
425 /* search the existing watch */
427 watch = listener->watchs;
428 while(watch != NULL) {
429 if (watch->event == evt) {
430 /* found: remove it */
431 if (watch->activity != 0) {
433 if (watch->activity == 0 && listener->itf->remove != NULL)
434 listener->itf->remove(listener->closure, evt->name, evt->id);
438 watch = watch->next_by_listener;