From 447c64b41c5795c066936f61cfc06eb3a9d36854 Mon Sep 17 00:00:00 2001 From: hvanwelbergen <hvanwelbergen@techfak.uni-bielefeld.de> Date: Sun, 3 Apr 2016 13:50:52 +0200 Subject: [PATCH] - added removeHandler to Buffer - FutureIU can now use an existing InputBuffer --- ipaacalib/java/src/ipaaca/Buffer.java | 5 ++ .../ipaaca/util/communication/FutureIU.java | 48 ++++++++++++------- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/ipaacalib/java/src/ipaaca/Buffer.java b/ipaacalib/java/src/ipaaca/Buffer.java index d8851d4..c56a985 100644 --- a/ipaacalib/java/src/ipaaca/Buffer.java +++ b/ipaacalib/java/src/ipaaca/Buffer.java @@ -110,6 +110,11 @@ public abstract class Buffer { eventHandlers.add(handler); } + + public void removeHandler(IUEventHandler handler) + { + eventHandlers.remove(handler); + } public void registerHandler(HandlerFunctor func) { IUEventHandler handler; diff --git a/ipaacalib/java/src/ipaaca/util/communication/FutureIU.java b/ipaacalib/java/src/ipaaca/util/communication/FutureIU.java index 1cd4854..7c69017 100644 --- a/ipaacalib/java/src/ipaaca/util/communication/FutureIU.java +++ b/ipaacalib/java/src/ipaaca/util/communication/FutureIU.java @@ -8,6 +8,7 @@ import com.google.common.collect.ImmutableSet; import ipaaca.AbstractIU; import ipaaca.HandlerFunctor; +import ipaaca.IUEventHandler; import ipaaca.IUEventType; import ipaaca.InputBuffer; @@ -15,18 +16,19 @@ import ipaaca.InputBuffer; * Obtain a IU in the future. Usage:<br> * FutureIU fu = FutureIU("componentx", "status", "started"); //wait for componentx to send a message that is it fully started<br> * [Start componentx, assumes that component x will send a message or other iu with status=started in the payload]<br> - * AbstractIU iu = fu.take(); //get the actual IU + * AbstractIU iu = fu.take(); //get the actual IU * @author hvanwelbergen */ public class FutureIU { private final InputBuffer inBuffer; private final BlockingQueue<AbstractIU> queue = new ArrayBlockingQueue<AbstractIU>(1); - - public FutureIU(String category, String idKey, String idVal) + private final IUEventHandler handler; + + public FutureIU(String category, String idKey, String idVal, InputBuffer inBuffer) { - inBuffer = new InputBuffer("FutureIU", ImmutableSet.of(category)); - inBuffer.registerHandler(new HandlerFunctor() + this.inBuffer = inBuffer; + handler = new IUEventHandler(new HandlerFunctor() { @Override public void handle(AbstractIU iu, IUEventType type, boolean local) @@ -34,14 +36,32 @@ public class FutureIU String id = iu.getPayload().get(idKey); if (idVal.equals(id)) { - queue.offer(iu); + queue.offer(iu); } } }, ImmutableSet.of(category)); + inBuffer.registerHandler(handler); + } + + public FutureIU(String category, String idKey, String idVal) + { + this(category, idKey, idVal, new InputBuffer("FutureIU", ImmutableSet.of(category))); + } + + /** + * Closes the FutureIU, use only if get is not used. + */ + public void cleanup() + { + inBuffer.removeHandler(handler); + if (inBuffer.getOwningComponentName().equals("FutureIU")) + { + inBuffer.close(); + } } /** - * Waits (if necessary) for the IU and take it (can be done only once) + * Waits (if necessary) for the IU and take it (can be done only once) */ public AbstractIU take() throws InterruptedException { @@ -52,13 +72,13 @@ public class FutureIU } finally { - inBuffer.close(); + cleanup(); } return iu; } /** - * Wait for at most the given time for the IU and take it (can be done only once), return null on timeout + * Wait for at most the given time for the IU and take it (can be done only once), return null on timeout */ public AbstractIU take(long timeout, TimeUnit unit) throws InterruptedException { @@ -69,16 +89,8 @@ public class FutureIU } finally { - inBuffer.close(); + cleanup(); } return iu; } - - /** - * Closes the FutureIU, use only if get is not used. - */ - public void close() - { - inBuffer.close(); - } } -- GitLab