Refactoring requests and context handling
[src/app-framework-binder.git] / src / afb-context.c
1 /*
2  * Copyright (C) 2015 "IoT.bzh"
3  * Author "Fulup Ar Foll"
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 <assert.h>
21 #include <stdlib.h>
22
23 #include "session.h"
24 #include "afb-context.h"
25
26
27 void afb_context_init(struct afb_context *context, struct AFB_clientCtx *session, const char *token)
28 {
29         assert(session != NULL);
30
31         /* reset the context for the session */
32         context->session = session;
33         context->flags = 0;
34         context->api_index = -1;
35
36         /* check the token */
37         if (token != NULL) {
38                 if (ctxTokenCheck(session, token))
39                         context->validated = 1;
40                 else
41                         context->invalidated = 1;
42         }
43 }
44
45 int afb_context_connect(struct afb_context *context, const char *uuid, const char *token)
46 {
47         int created;
48         struct AFB_clientCtx *session;
49
50         session = ctxClientGetSession (uuid, &created);
51         if (session == NULL)
52                 return -1;
53         afb_context_init(context, session, token);
54         if (created)
55                 context->created = 1;
56         return 0;
57 }
58
59 void afb_context_disconnect(struct afb_context *context)
60 {
61         if (context->session != NULL) {
62                 if (context->closing && !context->closed) {
63                         context->closed = 1;
64                         ctxClientClose(context->session);
65                 }
66                 ctxClientUnref(context->session);
67         }
68 }
69
70 const char *afb_context_sent_token(struct afb_context *context)
71 {
72         if (context->session == NULL || context->closing)
73                 return NULL;
74         if (!(context->created || context->refreshing))
75                 return NULL;
76         if (!context->refreshed) {
77                 ctxTokenNew (context->session);
78                 context->refreshed = 1;
79         }
80         return ctxClientGetToken(context->session);
81 }
82
83 const char *afb_context_sent_uuid(struct afb_context *context)
84 {
85         if (context->session == NULL || context->closing)
86                 return NULL;
87         if (!context->created)
88                 return NULL;
89         return ctxClientGetUuid(context->session);
90 }
91
92 void *afb_context_get(struct afb_context *context)
93 {
94         assert(context->session != NULL);
95         return ctxClientValueGet(context->session, context->api_index);
96 }
97
98 void afb_context_set(struct afb_context *context, void *value, void (*free_value)(void*))
99 {
100         assert(context->session != NULL);
101         return ctxClientValueSet(context->session, context->api_index, value, free_value);
102 }
103
104 void afb_context_close(struct afb_context *context)
105 {
106         context->closing = 1;
107 }
108
109 void afb_context_refresh(struct afb_context *context)
110 {
111         context->refreshing = 1;
112 }
113
114 int afb_context_check(struct afb_context *context)
115 {
116         return context->validated;
117 }
118
119 int afb_context_create(struct afb_context *context)
120 {
121         return context->created;
122 }