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 | |
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>
-rw-r--r-- | vpp-japi/japi/org/openvpp/vppjapi/vppApiCallbacks.java | 5 | ||||
-rw-r--r-- | vpp-japi/japi/org/openvpp/vppjapi/vppBridgeDomainDetails.java | 2 | ||||
-rw-r--r-- | vpp-japi/japi/org/openvpp/vppjapi/vppBridgeDomainInterfaceDetails.java | 2 | ||||
-rw-r--r-- | vpp-japi/japi/org/openvpp/vppjapi/vppConn.java | 184 | ||||
-rw-r--r-- | vpp-japi/japi/org/openvpp/vppjapi/vppIPv6Address.java | 6 | ||||
-rw-r--r-- | vpp-japi/japi/org/openvpp/vppjapi/vppInterfaceCounters.java | 2 | ||||
-rw-r--r-- | vpp-japi/japi/test/demo.java | 17 | ||||
-rw-r--r-- | vpp-japi/japi/test/vppApi.java | 4 | ||||
-rw-r--r-- | vppapigen/node.c | 118 |
9 files changed, 252 insertions, 88 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; diff --git a/vpp-japi/japi/test/demo.java b/vpp-japi/japi/test/demo.java index ef419d70658..ea1db84bda2 100644 --- a/vpp-japi/japi/test/demo.java +++ b/vpp-japi/japi/test/demo.java @@ -15,9 +15,11 @@ import org.openvpp.vppjapi.*; -public class demo extends vppApi { +public class demo { public static void main (String[] args) throws Exception { - vppApi api = new vppApi (); + vppApi api = new vppApi ("JavaTest"); + System.out.printf ("Connected OK..."); + String intlist; int [] contexts; int i, limit; @@ -25,15 +27,6 @@ public class demo extends vppApi { int rv, errors, saved_error; long before, after; - rv = api.clientConnect ("JavaTest"); - if (rv == 0) - System.out.printf ("Connected OK..."); - else - { - System.out.printf ("clientConnect returned %d\n", rv); - System.exit (1); - } - if (false) { intlist = api.getInterfaceList (""); @@ -171,7 +164,7 @@ public class demo extends vppApi { limit, after - before, limit / (after - before)); - api.clientDisconnect(); + api.close(); System.out.printf ("Done...\n"); } } diff --git a/vpp-japi/japi/test/vppApi.java b/vpp-japi/japi/test/vppApi.java index 96ba4a277a7..87af3292449 100644 --- a/vpp-japi/japi/test/vppApi.java +++ b/vpp-japi/japi/test/vppApi.java @@ -16,13 +16,13 @@ import java.net.InetAddress; import org.openvpp.vppjapi.*; -public class vppApi extends vppConn { +public class vppApi { native int controlPing(); native void test (byte[] array, byte[] array2); public static void main (String[] args) throws Exception { - vppApi api = new vppApi (); + vppConn api = new vppConn (); String ipv6 = "db01::feed"; String ipv4 = "192.168.1.1"; InetAddress addr6 = InetAddress.getByName(ipv6); diff --git a/vppapigen/node.c b/vppapigen/node.c index b82816e2fcf..966ff817359 100644 --- a/vppapigen/node.c +++ b/vppapigen/node.c @@ -172,6 +172,20 @@ void primtype_recursive_generate(node_t *this, enum passid which, FILE *ofp, } } +static int hidden_from_java(const node_t * deeper) +{ + if (current_java_parameter_number++ < 3) { + if (!strncmp ((char *)(deeper->data[0]), "client_index", 12)) + return 1; + else if (!strncmp ((char *)(deeper->data[0]), "context", 7)) + return 1; + else if (!strncmp ((char *)(deeper->data[0]), "_vl_msg_id", 10)) + return 1; + } + + return 0; +} + void primtype_java_method (node_t * this, enum passid which, FILE *ofp, char *java_type_name) { @@ -180,13 +194,8 @@ void primtype_java_method (node_t * this, enum passid which, FILE *ofp, deeper = this->deeper; /* We'll take care of _msg_id, client_index, and context ourselves */ - if (current_java_parameter_number++ < 3) { - if (!strncmp ((char *)(deeper->data[0]), "client_index", 12)) - return; - else if (!strncmp ((char *)(deeper->data[0]), "context", 7)) - return; - else if (!strncmp ((char *)(deeper->data[0]), "_vl_msg_id", 10)) - return; + if (hidden_from_java(deeper)) { + return; } if (deeper->type == NODE_SCALAR) @@ -207,13 +216,8 @@ void primtype_java_parameter (node_t * this, enum passid which, FILE *ofp, deeper = this->deeper; /* We'll take care of _msg_id, client_index, and context ourselves */ - if (current_java_parameter_number++ < 3) { - if (!strncmp ((char *)(deeper->data[0]), "client_index", 12)) - return; - else if (!strncmp ((char *)(deeper->data[0]), "context", 7)) - return; - else if (!strncmp ((char *)(deeper->data[0]), "_vl_msg_id", 10)) - return; + if (hidden_from_java(deeper)) { + return; } if (current_java_parameter_need_comma_space) { current_java_parameter_need_comma_space = 0; @@ -236,13 +240,8 @@ void primtype_java_setup (node_t * this, enum passid which, FILE *ofp, deeper = this->deeper; /* We'll take care of _msg_id, client_index, and context ourselves */ - if (current_java_parameter_number++ < 3) { - if (!strncmp ((char *)(deeper->data[0]), "client_index", 12)) - return; - else if (!strncmp ((char *)(deeper->data[0]), "context", 7)) - return; - else if (!strncmp ((char *)(deeper->data[0]), "_vl_msg_id", 10)) - return; + if (hidden_from_java(deeper)) { + return; } if (deeper->type == NODE_VECTOR) { @@ -265,13 +264,8 @@ void primtype_java_code (node_t * this, enum passid which, FILE *ofp, deeper = this->deeper; /* We'll take care of _msg_id, client_index, and context ourselves */ - if (current_java_parameter_number++ < 3) { - if (!strncmp ((char *)(deeper->data[0]), "client_index", 12)) - return; - else if (!strncmp ((char *)(deeper->data[0]), "context", 7)) - return; - else if (!strncmp ((char *)(deeper->data[0]), "_vl_msg_id", 10)) - return; + if (hidden_from_java(deeper)) { + return; } indent_me(ofp); @@ -319,13 +313,8 @@ void primtype_java_teardown (node_t * this, enum passid which, FILE *ofp, deeper = this->deeper; /* We'll take care of _msg_id, client_index, and context ourselves */ - if (current_java_parameter_number++ < 3) { - if (!strncmp ((char *)(deeper->data[0]), "client_index", 12)) - return; - else if (!strncmp ((char *)(deeper->data[0]), "context", 7)) - return; - else if (!strncmp ((char *)(deeper->data[0]), "_vl_msg_id", 10)) - return; + if (hidden_from_java(deeper)) { + return; } if (deeper->type == NODE_VECTOR) { @@ -720,6 +709,18 @@ void node_define_print (node_t *this) fprintf(stdout, "};\n"); } +static void emit_java_arg_declaration(node_t *child, FILE *fp) { + current_java_parameter_number = 0; + while (child) { + node_vft_t *vftp = the_vft[child->type]; + current_java_emitted_parameter = 0; + vftp->java_method_function(child, JAVA_METHOD_PASS, fp); + child = child->peer; + if (child && current_java_emitted_parameter) + fputs (", ", fp); + } +} + void node_define_generate (node_t *this, enum passid which, FILE *fp) { node_t *child, *save_child; @@ -752,27 +753,49 @@ void node_define_generate (node_t *this, enum passid which, FILE *fp) case JAVA_METHOD_PASS: indent += 4; indent_me(fp); - fprintf (fp, "public native int %s (", - java_name_mangle(CDATA0)); + + /* Generate private native declaration */ + fprintf (fp, "private static native int %s0(", java_name_mangle(CDATA0)); + emit_java_arg_declaration(this->deeper, fp); + fputs (");\n", fp); + + /* Generate public Java method */ + indent_me(fp); + fprintf (fp, "public final int %s(", java_name_mangle(CDATA0)); + emit_java_arg_declaration(this->deeper, fp); + fputs (") {\n", fp); + + indent += 4; + indent_me(fp); + fputs ("checkConnected();\n", fp); + indent_me(fp); + fprintf (fp, "return %s.%s0(", java_class, java_name_mangle(CDATA0)); + child = this->deeper; + current_java_parameter_number = 0; + while (child && hidden_from_java(child->deeper)) { + child = child->peer; + } while (child) { - node_vft_t *vftp = the_vft[child->type]; - current_java_emitted_parameter = 0; - vftp->java_method_function(child, which, fp); + fputs(java_name_mangle((char *)(child->deeper->data[0])), fp); child = child->peer; - if (child && current_java_emitted_parameter) + if (child) fputs (", ", fp); } - fprintf (fp, ");\n"); + + fputs (");\n", fp); + indent -= 4; + indent_me(fp); + fputs ("}\n\n", fp); indent -= 4; break; case JAVA_JNI_PASS: /* Generate function prototype */ - fprintf (fp, "JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_%s_%s\n", + fprintf (fp, "JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_%s_%s0\n", java_class, java_name_mangle(CDATA0)); - fprintf (fp, "(JNIEnv * env, jobject obj"); + fprintf (fp, "(JNIEnv * env, jclass clazz"); current_java_parameter_need_comma_space = 1; child = this->deeper; save_child = child; @@ -1773,9 +1796,12 @@ void generate_java_top_boilerplate(FILE *fp) fprintf (fp, " */\n\n"); fprintf (fp, "package org.openvpp.vppjapi;\n\n"); - fprintf (fp, "import org.openvpp.vppjapi.vppConn;\n\n"); - fprintf (fp, "public class %s extends vppConn {\n\n", + fprintf (fp, "import java.io.IOException;\n\n"); + fprintf (fp, "public class %s extends vppConn {\n", java_class); + fprintf (fp, " public %s(String clientName) throws IOException {\n", java_class); + fprintf (fp, " super(clientName);\n"); + fprintf (fp, " }\n\n"); } void generate_java_bottom_boilerplate(FILE *fp) |