diff options
author | Robert Varga <nite@hq.sk> | 2016-01-31 12:20:47 +0100 |
---|---|---|
committer | Dave Wallace <dwallacelf@gmail.com> | 2016-02-01 04:07:26 +0000 |
commit | 67ba3bed006c8fdaec5cbee897843d54cf2f538d (patch) | |
tree | c2621006466ad41855aaa76ebe97b27521ac4a10 /vpp-japi/japi/org/openvpp/vppjapi | |
parent | d5201c80078fdecad8e6524e430b4b255f91b6b6 (diff) |
Refactor vpp-japi
vlib does not allow concurrent connections, hence make sure the Java API
does not allow the user to instantiate them.
Also hide native methods from direct user invocation behind defensive
methods also detect if a particular connection has been terminated.
Disconnect is hidden befind standard close() method, as specified by
AutoCloseable.
Change-Id: Ib5079200ae4216cad84358a2174a41e90271a30b
Signed-off-by: Robert Varga <nite@hq.sk>
Diffstat (limited to 'vpp-japi/japi/org/openvpp/vppjapi')
6 files changed, 173 insertions, 28 deletions
diff --git a/vpp-japi/japi/org/openvpp/vppjapi/vppApiCallbacks.java b/vpp-japi/japi/org/openvpp/vppjapi/vppApiCallbacks.java index 5c35897c6a7..df5b953300e 100644 --- a/vpp-japi/japi/org/openvpp/vppjapi/vppApiCallbacks.java +++ b/vpp-japi/japi/org/openvpp/vppjapi/vppApiCallbacks.java @@ -14,9 +14,14 @@ */ package org.openvpp.vppjapi; + +import java.io.IOException; import org.openvpp.vppjapi.vppApi; public abstract class vppApiCallbacks extends vppApi { + public vppApiCallbacks(String clientName) throws IOException { + super(clientName); + } /* Disabled! * * public abstract void interfaceDetails( diff --git a/vpp-japi/japi/org/openvpp/vppjapi/vppBridgeDomainDetails.java b/vpp-japi/japi/org/openvpp/vppjapi/vppBridgeDomainDetails.java index 01fe11d61a2..db859a07204 100644 --- a/vpp-japi/japi/org/openvpp/vppjapi/vppBridgeDomainDetails.java +++ b/vpp-japi/japi/org/openvpp/vppjapi/vppBridgeDomainDetails.java @@ -17,7 +17,7 @@ package org.openvpp.vppjapi; import org.openvpp.vppjapi.vppBridgeDomainInterfaceDetails; -public class vppBridgeDomainDetails { +public final class vppBridgeDomainDetails { public String name; public int bdId; public boolean flood; diff --git a/vpp-japi/japi/org/openvpp/vppjapi/vppBridgeDomainInterfaceDetails.java b/vpp-japi/japi/org/openvpp/vppjapi/vppBridgeDomainInterfaceDetails.java index f17c0620106..ab99ee45347 100644 --- a/vpp-japi/japi/org/openvpp/vppjapi/vppBridgeDomainInterfaceDetails.java +++ b/vpp-japi/japi/org/openvpp/vppjapi/vppBridgeDomainInterfaceDetails.java @@ -15,7 +15,7 @@ package org.openvpp.vppjapi; -public class vppBridgeDomainInterfaceDetails { +public final class vppBridgeDomainInterfaceDetails { public String interfaceName; public byte splitHorizonGroup; } diff --git a/vpp-japi/japi/org/openvpp/vppjapi/vppConn.java b/vpp-japi/japi/org/openvpp/vppjapi/vppConn.java index cd957808683..8de806dc12a 100644 --- a/vpp-japi/japi/org/openvpp/vppjapi/vppConn.java +++ b/vpp-japi/japi/org/openvpp/vppjapi/vppConn.java @@ -23,6 +23,7 @@ import java.nio.file.StandardCopyOption; import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.PosixFilePermissions; import java.util.Set; +import java.util.concurrent.atomic.AtomicBoolean; import org.openvpp.vppjapi.vppVersion; import org.openvpp.vppjapi.vppInterfaceDetails; @@ -32,7 +33,7 @@ import org.openvpp.vppjapi.vppIPv4Address; import org.openvpp.vppjapi.vppIPv6Address; import org.openvpp.vppjapi.vppVxlanTunnelDetails; -public class vppConn { +public class vppConn implements AutoCloseable { private static final String LIBNAME = "libvppjni.so.0.0.0"; static { @@ -40,6 +41,7 @@ public class vppConn { loadLibrary(); } catch (IOException | RuntimeException e) { System.out.printf ("Can't find vpp jni library: %s\n", LIBNAME); + throw new ExceptionInInitializerError(e); } } @@ -72,25 +74,163 @@ public class vppConn { } } - public native int clientConnect(String clientName); - public native void clientDisconnect(); - public native int getRetval(int context, int release); - public native String getInterfaceList (String nameFilter); - public native int swIfIndexFromName (String interfaceName); - public native String interfaceNameFromSwIfIndex (int swIfIndex); - public native void clearInterfaceTable (); - public native vppInterfaceDetails[] swInterfaceDump (byte nameFilterValid, byte [] nameFilter); - public native int bridgeDomainIdFromName(String bridgeDomain); - public native int findOrAddBridgeDomainId(String bridgeDomain); - public native vppVersion getVppVersion(); - public native vppInterfaceCounters getInterfaceCounters(int swIfIndex); - public native int[] bridgeDomainDump(int bdId); - public native vppBridgeDomainDetails getBridgeDomainDetails(int bdId); - public native vppL2Fib[] l2FibTableDump(int bdId); - public native int bridgeDomainIdFromInterfaceName(String interfaceName); - public native vppIPv4Address[] ipv4AddressDump(String interfaceName); - public native vppIPv6Address[] ipv6AddressDump(String interfaceName); - public native vppVxlanTunnelDetails[] vxlanTunnelDump(int swIfIndex); - public native int setInterfaceDescription (String ifName, String ifDesc); - public native String getInterfaceDescription (String ifName); + private static vppConn currentConnection = null; + private final AtomicBoolean disconnected = new AtomicBoolean(false); + private final String clientName; + + // Hidden on purpose to prevent external instantiation + vppConn(final String clientName) throws IOException { + this.clientName = clientName; + + synchronized (vppConn.class) { + if (currentConnection != null) { + throw new IOException("Already connected as " + currentConnection.clientName); + } + + final int ret = clientConnect(clientName); + if (ret != 0) { + throw new IOException("Connection returned error " + ret); + } + + currentConnection = this; + } + } + + @Override + public final void close() { + if (disconnected.compareAndSet(false, true)) { + synchronized (vppConn.class) { + clientDisconnect(); + currentConnection = null; + } + } + } + + /** + * Check if this instance is connected. + * + * @throws IllegalStateException if this instance was disconnected. + */ + protected final void checkConnected() { + if (disconnected.get()) { + throw new IllegalStateException("Disconnected client " + clientName); + } + } + + public final int getRetval(int context, int release) { + checkConnected(); + return getRetval0(context, release); + } + + public final String getInterfaceList (String nameFilter) { + checkConnected(); + return getInterfaceList0(nameFilter); + } + + public final int swIfIndexFromName (String interfaceName) { + checkConnected(); + return swIfIndexFromName0(interfaceName); + } + + public final String interfaceNameFromSwIfIndex (int swIfIndex) { + checkConnected(); + return interfaceNameFromSwIfIndex0(swIfIndex); + } + + public final void clearInterfaceTable () { + checkConnected(); + clearInterfaceTable0(); + } + + public final vppInterfaceDetails[] swInterfaceDump (byte nameFilterValid, byte [] nameFilter) { + checkConnected(); + return swInterfaceDump0(nameFilterValid, nameFilter); + } + + public final int bridgeDomainIdFromName(String bridgeDomain) { + checkConnected(); + return bridgeDomainIdFromName0(bridgeDomain); + } + + public final int findOrAddBridgeDomainId(String bridgeDomain) { + checkConnected(); + return findOrAddBridgeDomainId0(bridgeDomain); + } + + public final vppVersion getVppVersion() { + checkConnected(); + return getVppVersion0(); + } + + public final vppInterfaceCounters getInterfaceCounters(int swIfIndex) { + checkConnected(); + return getInterfaceCounters0(swIfIndex); + } + + public final int[] bridgeDomainDump(int bdId) { + checkConnected(); + return bridgeDomainDump0(bdId); + } + + public final vppBridgeDomainDetails getBridgeDomainDetails(int bdId) { + checkConnected(); + return getBridgeDomainDetails0(bdId); + } + + public final vppL2Fib[] l2FibTableDump(int bdId) { + checkConnected(); + return l2FibTableDump0(bdId); + } + + public final int bridgeDomainIdFromInterfaceName(String interfaceName) { + checkConnected(); + return bridgeDomainIdFromInterfaceName0(interfaceName); + } + + public final vppIPv4Address[] ipv4AddressDump(String interfaceName) { + checkConnected(); + return ipv4AddressDump0(interfaceName); + } + + public final vppIPv6Address[] ipv6AddressDump(String interfaceName) { + checkConnected(); + return ipv6AddressDump0(interfaceName); + } + + public final vppVxlanTunnelDetails[] vxlanTunnelDump(int swIfIndex) { + checkConnected(); + return vxlanTunnelDump0(swIfIndex); + } + + public final int setInterfaceDescription(String ifName, String ifDesc) { + checkConnected(); + return setInterfaceDescription0(ifName, ifDesc); + } + + public final String getInterfaceDescription(String ifName) { + checkConnected(); + return getInterfaceDescription0(ifName); + } + + private static native int clientConnect(String clientName); + private static native void clientDisconnect(); + private static native int getRetval0(int context, int release); + private static native String getInterfaceList0(String nameFilter); + private static native int swIfIndexFromName0(String interfaceName); + private static native String interfaceNameFromSwIfIndex0(int swIfIndex); + private static native void clearInterfaceTable0(); + private static native vppInterfaceDetails[] swInterfaceDump0(byte nameFilterValid, byte [] nameFilter); + private static native int bridgeDomainIdFromName0(String bridgeDomain); + private static native int findOrAddBridgeDomainId0(String bridgeDomain); + private static native vppVersion getVppVersion0(); + private static native vppInterfaceCounters getInterfaceCounters0(int swIfIndex); + private static native int[] bridgeDomainDump0(int bdId); + private static native vppBridgeDomainDetails getBridgeDomainDetails0(int bdId); + private static native vppL2Fib[] l2FibTableDump0(int bdId); + private static native int bridgeDomainIdFromInterfaceName0(String interfaceName); + private static native vppIPv4Address[] ipv4AddressDump0(String interfaceName); + private static native vppIPv6Address[] ipv6AddressDump0(String interfaceName); + private static native vppVxlanTunnelDetails[] vxlanTunnelDump0(int swIfIndex); + private static native int setInterfaceDescription0(String ifName, String ifDesc); + private static native String getInterfaceDescription0(String ifName); } diff --git a/vpp-japi/japi/org/openvpp/vppjapi/vppIPv6Address.java b/vpp-japi/japi/org/openvpp/vppjapi/vppIPv6Address.java index 7bf10f23402..6690a5db981 100644 --- a/vpp-japi/japi/org/openvpp/vppjapi/vppIPv6Address.java +++ b/vpp-japi/japi/org/openvpp/vppjapi/vppIPv6Address.java @@ -20,8 +20,8 @@ public class vppIPv6Address { public final byte[] ip; public final byte prefixLength; - public vppIPv6Address(byte[] _ip, byte _prefixLength) { - ip = _ip; - prefixLength = _prefixLength; + public vppIPv6Address(byte[] ip, byte prefixLength) { + this.ip = ip; + this.prefixLength = prefixLength; } } diff --git a/vpp-japi/japi/org/openvpp/vppjapi/vppInterfaceCounters.java b/vpp-japi/japi/org/openvpp/vppjapi/vppInterfaceCounters.java index 8a02ad09633..b2687fb8efb 100644 --- a/vpp-japi/japi/org/openvpp/vppjapi/vppInterfaceCounters.java +++ b/vpp-japi/japi/org/openvpp/vppjapi/vppInterfaceCounters.java @@ -15,7 +15,7 @@ package org.openvpp.vppjapi; -public class vppInterfaceCounters { +public final class vppInterfaceCounters { public final long rxOctets; public final long rxIp4; |