2 * Copyright (C) 2015-2018 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
26 #include <json-c/json.h>
27 #include <afb/afb-event-x2-itf.h>
28 #include <afb/afb-event-x1.h>
39 * Structure for event listeners
41 struct afb_evt_listener {
43 /* chaining listeners */
44 struct afb_evt_listener *next;
46 /* interface for callbacks */
47 const struct afb_evt_itf *itf;
49 /* closure for the callback */
52 /* head of the list of events listened */
53 struct afb_evt_watch *watchs;
55 /* rwlock of the listener */
56 pthread_rwlock_t rwlock;
58 /* count of reference to the listener */
63 * Structure for describing events
68 struct afb_event_x2 eventid;
71 struct afb_evtid *next;
73 /* head of the list of listeners watching the event */
74 struct afb_evt_watch *watchs;
76 /* rwlock of the event */
77 pthread_rwlock_t rwlock;
91 /* fullname of the event */
96 * Structure for associating events and listeners
98 struct afb_evt_watch {
101 struct afb_evtid *evtid;
103 /* link to the next watcher for the same evtid */
104 struct afb_evt_watch *next_by_evtid;
107 struct afb_evt_listener *listener;
109 /* link to the next watcher for the same listener */
110 struct afb_evt_watch *next_by_listener;
117 * structure for job of broadcasting events
121 /** object atached to the event */
122 struct json_object *object;
124 /** the uuid of the event */
130 /** name of the event to broadcast */
135 * structure for job of broadcasting or pushing events
139 /** the event to broadcast */
140 struct afb_evtid *evtid;
142 /** object atached to the event */
143 struct json_object *object;
146 /* the interface for events */
147 static struct afb_event_x2_itf afb_evt_event_x2_itf = {
148 .broadcast = (void*)afb_evt_evtid_broadcast,
149 .push = (void*)afb_evt_evtid_push,
150 .unref = (void*)afb_evt_evtid_unref,
151 .name = (void*)afb_evt_evtid_name,
152 .addref = (void*)afb_evt_evtid_addref
155 /* the interface for events */
156 static struct afb_event_x2_itf afb_evt_hooked_eventid_itf = {
157 .broadcast = (void*)afb_evt_evtid_hooked_broadcast,
158 .push = (void*)afb_evt_evtid_hooked_push,
159 .unref = (void*)afb_evt_evtid_hooked_unref,
160 .name = (void*)afb_evt_evtid_hooked_name,
161 .addref = (void*)afb_evt_evtid_hooked_addref
164 /* job groups for events push/broadcast */
165 #define BROADCAST_JOB_GROUP (&afb_evt_event_x2_itf)
166 #define PUSH_JOB_GROUP (&afb_evt_event_x2_itf)
168 /* head of the list of listeners */
169 static pthread_rwlock_t listeners_rwlock = PTHREAD_RWLOCK_INITIALIZER;
170 static struct afb_evt_listener *listeners = NULL;
172 /* handling id of events */
173 static pthread_rwlock_t events_rwlock = PTHREAD_RWLOCK_INITIALIZER;
174 static struct afb_evtid *evtids = NULL;
175 static int event_id_counter = 0;
176 static int event_id_wrapped = 0;
178 /* head of uniqueness of events */
179 #if !defined(EVENT_BROADCAST_HOP_MAX)
180 # define EVENT_BROADCAST_HOP_MAX 10
182 #if !defined(EVENT_BROADCAST_MEMORY_COUNT)
183 # define EVENT_BROADCAST_MEMORY_COUNT 8
186 #if EVENT_BROADCAST_MEMORY_COUNT
188 pthread_mutex_t mutex;
191 uuid_binary_t uuids[EVENT_BROADCAST_MEMORY_COUNT];
193 .mutex = PTHREAD_MUTEX_INITIALIZER,
200 * Create structure for job of broadcasting string 'event' with 'object'
201 * Returns the created structure or NULL if out of memory
203 static struct job_broadcast *make_job_broadcast(const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop)
205 size_t sz = 1 + strlen(event);
206 struct job_broadcast *jb = malloc(sz + sizeof *jb);
209 memcpy(jb->uuid, uuid, sizeof jb->uuid);
211 memcpy(jb->event, event, sz);
217 * Destroy structure 'jb' for job of broadcasting string events
219 static void destroy_job_broadcast(struct job_broadcast *jb)
221 json_object_put(jb->object);
226 * Create structure for job of broadcasting or pushing 'evtid' with 'object'
227 * Returns the created structure or NULL if out of memory
229 static struct job_evtid *make_job_evtid(struct afb_evtid *evtid, struct json_object *object)
231 struct job_evtid *je = malloc(sizeof *je);
233 je->evtid = afb_evt_evtid_addref(evtid);
240 * Destroy structure for job of broadcasting or pushing evtid
242 static void destroy_job_evtid(struct job_evtid *je)
244 afb_evt_evtid_unref(je->evtid);
245 json_object_put(je->object);
250 * Broadcasts the 'event' of 'id' with its 'object'
252 static void broadcast(struct job_broadcast *jb)
254 struct afb_evt_listener *listener;
256 pthread_rwlock_rdlock(&listeners_rwlock);
257 listener = listeners;
259 if (listener->itf->broadcast != NULL)
260 listener->itf->broadcast(listener->closure, jb->event, json_object_get(jb->object), jb->uuid, jb->hop);
261 listener = listener->next;
263 pthread_rwlock_unlock(&listeners_rwlock);
267 * Jobs callback for broadcasting string asynchronously
269 static void broadcast_job(int signum, void *closure)
271 struct job_broadcast *jb = closure;
275 destroy_job_broadcast(jb);
279 * Broadcasts the string 'event' with its 'object'
281 static int unhooked_broadcast(const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop)
283 uuid_binary_t local_uuid;
284 struct job_broadcast *jb;
286 #if EVENT_BROADCAST_MEMORY_COUNT
290 /* check if lately sent */
292 uuid_new_binary(local_uuid);
294 hop = EVENT_BROADCAST_HOP_MAX;
295 #if EVENT_BROADCAST_MEMORY_COUNT
296 pthread_mutex_lock(&uniqueness.mutex);
298 pthread_mutex_lock(&uniqueness.mutex);
299 iter = (int)uniqueness.base;
300 count = (int)uniqueness.count;
302 if (0 == memcmp(uuid, uniqueness.uuids[iter], sizeof(uuid_binary_t))) {
303 pthread_mutex_unlock(&uniqueness.mutex);
306 if (++iter == EVENT_BROADCAST_MEMORY_COUNT)
311 iter = (int)uniqueness.base;
312 if (uniqueness.count < EVENT_BROADCAST_MEMORY_COUNT)
313 iter += (int)(uniqueness.count++);
314 else if (++uniqueness.base == EVENT_BROADCAST_MEMORY_COUNT)
316 memcpy(uniqueness.uuids[iter], uuid, sizeof(uuid_binary_t));
317 pthread_mutex_unlock(&uniqueness.mutex);
322 /* create the structure for the job */
323 jb = make_job_broadcast(event, object, uuid, hop);
325 ERROR("Cant't create broadcast string job item for %s(%s)",
326 event, json_object_to_json_string(object));
327 json_object_put(object);
332 rc = jobs_queue(BROADCAST_JOB_GROUP, 0, broadcast_job, jb);
334 ERROR("cant't queue broadcast string job item for %s(%s)",
335 event, json_object_to_json_string(object));
336 destroy_job_broadcast(jb);
342 * Broadcasts the event 'evtid' with its 'object'
343 * 'object' is released (like json_object_put)
344 * Returns the count of listener that received the event.
346 int afb_evt_evtid_broadcast(struct afb_evtid *evtid, struct json_object *object)
348 return unhooked_broadcast(evtid->fullname, object, NULL, 0);
352 * Broadcasts the event 'evtid' with its 'object'
353 * 'object' is released (like json_object_put)
354 * Returns the count of listener that received the event.
356 int afb_evt_evtid_hooked_broadcast(struct afb_evtid *evtid, struct json_object *object)
360 json_object_get(object);
362 if (evtid->hookflags & afb_hook_flag_evt_broadcast_before)
363 afb_hook_evt_broadcast_before(evtid->fullname, evtid->id, object);
365 result = afb_evt_evtid_broadcast(evtid, object);
367 if (evtid->hookflags & afb_hook_flag_evt_broadcast_after)
368 afb_hook_evt_broadcast_after(evtid->fullname, evtid->id, object, result);
370 json_object_put(object);
375 int afb_evt_rebroadcast(const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop)
380 json_object_get(object);
381 afb_hook_evt_broadcast_before(event, 0, object);
384 result = unhooked_broadcast(event, object, uuid, hop);
387 afb_hook_evt_broadcast_after(event, 0, object, result);
388 json_object_put(object);
394 * Broadcasts the 'event' with its 'object'
395 * 'object' is released (like json_object_put)
396 * Returns the count of listener having receive the event.
398 int afb_evt_broadcast(const char *event, struct json_object *object)
400 return afb_evt_rebroadcast(event, object, NULL, 0);
404 * Pushes the event 'evtid' with 'obj' to its listeners
405 * Returns the count of listener that received the event.
407 static void push_evtid(struct afb_evtid *evtid, struct json_object *object)
410 struct afb_evt_watch *watch;
411 struct afb_evt_listener *listener;
414 pthread_rwlock_rdlock(&evtid->rwlock);
415 watch = evtid->watchs;
417 listener = watch->listener;
418 assert(listener->itf->push != NULL);
419 if (watch->activity != 0) {
420 listener->itf->push(listener->closure, evtid->fullname, evtid->id, json_object_get(object));
423 watch = watch->next_by_evtid;
425 evtid->has_client = has_client;
426 pthread_rwlock_unlock(&evtid->rwlock);
430 * Jobs callback for pushing evtid asynchronously
432 static void push_job_evtid(int signum, void *closure)
434 struct job_evtid *je = closure;
437 push_evtid(je->evtid, je->object);
438 destroy_job_evtid(je);
442 * Pushes the event 'evtid' with 'obj' to its listeners
443 * 'obj' is released (like json_object_put)
444 * Returns 1 if at least one listener exists or 0 if no listener exists or
445 * -1 in case of error and the event can't be delivered
447 int afb_evt_evtid_push(struct afb_evtid *evtid, struct json_object *object)
449 struct job_evtid *je;
452 je = make_job_evtid(evtid, object);
454 ERROR("Cant't create push evtid job item for %s(%s)",
455 evtid->fullname, json_object_to_json_string(object));
456 json_object_put(object);
460 rc = jobs_queue(PUSH_JOB_GROUP, 0, push_job_evtid, je);
462 rc = evtid->has_client;
464 ERROR("cant't queue push evtid job item for %s(%s)",
465 evtid->fullname, json_object_to_json_string(object));
466 destroy_job_evtid(je);
473 * Pushes the event 'evtid' with 'obj' to its listeners
474 * 'obj' is released (like json_object_put)
475 * Emits calls to hooks.
476 * Returns the count of listener taht received the event.
478 int afb_evt_evtid_hooked_push(struct afb_evtid *evtid, struct json_object *obj)
483 /* lease the object */
484 json_object_get(obj);
486 /* hook before push */
487 if (evtid->hookflags & afb_hook_flag_evt_push_before)
488 afb_hook_evt_push_before(evtid->fullname, evtid->id, obj);
491 result = afb_evt_evtid_push(evtid, obj);
493 /* hook after push */
494 if (evtid->hookflags & afb_hook_flag_evt_push_after)
495 afb_hook_evt_push_after(evtid->fullname, evtid->id, obj, result);
497 /* release the object */
498 json_object_put(obj);
505 static void remove_watch(struct afb_evt_watch *watch)
507 struct afb_evt_watch **prv;
508 struct afb_evtid *evtid;
509 struct afb_evt_listener *listener;
511 /* notify listener if needed */
512 evtid = watch->evtid;
513 listener = watch->listener;
514 if (watch->activity != 0 && listener->itf->remove != NULL)
515 listener->itf->remove(listener->closure, evtid->fullname, evtid->id);
517 /* unlink the watch for its event */
518 prv = &evtid->watchs;
520 prv = &(*prv)->next_by_evtid;
521 *prv = watch->next_by_evtid;
523 /* unlink the watch for its listener */
524 prv = &listener->watchs;
526 prv = &(*prv)->next_by_listener;
527 *prv = watch->next_by_listener;
534 * Creates an event of name 'fullname' and returns it or NULL on error.
536 struct afb_evtid *afb_evt_evtid_create(const char *fullname)
539 struct afb_evtid *evtid, *oevt;
541 /* allocates the event */
542 len = strlen(fullname);
543 evtid = malloc(len + 1 + sizeof * evtid);
547 /* allocates the id */
548 pthread_rwlock_wrlock(&events_rwlock);
550 if (++event_id_counter < 0) {
551 event_id_wrapped = 1;
552 event_id_counter = 1024; /* heuristic: small numbers are not destroyed */
554 if (!event_id_wrapped)
557 while(oevt != NULL && oevt->id != event_id_counter)
559 } while (oevt != NULL);
561 /* initialize the event */
562 memcpy(evtid->fullname, fullname, len + 1);
563 evtid->next = evtids;
565 evtid->watchs = NULL;
566 evtid->id = event_id_counter;
567 evtid->has_client = 0;
568 pthread_rwlock_init(&evtid->rwlock, NULL);
570 evtid->hookflags = afb_hook_flags_evt(evtid->fullname);
571 evtid->eventid.itf = evtid->hookflags ? &afb_evt_hooked_eventid_itf : &afb_evt_event_x2_itf;
572 if (evtid->hookflags & afb_hook_flag_evt_create)
573 afb_hook_evt_create(evtid->fullname, evtid->id);
574 pthread_rwlock_unlock(&events_rwlock);
576 /* returns the event */
583 * Creates an event of name 'prefix'/'name' and returns it or NULL on error.
585 struct afb_evtid *afb_evt_evtid_create2(const char *prefix, const char *name)
587 size_t prelen, postlen;
590 /* makes the event fullname */
591 prelen = strlen(prefix);
592 postlen = strlen(name);
593 fullname = alloca(prelen + postlen + 2);
594 memcpy(fullname, prefix, prelen);
595 fullname[prelen] = '/';
596 memcpy(fullname + prelen + 1, name, postlen + 1);
598 /* create the event */
599 return afb_evt_evtid_create(fullname);
603 * increment the reference count of the event 'evtid'
605 struct afb_evtid *afb_evt_evtid_addref(struct afb_evtid *evtid)
607 __atomic_add_fetch(&evtid->refcount, 1, __ATOMIC_RELAXED);
612 * increment the reference count of the event 'evtid'
614 struct afb_evtid *afb_evt_evtid_hooked_addref(struct afb_evtid *evtid)
616 if (evtid->hookflags & afb_hook_flag_evt_addref)
617 afb_hook_evt_addref(evtid->fullname, evtid->id);
618 return afb_evt_evtid_addref(evtid);
622 * decrement the reference count of the event 'evtid'
623 * and destroy it when the count reachs zero
625 void afb_evt_evtid_unref(struct afb_evtid *evtid)
628 struct afb_evtid **prv;
629 struct afb_evt_listener *listener;
631 if (!__atomic_sub_fetch(&evtid->refcount, 1, __ATOMIC_RELAXED)) {
632 /* unlinks the event if valid! */
633 pthread_rwlock_wrlock(&events_rwlock);
636 while (*prv && !(found = (*prv == evtid)))
640 pthread_rwlock_unlock(&events_rwlock);
642 /* destroys the event */
644 ERROR("event not found");
646 /* removes all watchers */
647 while(evtid->watchs != NULL) {
648 listener = evtid->watchs->listener;
649 pthread_rwlock_wrlock(&listener->rwlock);
650 pthread_rwlock_wrlock(&evtid->rwlock);
651 remove_watch(evtid->watchs);
652 pthread_rwlock_unlock(&evtid->rwlock);
653 pthread_rwlock_unlock(&listener->rwlock);
657 pthread_rwlock_destroy(&evtid->rwlock);
664 * decrement the reference count of the event 'evtid'
665 * and destroy it when the count reachs zero
667 void afb_evt_evtid_hooked_unref(struct afb_evtid *evtid)
669 if (evtid->hookflags & afb_hook_flag_evt_unref)
670 afb_hook_evt_unref(evtid->fullname, evtid->id);
671 afb_evt_evtid_unref(evtid);
675 * Returns the true name of the 'event'
677 const char *afb_evt_evtid_fullname(struct afb_evtid *evtid)
679 return evtid->fullname;
683 * Returns the name of the 'event'
685 const char *afb_evt_evtid_name(struct afb_evtid *evtid)
687 const char *name = strchr(evtid->fullname, '/');
688 return name ? name + 1 : evtid->fullname;
692 * Returns the name associated to the event 'evtid'.
694 const char *afb_evt_evtid_hooked_name(struct afb_evtid *evtid)
696 const char *result = afb_evt_evtid_name(evtid);
697 if (evtid->hookflags & afb_hook_flag_evt_name)
698 afb_hook_evt_name(evtid->fullname, evtid->id, result);
703 * Returns the id of the 'event'
705 int afb_evt_evtid_id(struct afb_evtid *evtid)
711 * Returns an instance of the listener defined by the 'send' callback
713 * Returns NULL in case of memory depletion.
715 struct afb_evt_listener *afb_evt_listener_create(const struct afb_evt_itf *itf, void *closure)
717 struct afb_evt_listener *listener;
719 /* search if an instance already exists */
720 pthread_rwlock_wrlock(&listeners_rwlock);
721 listener = listeners;
722 while (listener != NULL) {
723 if (listener->itf == itf && listener->closure == closure) {
724 listener = afb_evt_listener_addref(listener);
727 listener = listener->next;
731 listener = calloc(1, sizeof *listener);
732 if (listener != NULL) {
735 listener->closure = closure;
736 listener->watchs = NULL;
737 listener->refcount = 1;
738 pthread_rwlock_init(&listener->rwlock, NULL);
739 listener->next = listeners;
740 listeners = listener;
743 pthread_rwlock_unlock(&listeners_rwlock);
748 * Increases the reference count of 'listener' and returns it
750 struct afb_evt_listener *afb_evt_listener_addref(struct afb_evt_listener *listener)
752 __atomic_add_fetch(&listener->refcount, 1, __ATOMIC_RELAXED);
757 * Decreases the reference count of the 'listener' and destroys it
760 void afb_evt_listener_unref(struct afb_evt_listener *listener)
762 struct afb_evt_listener **prv;
763 struct afb_evtid *evtid;
765 if (listener && !__atomic_sub_fetch(&listener->refcount, 1, __ATOMIC_RELAXED)) {
767 /* unlink the listener */
768 pthread_rwlock_wrlock(&listeners_rwlock);
770 while (*prv != listener)
772 *prv = listener->next;
773 pthread_rwlock_unlock(&listeners_rwlock);
775 /* remove the watchers */
776 pthread_rwlock_wrlock(&listener->rwlock);
777 while (listener->watchs != NULL) {
778 evtid = listener->watchs->evtid;
779 pthread_rwlock_wrlock(&evtid->rwlock);
780 remove_watch(listener->watchs);
781 pthread_rwlock_unlock(&evtid->rwlock);
783 pthread_rwlock_unlock(&listener->rwlock);
785 /* free the listener */
786 pthread_rwlock_destroy(&listener->rwlock);
792 * Makes the 'listener' watching 'evtid'
793 * Returns 0 in case of success or else -1.
795 int afb_evt_watch_add_evtid(struct afb_evt_listener *listener, struct afb_evtid *evtid)
797 struct afb_evt_watch *watch;
799 /* check parameter */
800 if (listener->itf->push == NULL) {
805 /* search the existing watch for the listener */
806 pthread_rwlock_wrlock(&listener->rwlock);
807 watch = listener->watchs;
808 while(watch != NULL) {
809 if (watch->evtid == evtid)
811 watch = watch->next_by_listener;
814 /* not found, allocate a new */
815 watch = malloc(sizeof *watch);
817 pthread_rwlock_unlock(&listener->rwlock);
822 /* initialise and link */
823 watch->evtid = evtid;
825 watch->listener = listener;
826 watch->next_by_listener = listener->watchs;
827 listener->watchs = watch;
828 pthread_rwlock_wrlock(&evtid->rwlock);
829 watch->next_by_evtid = evtid->watchs;
830 evtid->watchs = watch;
831 pthread_rwlock_unlock(&evtid->rwlock);
834 if (watch->activity == 0 && listener->itf->add != NULL)
835 listener->itf->add(listener->closure, evtid->fullname, evtid->id);
837 evtid->has_client = 1;
838 pthread_rwlock_unlock(&listener->rwlock);
844 * Avoids the 'listener' to watch 'evtid'
845 * Returns 0 in case of success or else -1.
847 int afb_evt_watch_sub_evtid(struct afb_evt_listener *listener, struct afb_evtid *evtid)
849 struct afb_evt_watch *watch;
851 /* search the existing watch */
852 pthread_rwlock_wrlock(&listener->rwlock);
853 watch = listener->watchs;
854 while(watch != NULL) {
855 if (watch->evtid == evtid) {
856 if (watch->activity != 0) {
858 if (watch->activity == 0 && listener->itf->remove != NULL)
859 listener->itf->remove(listener->closure, evtid->fullname, evtid->id);
861 pthread_rwlock_unlock(&listener->rwlock);
864 watch = watch->next_by_listener;
866 pthread_rwlock_unlock(&listener->rwlock);
872 * update the hooks for events
874 void afb_evt_update_hooks()
876 struct afb_evtid *evtid;
878 pthread_rwlock_rdlock(&events_rwlock);
879 for (evtid = evtids ; evtid ; evtid = evtid->next) {
880 evtid->hookflags = afb_hook_flags_evt(evtid->fullname);
881 evtid->eventid.itf = evtid->hookflags ? &afb_evt_hooked_eventid_itf : &afb_evt_event_x2_itf;
883 pthread_rwlock_unlock(&events_rwlock);
886 inline struct afb_evtid *afb_evt_event_x2_to_evtid(struct afb_event_x2 *eventid)
888 return (struct afb_evtid*)eventid;
891 inline struct afb_event_x2 *afb_evt_event_x2_from_evtid(struct afb_evtid *evtid)
893 return &evtid->eventid;
897 * Creates an event of 'fullname' and returns it.
898 * Returns an event with closure==NULL in case of error.
900 struct afb_event_x2 *afb_evt_event_x2_create(const char *fullname)
902 return afb_evt_event_x2_from_evtid(afb_evt_evtid_create(fullname));
906 * Creates an event of name 'prefix'/'name' and returns it.
907 * Returns an event with closure==NULL in case of error.
909 struct afb_event_x2 *afb_evt_event_x2_create2(const char *prefix, const char *name)
911 return afb_evt_event_x2_from_evtid(afb_evt_evtid_create2(prefix, name));
915 * Returns the fullname of the 'eventid'
917 const char *afb_evt_event_x2_fullname(struct afb_event_x2 *eventid)
919 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
920 return evtid ? evtid->fullname : NULL;
924 * Returns the id of the 'eventid'
926 int afb_evt_event_x2_id(struct afb_event_x2 *eventid)
928 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
929 return evtid ? evtid->id : 0;
933 * Makes the 'listener' watching 'eventid'
934 * Returns 0 in case of success or else -1.
936 int afb_evt_event_x2_add_watch(struct afb_evt_listener *listener, struct afb_event_x2 *eventid)
938 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
940 /* check parameter */
946 /* search the existing watch for the listener */
947 return afb_evt_watch_add_evtid(listener, evtid);
951 * Avoids the 'listener' to watch 'eventid'
952 * Returns 0 in case of success or else -1.
954 int afb_evt_event_x2_remove_watch(struct afb_evt_listener *listener, struct afb_event_x2 *eventid)
956 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
958 /* check parameter */
964 /* search the existing watch */
965 return afb_evt_watch_sub_evtid(listener, evtid);
968 int afb_evt_event_x2_push(struct afb_event_x2 *eventid, struct json_object *object)
970 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
972 return afb_evt_evtid_hooked_push(evtid, object);
973 json_object_put(object);
977 int afb_evt_event_x2_unhooked_push(struct afb_event_x2 *eventid, struct json_object *object)
979 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
981 return afb_evt_evtid_push(evtid, object);
982 json_object_put(object);
986 struct afb_event_x1 afb_evt_event_from_evtid(struct afb_evtid *evtid)
989 ? (struct afb_event_x1){ .itf = &afb_evt_hooked_eventid_itf, .closure = &evtid->eventid }
990 : (struct afb_event_x1){ .itf = NULL, .closure = NULL };
993 void afb_evt_event_x2_unref(struct afb_event_x2 *eventid)
995 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
997 afb_evt_evtid_unref(evtid);
1000 struct afb_event_x2 *afb_evt_event_x2_addref(struct afb_event_x2 *eventid)
1002 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
1004 afb_evt_evtid_addref(evtid);