aboutsummaryrefslogtreecommitdiffstats
path: root/src/afb-svc.c
blob: 7d9f8dcb2c16c1abd5e6110e844ca30218780323 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
 * Copyright (C) 2016, 2017 "IoT.bzh"
 * Author: José Bollo <jose.bollo@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _GNU_SOURCE

#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <json-c/json.h>

#include <afb/afb-binding-v1.h>
#include <afb/afb-binding-v2.h>

#include "afb-session.h"
#include "afb-context.h"
#include "afb-evt.h"
#include "afb-msg-json.h"
#include "afb-svc.h"
#include "afb-xreq.h"
#include "afb-cred.h"
#include "afb-apiset.h"
#include "afb-hook.h"
#include "jobs.h"
#include "verbose.h"


#define HOOK(x,...) if((svc)->hookflags & afb_hook_flag_svc_##x) afb_hook_svc_##x(__VA_ARGS__)

/*
 * Structure for requests initiated by the service
 */
struct svc_req
{
	struct afb_xreq xreq;

	struct afb_svc *svc;

	/* the args */
	void (*callback)(void*, int, struct json_object*);
	void *closure;

	/* sync */
	struct jobloop *jobloop;
	struct json_object *result;
	int status;
	int async;
};

/* functions for services */
static void svc_on_event(void *closure, const char *event, int eventid, struct json_object *object);
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);
static int svc_call_sync(void *closure, const char *api, const char *verb, struct json_object *args,
				struct json_object **result);

/* the interface for services */
static const struct afb_service_itf service_itf = {
	.call = svc_call,
	.call_sync = svc_call_sync
};

/* the interface for events */
static const struct afb_evt_itf evt_itf = {
	.broadcast = svc_on_event,
	.push = svc_on_event
};

/* functions for requests of services */
static void svcreq_destroy(struct afb_xreq *xreq);
static void svcreq_reply(struct afb_xreq *xreq, int status, json_object *obj);

/* interface for requests of services */
const struct afb_xreq_query_itf afb_svc_xreq_itf = {
	.unref = svcreq_destroy,
	.reply = svcreq_reply
};

/* the common session for services sharing their session */
static struct afb_session *common_session;

static inline struct afb_service to_afb_service(struct afb_svc *svc)
{
	return (struct afb_service){ .itf = &service_itf, .closure = svc };
}

/*
 * Frees a service
 */
static void svc_free(struct afb_svc *svc)
{
	if (svc->listener != NULL)
		afb_evt_listener_unref(svc->listener);
	if (svc->session)
		afb_session_unref(svc->session);
	afb_apiset_unref(svc->apiset);
	free(svc);
}

/*
 * Allocates a new service
 */
static struct afb_svc *afb_svc_alloc(
			const char *api,
			struct afb_apiset *apiset,
			int share_session
)
{
	struct afb_svc *svc;

	/* allocates the svc handler */
	svc = calloc(1, sizeof * svc);
	if (svc == NULL) {
		errno = ENOMEM;
		return NULL;
	}

	/* instanciate the apiset */
	svc->api = api;
	svc->apiset = afb_apiset_addref(apiset);

	/* instanciate the session */
	if (share_session) {
		/* session shared with other svcs */
		if (common_session == NULL) {
			common_session = afb_session_create (NULL, 0);
			if (common_session == NULL)
				goto error;
		}
		svc->session = afb_session_addref(common_session);
	} else {
		/* session dedicated to the svc */
		svc->session = afb_session_create (NULL, 0);
		if (svc->session == NULL)
			goto error;
	}

	svc->hookflags = afb_hook_flags_svc(svc->api);
	return svc;

error:
	svc_free(svc);
	return NULL;
}

/*
 * Creates a new service
 */
struct afb_svc *afb_svc_create_v1(
			const char *api,
			struct afb_apiset *apiset,
			int share_session,
			int (*start)(struct afb_service service),
			void (*on_event)(const char *event, struct json_object *object)
)
{
	int rc;
	struct afb_svc *svc;

	/* allocates the svc handler */
	svc = afb_svc_alloc(api, apiset, share_session);
	if (svc == NULL)
		goto error;

	/* initialises the listener if needed */
	if (on_event) {
		svc->on_event = on_event;
		svc->listener = afb_evt_listener_create(&evt_itf, svc);
		if (svc->listener == NULL)
			goto error;
	}

	/* initialises the svc now */
	if (start) {
		HOOK(start_before, svc);
		rc = start(to_afb_service(svc));
		HOOK(start_after, svc, rc);
		if (rc < 0)
			goto error;
	}

	return svc;

error:
	svc_free(svc);
	return NULL;
}

/*
 * Creates a new service
 */
struct afb_svc *afb_svc_create_v2(
			const char *api,
			struct afb_apiset *apiset,
			int share_session,
			int (*start)(),
			void (*on_event)(const char *event, struct json_object *object),
			struct afb_binding_data_v2 *data
)
{
	int rc;
	struct afb_svc *svc;

	/* allocates the svc handler */
	svc = afb_svc_alloc(api, apiset, share_session);
	if (svc == NULL)
		goto error;
	data->service = to_afb_service(svc);

	/* initialises the listener if needed */
	if (on_event) {
		svc->on_event = on_event;
		svc->listener = afb_evt_listener_create(&evt_itf, svc);
		if (svc->listener == NULL)
			goto error;
	}

	/* starts the svc if needed */
	if (start) {
		HOOK(start_before, svc);
		rc = start();
		HOOK(start_after, svc, rc);
		if (rc < 0)
			goto error;
	}

	return svc;

error:
	svc_free(svc);
	return NULL;
}

void afb_svc_update_hook(struct afb_svc *svc)
{
	svc->hookflags = afb_hook_flags_svc(svc->api);
}

/*
 * Propagates the event to the service
 */
static void svc_on_event(void *closure, const char *event, int eventid, struct json_object *object)
{
	struct afb_svc *svc = closure;

	HOOK(on_event_before, svc, event, eventid, object);
	svc->on_event(event, object);
	HOOK(on_event_after, svc, event, eventid, object);
	json_object_put(object);
}

/*
 * create an svc_req
 */
static struct svc_req *svcreq_create(struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
{
	struct svc_req *svcreq;
	size_t lenapi, lenverb;
	char *copy;

	/* allocates the request */
	lenapi = 1 + strlen(api);
	lenverb = 1 + strlen(verb);
	svcreq = malloc(lenapi + lenverb + sizeof *svcreq);
	if (svcreq != NULL) {
		/* initialises the request */
		afb_xreq_init(&svcreq->xreq, &afb_svc_xreq_itf);
		afb_context_init(&svcreq->xreq.context, svc->session, NULL);
		svcreq->xreq.context.validated = 1;
		copy = (char*)&svcreq[1];
		memcpy(copy, api, lenapi);
		svcreq->xreq.api = copy;
		copy = &copy[lenapi];
		memcpy(copy, verb, lenverb);
		svcreq->xreq.verb = copy;
		svcreq->xreq.listener = svc->listener;
		svcreq->xreq.json = args;
		svcreq->svc = svc;
	}
	return svcreq;
}

/*
 * destroys the svc_req
 */
static void svcreq_destroy(struct afb_xreq *xreq)
{
	struct svc_req *svcreq = CONTAINER_OF_XREQ(struct svc_req, xreq);

	afb_context_disconnect(&svcreq->xreq.context);
	json_object_put(svcreq->xreq.json);
	afb_cred_unref(svcreq->xreq.cred);
	free(svcreq);
}

static void svcreq_sync_leave(struct svc_req *svcreq)
{
	struct jobloop *jobloop = svcreq->jobloop;

	if (jobloop) {
		svcreq->jobloop = NULL;
		jobs_leave(jobloop);
	}
}

static void svcreq_reply(struct afb_xreq *xreq, int status, json_object *obj)
{
	struct svc_req *svcreq = CONTAINER_OF_XREQ(struct svc_req, xreq);
	if (svcreq->async) {
		struct afb_svc *svc = svcreq->svc;
		if (svcreq->callback)
			svcreq->callback(svcreq->closure, status, obj);
		HOOK(call_result, svc, status, obj);
		json_object_put(obj);
	} else {
		svcreq->status = status;
		svcreq->result = obj;
		svcreq_sync_leave(svcreq);
	}
}

static void svcreq_sync_enter(int signum, void *closure, struct jobloop *jobloop)
{
	struct svc_req *svcreq = closure;

	if (!signum) {
		svcreq->jobloop = jobloop;
		afb_xreq_process(&svcreq->xreq, svcreq->svc->apiset);
	} else {
		svcreq->result = afb_msg_json_internal_error();
		svcreq->status = -1;
		svcreq_sync_leave(svcreq);
	}
}

/*
 * Initiates a call for the service
 */
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)
{
	struct afb_svc *svc = closure;
	struct svc_req *svcreq;
	struct json_object *ierr;

	HOOK(call, svc, api, verb, args);

	/* allocates the request */
	svcreq = svcreq_create(svc, api, verb, args);
	if (svcreq == NULL) {
		ERROR("out of memory");
		json_object_put(args);
		ierr = afb_msg_json_internal_error();
		if (callback)
			callback(cbclosure, -1, ierr);
		HOOK(call_result, svc, -1, ierr);
		json_object_put(ierr);
		return;
	}

	/* initialises the request */
	svcreq->jobloop = NULL;
	svcreq->callback = callback;
	svcreq->closure = cbclosure;
	svcreq->async = 1;

	/* terminates and frees ressources if needed */
	afb_xreq_process(&svcreq->xreq, svc->apiset);
}

static int svc_call_sync(void *closure, const char *api, const char *verb, struct json_object *args,
				struct json_object **result)
{
	struct afb_svc *svc = closure;
	struct svc_req *svcreq;
	struct json_object *resu;
	int rc;

	HOOK(callsync, svc, api, verb, args);

	/* allocates the request */
	svcreq = svcreq_create(svc, api, verb, args);
	if (svcreq == NULL) {
		ERROR("out of memory");
		errno = ENOMEM;
		json_object_put(args);
		resu = afb_msg_json_internal_error();
		rc = -1;
	} else {
		/* initialises the request */
		svcreq->jobloop = NULL;
		svcreq->callback = NULL;
		svcreq->result = NULL;
		svcreq->status = 0;
		svcreq->async = 0;
		afb_xreq_addref(&svcreq->xreq);
		rc = jobs_enter(NULL, 0, svcreq_sync_enter, svcreq);
		if (rc >= 0)
			rc = svcreq->status;
		resu = (rc >= 0 || svcreq->result) ? svcreq->result : afb_msg_json_internal_error();
		afb_xreq_unref(&svcreq->xreq);
	}
	HOOK(callsync_result, svc, rc, resu);
	if (result)
		*result = resu;
	else
		json_object_put(resu);
	return rc;
}