11e6872f2d7d5ecfb693ff454e1b728f58301fec
[src/app-framework-binder.git] / src / afb-svc.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
22 #include <json-c/json.h>
23
24 #include <afb/afb-req-itf.h>
25 #include <afb/afb-service-itf.h>
26
27 #include "afb-session.h"
28 #include "afb-context.h"
29 #include "afb-evt.h"
30 #include "afb-subcall.h"
31 #include "afb-svc.h"
32
33 /*
34  * Structure for recording service
35  */
36 struct afb_svc
37 {
38         /* session of the service */
39         struct afb_session *session;
40
41         /* event listener of the service or NULL */
42         struct afb_evt_listener *listener;
43
44         /* on event callback for the service */
45         void (*on_event)(const char *event, struct json_object *object);
46 };
47
48 /*
49  * Structure for requests initiated by the service
50  */
51 struct svc_req
52 {
53         /*
54          * CAUTION: 'context' field should be the first because there
55          * is an implicit convertion to struct afb_context
56          */
57         struct afb_context context;
58
59         /* the service */
60         struct afb_svc *svc;
61
62         /* the count of references to the request */
63         int refcount;
64 };
65
66 /* functions for services */
67 static void svc_on_event(void *closure, const char *event, int eventid, struct json_object *object);
68 static void svc_call(void *closure, const char *api, const char *verb, struct json_object *args,
69                                 void (*callback)(void*, int, struct json_object*), void *cbclosure);
70
71 /* the interface for services */
72 static const struct afb_service_itf service_itf = {
73         .call = svc_call
74 };
75
76 /* the interface for events */
77 static const struct afb_evt_itf evt_itf = {
78         .broadcast = svc_on_event,
79         .push = svc_on_event
80 };
81
82 /* functions for requests of services */
83 static void svcreq_addref(struct svc_req *svcreq);
84 static void svcreq_unref(struct svc_req *svcreq);
85 static int svcreq_subscribe(struct svc_req *svcreq, struct afb_event event);
86 static int svcreq_unsubscribe(struct svc_req *svcreq, struct afb_event event);
87 static void svcreq_subcall(struct svc_req *svcreq, const char *api, const char *verb, struct json_object *args,
88                                 void (*callback)(void*, int, struct json_object*), void *closure);
89
90 /* interface for requests of services */
91 const struct afb_req_itf afb_svc_req_itf = {
92         .addref = (void*)svcreq_addref,
93         .unref = (void*)svcreq_unref,
94         .context_get = (void*)afb_context_get,
95         .context_set = (void*)afb_context_set,
96         .session_close = (void*)afb_context_close,
97         .session_set_LOA = (void*)afb_context_change_loa,
98         .subscribe = (void*)svcreq_subscribe,
99         .unsubscribe = (void*)svcreq_unsubscribe,
100         .subcall = (void*)svcreq_subcall
101 };
102
103 /* the common session for services sharing their session */
104 static struct afb_session *common_session;
105
106 /*
107  * Allocates a new service
108  */
109 static struct afb_svc *afb_svc_alloc(int share_session, void (*on_event)(const char *event, struct json_object *object))
110 {
111         struct afb_svc *svc;
112
113         /* allocates the svc handler */
114         svc = malloc(sizeof * svc);
115         if (svc == NULL)
116                 goto error;
117
118         /* instanciate the session */
119         if (share_session) {
120                 /* session shared with other svcs */
121                 if (common_session == NULL) {
122                         common_session = afb_session_create (NULL, 0);
123                         if (common_session == NULL)
124                                 goto error2;
125                 }
126                 svc->session = afb_session_addref(common_session);
127         } else {
128                 /* session dedicated to the svc */
129                 svc->session = afb_session_create (NULL, 0);
130                 if (svc->session == NULL)
131                         goto error2;
132         }
133
134         /* initialises the listener if needed */
135         svc->on_event = on_event;
136         if (on_event == NULL)
137                 svc->listener = NULL;
138         else {
139                 svc->listener = afb_evt_listener_create(&evt_itf, svc);
140                 if (svc->listener == NULL)
141                         goto error3;
142         }
143
144         return svc;
145
146 error3:
147         afb_session_unref(svc->session);
148 error2:
149         free(svc);
150 error:
151         return NULL;
152 }
153
154 /*
155  * Creates a new service
156  */
157 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))
158 {
159         int rc;
160         struct afb_svc *svc;
161
162         /* allocates the svc handler */
163         svc = afb_svc_alloc(share_session, on_event);
164         if (svc == NULL)
165                 goto error;
166
167         /* initialises the svc now */
168         rc = init((struct afb_service){ .itf = &service_itf, .closure = svc });
169         if (rc < 0)
170                 goto error2;
171
172         return svc;
173
174 error2:
175         if (svc->listener != NULL)
176                 afb_evt_listener_unref(svc->listener);
177         afb_session_unref(svc->session);
178         free(svc);
179 error:
180         return NULL;
181 }
182
183 /*
184  * Creates a new service
185  */
186 struct afb_svc *afb_svc_create_v2(
187                         int share_session,
188                         void (*on_event)(const char *event, struct json_object *object),
189                         int (*start)(const struct afb_binding_interface *interface, struct afb_service service),
190                         const struct afb_binding_interface *interface)
191 {
192         int rc;
193         struct afb_svc *svc;
194
195         /* allocates the svc handler */
196         svc = afb_svc_alloc(share_session, on_event);
197         if (svc == NULL)
198                 goto error;
199
200         /* initialises the svc now */
201         rc = start(interface, (struct afb_service){ .itf = &service_itf, .closure = svc });
202         if (rc < 0)
203                 goto error2;
204
205         return svc;
206
207 error2:
208         if (svc->listener != NULL)
209                 afb_evt_listener_unref(svc->listener);
210         afb_session_unref(svc->session);
211         free(svc);
212 error:
213         return NULL;
214 }
215
216 /*
217  * Propagates the event to the service
218  */
219 static void svc_on_event(void *closure, const char *event, int eventid, struct json_object *object)
220 {
221         struct afb_svc *svc = closure;
222         svc->on_event(event, object);
223         json_object_put(object);
224 }
225
226 /*
227  * Initiates a call for the service
228  */
229 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)
230 {
231         struct afb_svc *svc = closure;
232         struct svc_req *svcreq;
233
234         /* allocates the request */
235         svcreq = malloc(sizeof *svcreq);
236         if (svcreq == NULL)
237                 return afb_subcall_internal_error(callback, cbclosure);
238
239         /* initialises the request */
240         afb_context_init(&svcreq->context, svc->session, NULL);
241         svcreq->context.validated = 1;
242         svcreq->svc = svc;
243         svcreq->refcount = 1;
244
245         /* makes the call */
246         afb_subcall(&svcreq->context, api, verb, args, callback, cbclosure, (struct afb_req){ .itf = &afb_svc_req_itf, .closure = svcreq });
247
248         /* terminates and frees ressources if needed */
249         svcreq_unref(svcreq);
250 }
251
252 static void svcreq_addref(struct svc_req *svcreq)
253 {
254         svcreq->refcount++;
255 }
256
257 static void svcreq_unref(struct svc_req *svcreq)
258 {
259         if (0 == --svcreq->refcount) {
260                 afb_context_disconnect(&svcreq->context);
261                 free(svcreq);
262         }
263 }
264
265 static int svcreq_subscribe(struct svc_req *svcreq, struct afb_event event)
266 {
267         if (svcreq->svc->listener == NULL)
268                 return -1;
269         return afb_evt_add_watch(svcreq->svc->listener, event);
270 }
271
272 static int svcreq_unsubscribe(struct svc_req *svcreq, struct afb_event event)
273 {
274         if (svcreq->svc->listener == NULL)
275                 return -1;
276         return afb_evt_remove_watch(svcreq->svc->listener, event);
277 }
278
279 static void svcreq_subcall(struct svc_req *svcreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *closure)
280 {
281         afb_subcall(&svcreq->context, api, verb, args, callback, closure, (struct afb_req){ .itf = &afb_svc_req_itf, .closure = svcreq });
282 }
283