+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+// This function will return exiting client context or newly created client context
+AFB_clientCtx *_ctxClientGet (const char *uuid)
+{
+ uuid_t newuuid;
+ AFB_clientCtx *clientCtx;
+
+ /* search for an existing one not too old */
+ clientCtx = uuid != NULL ? ctxStoreSearch (uuid) : NULL;
+ if (clientCtx) {
+ if (!ctxStoreTooOld (clientCtx, NOW))
+ return clientCtx;
+ ctxStoreDel (clientCtx);
+ }
+
+ /* mimic old behaviour */
+ if (sessions.initok == NULL)
+ return NULL;
+
+ /* cleanup before creating */
+ if(2 * sessions.count >= sessions.max)
+ ctxStoreGarbage();
+
+ /* returns a new one */
+ clientCtx = calloc(1, sizeof(AFB_clientCtx)); // init NULL clientContext
+ if (clientCtx != NULL) {
+ clientCtx->contexts = calloc ((unsigned)sessions.apicount, sizeof (void*));
+ if (clientCtx->contexts != NULL) {
+ /* generate the uuid */
+ uuid_generate(newuuid);
+ uuid_unparse_lower(newuuid, clientCtx->uuid);
+ clientCtx->timeStamp = time(NULL) + sessions.timeout;
+ strcpy(clientCtx->token, sessions.initok);
+ if (AFB_SUCCESS == ctxStoreAdd (clientCtx))
+ return clientCtx;
+ free(clientCtx->contexts);
+ }
+ free(clientCtx);
+ }
+ return NULL;
+}
+
+// Free Client Session Context
+AFB_error _ctxClientDel (AFB_clientCtx *clientCtx)
+{
+ assert(clientCtx != NULL);
+ return ctxStoreDel (clientCtx);
+}
+
+// Sample Generic Ping Debug API
+AFB_error _ctxTokenCheck (AFB_clientCtx *clientCtx, const char *token)
+{
+ assert(clientCtx != NULL);
+ assert(token != NULL);
+
+ // compare current token with previous one
+ if (ctxStoreTooOld (clientCtx, NOW))
+ return AFB_FAIL;
+ if (!clientCtx->token[0] || 0 == strcmp (token, clientCtx->token)) {
+ clientCtx->timeStamp = time(NULL) + sessions.timeout;
+ return AFB_SUCCESS;
+ }
+
+ // Token is not valid let move level of assurance to zero and free attached client handle
+ return AFB_FAIL;
+}
+
+// generate a new token and update client context
+AFB_error _ctxTokenNew (AFB_clientCtx *clientCtx)
+{
+ uuid_t newuuid;
+
+ assert(clientCtx != NULL);
+
+ // Old token was valid let's regenerate a new one
+ uuid_generate(newuuid); // create a new UUID
+ uuid_unparse_lower(newuuid, clientCtx->token);
+
+ // keep track of time for session timeout and further clean up
+ clientCtx->timeStamp = time(NULL) + sessions.timeout;
+
+ return AFB_SUCCESS;
+}
+