summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2018-02-15 16:51:25 +0100
committerJosé Bollo <jose.bollo@iot.bzh>2018-02-15 17:23:33 +0100
commit66df82496e8b5cf0e49e8fe4ddd57827bbd0e3c5 (patch)
treef42e3ccf14aa727a5ba78a761564eba72d0c6bba
parent6e7f3d28a2d08a8976fdd9c586b4cbddcaeb37bd (diff)
afb-session: Verbs for timeout and remining time
Change-Id: Ia1922187aa78d8633835c4a77e8aa533fa5d5f7d Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r--src/afb-session.c38
-rw-r--r--src/afb-session.h2
2 files changed, 29 insertions, 11 deletions
diff --git a/src/afb-session.c b/src/afb-session.c
index c312b46c..c63c6c46 100644
--- a/src/afb-session.c
+++ b/src/afb-session.c
@@ -22,6 +22,7 @@
#include <pthread.h>
#include <stdlib.h>
#include <stdint.h>
+#include <limits.h>
#include <string.h>
#include <uuid/uuid.h>
#include <errno.h>
@@ -227,21 +228,12 @@ static void session_destroy (struct afb_session *session)
/* update expiration of 'session' according to 'now' */
static void session_update_expiration(struct afb_session *session, time_t now)
{
- int timeout;
time_t expiration;
/* compute expiration */
- timeout = session->timeout;
- if (timeout == AFB_SESSION_TIMEOUT_INFINITE)
+ expiration = now + afb_session_timeout(session);
+ if (expiration < 0)
expiration = MAX_EXPIRATION;
- else {
- if (timeout == AFB_SESSION_TIMEOUT_DEFAULT)
- expiration = now + sessions.timeout;
- else
- expiration = now + timeout;
- if (expiration < 0)
- expiration = MAX_EXPIRATION;
- }
/* record the expiration */
session->expiration = expiration;
@@ -416,6 +408,30 @@ struct afb_session *afb_session_create (int timeout)
return afb_session_get(NULL, timeout, NULL);
}
+/**
+ * Returns the timeout of 'session' in seconds
+ */
+int afb_session_timeout(struct afb_session *session)
+{
+ int timeout;
+
+ /* compute timeout */
+ timeout = session->timeout;
+ if (timeout == AFB_SESSION_TIMEOUT_DEFAULT)
+ timeout = sessions.timeout;
+ if (timeout < 0)
+ timeout = INT_MAX;
+ return timeout;
+}
+
+/**
+ * Returns the second remaining before expiration of 'session'
+ */
+int afb_session_what_remains(struct afb_session *session)
+{
+ return (int)(session->expiration - NOW);
+}
+
/* This function will return exiting session or newly created session */
struct afb_session *afb_session_get (const char *uuid, int timeout, int *created)
{
diff --git a/src/afb-session.h b/src/afb-session.h
index c93db5c4..8e2ee3d6 100644
--- a/src/afb-session.h
+++ b/src/afb-session.h
@@ -43,6 +43,8 @@ extern int afb_session_is_closed (struct afb_session *session);
extern int afb_session_check_token(struct afb_session *session, const char *token);
extern void afb_session_new_token(struct afb_session *session);
extern const char *afb_session_token(struct afb_session *session);
+extern int afb_session_timeout(struct afb_session *session);
+extern int afb_session_what_remains(struct afb_session *session);
extern void *afb_session_get_cookie(struct afb_session *session, const void *key);
extern void *afb_session_cookie(struct afb_session *session, const void *key, void *(*makecb)(void *closure), void (*freecb)(void *item), void *closure, int replace);