}
}
-// 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;
if (uuid == NULL) {
new_uuid(clientCtx->uuid);
} else {
+ if (strlen(uuid) >= sizeof clientCtx->uuid) {
+ errno = EINVAL;
+ goto error2;
+ }
strcpy(clientCtx->uuid, uuid);
}
errno = ENOMEM;
goto error2;
}
- *created = 1;
-found:
clientCtx->access = now;
- clientCtx->refcount++;
+ clientCtx->refcount = 1;
return clientCtx;
error2:
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)
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);