};
/* functions for services */
-static void svc_on_event(struct afb_svc *svc, const char *event, int eventid, struct json_object *object);
-static void svc_call(struct afb_svc *svc, const char *api, const char *verb, struct json_object *args,
- void (*callback)(void*, int, struct json_object*), void *closure);
+static void svc_on_event(void *closure, const char *event, int eventid, struct json_object *object);
+static void svc_call(void *closure, const char *api, const char *verb, struct json_object *args,
+ void (*callback)(void*, int, struct json_object*), void *cbclosure);
/* the interface for services */
static const struct afb_service_itf service_itf = {
- .call = (void*)svc_call
+ .call = svc_call
};
/* the interface for events */
static const struct afb_evt_itf evt_itf = {
- .broadcast = (void*)svc_on_event,
- .push = (void*)svc_on_event
+ .broadcast = svc_on_event,
+ .push = svc_on_event
};
/* functions for requests of services */
static struct afb_session *common_session;
/*
- * Creates a new service
+ * Allocates a new service
*/
-struct afb_svc *afb_svc_create(int share_session, int (*init)(struct afb_service service), void (*on_event)(const char *event, struct json_object *object))
+static struct afb_svc *afb_svc_alloc(int share_session, void (*on_event)(const char *event, struct json_object *object))
{
- int rc;
struct afb_svc *svc;
/* allocates the svc handler */
goto error3;
}
+ return svc;
+
+error3:
+ afb_session_unref(svc->session);
+error2:
+ free(svc);
+error:
+ return NULL;
+}
+
+/*
+ * Creates a new service
+ */
+struct afb_svc *afb_svc_create(int share_session, int (*init)(struct afb_service service), void (*on_event)(const char *event, struct json_object *object))
+{
+ int rc;
+ struct afb_svc *svc;
+
+ /* allocates the svc handler */
+ svc = afb_svc_alloc(share_session, on_event);
+ if (svc == NULL)
+ goto error;
+
/* initialises the svc now */
rc = init((struct afb_service){ .itf = &service_itf, .closure = svc });
if (rc < 0)
- goto error4;
+ goto error2;
return svc;
-error4:
+error2:
if (svc->listener != NULL)
afb_evt_listener_unref(svc->listener);
-error3:
afb_session_unref(svc->session);
+ free(svc);
+error:
+ return NULL;
+}
+
+/*
+ * Creates a new service
+ */
+struct afb_svc *afb_svc_create_v2(
+ int share_session,
+ void (*on_event)(const char *event, struct json_object *object),
+ int (*start)(const struct afb_binding_interface *interface, struct afb_service service),
+ const struct afb_binding_interface *interface)
+{
+ int rc;
+ struct afb_svc *svc;
+
+ /* allocates the svc handler */
+ svc = afb_svc_alloc(share_session, on_event);
+ if (svc == NULL)
+ goto error;
+
+ /* initialises the svc now */
+ rc = start(interface, (struct afb_service){ .itf = &service_itf, .closure = svc });
+ if (rc < 0)
+ goto error2;
+
+ return svc;
+
error2:
+ if (svc->listener != NULL)
+ afb_evt_listener_unref(svc->listener);
+ afb_session_unref(svc->session);
free(svc);
error:
return NULL;
/*
* Propagates the event to the service
*/
-static void svc_on_event(struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
+static void svc_on_event(void *closure, const char *event, int eventid, struct json_object *object)
{
+ struct afb_svc *svc = closure;
svc->on_event(event, object);
json_object_put(object);
}
/*
* Initiates a call for the service
*/
-static void svc_call(struct afb_svc *svc, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *closure)
+static void svc_call(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cbclosure)
{
+ struct afb_svc *svc = closure;
struct svc_req *svcreq;
/* allocates the request */
svcreq = malloc(sizeof *svcreq);
if (svcreq == NULL)
- return afb_subcall_internal_error(callback, closure);
+ return afb_subcall_internal_error(callback, cbclosure);
/* initialises the request */
afb_context_init(&svcreq->context, svc->session, NULL);
svcreq->refcount = 1;
/* makes the call */
- afb_subcall(&svcreq->context, api, verb, args, callback, closure, (struct afb_req){ .itf = &afb_svc_req_itf, .closure = svcreq });
+ afb_subcall(&svcreq->context, api, verb, args, callback, cbclosure, (struct afb_req){ .itf = &afb_svc_req_itf, .closure = svcreq });
/* terminates and frees ressources if needed */
svcreq_unref(svcreq);