Browse Source

add PeerRole.fromInt

Brenton Bostick 2 years ago
parent
commit
056cef7292

+ 2 - 6
java/jni/ZT_jnicache.cpp

@@ -104,6 +104,7 @@ jmethodID VirtualNetworkRoute_ctor;
 
 jmethodID Event_fromInt_method;
 jmethodID InetAddress_getByAddress_method;
+jmethodID PeerRole_fromInt_method;
 
 //
 // Instance fields
@@ -159,9 +160,6 @@ jfieldID VirtualNetworkRoute_via_field;
 // Static fields
 //
 
-jfieldID PeerRole_PEER_ROLE_LEAF_field;
-jfieldID PeerRole_PEER_ROLE_MOON_field;
-jfieldID PeerRole_PEER_ROLE_PLANET_field;
 jfieldID ResultCode_RESULT_ERROR_BAD_PARAMETER_field;
 jfieldID ResultCode_RESULT_ERROR_NETWORK_NOT_FOUND_field;
 jfieldID ResultCode_RESULT_ERROR_UNSUPPORTED_OPERATION_field;
@@ -260,6 +258,7 @@ void setupJNICache(JavaVM *vm) {
 
     EXCEPTIONANDNULLCHECK(Event_fromInt_method = env->GetStaticMethodID(Event_class, "fromInt", "(I)Lcom/zerotier/sdk/Event;"));
     EXCEPTIONANDNULLCHECK(InetAddress_getByAddress_method = env->GetStaticMethodID(InetAddress_class, "getByAddress", "([B)Ljava/net/InetAddress;"));
+    EXCEPTIONANDNULLCHECK(PeerRole_fromInt_method = env->GetStaticMethodID(PeerRole_class, "fromInt", "(I)Lcom/zerotier/sdk/PeerRole;"));
 
     //
     // Instance fields
@@ -315,9 +314,6 @@ void setupJNICache(JavaVM *vm) {
     // Static fields
     //
 
-    EXCEPTIONANDNULLCHECK(PeerRole_PEER_ROLE_LEAF_field = env->GetStaticFieldID(PeerRole_class, "PEER_ROLE_LEAF", "Lcom/zerotier/sdk/PeerRole;"));
-    EXCEPTIONANDNULLCHECK(PeerRole_PEER_ROLE_MOON_field = env->GetStaticFieldID(PeerRole_class, "PEER_ROLE_MOON", "Lcom/zerotier/sdk/PeerRole;"));
-    EXCEPTIONANDNULLCHECK(PeerRole_PEER_ROLE_PLANET_field = env->GetStaticFieldID(PeerRole_class, "PEER_ROLE_PLANET", "Lcom/zerotier/sdk/PeerRole;"));
     EXCEPTIONANDNULLCHECK(ResultCode_RESULT_ERROR_BAD_PARAMETER_field = env->GetStaticFieldID(ResultCode_class, "RESULT_ERROR_BAD_PARAMETER", "Lcom/zerotier/sdk/ResultCode;"));
     EXCEPTIONANDNULLCHECK(ResultCode_RESULT_ERROR_NETWORK_NOT_FOUND_field = env->GetStaticFieldID(ResultCode_class, "RESULT_ERROR_NETWORK_NOT_FOUND", "Lcom/zerotier/sdk/ResultCode;"));
     EXCEPTIONANDNULLCHECK(ResultCode_RESULT_ERROR_UNSUPPORTED_OPERATION_field = env->GetStaticFieldID(ResultCode_class, "RESULT_ERROR_UNSUPPORTED_OPERATION", "Lcom/zerotier/sdk/ResultCode;"));

+ 1 - 3
java/jni/ZT_jnicache.h

@@ -73,6 +73,7 @@ extern jmethodID VirtualNetworkRoute_ctor;
 
 extern jmethodID Event_fromInt_method;
 extern jmethodID InetAddress_getByAddress_method;
+extern jmethodID PeerRole_fromInt_method;
 
 //
 // Instance fields
@@ -128,9 +129,6 @@ extern jfieldID VirtualNetworkRoute_via_field;
 // Static fields
 //
 
-extern jfieldID PeerRole_PEER_ROLE_LEAF_field;
-extern jfieldID PeerRole_PEER_ROLE_MOON_field;
-extern jfieldID PeerRole_PEER_ROLE_PLANET_field;
 extern jfieldID ResultCode_RESULT_ERROR_BAD_PARAMETER_field;
 extern jfieldID ResultCode_RESULT_ERROR_NETWORK_NOT_FOUND_field;
 extern jfieldID ResultCode_RESULT_ERROR_UNSUPPORTED_OPERATION_field;

+ 4 - 16
java/jni/ZT_jniutils.cpp

@@ -126,24 +126,12 @@ jobject createEvent(JNIEnv *env, ZT_Event event)
 
 jobject createPeerRole(JNIEnv *env, ZT_PeerRole role)
 {
-    jobject peerRoleObject = NULL;
-
-    jfieldID field;
-    switch(role)
-    {
-    case ZT_PEER_ROLE_LEAF:
-        field = PeerRole_PEER_ROLE_LEAF_field;
-        break;
-    case ZT_PEER_ROLE_MOON:
-        field = PeerRole_PEER_ROLE_MOON_field;
-        break;
-    case ZT_PEER_ROLE_PLANET:
-        field = PeerRole_PEER_ROLE_PLANET_field;
-        break;
+    jobject peerRoleObject = env->CallStaticObjectMethod(PeerRole_class, PeerRole_fromInt_method, role);
+    if (env->ExceptionCheck() || peerRoleObject == NULL) {
+        LOGE("Error creating PeerRole object");
+        return NULL;
     }
 
-    peerRoleObject = env->GetStaticObjectField(PeerRole_class, field);
-
     return peerRoleObject;
 }
 

+ 29 - 4
java/src/com/zerotier/sdk/PeerRole.java

@@ -27,20 +27,45 @@
 
 package com.zerotier.sdk;
 
+/**
+ * What trust hierarchy role does this peer have?
+ *
+ * Defined in ZeroTierOne.h as ZT_PeerRole
+ */
 public enum PeerRole {
 
     /**
      * An ordinary node
      */
-    PEER_ROLE_LEAF,
+    PEER_ROLE_LEAF(0),
 
     /**
      * moon root
      */
-    PEER_ROLE_MOON,
+    PEER_ROLE_MOON(1),
 
     /**
      * planetary root
      */
-    PEER_ROLE_PLANET
-}
+    PEER_ROLE_PLANET(2);
+
+    @SuppressWarnings({"FieldCanBeLocal", "unused"})
+    private final int id;
+
+    PeerRole(int id) {
+        this.id = id;
+    }
+
+    public static PeerRole fromInt(int id) {
+        switch (id) {
+            case 0:
+                return PEER_ROLE_LEAF;
+            case 1:
+                return PEER_ROLE_MOON;
+            case 2:
+                return PEER_ROLE_PLANET;
+            default:
+                throw new RuntimeException("Unhandled value: " + id);
+        }
+    }
+}