diff options
author | 2016-05-13 10:17:51 +0200 | |
---|---|---|
committer | 2016-05-23 06:25:05 +0000 | |
commit | 071d610dc446f2914c0ca4c709be38719baa041c (patch) | |
tree | e97337183e4078ab00557665a02b9e3f0ee57071 /vpp-api/java/jvpp/org/openvpp | |
parent | 6931f59e54c9643196c0edda20840e49e9eb1da1 (diff) |
Changed JVPP interface for construction and connectivity
Simplified construction, autoconnected; possible connect/close
See updated sample test cases with changed interface usage
Change-Id: Ib53e855880bc414868aa2b9bb8f5df086917e375
Signed-off-by: Tibor Sirovatka <tsirovat@cisco.com>
Diffstat (limited to 'vpp-api/java/jvpp/org/openvpp')
7 files changed, 38 insertions, 30 deletions
diff --git a/vpp-api/java/jvpp/org/openvpp/jvpp/VppConnection.java b/vpp-api/java/jvpp/org/openvpp/jvpp/VppConnection.java index 72ff62c944b..19733985452 100644 --- a/vpp-api/java/jvpp/org/openvpp/jvpp/VppConnection.java +++ b/vpp-api/java/jvpp/org/openvpp/jvpp/VppConnection.java @@ -16,6 +16,8 @@ package org.openvpp.jvpp; +import java.io.IOException; + /** * Representation of a management connection to VPP. * Connection is initiated when instance is created, closed with close(). @@ -23,6 +25,15 @@ package org.openvpp.jvpp; public interface VppConnection extends AutoCloseable { /** + * Open VppConnection for communication with VPP + * + * @param callback instance handling responses + * + * @throws IOException if connection is not established + */ + void connect(final org.openvpp.jvpp.callback.JVppCallback callback) throws IOException; + + /** * Check if this instance connection is active. * * @throws IllegalStateException if this instance was disconnected. diff --git a/vpp-api/java/jvpp/org/openvpp/jvpp/VppJNIConnection.java b/vpp-api/java/jvpp/org/openvpp/jvpp/VppJNIConnection.java index bf18f1289da..7401bcadc47 100644 --- a/vpp-api/java/jvpp/org/openvpp/jvpp/VppJNIConnection.java +++ b/vpp-api/java/jvpp/org/openvpp/jvpp/VppJNIConnection.java @@ -77,7 +77,12 @@ public final class VppJNIConnection implements VppConnection { private final String clientName; private volatile boolean disconnected = false; - private VppJNIConnection(final String clientName) { + /** + * Create VPPJNIConnection instance for client connecting to VPP. + * + * @param clientName client name instance to be used for communication. Single connection per clientName is allowed. + */ + public VppJNIConnection(final String clientName) { this.clientName = Objects.requireNonNull(clientName,"Null clientName"); } @@ -87,32 +92,28 @@ public final class VppJNIConnection implements VppConnection { private static final Map<String, VppJNIConnection> connections = new HashMap<>(); /** - * Create a new Vpp connection identified by clientName parameter. + * Initiate VPP connection for current instance * * Multiple instances are allowed since this class is not a singleton * (VPP allows multiple management connections). * * However only a single connection per clientName is allowed. * - * @param clientName identifier of vpp connection * @param callback global callback to receive response calls from vpp * - * @return new Vpp connection - * @throws IOException in case the connection could not be established, or there already is a connection with the same name + * @throws IOException in case the connection could not be established */ - public static VppJNIConnection create(final String clientName, final JVppCallback callback) throws IOException { + public void connect(final JVppCallback callback) throws IOException { synchronized (VppJNIConnection.class) { if(connections.containsKey(clientName)) { throw new IOException("Client " + clientName + " already connected"); } - final VppJNIConnection vppJNIConnection = new VppJNIConnection(clientName); final int ret = clientConnect(clientName, callback); if (ret != 0) { throw new IOException("Connection returned error " + ret); } - connections.put(clientName, vppJNIConnection); - return vppJNIConnection; + connections.put(clientName, this); } } diff --git a/vpp-api/java/jvpp/org/openvpp/jvpp/future/FutureJVppInvokerFacade.java b/vpp-api/java/jvpp/org/openvpp/jvpp/future/FutureJVppInvokerFacade.java index 6b63ade2ade..b005b137b1f 100644 --- a/vpp-api/java/jvpp/org/openvpp/jvpp/future/FutureJVppInvokerFacade.java +++ b/vpp-api/java/jvpp/org/openvpp/jvpp/future/FutureJVppInvokerFacade.java @@ -49,6 +49,10 @@ public class FutureJVppInvokerFacade implements FutureJVppInvoker { this.requests = Objects.requireNonNull(requestMap, "Null requestMap"); } + protected final Map<Integer, CompletableFuture<? extends JVppReply<?>>> getRequests() { + return this.requests; + } + // TODO use Optional in Future, java8 @Override diff --git a/vpp-api/java/jvpp/org/openvpp/jvpp/test/CallbackApiTest.java b/vpp-api/java/jvpp/org/openvpp/jvpp/test/CallbackApiTest.java index 5ac4b69a52d..c3bb272368c 100644 --- a/vpp-api/java/jvpp/org/openvpp/jvpp/test/CallbackApiTest.java +++ b/vpp-api/java/jvpp/org/openvpp/jvpp/test/CallbackApiTest.java @@ -57,7 +57,8 @@ public class CallbackApiTest { private static void testCallbackApi() throws Exception { System.out.println("Testing Java callback API"); - JVpp jvpp = new JVppImpl(VppJNIConnection.create("CallbackApiTest", new TestCallback())); + JVpp jvpp = new JVppImpl( new VppJNIConnection("CallbackApiTest")); + jvpp.connect( new TestCallback()); System.out.println("Successfully connected to VPP"); System.out.println("Sending ShowVersion request..."); diff --git a/vpp-api/java/jvpp/org/openvpp/jvpp/test/CallbackJVppFacadeTest.java b/vpp-api/java/jvpp/org/openvpp/jvpp/test/CallbackJVppFacadeTest.java index df3b0e7056c..b5e505741d5 100644 --- a/vpp-api/java/jvpp/org/openvpp/jvpp/test/CallbackJVppFacadeTest.java +++ b/vpp-api/java/jvpp/org/openvpp/jvpp/test/CallbackJVppFacadeTest.java @@ -16,15 +16,11 @@ package org.openvpp.jvpp.test; -import java.util.HashMap; -import java.util.Map; import org.openvpp.jvpp.JVpp; import org.openvpp.jvpp.JVppImpl; import org.openvpp.jvpp.VppJNIConnection; -import org.openvpp.jvpp.callback.JVppCallback; import org.openvpp.jvpp.callback.ShowVersionCallback; import org.openvpp.jvpp.callfacade.CallbackJVppFacade; -import org.openvpp.jvpp.callfacade.CallbackJVppFacadeCallback; /** * CallbackJVppFacade together with CallbackJVppFacadeCallback allow for setting different callback for each request. @@ -45,19 +41,19 @@ public class CallbackJVppFacadeTest { private static void testCallbackFacade() throws Exception { System.out.println("Testing CallbackJVppFacade"); - final Map<Integer, JVppCallback> callbackMap = new HashMap<>(); - JVpp impl = new JVppImpl(VppJNIConnection.create("CallbackApiTest", new CallbackJVppFacadeCallback(callbackMap))); - CallbackJVppFacade jvpp = new CallbackJVppFacade(impl, callbackMap); + JVpp jvpp = new JVppImpl(new VppJNIConnection("CallbackApiTest")); + + CallbackJVppFacade jvppCallbackFacade = new CallbackJVppFacade(jvpp); System.out.println("Successfully connected to VPP"); - jvpp.showVersion(showVersionCallback1); - jvpp.showVersion(showVersionCallback2); + jvppCallbackFacade.showVersion(showVersionCallback1); + jvppCallbackFacade.showVersion(showVersionCallback2); Thread.sleep(2000); System.out.println("Disconnecting..."); - impl.close(); + jvpp.close(); Thread.sleep(1000); } diff --git a/vpp-api/java/jvpp/org/openvpp/jvpp/test/ControlPingTest.java b/vpp-api/java/jvpp/org/openvpp/jvpp/test/ControlPingTest.java index f1bd41de86f..bec302b8089 100644 --- a/vpp-api/java/jvpp/org/openvpp/jvpp/test/ControlPingTest.java +++ b/vpp-api/java/jvpp/org/openvpp/jvpp/test/ControlPingTest.java @@ -28,13 +28,14 @@ public class ControlPingTest { private static void testControlPing() throws Exception { System.out.println("Testing ControlPing using Java callback API"); - JVpp jvpp = new JVppImpl(VppJNIConnection.create("ControlPingTest", new ControlPingCallback() { + JVpp jvpp = new JVppImpl( new VppJNIConnection("ControlPingTest")); + jvpp.connect( new ControlPingCallback() { @Override public void onControlPingReply(final ControlPingReply reply) { System.out.printf("Received ControlPingReply: context=%d, retval=%d, clientIndex=%d vpePid=%d\n", reply.context, reply.retval, reply.clientIndex, reply.vpePid); } - })); + }); System.out.println("Successfully connected to VPP"); Thread.sleep(1000); diff --git a/vpp-api/java/jvpp/org/openvpp/jvpp/test/FutureApiTest.java b/vpp-api/java/jvpp/org/openvpp/jvpp/test/FutureApiTest.java index fc84c9519d1..745482d1667 100644 --- a/vpp-api/java/jvpp/org/openvpp/jvpp/test/FutureApiTest.java +++ b/vpp-api/java/jvpp/org/openvpp/jvpp/test/FutureApiTest.java @@ -16,22 +16,17 @@ package org.openvpp.jvpp.test; -import java.util.HashMap; -import java.util.Map; import java.util.Objects; -import java.util.concurrent.CompletableFuture; import java.util.concurrent.Future; import org.openvpp.jvpp.VppJNIConnection; import org.openvpp.jvpp.dto.GetNodeIndex; import org.openvpp.jvpp.dto.GetNodeIndexReply; -import org.openvpp.jvpp.dto.JVppReply; import org.openvpp.jvpp.dto.ShowVersion; import org.openvpp.jvpp.dto.ShowVersionReply; import org.openvpp.jvpp.dto.SwInterfaceDetails; import org.openvpp.jvpp.dto.SwInterfaceDetailsReplyDump; import org.openvpp.jvpp.dto.SwInterfaceDump; import org.openvpp.jvpp.future.FutureJVppFacade; -import org.openvpp.jvpp.future.FutureJVppFacadeCallback; public class FutureApiTest { @@ -104,10 +99,9 @@ public class FutureApiTest { private static void testFutureApi() throws Exception { System.out.println("Testing Java future API"); - final Map<Integer, CompletableFuture<? extends JVppReply<?>>> map = new HashMap<>(); final org.openvpp.jvpp.JVppImpl impl = - new org.openvpp.jvpp.JVppImpl(VppJNIConnection.create("FutureApiTest", new FutureJVppFacadeCallback(map))); - final FutureJVppFacade jvppFacade = new FutureJVppFacade(impl, map); + new org.openvpp.jvpp.JVppImpl(new VppJNIConnection("FutureApiTest")); + final FutureJVppFacade jvppFacade = new FutureJVppFacade(impl); System.out.println("Successfully connected to VPP"); testShowVersion(jvppFacade); |