- /* returns a new one */
- clientCtx = calloc(1, sizeof(struct AFB_clientCtx)); // init NULL clientContext
- if (clientCtx != NULL) {
- clientCtx->contexts = calloc ((unsigned)sessions.apicount, sizeof(*clientCtx->contexts));
- if (clientCtx->contexts != NULL) {
- /* generate the uuid */
- if (uuid == NULL) {
- uuid_generate(newuuid);
- uuid_unparse_lower(newuuid, clientCtx->uuid);
- } else {
- strcpy(clientCtx->uuid, uuid);
- }
- strcpy(clientCtx->token, sessions.initok);
- clientCtx->expiration = now + sessions.timeout;
- clientCtx->refcount = 1;
- if (ctxStoreAdd (clientCtx))
- return clientCtx;
- free(clientCtx->contexts);
+ 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;