aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2016-06-10 17:35:54 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2016-06-10 17:35:54 +0200
commit5f65a54eda6644f3e151c4f60ae88d9e163abcba (patch)
tree7767ede36eba91e9d39910864ca0567836604485
parent7a322e20bde6e9bfbc7e98aa5a7b376ba53dccb4 (diff)
session: tiny refactor of creations
Change-Id: Ie4bc15448203621d0f84c9be6f57c68cc88983b4 Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r--src/session.c73
-rw-r--r--src/session.h1
2 files changed, 50 insertions, 24 deletions
diff --git a/src/session.c b/src/session.c
index e0d0a8e6..ddff2c3e 100644
--- a/src/session.c
+++ b/src/session.c
@@ -196,30 +196,11 @@ static void ctxStoreCleanUp (time_t now)
}
}
-// This function will return exiting client context or newly created client context
-struct AFB_clientCtx *ctxClientGetSession (const char *uuid, int *created)
+static struct AFB_clientCtx *new_context (const char *uuid, int timeout, time_t now)
{
struct AFB_clientCtx *clientCtx;
- time_t now;
-
- /* cleaning */
- now = NOW;
- ctxStoreCleanUp (now);
-
- /* search for an existing one not too old */
- if (uuid != NULL) {
- if (strlen(uuid) >= sizeof clientCtx->uuid) {
- errno = EINVAL;
- goto error;
- }
- clientCtx = ctxStoreSearch(uuid);
- if (clientCtx != NULL) {
- *created = 0;
- goto found;
- }
- }
- /* returns a new one */
+ /* allocates a new one */
clientCtx = calloc(1, sizeof(struct AFB_clientCtx) + ((unsigned)sessions.apicount * sizeof(*clientCtx->values)));
if (clientCtx == NULL) {
errno = ENOMEM;
@@ -231,6 +212,10 @@ struct AFB_clientCtx *ctxClientGetSession (const char *uuid, int *created)
if (uuid == NULL) {
new_uuid(clientCtx->uuid);
} else {
+ if (strlen(uuid) >= sizeof clientCtx->uuid) {
+ errno = EINVAL;
+ goto error2;
+ }
strcpy(clientCtx->uuid, uuid);
}
@@ -241,11 +226,9 @@ struct AFB_clientCtx *ctxClientGetSession (const char *uuid, int *created)
errno = ENOMEM;
goto error2;
}
- *created = 1;
-found:
clientCtx->access = now;
- clientCtx->refcount++;
+ clientCtx->refcount = 1;
return clientCtx;
error2:
@@ -254,6 +237,48 @@ error:
return NULL;
}
+struct AFB_clientCtx *ctxClientCreate (const char *uuid, int timeout)
+{
+ time_t now;
+
+ /* cleaning */
+ now = NOW;
+ ctxStoreCleanUp (now);
+
+ /* search for an existing one not too old */
+ if (uuid != NULL && ctxStoreSearch(uuid) != NULL) {
+ errno = EEXIST;
+ return NULL;
+ }
+
+ return new_context(uuid, timeout, now);
+}
+
+// This function will return exiting client context or newly created client context
+struct AFB_clientCtx *ctxClientGetSession (const char *uuid, int *created)
+{
+ struct AFB_clientCtx *clientCtx;
+ time_t now;
+
+ /* cleaning */
+ now = NOW;
+ ctxStoreCleanUp (now);
+
+ /* search for an existing one not too old */
+ if (uuid != NULL) {
+ clientCtx = ctxStoreSearch(uuid);
+ if (clientCtx != NULL) {
+ *created = 0;
+ clientCtx->access = now;
+ clientCtx->refcount++;
+ return clientCtx;
+ }
+ }
+
+ *created = 1;
+ return new_context(uuid, sessions.timeout, now);
+}
+
struct AFB_clientCtx *ctxClientAddRef(struct AFB_clientCtx *clientCtx)
{
if (clientCtx != NULL)
diff --git a/src/session.h b/src/session.h
index 497951af..44c6d7be 100644
--- a/src/session.h
+++ b/src/session.h
@@ -22,6 +22,7 @@ struct AFB_clientCtx;
extern void ctxStoreInit (int max_session_count, int timeout, const char *initok, int context_count);
+extern struct AFB_clientCtx *ctxClientCreate (const char *uuid, int timeout);
extern struct AFB_clientCtx *ctxClientGetSession (const char *uuid, int *created);
extern struct AFB_clientCtx *ctxClientAddRef(struct AFB_clientCtx *clientCtx);
extern void ctxClientUnref(struct AFB_clientCtx *clientCtx);