2 * Copyright (C) 2016, 2017 "IoT.bzh"
3 * Author "Fulup Ar Foll"
4 * Author José Bollo <jose.bollo@iot.bzh>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
26 #include "afb-session.h"
29 #include "afb-context.h"
34 #include <afb/afb-req-itf.h>
37 * Internal description of an api
40 const char *name; /**< name of the api */
41 struct afb_api api; /**< handler of the api */
44 static struct api_desc *apis_array = NULL;
45 static int apis_count = 0;
46 static int apis_timeout = 15;
50 * @param to the timeout in seconds
52 void afb_apis_set_timeout(int to)
58 * Checks wether 'name' is a valid API name.
59 * @return 1 if valid, 0 otherwise
61 int afb_apis_is_valid_api_name(const char *name)
65 c = (unsigned char)*name;
67 /* empty names aren't valid */
71 if (c < (unsigned char)'\x80') {
89 c = (unsigned char)*++name;
95 * Adds the api of 'name' described by 'api'.
96 * @param name the name of the api to add (have to survive, not copied!)
98 * @returns 0 in case of success or -1 in case
99 * of error with errno set:
100 * - EINVAL if name isn't valid
101 * - EEXIST if name already registered
102 * - ENOMEM when out of memory
104 int afb_apis_add(const char *name, struct afb_api api)
106 struct api_desc *apis;
109 /* Checks the api name */
110 if (!afb_apis_is_valid_api_name(name)) {
111 ERROR("invalid api name forbidden (name is '%s')", name);
116 /* check previously existing plugin */
117 for (i = 0 ; i < apis_count ; i++) {
118 c = strcasecmp(apis_array[i].name, name);
120 ERROR("api of name %s already exists", name);
128 /* allocates enough memory */
129 apis = realloc(apis_array, ((unsigned)apis_count + 1) * sizeof * apis);
131 ERROR("out of memory");
137 /* copy higher part of the array */
140 apis_array[c] = apis_array[c - 1];
144 /* record the plugin */
145 apis = &apis_array[i];
150 NOTICE("API %s added", name);
160 * @param api the api of the verb
161 * @return the descriptor if found or NULL otherwise
163 static const struct api_desc *search(const char *api)
166 const struct api_desc *a;
168 /* dichotomic search of the api */
173 /* check remaining slice */
178 /* check the mid of the slice */
181 c = strcasecmp(a->name, api);
186 /* update the slice */
195 * Starts a service by its 'api' name.
196 * @param api name of the service to start
197 * @param share_session if true start the servic"e in a shared session
198 * if false start it in its own session
199 * @param onneed if true start the service if possible, if false the api
201 * @return a positive number on success
203 int afb_apis_start_service(const char *api, int share_session, int onneed)
207 for (i = 0 ; i < apis_count ; i++) {
208 if (!strcasecmp(apis_array[i].name, api))
209 return apis_array[i].api.service_start(apis_array[i].api.closure, share_session, onneed);
211 ERROR("can't find service %s", api);
217 * Starts all possible services but stops at first error.
218 * @param share_session if true start the servic"e in a shared session
219 * if false start it in its own session
220 * @return 0 on success or a negative number when an error is found
222 int afb_apis_start_all_services(int share_session)
226 for (i = 0 ; i < apis_count ; i++) {
227 rc = apis_array[i].api.service_start(apis_array[i].api.closure, share_session, 1);
239 static void do_call_async(int signum, void *arg)
241 struct afb_xreq *xreq = arg;
242 const struct api_desc *a;
245 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
248 a = search(xreq->api);
250 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
252 xreq->context.api_key = a->api.closure;
253 a->api.call(a->api.closure, xreq);
256 afb_xreq_unref(xreq);
260 * Dispatch the request 'req' with the 'context' to the
261 * method of 'api' and 'verb'.
262 * @param req the request to dispatch
263 * @param context the context of the request
264 * @param api the api of the verb
265 * @param verb the verb within the api
267 void afb_apis_call(struct afb_xreq *xreq)
271 /* init hooking the request */
272 // TODO req = afb_hook_req_call(req, context, api, verb);
274 afb_xreq_addref(xreq);
275 rc = jobs_queue(NULL, apis_timeout, do_call_async, xreq);
277 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
278 ERROR("can't process job with threads: %m");
279 afb_xreq_fail_f(xreq, "cancelled", "not able to create a job for the task");
280 afb_xreq_unref(xreq);