afb-session: Add timeout features for sessions
authorJosé Bollo <jose.bollo@iot.bzh>
Mon, 6 Nov 2017 14:31:34 +0000 (15:31 +0100)
committerJosé Bollo <jose.bollo@iot.bzh>
Mon, 6 Nov 2017 14:31:34 +0000 (15:31 +0100)
Change-Id: I0aa8a82c0bbf709aa380ef7e5efe2e4ebaf454c0
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/afb-context.c
src/afb-session.c
src/afb-session.h
src/afb-trace.c

index 759ee90..c2649a4 100644 (file)
@@ -63,7 +63,7 @@ int afb_context_connect(struct afb_context *context, const char *uuid, const cha
        int created;
        struct afb_session *session;
 
-       session = afb_session_get (uuid, &created);
+       session = afb_session_get (uuid, AFB_SESSION_TIMEOUT_DEFAULT, &created);
        if (session == NULL)
                return -1;
        init_context(context, session, token);
index a28a35f..734e0b2 100644 (file)
@@ -240,6 +240,12 @@ static struct afb_session *make_session (const char *uuid, int timeout, time_t n
 {
        struct afb_session *session;
 
+       if (!AFB_SESSION_TIMEOUT_IS_VALID(timeout)
+        || (uuid && strlen(uuid) >= sizeof session->uuid)) {
+               errno = EINVAL;
+               goto error;
+       }
+
        /* allocates a new one */
        session = calloc(1, sizeof *session);
        if (session == NULL) {
@@ -252,19 +258,18 @@ static struct afb_session *make_session (const char *uuid, int timeout, time_t n
        if (uuid == NULL) {
                do { new_uuid(session->uuid); } while(search(session->uuid));
        } else {
-               if (strlen(uuid) >= sizeof session->uuid) {
-                       errno = EINVAL;
-                       goto error2;
-               }
                strcpy(session->uuid, uuid);
        }
 
        /* init the token */
        strcpy(session->token, sessions.initok);
+
+       /* init timeout */
+       if (timeout == AFB_SESSION_TIMEOUT_DEFAULT)
+               timeout = sessions.timeout;
        session->timeout = timeout;
-       if (timeout != 0)
-               session->expiration = now + timeout;
-       else {
+       session->expiration = now + timeout;
+       if (timeout == AFB_SESSION_TIMEOUT_INFINITE || session->expiration < 0) {
                session->expiration = (time_t)(~(time_t)0);
                if (session->expiration < 0)
                        session->expiration = (time_t)(((unsigned long long)session->expiration) >> 1);
@@ -300,16 +305,18 @@ struct afb_session *afb_session_create (int timeout)
 struct afb_session *afb_session_search (const char *uuid)
 {
        time_t now;
+       struct afb_session *session;
 
        /* cleaning */
        now = NOW;
        cleanup (now);
-       return search(uuid);
+       session = search(uuid);
+       return session;
 
 }
 
 /* This function will return exiting session or newly created session */
-struct afb_session *afb_session_get (const char *uuid, int *created)
+struct afb_session *afb_session_get (const char *uuid, int timeout, int *created)
 {
        struct afb_session *session;
        time_t now;
@@ -331,7 +338,7 @@ struct afb_session *afb_session_get (const char *uuid, int *created)
        }
 
        /* no existing session found, create it */
-       session = make_session(uuid, sessions.timeout, now);
+       session = make_session(uuid, timeout, now);
        if (created)
                *created = !!session;
 
index a8009bf..37cbeee 100644 (file)
 
 struct afb_session;
 
+#define AFB_SESSION_TIMEOUT_INFINITE  -1
+#define AFB_SESSION_TIMEOUT_DEFAULT   -2
+#define AFB_SESSION_TIMEOUT_IS_VALID(x) ((x) >= AFB_SESSION_TIMEOUT_DEFAULT)
+
 extern void afb_session_init(int max_session_count, int timeout, const char *initok);
 extern const char *afb_session_initial_token();
 
 extern struct afb_session *afb_session_create (int timeout);
 extern struct afb_session *afb_session_search (const char *uuid);
-extern struct afb_session *afb_session_get (const char *uuid, int *created);
+extern struct afb_session *afb_session_get (const char *uuid, int timeout, int *created);
 extern const char *afb_session_uuid (struct afb_session *session);
 
 extern struct afb_session *afb_session_addref(struct afb_session *session);
index eb1122d..f0efd5f 100644 (file)
@@ -1126,7 +1126,7 @@ static struct afb_session *trace_get_session_by_uuid(struct afb_trace *trace, co
        if (!alloc)
                cookie.session = afb_session_search(uuid);
        else {
-               cookie.session = afb_session_get(uuid, NULL);
+               cookie.session = afb_session_get(uuid, AFB_SESSION_TIMEOUT_DEFAULT, NULL);
                if (cookie.session) {
                        cookie.trace = trace;
                        afb_session_cookie(cookie.session, cookie.trace, session_open, session_closed, &cookie, 0);