afb-xreq: hide internal addref/unref to xreqs
[src/app-framework-binder.git] / src / afb-api-dbus.c
index b8ec3d0..2d03269 100644 (file)
@@ -19,6 +19,7 @@
 #define NO_PLUGIN_VERBOSE_MACRO
 
 #include <stdlib.h>
+#include <stdio.h>
 #include <string.h>
 #include <assert.h>
 #include <errno.h>
 #include <systemd/sd-bus.h>
 #include <json-c/json.h>
 
-#include <afb/afb-req-itf.h>
+#include <afb/afb-event-itf.h>
 
 #include "afb-common.h"
 
 #include "afb-session.h"
 #include "afb-msg-json.h"
-#include "afb-apis.h"
-#include "afb-api-so.h"
+#include "afb-api.h"
+#include "afb-apiset.h"
+#include "afb-api-dbus.h"
 #include "afb-context.h"
 #include "afb-cred.h"
 #include "afb-evt.h"
@@ -69,6 +71,7 @@ struct api_dbus
                        struct sd_bus_slot *slot_call;
                        struct afb_evt_listener *listener; /* listener for broadcasted events */
                        struct origin *origins;
+                       struct afb_apiset *apiset;
                } server;
        };
 };
@@ -110,7 +113,7 @@ static struct api_dbus *make_api_dbus_3(int system, const char *path, size_t pat
                goto error2;
        }
        api->api++;
-       if (!afb_apis_is_valid_api_name(api->api)) {
+       if (!afb_api_is_valid_name(api->api)) {
                errno = EINVAL;
                goto error2;
        }
@@ -235,7 +238,7 @@ static struct dbus_memo *api_dbus_client_memo_make(struct api_dbus *api, struct
 
        memo = malloc(sizeof *memo);
        if (memo != NULL) {
-               afb_xreq_addref(xreq);
+               afb_xreq_unhooked_addref(xreq);
                memo->xreq = xreq;
                memo->msgid = 0;
                memo->api = api;
@@ -259,7 +262,7 @@ static void api_dbus_client_memo_destroy(struct dbus_memo *memo)
                prv = &(*prv)->next;
        }
 
-       afb_xreq_unref(memo->xreq);
+       afb_xreq_unhooked_unref(memo->xreq);
        free(memo);
 }
 
@@ -358,19 +361,6 @@ end:
        sd_bus_message_unref(msg);
 }
 
-static int api_dbus_service_start(void *closure, int share_session, int onneed)
-{
-       struct api_dbus *api = closure;
-
-       /* not an error when onneed */
-       if (onneed != 0)
-               return 0;
-
-       /* already started: it is an error */
-       ERROR("The Dbus binding %s is not a startable service", api->name);
-       return -1;
-}
-
 /* receives broadcasted events */
 static int api_dbus_client_on_broadcast_event(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
 {
@@ -578,12 +568,11 @@ static int api_dbus_client_on_manage_event(sd_bus_message *m, void *userdata, sd
 }
 
 static struct afb_api_itf dbus_api_itf = {
-       .call = api_dbus_client_call,
-       .service_start = api_dbus_service_start
+       .call = api_dbus_client_call
 };
 
 /* adds a afb-dbus-service client api */
-int afb_api_dbus_add_client(const char *path)
+int afb_api_dbus_add_client(const char *path, struct afb_apiset *apiset)
 {
        int rc;
        struct api_dbus *api;
@@ -621,7 +610,8 @@ int afb_api_dbus_add_client(const char *path)
        /* record it as an API */
        afb_api.closure = api;
        afb_api.itf = &dbus_api_itf;
-       if (afb_apis_add(api->api, afb_api) < 0)
+       afb_api.noconcurrency = 0;
+       if (afb_apiset_add(apiset, api->api, afb_api) < 0)
                goto error2;
 
        return 0;
@@ -756,7 +746,7 @@ static void afb_api_dbus_server_listener_free(struct listener *listener)
        free(listener);
 }
 
-static struct listener *afb_api_dbus_server_listerner_get(struct api_dbus *api, const char *sender, struct afb_session *session)
+static struct listener *afb_api_dbus_server_listener_get(struct api_dbus *api, const char *sender, struct afb_session *session)
 {
        int rc;
        struct listener *listener;
@@ -982,7 +972,7 @@ static int api_dbus_server_on_object_called(sd_bus_message *message, void *userd
        session = dreq->xreq.context.session;
 
        /* get the listener */
-       listener = afb_api_dbus_server_listerner_get(api, sd_bus_message_get_sender(message), session);
+       listener = afb_api_dbus_server_listener_get(api, sd_bus_message_get_sender(message), session);
        if (listener == NULL)
                goto out_of_memory;
 
@@ -997,8 +987,7 @@ static int api_dbus_server_on_object_called(sd_bus_message *message, void *userd
        dreq->listener = listener;
        dreq->xreq.api = api->api;
        dreq->xreq.verb = method;
-       afb_apis_call(&dreq->xreq);
-       afb_xreq_unref(&dreq->xreq);
+       afb_xreq_process(&dreq->xreq, api->server.apiset);
        return 1;
 
 out_of_memory:
@@ -1009,7 +998,7 @@ error:
 }
 
 /* create the service */
-int afb_api_dbus_add_server(const char *path)
+int afb_api_dbus_add_server(const char *path, struct afb_apiset *apiset)
 {
        int rc;
        struct api_dbus *api;
@@ -1037,6 +1026,7 @@ int afb_api_dbus_add_server(const char *path)
        INFO("afb service over dbus installed, name %s, path %s", api->name, api->path);
 
        api->server.listener = afb_evt_listener_create(&evt_broadcast_itf, api);
+       api->server.apiset = afb_apiset_addref(apiset);
        return 0;
 error3:
        sd_bus_release_name(api->sdbus, api->name);