diff --git a/java/jni/ZT_jnicache.cpp b/java/jni/ZT_jnicache.cpp index ae1d6615d..095b647ce 100644 --- a/java/jni/ZT_jnicache.cpp +++ b/java/jni/ZT_jnicache.cpp @@ -114,13 +114,6 @@ jmethodID VirtualNetworkType_fromInt_method; // Instance fields // -jfieldID Peer_address_field; -jfieldID Peer_latency_field; -jfieldID Peer_paths_field; -jfieldID Peer_role_field; -jfieldID Peer_versionMajor_field; -jfieldID Peer_versionMinor_field; -jfieldID Peer_versionRev_field; jfieldID Version_major_field; jfieldID Version_minor_field; jfieldID Version_revision_field; @@ -208,7 +201,7 @@ void setupJNICache(JavaVM *vm) { EXCEPTIONANDNULLCHECK(PathChecker_onPathCheck_method = env->GetMethodID(PathChecker_class, "onPathCheck", "(JJLjava/net/InetSocketAddress;)Z")); EXCEPTIONANDNULLCHECK(PathChecker_onPathLookup_method = env->GetMethodID(PathChecker_class, "onPathLookup", "(JI)Ljava/net/InetSocketAddress;")); EXCEPTIONANDNULLCHECK(PeerPhysicalPath_ctor = env->GetMethodID(PeerPhysicalPath_class, "", "(Ljava/net/InetSocketAddress;JJZ)V")); - EXCEPTIONANDNULLCHECK(Peer_ctor = env->GetMethodID(Peer_class, "", "()V")); + EXCEPTIONANDNULLCHECK(Peer_ctor = env->GetMethodID(Peer_class, "", "(JIIIILcom/zerotier/sdk/PeerRole;[Lcom/zerotier/sdk/PeerPhysicalPath;)V")); EXCEPTIONANDNULLCHECK(Version_ctor = env->GetMethodID(Version_class, "", "()V")); EXCEPTIONANDNULLCHECK(VirtualNetworkConfigListener_onNetworkConfigurationUpdated_method = env->GetMethodID(VirtualNetworkConfigListener_class, "onNetworkConfigurationUpdated", "(JLcom/zerotier/sdk/VirtualNetworkConfigOperation;Lcom/zerotier/sdk/VirtualNetworkConfig;)I")); EXCEPTIONANDNULLCHECK(VirtualNetworkConfig_ctor = env->GetMethodID(VirtualNetworkConfig_class, "", "()V")); @@ -232,13 +225,6 @@ void setupJNICache(JavaVM *vm) { // Instance fields // - EXCEPTIONANDNULLCHECK(Peer_address_field = env->GetFieldID(Peer_class, "address", "J")); - EXCEPTIONANDNULLCHECK(Peer_latency_field = env->GetFieldID(Peer_class, "latency", "I")); - EXCEPTIONANDNULLCHECK(Peer_paths_field = env->GetFieldID(Peer_class, "paths", "[Lcom/zerotier/sdk/PeerPhysicalPath;")); - EXCEPTIONANDNULLCHECK(Peer_role_field = env->GetFieldID(Peer_class, "role", "Lcom/zerotier/sdk/PeerRole;")); - EXCEPTIONANDNULLCHECK(Peer_versionMajor_field = env->GetFieldID(Peer_class, "versionMajor", "I")); - EXCEPTIONANDNULLCHECK(Peer_versionMinor_field = env->GetFieldID(Peer_class, "versionMinor", "I")); - EXCEPTIONANDNULLCHECK(Peer_versionRev_field = env->GetFieldID(Peer_class, "versionRev", "I")); EXCEPTIONANDNULLCHECK(Version_major_field = env->GetFieldID(Version_class, "major", "I")); EXCEPTIONANDNULLCHECK(Version_minor_field = env->GetFieldID(Version_class, "minor", "I")); EXCEPTIONANDNULLCHECK(Version_revision_field = env->GetFieldID(Version_class, "revision", "I")); diff --git a/java/jni/ZT_jnicache.h b/java/jni/ZT_jnicache.h index 1c4ea210b..0630d6355 100644 --- a/java/jni/ZT_jnicache.h +++ b/java/jni/ZT_jnicache.h @@ -83,13 +83,6 @@ extern jmethodID VirtualNetworkType_fromInt_method; // Instance fields // -extern jfieldID Peer_address_field; -extern jfieldID Peer_latency_field; -extern jfieldID Peer_paths_field; -extern jfieldID Peer_role_field; -extern jfieldID Peer_versionMajor_field; -extern jfieldID Peer_versionMinor_field; -extern jfieldID Peer_versionRev_field; extern jfieldID Version_major_field; extern jfieldID Version_minor_field; extern jfieldID Version_revision_field; diff --git a/java/jni/ZT_jniutils.cpp b/java/jni/ZT_jniutils.cpp index 3f90c6f79..220f062d8 100644 --- a/java/jni/ZT_jniutils.cpp +++ b/java/jni/ZT_jniutils.cpp @@ -226,23 +226,32 @@ jobject newPeer(JNIEnv *env, const ZT_Peer &peer) { LOGV("newPeer called"); - jobject peerObject = env->NewObject(Peer_class, Peer_ctor); - if(env->ExceptionCheck() || peerObject == NULL) + jobject peerRoleObj = createPeerRole(env, peer.role); + if(env->ExceptionCheck() || peerRoleObj == NULL) { - LOGE("Error creating Peer object"); return NULL; // out of memory } - env->SetLongField(peerObject, Peer_address_field, (jlong)peer.address); - env->SetIntField(peerObject, Peer_versionMajor_field, peer.versionMajor); - env->SetIntField(peerObject, Peer_versionMinor_field, peer.versionMinor); - env->SetIntField(peerObject, Peer_versionRev_field, peer.versionRev); - env->SetIntField(peerObject, Peer_latency_field, peer.latency); - env->SetObjectField(peerObject, Peer_role_field, createPeerRole(env, peer.role)); - jobjectArray arrayObject = newPeerPhysicalPathArray(env, peer.paths, peer.pathCount); + if (env->ExceptionCheck() || arrayObject == NULL) { + return NULL; + } - env->SetObjectField(peerObject, Peer_paths_field, arrayObject); + jobject peerObject = env->NewObject( + Peer_class, + Peer_ctor, + peer.address, + peer.versionMajor, + peer.versionMinor, + peer.versionRev, + peer.latency, + peerRoleObj, + arrayObject); + if(env->ExceptionCheck() || peerObject == NULL) + { + LOGE("Error creating Peer object"); + return NULL; + } return peerObject; } diff --git a/java/src/com/zerotier/sdk/Peer.java b/java/src/com/zerotier/sdk/Peer.java index 63ef689b1..e3d544381 100644 --- a/java/src/com/zerotier/sdk/Peer.java +++ b/java/src/com/zerotier/sdk/Peer.java @@ -27,68 +27,92 @@ package com.zerotier.sdk; -import java.util.ArrayList; +import com.zerotier.sdk.util.StringUtils; + +import java.util.Arrays; /** - * Peer status result + * Peer status result buffer + * + * Defined in ZeroTierOne.h as ZT_Peer */ -public final class Peer { - private long address; - private int versionMajor; - private int versionMinor; - private int versionRev; - private int latency; - private PeerRole role; - private PeerPhysicalPath[] paths; +public class Peer { - private Peer() {} + private final long address; + + private final int versionMajor; + + private final int versionMinor; + + private final int versionRev; + + private final int latency; + + private final PeerRole role; + + private final PeerPhysicalPath[] paths; + + public Peer(long address, int versionMajor, int versionMinor, int versionRev, int latency, PeerRole role, PeerPhysicalPath[] paths) { + this.address = address; + this.versionMajor = versionMajor; + this.versionMinor = versionMinor; + this.versionRev = versionRev; + this.latency = latency; + this.role = role; + this.paths = paths; + } + + @Override + public String toString() { + return "Peer(" + StringUtils.addressToString(address) + ", " + versionMajor + ", " + versionMinor + ", " + versionRev + ", " + latency + ", " + role + ", " + Arrays.toString(paths) + ")"; + } /** * ZeroTier address (40 bits) */ - public final long address() { + public long getAddress() { return address; } /** * Remote major version or -1 if not known */ - public final int versionMajor() { + public int getVersionMajor() { return versionMajor; } /** * Remote minor version or -1 if not known */ - public final int versionMinor() { + public int getVersionMinor() { return versionMinor; } /** * Remote revision or -1 if not known */ - public final int versionRev() { + public int getVersionRev() { return versionRev; } /** * Last measured latency in milliseconds or zero if unknown */ - public final int latency() { + public int getLatency() { return latency; } /** * What trust hierarchy role does this device have? */ - public final PeerRole role() { + public PeerRole getRole() { return role; } /** * Known network paths to peer */ - public final PeerPhysicalPath[] paths() { + public PeerPhysicalPath[] getPaths() { return paths; } }