2 * Copyright (C) 2015, 2016 "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);
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
105 /* head of the list of listeners */
106 static struct afb_evt_listener *listeners = NULL;
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;
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.
118 static int evt_broadcast(struct afb_evt_event *evt, struct json_object *object)
120 return afb_evt_broadcast(evt->name, object);
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.
128 int afb_evt_broadcast(const char *event, struct json_object *object)
131 struct afb_evt_listener *listener;
134 listener = listeners;
136 if (listener->itf->broadcast != NULL) {
137 listener->itf->broadcast(listener->closure, event, 0, json_object_get(object));
140 listener = listener->next;
142 json_object_put(object);
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.
151 static int evt_push(struct afb_evt_event *evt, struct json_object *obj)
154 struct afb_evt_watch *watch;
155 struct afb_evt_listener *listener;
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;
167 json_object_put(obj);
174 static void remove_watch(struct afb_evt_watch *watch)
176 struct afb_evt_watch **prv;
177 struct afb_evt_event *evt;
178 struct afb_evt_listener *listener;
180 /* notify listener if needed */
182 listener = watch->listener;
183 if (watch->activity != 0 && listener->itf->remove != NULL)
184 listener->itf->remove(listener->closure, evt->name, evt->id);
186 /* unlink the watch for its event */
189 prv = &(*prv)->next_by_event;
190 *prv = watch->next_by_event;
192 /* unlink the watch for its listener */
193 prv = &listener->watchs;
195 prv = &(*prv)->next_by_listener;
196 *prv = watch->next_by_listener;
203 * Destroys the event 'evt'
205 static void evt_destroy(struct afb_evt_event *evt)
207 struct afb_evt_event **prv;
209 /* removes the event if valid! */
211 while (*prv != NULL) {
218 /* removes all watchers */
219 while(evt->watchs != NULL)
220 remove_watch(evt->watchs);
231 * Creates an event of 'name' and returns it.
232 * Returns an event with closure==NULL in case of error.
234 struct afb_event afb_evt_create_event(const char *name)
237 struct afb_evt_event *evt;
239 /* allocates the id */
241 if (++event_id_counter < 0) {
242 event_id_wrapped = 1;
243 event_id_counter = 1024; /* heuristic: small numbers are not destroyed */
245 if (!event_id_wrapped)
248 while(evt != NULL && evt->id != event_id_counter)
250 } while (evt != NULL);
252 /* allocates the event */
254 evt = malloc(len + sizeof * evt);
258 /* initialize the event */
261 evt->id = event_id_counter;
263 memcpy(evt->name, name, len + 1);
266 /* returns the event */
267 return (struct afb_event){ .itf = &afb_evt_event_itf, .closure = evt };
269 return (struct afb_event){ .itf = NULL, .closure = NULL };
273 * Returns the name of the 'event'
275 const char *afb_evt_event_name(struct afb_event event)
277 return (event.itf != &afb_evt_event_itf) ? NULL : ((struct afb_evt_event *)event.closure)->name;
281 * Returns the id of the 'event'
283 int afb_evt_event_id(struct afb_event event)
285 return (event.itf != &afb_evt_event_itf) ? 0 : ((struct afb_evt_event *)event.closure)->id;
289 * Returns an instance of the listener defined by the 'send' callback
291 * Returns NULL in case of memory depletion.
293 struct afb_evt_listener *afb_evt_listener_create(const struct afb_evt_itf *itf, void *closure)
295 struct afb_evt_listener *listener;
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;
306 listener = calloc(1, sizeof *listener);
307 if (listener != NULL) {
309 listener->next = listeners;
311 listener->closure = closure;
312 listener->watchs = NULL;
313 listener->refcount = 1;
314 listeners = listener;
320 * Increases the reference count of 'listener' and returns it
322 struct afb_evt_listener *afb_evt_listener_addref(struct afb_evt_listener *listener)
324 listener->refcount++;
329 * Decreases the reference count of the 'listener' and destroys it
332 void afb_evt_listener_unref(struct afb_evt_listener *listener)
334 if (0 == --listener->refcount) {
335 struct afb_evt_listener **prv;
337 /* remove the watchers */
338 while (listener->watchs != NULL)
339 remove_watch(listener->watchs);
341 /* unlink the listener */
343 while (*prv != listener)
345 *prv = listener->next;
347 /* free the listener */
353 * Makes the 'listener' watching 'event'
354 * Returns 0 in case of success or else -1.
356 int afb_evt_add_watch(struct afb_evt_listener *listener, struct afb_event event)
358 struct afb_evt_watch *watch;
359 struct afb_evt_event *evt;
361 /* check parameter */
362 if (event.itf != &afb_evt_event_itf || listener->itf->push == NULL) {
367 /* search the existing watch for the listener */
369 watch = listener->watchs;
370 while(watch != NULL) {
371 if (watch->event == evt)
373 watch = watch->next_by_listener;
376 /* not found, allocate a new */
377 watch = malloc(sizeof *watch);
383 /* initialise and link */
385 watch->next_by_event = evt->watchs;
386 watch->listener = listener;
387 watch->next_by_listener = listener->watchs;
390 listener->watchs = watch;
393 if (watch->activity == 0 && listener->itf->add != NULL)
394 listener->itf->add(listener->closure, evt->name, evt->id);
401 * Avoids the 'listener' to watch 'event'
402 * Returns 0 in case of success or else -1.
404 int afb_evt_remove_watch(struct afb_evt_listener *listener, struct afb_event event)
406 struct afb_evt_watch *watch;
407 struct afb_evt_event *evt;
409 /* check parameter */
410 if (event.itf != &afb_evt_event_itf) {
415 /* search the existing watch */
417 watch = listener->watchs;
418 while(watch != NULL) {
419 if (watch->event == evt) {
420 /* found: remove it */
421 if (watch->activity != 0) {
423 if (watch->activity == 0 && listener->itf->remove != NULL)
424 listener->itf->remove(listener->closure, evt->name, evt->id);
428 watch = watch->next_by_listener;