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 the name of the 'event'
210 const char *afb_evt_event_name(struct afb_event event)
212 return (event.itf != &afb_evt_event_itf) ? NULL : ((struct afb_evt_event *)event.closure)->name;
216 * Returns an instance of the listener defined by the 'send' callback
218 * Returns NULL in case of memory depletion.
220 struct afb_evt_listener *afb_evt_listener_create(void (*send)(void *closure, const char *event, struct json_object *object), void *closure)
222 struct afb_evt_listener *listener;
224 /* search if an instance already exists */
225 listener = listeners;
226 while (listener != NULL) {
227 if (listener->send == send && listener->closure == closure)
228 return afb_evt_listener_addref(listener);
229 listener = listener->next;
233 listener = calloc(1, sizeof *listener);
234 if (listener != NULL) {
236 listener->next = listeners;
237 listener->send = send;
238 listener->closure = closure;
239 listener->watchs = NULL;
240 listener->refcount = 1;
241 listeners = listener;
247 * Increases the reference count of 'listener' and returns it
249 struct afb_evt_listener *afb_evt_listener_addref(struct afb_evt_listener *listener)
251 listener->refcount++;
256 * Decreases the reference count of the 'listener' and destroys it
259 void afb_evt_listener_unref(struct afb_evt_listener *listener)
261 if (0 == --listener->refcount) {
262 struct afb_evt_listener **prv;
264 /* remove the watchers */
265 while (listener->watchs != NULL)
266 remove_watch(listener->watchs);
268 /* unlink the listener */
270 while (*prv != listener)
272 *prv = listener->next;
274 /* free the listener */
280 * Makes the 'listener' watching 'event'
281 * Returns 0 in case of success or else -1.
283 int afb_evt_add_watch(struct afb_evt_listener *listener, struct afb_event event)
285 struct afb_evt_watch *watch;
286 struct afb_evt_event *evt;
288 /* check parameter */
289 if (event.itf != &afb_evt_event_itf) {
294 /* search the existing watch */
295 watch = listener->watchs;
296 while(watch != NULL) {
297 if (watch->event == event.closure)
299 watch = watch->next_by_listener;
302 /* not found, allocate a new */
303 watch = malloc(sizeof *watch);
309 /* initialise and link */
312 watch->next_by_event = evt->watchs;
313 watch->listener = listener;
314 watch->next_by_listener = listener->watchs;
316 listener->watchs = watch;
322 * Avoids the 'listener' to watch 'event'
323 * Returns 0 in case of success or else -1.
325 int afb_evt_remove_watch(struct afb_evt_listener *listener, struct afb_event event)
327 struct afb_evt_watch *watch;
329 /* check parameter */
330 if (event.itf != &afb_evt_event_itf) {
335 /* search the existing watch */
336 watch = listener->watchs;
337 while(watch != NULL) {
338 if (watch->event == event.closure) {
339 /* found: remove it */
343 watch = watch->next_by_listener;