From 541756a5f6af26338add0b1ac7a1c0c3bb518b80 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=83=C2=A9=20Bollo?= Date: Mon, 6 Nov 2017 15:05:45 +0100 Subject: [PATCH] afb-session: add function afb_session_search MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Also the function afb_session_get always create a session even if 'created' is NULL Change-Id: Ia5ac1231e1d61e92cb9bbc07c968e3000d6864ff Signed-off-by: José Bollo --- src/afb-session.c | 26 ++++++++++++++++++++------ src/afb-session.h | 1 + src/afb-trace.c | 13 ++++++++----- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/afb-session.c b/src/afb-session.c index e1d5a458..a28a35f6 100644 --- a/src/afb-session.c +++ b/src/afb-session.c @@ -284,6 +284,7 @@ error: return NULL; } +/* Creates a new session with 'timeout' */ struct afb_session *afb_session_create (int timeout) { time_t now; @@ -295,7 +296,19 @@ struct afb_session *afb_session_create (int timeout) return make_session(NULL, timeout, now); } -// This function will return exiting session or newly created session +/* Searchs the session of 'uuid' */ +struct afb_session *afb_session_search (const char *uuid) +{ + time_t now; + + /* cleaning */ + now = NOW; + cleanup (now); + return search(uuid); + +} + +/* This function will return exiting session or newly created session */ struct afb_session *afb_session_get (const char *uuid, int *created) { struct afb_session *session; @@ -308,20 +321,21 @@ struct afb_session *afb_session_get (const char *uuid, int *created) /* search for an existing one not too old */ if (uuid != NULL) { session = search(uuid); - if (!created) - return session; if (session != NULL) { - *created = 0; + if (created) + *created = 0; session->access = now; session->refcount++; return session; } } + /* no existing session found, create it */ + session = make_session(uuid, sessions.timeout, now); if (created) - *created = 1; + *created = !!session; - return make_session(uuid, sessions.timeout, now); + return session; } struct afb_session *afb_session_addref(struct afb_session *session) diff --git a/src/afb-session.h b/src/afb-session.h index c76d6769..a8009bfd 100644 --- a/src/afb-session.h +++ b/src/afb-session.h @@ -23,6 +23,7 @@ extern void afb_session_init(int max_session_count, int timeout, const char *ini 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 const char *afb_session_uuid (struct afb_session *session); diff --git a/src/afb-trace.c b/src/afb-trace.c index cefd2058..eb1122d4 100644 --- a/src/afb-trace.c +++ b/src/afb-trace.c @@ -1122,12 +1122,15 @@ static void *session_open(void *closure) static struct afb_session *trace_get_session_by_uuid(struct afb_trace *trace, const char *uuid, int alloc) { struct cookie cookie; - int created; - cookie.session = afb_session_get(uuid, alloc ? &created : NULL); - if (cookie.session) { - cookie.trace = trace; - afb_session_cookie(cookie.session, cookie.trace, session_open, session_closed, &cookie, 0); + if (!alloc) + cookie.session = afb_session_search(uuid); + else { + cookie.session = afb_session_get(uuid, NULL); + if (cookie.session) { + cookie.trace = trace; + afb_session_cookie(cookie.session, cookie.trace, session_open, session_closed, &cookie, 0); + } } return cookie.session; } -- 2.16.6