Definitive switch to internal's xreq
[src/app-framework-binder.git] / src / afb-subcall.c
1 /*
2  * Copyright (C) 2016, 2017 "IoT.bzh"
3  * Author: José Bollo <jose.bollo@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <json-c/json.h>
24 #include <afb/afb-req-itf.h>
25
26 #include "afb-subcall.h"
27 #include "afb-msg-json.h"
28 #include "afb-apis.h"
29 #include "afb-context.h"
30 #include "afb-xreq.h"
31 #include "verbose.h"
32
33 struct subcall;
34
35 static void subcall_destroy(void *closure);
36 static void subcall_reply(void *closure, int iserror, struct json_object *obj);
37 static int subcall_subscribe(void *closure, struct afb_event event);
38 static int subcall_unsubscribe(void *closure, struct afb_event event);
39
40 const struct afb_xreq_query_itf afb_subcall_xreq_itf = {
41         .reply = subcall_reply,
42         .unref = subcall_destroy,
43         .subscribe = subcall_subscribe,
44         .unsubscribe = subcall_unsubscribe
45 };
46
47 struct subcall
48 {
49         struct afb_xreq xreq;
50         struct afb_xreq *caller;
51         void (*callback)(void*, int, struct json_object*);
52         void *closure;
53 };
54
55 static void subcall_destroy(void *closure)
56 {
57         struct subcall *subcall = closure;
58
59         json_object_put(subcall->xreq.json);
60         afb_xreq_unref(subcall->caller);
61         free(subcall);
62 }
63
64 static void subcall_reply(void *closure, int iserror, struct json_object *obj)
65 {
66         struct subcall *subcall = closure;
67
68         subcall->callback(subcall->closure, iserror, obj);
69         json_object_put(obj);
70 }
71
72 static int subcall_subscribe(void *closure, struct afb_event event)
73 {
74         struct subcall *subcall = closure;
75
76         return afb_xreq_subscribe(subcall->caller, event);
77 }
78
79 static int subcall_unsubscribe(void *closure, struct afb_event event)
80 {
81         struct subcall *subcall = closure;
82
83         return afb_xreq_unsubscribe(subcall->caller, event);
84 }
85
86 void afb_subcall_internal_error(void (*callback)(void*, int, struct json_object*), void *closure)
87 {
88         static struct json_object *obj;
89
90         if (obj == NULL)
91                 obj = afb_msg_json_reply_error("failed", "internal error", NULL, NULL);
92
93         callback(closure, 1, obj);
94 }
95
96 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)
97 {
98         struct subcall *subcall;
99
100         subcall = calloc(1, sizeof *subcall);
101         if (subcall == NULL) {
102                 return NULL;
103         }
104
105
106         afb_context_subinit(&subcall->xreq.context, &caller->context);
107         subcall->xreq.refcount = 1;
108         subcall->xreq.json = args;
109         subcall->xreq.api = api; /* TODO: alloc ? */
110         subcall->xreq.verb = verb; /* TODO: alloc ? */
111         subcall->xreq.query = subcall;
112         subcall->xreq.queryitf = &afb_subcall_xreq_itf;
113         subcall->caller = caller;
114         subcall->callback = callback;
115         subcall->closure = closure;
116         return subcall;
117 }
118
119 void afb_subcall(
120                 struct afb_xreq *caller,
121                 const char *api,
122                 const char *verb,
123                 struct json_object *args,
124                 void (*callback)(void*, int, struct json_object*),
125                 void *closure
126 )
127 {
128         struct subcall *subcall;
129
130         subcall = create_subcall(caller, api, verb, args, callback, closure);
131         if (subcall == NULL) {
132                 afb_subcall_internal_error(callback, closure);
133                 return;
134         }
135
136         afb_xreq_addref(caller);
137         afb_apis_call(&subcall->xreq);
138         afb_xreq_unref(&subcall->xreq);
139 }
140
141