From 560e809b4459f508b756a19493de746e0892389e Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Mon, 19 Sep 2016 15:35:41 +0200 Subject: VPP-348 Return empty DumpReply instead of null Change-Id: If44f8d37649e5a9d5033ec2c0ab7452397e22691 Signed-off-by: Maros Marsalek --- .../jvpp/future/AbstractFutureJVppInvoker.java | 57 ++++++++++++++-------- .../org/openvpp/jvpp/future/FutureJVppInvoker.java | 10 ++++ 2 files changed, 48 insertions(+), 19 deletions(-) (limited to 'vpp-api/java/jvpp-registry/org') diff --git a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/future/AbstractFutureJVppInvoker.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/future/AbstractFutureJVppInvoker.java index d8b105f359e..53a445e6977 100644 --- a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/future/AbstractFutureJVppInvoker.java +++ b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/future/AbstractFutureJVppInvoker.java @@ -30,8 +30,8 @@ import org.openvpp.jvpp.dto.JVppReplyDump; import org.openvpp.jvpp.dto.JVppRequest; /** -* Future facade on top of JVpp -*/ + * Future facade on top of JVpp + */ public abstract class AbstractFutureJVppInvoker implements FutureJVppInvoker { private final JVpp jvpp; @@ -43,7 +43,7 @@ public abstract class AbstractFutureJVppInvoker implements FutureJVppInvoker { private final Map>> requests; protected AbstractFutureJVppInvoker(final JVpp jvpp, final JVppRegistry registry, - final Map>> requestMap) { + final Map>> requestMap) { this.jvpp = Objects.requireNonNull(jvpp, "jvpp should not be null"); this.registry = Objects.requireNonNull(registry, "registry should not be null"); // Request map represents the shared state between this facade and it's callback @@ -66,15 +66,10 @@ public abstract class AbstractFutureJVppInvoker implements FutureJVppInvoker { final int contextId = jvpp.send(req); if(req instanceof JVppDump) { - replyCompletableFuture = (CompletableFuture) new CompletableDumpFuture<>(contextId); - } else { - replyCompletableFuture = new CompletableFuture<>(); + throw new IllegalArgumentException("Send with empty reply dump has to be used in case of dump calls"); } - + replyCompletableFuture = new CompletableFuture<>(); requests.put(contextId, replyCompletableFuture); - if(req instanceof JVppDump) { - requests.put(registry.controlPing(jvpp.getClass()), replyCompletableFuture); - } // TODO in case of timeouts/missing replies, requests from the map are not removed // consider adding cancel method, that would remove requests from the map and cancel @@ -89,15 +84,43 @@ public abstract class AbstractFutureJVppInvoker implements FutureJVppInvoker { } } + @Override + @SuppressWarnings("unchecked") + public , DUMP extends JVppReplyDump> CompletionStage send( + REQ req, DUMP emptyReplyDump) { + synchronized(requests) { + try { + final CompletableDumpFuture replyCompletableFuture; + final int contextId = jvpp.send(req); + + if(!(req instanceof JVppDump)) { + throw new IllegalArgumentException("Send without empty reply dump has to be used in case of regular calls"); + } + replyCompletableFuture = new CompletableDumpFuture<>(contextId, emptyReplyDump); + + requests.put(contextId, replyCompletableFuture); + requests.put(registry.controlPing(jvpp.getClass()), replyCompletableFuture); + + // TODO in case of timeouts/missing replies, requests from the map are not removed + // consider adding cancel method, that would remove requests from the map and cancel + // associated replyCompletableFuture + + return replyCompletableFuture; + } catch (VppInvocationException ex) { + final CompletableFuture replyCompletableFuture = new CompletableFuture<>(); + replyCompletableFuture.completeExceptionally(ex); + return replyCompletableFuture; + } + } + } + public static final class CompletableDumpFuture> extends CompletableFuture { - // The reason why this is not final is the instantiation of ReplyDump DTOs - // Their instantiation must be generated, so currently the DTOs are created in callback and set when first dump reponses - // is handled in the callback. - private T replyDump; + private final T replyDump; private final long contextId; - public CompletableDumpFuture(final long contextId) { + public CompletableDumpFuture(final long contextId, final T emptyDump) { this.contextId = contextId; + this.replyDump = emptyDump; } public long getContextId() { @@ -107,10 +130,6 @@ public abstract class AbstractFutureJVppInvoker implements FutureJVppInvoker { public T getReplyDump() { return replyDump; } - - public void setReplyDump(final T replyDump) { - this.replyDump = replyDump; - } } @Override diff --git a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/future/FutureJVppInvoker.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/future/FutureJVppInvoker.java index 1683bd75139..721f95c9f4d 100644 --- a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/future/FutureJVppInvoker.java +++ b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/future/FutureJVppInvoker.java @@ -18,6 +18,7 @@ package org.openvpp.jvpp.future; import org.openvpp.jvpp.dto.JVppReply; +import org.openvpp.jvpp.dto.JVppReplyDump; import org.openvpp.jvpp.dto.JVppRequest; import java.util.concurrent.CompletionStage; @@ -36,4 +37,13 @@ public interface FutureJVppInvoker extends NotificationRegistryProvider, AutoClo */ > CompletionStage send(REQ req); + + /** + * Invoke asynchronous dump operation on VPP + * + * @return CompletionStage with aggregated future result of an async VPP dump call + * @throws org.openvpp.jvpp.VppInvocationException when send request failed with details + */ + , DUMP extends JVppReplyDump> CompletionStage send( + REQ req, DUMP emptyReplyDump); } -- cgit 1.2.3-korg