Add logging by request
[src/app-framework-binder.git] / bindings / samples / HelloWorld.c
index 505aee3..68af5fc 100644 (file)
@@ -182,10 +182,8 @@ static void subcall (struct afb_req 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)
@@ -200,13 +198,12 @@ static void subcallsync (struct afb_req request)
                afb_req_fail(request, "failed", "bad arguments");
        else {
                rc = afb_req_subcall_sync(request, api, verb, object, &result);
-               if (rc) {
+               if (rc)
                        afb_req_success(request, result, NULL);
-               else {
+               else {
                        afb_req_fail(request, "failed", json_object_to_json_string(result));
                        json_object_put(result);
                }
-               json_object_put(object);
        }
 }
 
@@ -284,9 +281,83 @@ static void eventpush (struct afb_req request)
        json_object_put(object);
 }
 
+static void callcb (void *prequest, int iserror, json_object *object)
+{
+       struct afb_req request = afb_req_unstore(prequest);
+       if (iserror)
+               afb_req_fail(request, "failed", json_object_to_json_string(object));
+       else
+               afb_req_success(request, json_object_get(object), NULL);
+       afb_req_unref(request);
+}
+
+static void call (struct afb_req request)
+{
+       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 *object = api && verb && args ? json_tokener_parse(args) : NULL;
+
+       if (object == NULL)
+               afb_req_fail(request, "failed", "bad arguments");
+       else
+               afb_service_call(api, verb, object, callcb, afb_req_store(request));
+}
+
+static void callsync (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_service_call_sync(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);
+               }
+       }
+}
+
+static void verbose (struct afb_req request)
+{
+       int level = 5;
+       json_object *query = afb_req_json(request), *l;
+
+       if (json_object_is_type(query,json_type_int))
+               level = json_object_get_int(query);
+       else if (json_object_object_get_ex(query, "level", &l) && json_object_is_type(l, json_type_int))
+               level = json_object_get_int(l);
+
+       if (!json_object_object_get_ex(query,"message",&l))
+               l = query;
+
+       AFB_REQ_VERBOSE(request, level, "verbose called for %s", json_object_get_string(l));
+       afb_req_success(request, NULL, NULL);
+}
+
 static void exitnow (struct afb_req request)
 {
-       exit(0);
+       int code = 0;
+       json_object *query = afb_req_json(request), *l;
+
+       if (json_object_is_type(query,json_type_int))
+               code = json_object_get_int(query);
+       else if (json_object_object_get_ex(query, "code", &l) && json_object_is_type(l, json_type_int))
+               code = json_object_get_int(l);
+
+       if (!json_object_object_get_ex(query,"reason",&l))
+               l = NULL;
+
+       REQ_NOTICE(request, "in phase of exiting with code %d, reason: %s", code, l ? json_object_get_string(l) : "unknown");
+       afb_req_success(request, NULL, NULL);
+       exit(code);
 }
 
 static int preinit()
@@ -301,6 +372,11 @@ static int init()
        return 0;
 }
 
+static void onevent(const char *event, struct json_object *object)
+{
+       NOTICE("received event %s(%s)", event, json_object_to_json_string(object));
+}
+
 // NOTE: this sample does not use session to keep test a basic as possible
 //       in real application most APIs should be protected with AFB_SESSION_CHECK
 static const struct afb_verb_v2 verbs[]= {
@@ -317,6 +393,9 @@ static const struct afb_verb_v2 verbs[]= {
   { "eventsub",     eventsub   , NULL, AFB_SESSION_NONE },
   { "eventunsub",   eventunsub , NULL, AFB_SESSION_NONE },
   { "eventpush",    eventpush  , NULL, AFB_SESSION_NONE },
+  { "call",         call       , NULL, AFB_SESSION_NONE },
+  { "callsync",     callsync   , NULL, AFB_SESSION_NONE },
+  { "verbose",      verbose    , NULL, AFB_SESSION_NONE },
   { "exit",         exitnow    , NULL, AFB_SESSION_NONE },
   { NULL}
 };
@@ -326,6 +405,7 @@ const struct afb_binding_v2 afbBindingV2 = {
        .specification = NULL,
        .verbs = verbs,
        .preinit = preinit,
-       .init = init
+       .init = init,
+       .onevent = onevent
 };