2 * Copyright (C) 2016 "IoT.bzh"
3 * Author: José Bollo <jose.bollo@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <json-c/json.h>
24 #include <afb/afb-req-itf.h>
25 #include <afb/afb-service-itf.h>
28 #include "afb-context.h"
30 #include "afb-subcall.h"
34 * Structure for recording service
38 /* session of the service */
39 struct AFB_clientCtx *session;
41 /* event listener of the service or NULL */
42 struct afb_evt_listener *listener;
44 /* on event callback for the service */
45 void (*on_event)(const char *event, struct json_object *object);
49 * Structure for requests initiated by the service
54 * CAUTION: 'context' field should be the first because there
55 * is an implicit convertion to struct afb_context
57 struct afb_context context;
62 /* the count of references to the request */
66 /* functions for services */
67 static void svc_on_event(struct afb_svc *svc, const char *event, int eventid, 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);
71 /* the interface for services */
72 static const struct afb_service_itf service_itf = {
73 .call = (void*)svc_call
76 /* the interface for events */
77 static const struct afb_evt_itf evt_itf = {
78 .broadcast = (void*)svc_on_event,
79 .push = (void*)svc_on_event
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);
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
103 /* the common session for services sahring their session */
104 static struct AFB_clientCtx *common_session;
107 * Creates a new service
109 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))
114 /* allocates the svc handler */
115 svc = malloc(sizeof * svc);
119 /* instanciate the session */
121 /* session shared with other svcs */
122 if (common_session == NULL) {
123 common_session = ctxClientCreate (NULL, 0);
124 if (common_session == NULL)
127 svc->session = ctxClientAddRef(common_session);
129 /* session dedicated to the svc */
130 svc->session = ctxClientCreate (NULL, 0);
131 if (svc->session == NULL)
135 /* initialises the listener if needed */
136 if (on_event == NULL)
137 svc->listener = NULL;
139 svc->listener = afb_evt_listener_create(&evt_itf, svc);
140 if (svc->listener == NULL)
144 /* initialises the svc now */
145 rc = init((struct afb_service){ .itf = &service_itf, .closure = svc });
152 if (svc->listener != NULL)
153 afb_evt_listener_unref(svc->listener);
155 ctxClientUnref(svc->session);
163 * Propagates the event to the service
165 static void svc_on_event(struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
167 svc->on_event(event, object);
171 * Initiates a call for the service
173 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)
175 struct svc_req *svcreq;
177 /* allocates the request */
178 svcreq = malloc(sizeof *svcreq);
180 return afb_subcall_internal_error(callback, closure);
182 /* initialises the request */
183 afb_context_init(&svcreq->context, svc->session, NULL);
184 svcreq->context.validated = 1;
186 svcreq->refcount = 1;
189 afb_subcall(&svcreq->context, api, verb, args, callback, closure, (struct afb_req){ .itf = &afb_svc_req_itf, .closure = svcreq });
191 /* terminates and frees ressources if needed */
192 svcreq_unref(svcreq);
195 static void svcreq_addref(struct svc_req *svcreq)
200 static void svcreq_unref(struct svc_req *svcreq)
202 if (0 == --svcreq->refcount) {
203 afb_context_disconnect(&svcreq->context);
208 static int svcreq_subscribe(struct svc_req *svcreq, struct afb_event event)
210 if (svcreq->svc->listener == NULL)
212 return afb_evt_add_watch(svcreq->svc->listener, event);
215 static int svcreq_unsubscribe(struct svc_req *svcreq, struct afb_event event)
217 if (svcreq->svc->listener == NULL)
219 return afb_evt_remove_watch(svcreq->svc->listener, event);
222 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)
224 afb_subcall(&svcreq->context, api, verb, args, callback, closure, (struct afb_req){ .itf = &afb_svc_req_itf, .closure = svcreq });