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 /* callback on event */
42 void (*send)(void *closure, const char *event, struct json_object *object);
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 {
59 /* head of the list of listeners watching the event */
60 struct afb_evt_watch *watchs;
62 /* name of the event */
67 * Structure for associating events and listeners
69 struct afb_evt_watch {
72 struct afb_evt_event *event;
74 /* link to the next listener for the same event */
75 struct afb_evt_watch *next_by_event;
78 struct afb_evt_listener *listener;
80 /* link to the next event for the same listener */
81 struct afb_evt_watch *next_by_listener;
84 /* declare functions */
85 static int evt_broadcast(struct afb_evt_event *evt, struct json_object *obj);
86 static int evt_push(struct afb_evt_event *evt, struct json_object *obj);
87 static void evt_destroy(struct afb_evt_event *evt);
89 /* the interface for events */
90 static struct afb_event_itf afb_evt_event_itf = {
91 .broadcast = (void*)evt_broadcast,
92 .push = (void*)evt_push,
93 .drop = (void*)evt_destroy
96 /* head of the list of listeners */
97 static struct afb_evt_listener *listeners = NULL;
100 * Broadcasts the event 'evt' with its 'object'
101 * 'object' is released (like json_object_put)
102 * Returns the count of listener that received the event.
104 static int evt_broadcast(struct afb_evt_event *evt, struct json_object *object)
106 return afb_evt_broadcast(evt->name, object);
110 * Broadcasts the 'event' with its 'object'
111 * 'object' is released (like json_object_put)
112 * Returns the count of listener having receive the event.
114 int afb_evt_broadcast(const char *event, struct json_object *object)
117 struct afb_evt_listener *listener;
120 listener = listeners;
122 listener->send(listener->closure, event, json_object_get(object));
123 listener = listener->next;
126 json_object_put(object);
131 * Broadcasts the event 'evt' with its 'object'
132 * 'object' is released (like json_object_put)
133 * Returns the count of listener taht received the event.
135 static int evt_push(struct afb_evt_event *evt, struct json_object *obj)
138 struct afb_evt_watch *watch;
139 struct afb_evt_listener *listener;
144 listener = watch->listener;
145 listener->send(listener->closure, evt->name, json_object_get(obj));
146 watch = watch->next_by_event;
149 json_object_put(obj);
156 static void remove_watch(struct afb_evt_watch *watch)
158 struct afb_evt_watch **prv;
160 /* unlink the watch for its event */
161 prv = &watch->event->watchs;
163 prv = &(*prv)->next_by_event;
164 *prv = watch->next_by_event;
166 /* unlink the watch for its listener */
167 prv = &watch->listener->watchs;
169 prv = &(*prv)->next_by_listener;
170 *prv = watch->next_by_listener;
177 * Destroys the event 'evt'
179 static void evt_destroy(struct afb_evt_event *evt)
182 /* removes all watchers */
183 while(evt->watchs != NULL)
184 remove_watch(evt->watchs);
190 * Creates an event of 'name' and returns it.
191 * Returns an event with closure==NULL in case of error.
193 struct afb_event afb_evt_create_event(const char *name)
196 struct afb_evt_event *evt;
199 evt = malloc(len + sizeof * evt);
202 memcpy(evt->name, name, len + 1);
204 return (struct afb_event){ .itf = &afb_evt_event_itf, .closure = evt };
208 * Returns an instance of the listener defined by the 'send' callback
210 * Returns NULL in case of memory depletion.
212 struct afb_evt_listener *afb_evt_listener_create(void (*send)(void *closure, const char *event, struct json_object *object), void *closure)
214 struct afb_evt_listener *listener;
216 /* search if an instance already exists */
217 listener = listeners;
218 while (listener != NULL) {
219 if (listener->send == send && listener->closure == closure)
220 return afb_evt_listener_addref(listener);
221 listener = listener->next;
225 listener = calloc(1, sizeof *listener);
226 if (listener != NULL) {
228 listener->next = listeners;
229 listener->send = send;
230 listener->closure = closure;
231 listener->watchs = NULL;
232 listener->refcount = 1;
233 listeners = listener;
239 * Increases the reference count of 'listener' and returns it
241 struct afb_evt_listener *afb_evt_listener_addref(struct afb_evt_listener *listener)
243 listener->refcount++;
248 * Decreases the reference count of the 'listener' and destroys it
251 void afb_evt_listener_unref(struct afb_evt_listener *listener)
253 if (0 == --listener->refcount) {
254 struct afb_evt_listener **prv;
256 /* remove the watchers */
257 while (listener->watchs != NULL)
258 remove_watch(listener->watchs);
260 /* unlink the listener */
262 while (*prv != listener)
264 *prv = listener->next;
266 /* free the listener */
272 * Makes the 'listener' watching 'event'
273 * Returns 0 in case of success or else -1.
275 int afb_evt_add_watch(struct afb_evt_listener *listener, struct afb_event event)
277 struct afb_evt_watch *watch;
278 struct afb_evt_event *evt;
280 /* check parameter */
281 if (event.itf != &afb_evt_event_itf) {
286 /* search the existing watch */
287 watch = listener->watchs;
288 while(watch != NULL) {
289 if (watch->event == event.closure)
291 watch = watch->next_by_listener;
294 /* not found, allocate a new */
295 watch = malloc(sizeof *watch);
301 /* initialise and link */
304 watch->next_by_event = evt->watchs;
305 watch->listener = listener;
306 watch->next_by_listener = listener->watchs;
308 listener->watchs = watch;
314 * Avoids the 'listener' to watch 'event'
315 * Returns 0 in case of success or else -1.
317 int afb_evt_remove_watch(struct afb_evt_listener *listener, struct afb_event event)
319 struct afb_evt_watch *watch;
321 /* check parameter */
322 if (event.itf != &afb_evt_event_itf) {
327 /* search the existing watch */
328 watch = listener->watchs;
329 while(watch != NULL) {
330 if (watch->event == event.closure) {
331 /* found: remove it */
335 watch = watch->next_by_listener;