03ff4b84b830ccb7a8853968ec49e1b2653a9b0b
[src/app-framework-binder.git] / src / afb-svc.c
1 /*
2  * Copyright (C) 2016 "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 "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_clientCtx *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(struct afb_svc *svc, const char *event, struct json_object *object);
68 static void svc_call(struct afb_svc *svc, const char *api, const char *verb, struct json_object *args,
69                                 void (*callback)(void*, int, struct json_object*), void *closure);
70
71 /* the interface for services */
72 static const struct afb_service_itf service_itf = {
73         .call = (void*)svc_call
74 };
75
76 /* functions for requests of services */
77 static void svcreq_addref(struct svc_req *svcreq);
78 static void svcreq_unref(struct svc_req *svcreq);
79 static int svcreq_subscribe(struct svc_req *svcreq, struct afb_event event);
80 static int svcreq_unsubscribe(struct svc_req *svcreq, struct afb_event event);
81 static void svcreq_subcall(struct svc_req *svcreq, const char *api, const char *verb, struct json_object *args,
82                                 void (*callback)(void*, int, struct json_object*), void *closure);
83
84 /* interface for requests of services */
85 const struct afb_req_itf afb_svc_req_itf = {
86         .addref = (void*)svcreq_addref,
87         .unref = (void*)svcreq_unref,
88         .context_get = (void*)afb_context_get,
89         .context_set = (void*)afb_context_set,
90         .session_close = (void*)afb_context_close,
91         .session_set_LOA = (void*)afb_context_change_loa,
92         .subscribe = (void*)svcreq_subscribe,
93         .unsubscribe = (void*)svcreq_unsubscribe,
94         .subcall = (void*)svcreq_subcall
95 };
96
97 /* the common session for services sahring their session */
98 static struct AFB_clientCtx *common_session;
99
100 /*
101  * Creates a new service
102  */
103 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))
104 {
105         int rc;
106         struct afb_svc *svc;
107
108         /* allocates the svc handler */
109         svc = malloc(sizeof * svc);
110         if (svc == NULL)
111                 goto error;
112
113         /* instanciate the session */
114         if (share_session) {
115                 /* session shared with other svcs */
116                 if (common_session == NULL) {
117                         common_session = ctxClientCreate (NULL, 0);
118                         if (common_session == NULL)
119                                 goto error2;
120                 }
121                 svc->session = ctxClientAddRef(common_session);
122         } else {
123                 /* session dedicated to the svc */
124                 svc->session = ctxClientCreate (NULL, 0);
125                 if (svc->session == NULL)
126                         goto error2;
127         }
128
129         /* initialises the listener if needed */
130         if (on_event == NULL)
131                 svc->listener = NULL;
132         else {
133                 svc->listener = afb_evt_listener_create((void*)svc_on_event, svc);
134                 if (svc->listener == NULL)
135                         goto error3;
136         }
137
138         /* initialises the svc now */
139         rc = init((struct afb_service){ .itf = &service_itf, .closure = svc });
140         if (rc < 0)
141                 goto error4;
142
143         return svc;
144
145 error4:
146         if (svc->listener != NULL)
147                 afb_evt_listener_unref(svc->listener);
148 error3:
149         ctxClientUnref(svc->session);
150 error2:
151         free(svc);
152 error:
153         return NULL;
154 }
155
156 /*
157  * Propagates the event to the service
158  */
159 static void svc_on_event(struct afb_svc *svc, const char *event, struct json_object *object)
160 {
161         svc->on_event(event, object);
162 }
163
164 /*
165  * Initiates a call for the service
166  */
167 static void svc_call(struct afb_svc *svc, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *closure)
168 {
169         struct svc_req *svcreq;
170
171         /* allocates the request */
172         svcreq = malloc(sizeof *svcreq);
173         if (svcreq == NULL)
174                 return afb_subcall_internal_error(callback, closure);
175
176         /* initialises the request */
177         afb_context_init(&svcreq->context, svc->session, NULL);
178         svcreq->context.validated = 1;
179         svcreq->svc = svc;
180         svcreq->refcount = 1;
181
182         /* makes the call */
183         afb_subcall(&svcreq->context, api, verb, args, callback, closure, (struct afb_req){ .itf = &afb_svc_req_itf, .closure = svcreq });
184
185         /* terminates and frees ressources if needed */
186         svcreq_unref(svcreq);
187 }
188
189 static void svcreq_addref(struct svc_req *svcreq)
190 {
191         svcreq->refcount++;
192 }
193
194 static void svcreq_unref(struct svc_req *svcreq)
195 {
196         if (0 == --svcreq->refcount) {
197                 afb_context_disconnect(&svcreq->context);
198                 free(svcreq);
199         }
200 }
201
202 static int svcreq_subscribe(struct svc_req *svcreq, struct afb_event event)
203 {
204         if (svcreq->svc->listener == NULL)
205                 return -1;
206         return afb_evt_add_watch(svcreq->svc->listener, event);
207 }
208
209 static int svcreq_unsubscribe(struct svc_req *svcreq, struct afb_event event)
210 {
211         if (svcreq->svc->listener == NULL)
212                 return -1;
213         return afb_evt_remove_watch(svcreq->svc->listener, event);
214 }
215
216 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)
217 {
218         afb_subcall(&svcreq->context, api, verb, args, callback, closure, (struct afb_req){ .itf = &afb_svc_req_itf, .closure = svcreq });
219 }
220