+/**
+ * Initialize the session manager with a 'max_session_count',
+ * an initial common 'timeout' and an initial common token 'initok'.
+ *
+ * @param max_session_count maximum allowed session count in the same time
+ * @param timeout the initial default timeout of sessions
+ * @param initok the initial default token of sessions
+ *
+ */
+int afb_session_init (int max_session_count, int timeout, const char *initok)
+{
+ /* check parameters */
+ if (initok && strlen(initok) >= sizeof sessions.initok) {
+ ERROR("initial token '%s' too long (max length %d)",
+ initok, ((int)(sizeof sessions.initok)) - 1);
+ errno = EINVAL;
+ return -1;
+ }
+
+ /* init the sessionset (after cleanup) */
+ sessionset_lock();
+ sessionset_cleanup(1);
+ sessions.max = max_session_count;
+ sessions.timeout = timeout;
+ if (initok == NULL)
+ new_uuid(sessions.initok);
+ else
+ strcpy(sessions.initok, initok);
+ sessionset_unlock();
+ 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
+ */
+void afb_session_purge()
+{
+ sessionset_lock();
+ sessionset_cleanup(0);
+ sessionset_unlock();
+}
+
+/**
+ * @return the initial token set at initialization
+ */
+const char *afb_session_initial_token()
+{
+ return sessions.initok;