if (iserror)
afb_req_fail(request, "failed", json_object_to_json_string(object));
else
- afb_req_success(request, object, NULL);
+ afb_req_success(request, json_object_get(object), NULL);
afb_req_unref(request);
}
if (object == NULL)
afb_req_fail(request, "failed", "bad arguments");
- else
+ else {
afb_req_subcall(request, api, verb, object, subcallcb, afb_req_store(request));
+ json_object_put(object);
+ }
+}
+
+static void subcallsync (struct afb_req request)
+{
+ int rc;
+ const char *api = afb_req_value(request, "api");
+ const char *verb = afb_req_value(request, "verb");
+ const char *args = afb_req_value(request, "args");
+ json_object *result, *object = api && verb && args ? json_tokener_parse(args) : NULL;
+
+ if (object == NULL)
+ afb_req_fail(request, "failed", "bad arguments");
+ else {
+ rc = afb_req_subcall_sync(request, api, verb, object, &result);
+ if (rc) {
+ afb_req_success(request, result, NULL);
+ } else {
+ afb_req_fail(request, "failed", json_object_to_json_string(result));
+ json_object_put(result);
+ }
+ json_object_put(object);
+ }
}
static void eventadd (struct afb_req request)
{"pingJson" , AFB_SESSION_NONE, pingJson , "Return a JSON object"},
{"pingevent", AFB_SESSION_NONE, pingEvent , "Send an event"},
{"subcall", AFB_SESSION_NONE, subcall , "Call api/verb(args)"},
+ {"subcallsync", AFB_SESSION_NONE, subcallsync , "Call api/verb(args)"},
{"eventadd", AFB_SESSION_NONE, eventadd , "adds the event of 'name' for the 'tag'"},
{"eventdel", AFB_SESSION_NONE, eventdel , "deletes the event of 'tag'"},
{"eventsub", AFB_SESSION_NONE, eventsub , "subscribes to the event of 'tag'"},
int (*unsubscribe)(void *closure, struct afb_event event);
void (*subcall)(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure);
+ int (*subcallsync)(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result);
};
/*
req.itf->subcall(req.closure, api, verb, args, callback, closure);
}
+/*
+ * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
+ * This call is made in the context of the request 'req'.
+ * This call is synchronous, it waits untill completion of the request.
+ * It returns 0 on an error answer and returns 1 when no error was detected.
+ * The object pointed by 'result' is filled and must be released by the caller
+ * after its use by calling 'json_object_put'.
+ */
+static inline int afb_req_subcall_sync(struct afb_req req, const char *api, const char *verb, struct json_object *args, struct json_object **result)
+{
+ return req.itf->subcallsync(req.closure, api, verb, args, result);
+}
+
/* internal use */
static inline const char *afb_req_raw(struct afb_req req, size_t *size)
{
return 0;
}
+/**
+ * Internal direct dispatch of the request 'xreq'
+ * @param xreq the request to dispatch
+ */
+static void do_call_direct(struct afb_xreq *xreq)
+{
+ const struct api_desc *a;
+ /* search the api */
+ a = search(xreq->api);
+ if (!a)
+ afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
+ else {
+ xreq->context.api_key = a->api.closure;
+ a->api.call(a->api.closure, xreq);
+ }
+}
-
-
-
+/**
+ * Asynchronous dispatch callback for the request 'xreq'
+ * @param signum 0 on normal flow or the signal number that interupted the normal flow
+ */
static void do_call_async(int signum, void *arg)
{
struct afb_xreq *xreq = arg;
- const struct api_desc *a;
if (signum != 0)
afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
else {
- /* search the api */
- a = search(xreq->api);
- if (!a)
- afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
- else {
- xreq->context.api_key = a->api.closure;
- a->api.call(a->api.closure, xreq);
- }
+ do_call_direct(xreq);
}
afb_xreq_unref(xreq);
}
/**
- * Dispatch the request 'req' with the 'context' to the
- * method of 'api' and 'verb'.
- * @param req the request to dispatch
- * @param context the context of the request
- * @param api the api of the verb
- * @param verb the verb within the api
+ * Dispatch the request 'xreq' synchronously and directly.
+ * @param xreq the request to dispatch
+ */
+void afb_apis_call_direct(struct afb_xreq *xreq)
+{
+ /* init hooking the request */
+ // TODO req = afb_hook_req_call(req, context, api, verb);
+
+ do_call_direct(xreq);
+}
+
+/**
+ * Dispatch the request 'xreq' asynchronously.
+ * @param xreq the request to dispatch
*/
void afb_apis_call(struct afb_xreq *xreq)
{
extern int afb_apis_start_service(const char *name, int share_session, int onneed);
extern void afb_apis_call(struct afb_xreq *xreq);
+extern void afb_apis_call_direct(struct afb_xreq *xreq);
return arg;
}
+struct json_object *afb_msg_json_internal_error()
+{
+ static struct json_object *obj;
+
+ if (obj == NULL)
+ obj = afb_msg_json_reply_error("failed", "internal error", NULL, NULL);
+
+ return obj;
+}
+
extern struct afb_arg afb_msg_json_get_arg(struct json_object *object, const char *name);
+extern struct json_object *afb_msg_json_internal_error();
#include "afb-context.h"
#include "afb-xreq.h"
#include "verbose.h"
+#include "jobs.h"
struct subcall;
static void subcall_reply(void *closure, int iserror, struct json_object *obj);
static int subcall_subscribe(void *closure, struct afb_event event);
static int subcall_unsubscribe(void *closure, struct afb_event event);
+static void subcall_sync_leave(struct subcall *subcall);
const struct afb_xreq_query_itf afb_subcall_xreq_itf = {
.reply = subcall_reply,
struct afb_xreq *caller;
void (*callback)(void*, int, struct json_object*);
void *closure;
+ struct jobloop *jobloop;
+ struct json_object *result;
+ int iserror;
};
static void subcall_destroy(void *closure)
{
struct subcall *subcall = closure;
- subcall->callback(subcall->closure, iserror, obj);
- json_object_put(obj);
+ if (subcall->callback) {
+ subcall->callback(subcall->closure, iserror, obj);
+ json_object_put(obj);
+ } else {
+ subcall->result = obj;
+ subcall->iserror = iserror;
+ subcall_sync_leave(subcall);
+ }
}
static int subcall_subscribe(void *closure, struct afb_event event)
return afb_xreq_unsubscribe(subcall->caller, event);
}
-void afb_subcall_internal_error(void (*callback)(void*, int, struct json_object*), void *closure)
-{
- static struct json_object *obj;
-
- if (obj == NULL)
- obj = afb_msg_json_reply_error("failed", "internal error", NULL, NULL);
-
- callback(closure, 1, obj);
-}
-
static struct subcall *create_subcall(struct afb_xreq *caller, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *closure)
{
struct subcall *subcall;
return NULL;
}
-
afb_context_subinit(&subcall->xreq.context, &caller->context);
subcall->xreq.refcount = 1;
subcall->xreq.json = args;
subcall->caller = caller;
subcall->callback = callback;
subcall->closure = closure;
+ afb_xreq_addref(caller);
+ json_object_get(args);
return subcall;
}
subcall = create_subcall(caller, api, verb, args, callback, closure);
if (subcall == NULL) {
- afb_subcall_internal_error(callback, closure);
+ callback(closure, 1, afb_msg_json_internal_error());
return;
}
- afb_xreq_addref(caller);
afb_apis_call(&subcall->xreq);
afb_xreq_unref(&subcall->xreq);
}
+static void subcall_sync_leave(struct subcall *subcall)
+{
+ struct jobloop *jobloop = subcall->jobloop;
+ subcall->jobloop = NULL;
+ if (jobloop)
+ jobs_leave(jobloop);
+}
+
+static void subcall_sync_cb(int signum, void *closure, struct jobloop *jobloop)
+{
+ struct subcall *subcall = closure;
+
+ if (!signum) {
+ subcall->jobloop = jobloop;
+ afb_apis_call_direct(&subcall->xreq);
+ } else {
+ afb_xreq_fail_f(&subcall->xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
+ }
+ subcall_sync_leave(subcall);
+}
+
+int afb_subcall_sync(
+ struct afb_xreq *caller,
+ const char *api,
+ const char *verb,
+ struct json_object *args,
+ struct json_object **result
+)
+{
+ int rc;
+ struct subcall *subcall;
+
+ subcall = create_subcall(caller, api, verb, args, NULL, NULL);
+ if (subcall == NULL) {
+ *result = json_object_get(afb_msg_json_internal_error());
+ return 0;
+ }
+
+ rc = jobs_enter(NULL, 0, subcall_sync_cb, subcall);
+ if (rc < 0) {
+ subcall->result = json_object_get(afb_msg_json_internal_error());
+ subcall->iserror = 1;
+ }
+ rc = !subcall->iserror;
+ *result = subcall->result;
+ afb_xreq_unref(&subcall->xreq);
+ return rc;
+}
+
void (*callback)(void*, int, struct json_object*),
void *closure);
-extern void afb_subcall_internal_error(
- void (*callback)(void*, int, struct json_object*),
- void *closure);
+extern int afb_subcall_sync(
+ struct afb_xreq *caller,
+ const char *api,
+ const char *verb,
+ struct json_object *args,
+ struct json_object **result);
#include "afb-session.h"
#include "afb-context.h"
#include "afb-evt.h"
-#include "afb-subcall.h"
+#include "afb-msg-json.h"
#include "afb-svc.h"
#include "afb-xreq.h"
#include "afb-apis.h"
if (svcreq == NULL) {
ERROR("out of memory");
json_object_put(args);
- return afb_subcall_internal_error(callback, cbclosure);
+ callback(cbclosure, 1, afb_msg_json_internal_error());
+ return;
}
/* initialises the request */
void (*callback)(void*, int, struct json_object*),
void *cb_closure);
+static int xreq_subcallsync_cb(
+ void *closure,
+ const char *api,
+ const char *verb,
+ struct json_object *args,
+ struct json_object **result);
+
const struct afb_req_itf xreq_itf = {
.json = xreq_json_cb,
.get = xreq_get_cb,
.session_set_LOA = xreq_session_set_LOA_cb,
.subscribe = xreq_subscribe_cb,
.unsubscribe = xreq_unsubscribe_cb,
- .subcall = xreq_subcall_cb
+ .subcall = xreq_subcall_cb,
+ .subcallsync = xreq_subcallsync_cb
};
static struct json_object *xreq_json_cb(void *closure)
afb_subcall(xreq, api, verb, args, callback, cb_closure);
}
+static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
+{
+ struct afb_xreq *xreq = closure;
+/*
+ if (xreq->queryitf->subcallsync)
+ xreq->queryitf->subcall(xreq->query, api, verb, args, callback, cb_closure);
+ else
+*/
+ return afb_subcall_sync(xreq, api, verb, args, result);
+}
+
void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
{
char *message;