session: tiny refactor of creations 43/5943/1
authorJosé Bollo <jose.bollo@iot.bzh>
Fri, 10 Jun 2016 15:35:54 +0000 (17:35 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Fri, 10 Jun 2016 15:35:54 +0000 (17:35 +0200)
Change-Id: Ie4bc15448203621d0f84c9be6f57c68cc88983b4
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/session.c
src/session.h

index e0d0a8e..ddff2c3 100644 (file)
@@ -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)
index 497951a..44c6d7b 100644 (file)
@@ -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);