From 7ffb5e1685bec55b3edc65ed93f9f0346178b31f Mon Sep 17 00:00:00 2001 From: Tamino Huxohl Date: Tue, 4 Oct 2022 21:34:00 +0200 Subject: [PATCH 1/3] move AuthenticationFutureList into its on file and fix #73 --- .../lib/future/AuthenticationFutureList.kt | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 module/authentication/lib/src/main/java/org/openbase/bco/authentication/lib/future/AuthenticationFutureList.kt diff --git a/module/authentication/lib/src/main/java/org/openbase/bco/authentication/lib/future/AuthenticationFutureList.kt b/module/authentication/lib/src/main/java/org/openbase/bco/authentication/lib/future/AuthenticationFutureList.kt new file mode 100644 index 0000000000..a63e08322d --- /dev/null +++ b/module/authentication/lib/src/main/java/org/openbase/bco/authentication/lib/future/AuthenticationFutureList.kt @@ -0,0 +1,55 @@ +package org.openbase.bco.authentication.lib.future + +import org.openbase.jul.iface.Shutdownable +import org.openbase.jul.schedule.GlobalScheduledExecutorService +import org.openbase.jul.schedule.SyncObject +import org.slf4j.LoggerFactory +import java.util.concurrent.ExecutionException +import java.util.concurrent.TimeUnit + +object AuthenticationFutureList { + + private const val SCHEDULE_RATE_IN_S = 5L + private const val INITIAL_DELAY_IN_S = 1L + private const val FUTURE_TIMEOUT_IN_MS = 1L + + private val listSync = SyncObject("AuthenticatedFutureListSync") + private val authenticatedFutureList: MutableList> = ArrayList() + private val logger = LoggerFactory.getLogger("AuthenticationFuture") + + private fun isFailed(future: AbstractAuthenticationFuture<*, *>): Boolean { + try { + // Note: the abstract authentication future will remove itself from this list if + // get finished successfully. + future.get(FUTURE_TIMEOUT_IN_MS, TimeUnit.MILLISECONDS) + } catch (ex: ExecutionException) { + return true + } + return false + } + + init { + synchronized(listSync) { + // create a task which makes sure that get is called on all of these futures so that tickets are renewed + val responseVerificationFuture = GlobalScheduledExecutorService.scheduleAtFixedRate( + { + synchronized(listSync) { + authenticatedFutureList + .filter { it.isCancelled || isFailed(it) } + .let { authenticatedFutureList.removeAll(it) } + + } + }, INITIAL_DELAY_IN_S, SCHEDULE_RATE_IN_S, TimeUnit.SECONDS + ) + Shutdownable.registerShutdownHook { responseVerificationFuture.cancel(true) } + } + } + + fun addFuture(authenticationFuture: AbstractAuthenticationFuture<*, *>) { + synchronized(listSync) { authenticatedFutureList.add(authenticationFuture) } + } + + fun removeFuture(authenticationFuture: AbstractAuthenticationFuture<*, *>) { + synchronized(listSync) { authenticatedFutureList.remove(authenticationFuture) } + } +} From d6055b5fad002da6d9954b72f460151750df851b Mon Sep 17 00:00:00 2001 From: Tamino Huxohl Date: Tue, 4 Oct 2022 21:34:31 +0200 Subject: [PATCH 2/3] also commit changes to AbstractAuthenticationFuture --- .../future/AbstractAuthenticationFuture.java | 42 ++----------------- 1 file changed, 3 insertions(+), 39 deletions(-) diff --git a/module/authentication/lib/src/main/java/org/openbase/bco/authentication/lib/future/AbstractAuthenticationFuture.java b/module/authentication/lib/src/main/java/org/openbase/bco/authentication/lib/future/AbstractAuthenticationFuture.java index dd5004a696..c51081ac72 100644 --- a/module/authentication/lib/src/main/java/org/openbase/bco/authentication/lib/future/AbstractAuthenticationFuture.java +++ b/module/authentication/lib/src/main/java/org/openbase/bco/authentication/lib/future/AbstractAuthenticationFuture.java @@ -32,6 +32,7 @@ import org.openbase.jul.iface.Shutdownable; import org.openbase.jul.schedule.GlobalScheduledExecutorService; import org.openbase.jul.schedule.SyncObject; +import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.openbase.type.domotic.authentication.TicketAuthenticatorWrapperType.TicketAuthenticatorWrapper; @@ -49,10 +50,6 @@ */ public abstract class AbstractAuthenticationFuture implements Future { - private static final List authenticatedFutureList = new ArrayList<>(); - private static final SyncObject listSync = new SyncObject("AuthenticatedFutureListSync"); - private static ScheduledFuture responseVerificationFuture = null; - private final Future internalFuture; private final SessionManager sessionManager; private final Class returnClass; @@ -83,38 +80,7 @@ public AbstractAuthenticationFuture(final Future internalFuture, final this.sessionManager = sessionManager; this.wrapper = wrapper; - synchronized (listSync) { - if (responseVerificationFuture == null) { - // create a task which makes sure that get is called on all of these futures so that tickets are renewed - try { - responseVerificationFuture = GlobalScheduledExecutorService.scheduleAtFixedRate(() -> { - synchronized (listSync) { - for (final AbstractAuthenticationFuture future : new ArrayList<>(authenticatedFutureList)) { - if (future.isCancelled()) { - authenticatedFutureList.remove(future); - } - - if (future.isDone()) { - try { - future.get(); - } catch (InterruptedException ex) { - Thread.currentThread().interrupt(); - } catch (ExecutionException ex) { - authenticatedFutureList.remove(future); - } - } - } - } - }, 1, 5, TimeUnit.SECONDS); - Shutdownable.registerShutdownHook(() -> responseVerificationFuture.cancel(true)); - } catch (CouldNotPerformException ex) { - if (!ExceptionProcessor.isCausedBySystemShutdown(ex)) { - ExceptionPrinter.printHistory("Could not initialize task which makes sure that authenticated response are verified", ex, LoggerFactory.getLogger(AbstractAuthenticationFuture.class)); - } - } - } - authenticatedFutureList.add(this); - } + AuthenticationFutureList.INSTANCE.addFuture(this); } /** @@ -214,9 +180,7 @@ private void verifyResponse(TicketAuthenticatorWrapper ticketAuthenticatorWrappe } catch (CouldNotPerformException ex) { throw new CouldNotPerformException("Could not verify ServiceServer Response", ex); } finally { - synchronized (listSync) { - authenticatedFutureList.remove(this); - } + AuthenticationFutureList.INSTANCE.removeFuture(this); } } From b1ca307c08a3ea312e57063f42f67e45700b6813 Mon Sep 17 00:00:00 2001 From: Tamino Huxohl Date: Thu, 6 Oct 2022 08:22:39 +0200 Subject: [PATCH 3/3] handle timeout exception in AuthenticationFutureList --- .../bco/authentication/lib/future/AuthenticationFutureList.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/module/authentication/lib/src/main/java/org/openbase/bco/authentication/lib/future/AuthenticationFutureList.kt b/module/authentication/lib/src/main/java/org/openbase/bco/authentication/lib/future/AuthenticationFutureList.kt index a63e08322d..6690f922cd 100644 --- a/module/authentication/lib/src/main/java/org/openbase/bco/authentication/lib/future/AuthenticationFutureList.kt +++ b/module/authentication/lib/src/main/java/org/openbase/bco/authentication/lib/future/AuthenticationFutureList.kt @@ -6,6 +6,7 @@ import org.openbase.jul.schedule.SyncObject import org.slf4j.LoggerFactory import java.util.concurrent.ExecutionException import java.util.concurrent.TimeUnit +import java.util.concurrent.TimeoutException object AuthenticationFutureList { @@ -24,6 +25,8 @@ object AuthenticationFutureList { future.get(FUTURE_TIMEOUT_IN_MS, TimeUnit.MILLISECONDS) } catch (ex: ExecutionException) { return true + } catch (ex: TimeoutException) { + return false } return false }