afb-session: Add function to enumerate sessions
authorJosé Bollo <jose.bollo@iot.bzh>
Mon, 12 Feb 2018 13:27:46 +0000 (14:27 +0100)
committerJosé Bollo <jose.bollo@iot.bzh>
Tue, 13 Feb 2018 08:20:24 +0000 (09:20 +0100)
Change-Id: Ifaa1aa02d0562c40763ac03b7c05df7f9f9c8127
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/afb-session.c
src/afb-session.h

index c23d414..17ca7af 100644 (file)
@@ -346,6 +346,28 @@ int afb_session_init (int max_session_count, int timeout, const char *initok)
        return 0;
 }
 
+/**
+ * Iterate the sessions and call 'callback' with
+ * the 'closure' for each session.
+ */
+void afb_session_foreach(void (*callback)(void *closure, struct afb_session *session), void *closure)
+{
+       struct afb_session *session;
+       int idx;
+
+       /* Loop on Sessions Table and remove anything that is older than timeout */
+       sessionset_lock();
+       for (idx = 0 ; idx < HEADCOUNT; idx++) {
+               session = sessions.heads[idx];
+               while (session) {
+                       if (!session->closed)
+                               callback(closure, session);
+                       session = session->next;
+               }
+       }
+       sessionset_unlock();
+}
+
 /**
  * Cleanup the sessionset of its closed or expired sessions
  */
index 2fdd1a6..c93db5c 100644 (file)
@@ -26,6 +26,7 @@ struct afb_session;
 extern int afb_session_init(int max_session_count, int timeout, const char *initok);
 extern void afb_session_purge();
 extern const char *afb_session_initial_token();
+extern void afb_session_foreach(void (*callback)(void *closure, struct afb_session *session), void *closure);
 
 extern struct afb_session *afb_session_create (int timeout);
 extern struct afb_session *afb_session_search (const char *uuid);