From: José Bollo Date: Fri, 10 Jun 2016 15:35:54 +0000 (+0200) Subject: session: tiny refactor of creations X-Git-Tag: blowfish_2.0.1~29 X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F43%2F5943%2F1;p=src%2Fapp-framework-binder.git session: tiny refactor of creations Change-Id: Ie4bc15448203621d0f84c9be6f57c68cc88983b4 Signed-off-by: José Bollo --- 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);