make Peer a plain data class

This commit is contained in:
Brenton Bostick 2023-01-31 19:37:45 -05:00
parent f51ce077a3
commit 6cc055dbba
4 changed files with 63 additions and 51 deletions

View File

@ -114,13 +114,6 @@ jmethodID VirtualNetworkType_fromInt_method;
// Instance fields // 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_major_field;
jfieldID Version_minor_field; jfieldID Version_minor_field;
jfieldID Version_revision_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_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(PathChecker_onPathLookup_method = env->GetMethodID(PathChecker_class, "onPathLookup", "(JI)Ljava/net/InetSocketAddress;"));
EXCEPTIONANDNULLCHECK(PeerPhysicalPath_ctor = env->GetMethodID(PeerPhysicalPath_class, "<init>", "(Ljava/net/InetSocketAddress;JJZ)V")); EXCEPTIONANDNULLCHECK(PeerPhysicalPath_ctor = env->GetMethodID(PeerPhysicalPath_class, "<init>", "(Ljava/net/InetSocketAddress;JJZ)V"));
EXCEPTIONANDNULLCHECK(Peer_ctor = env->GetMethodID(Peer_class, "<init>", "()V")); EXCEPTIONANDNULLCHECK(Peer_ctor = env->GetMethodID(Peer_class, "<init>", "(JIIIILcom/zerotier/sdk/PeerRole;[Lcom/zerotier/sdk/PeerPhysicalPath;)V"));
EXCEPTIONANDNULLCHECK(Version_ctor = env->GetMethodID(Version_class, "<init>", "()V")); EXCEPTIONANDNULLCHECK(Version_ctor = env->GetMethodID(Version_class, "<init>", "()V"));
EXCEPTIONANDNULLCHECK(VirtualNetworkConfigListener_onNetworkConfigurationUpdated_method = env->GetMethodID(VirtualNetworkConfigListener_class, "onNetworkConfigurationUpdated", "(JLcom/zerotier/sdk/VirtualNetworkConfigOperation;Lcom/zerotier/sdk/VirtualNetworkConfig;)I")); 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, "<init>", "()V")); EXCEPTIONANDNULLCHECK(VirtualNetworkConfig_ctor = env->GetMethodID(VirtualNetworkConfig_class, "<init>", "()V"));
@ -232,13 +225,6 @@ void setupJNICache(JavaVM *vm) {
// Instance fields // 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_major_field = env->GetFieldID(Version_class, "major", "I"));
EXCEPTIONANDNULLCHECK(Version_minor_field = env->GetFieldID(Version_class, "minor", "I")); EXCEPTIONANDNULLCHECK(Version_minor_field = env->GetFieldID(Version_class, "minor", "I"));
EXCEPTIONANDNULLCHECK(Version_revision_field = env->GetFieldID(Version_class, "revision", "I")); EXCEPTIONANDNULLCHECK(Version_revision_field = env->GetFieldID(Version_class, "revision", "I"));

View File

@ -83,13 +83,6 @@ extern jmethodID VirtualNetworkType_fromInt_method;
// Instance fields // 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_major_field;
extern jfieldID Version_minor_field; extern jfieldID Version_minor_field;
extern jfieldID Version_revision_field; extern jfieldID Version_revision_field;

View File

@ -226,23 +226,32 @@ jobject newPeer(JNIEnv *env, const ZT_Peer &peer)
{ {
LOGV("newPeer called"); LOGV("newPeer called");
jobject peerObject = env->NewObject(Peer_class, Peer_ctor); jobject peerRoleObj = createPeerRole(env, peer.role);
if(env->ExceptionCheck() || peerObject == NULL) if(env->ExceptionCheck() || peerRoleObj == NULL)
{ {
LOGE("Error creating Peer object");
return NULL; // out of memory 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); 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; return peerObject;
} }

View File

@ -27,68 +27,92 @@
package com.zerotier.sdk; 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 { public class Peer {
private long address;
private int versionMajor;
private int versionMinor;
private int versionRev;
private int latency;
private PeerRole role;
private PeerPhysicalPath[] paths;
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) * ZeroTier address (40 bits)
*/ */
public final long address() { public long getAddress() {
return address; return address;
} }
/** /**
* Remote major version or -1 if not known * Remote major version or -1 if not known
*/ */
public final int versionMajor() { public int getVersionMajor() {
return versionMajor; return versionMajor;
} }
/** /**
* Remote minor version or -1 if not known * Remote minor version or -1 if not known
*/ */
public final int versionMinor() { public int getVersionMinor() {
return versionMinor; return versionMinor;
} }
/** /**
* Remote revision or -1 if not known * Remote revision or -1 if not known
*/ */
public final int versionRev() { public int getVersionRev() {
return versionRev; return versionRev;
} }
/** /**
* Last measured latency in milliseconds or zero if unknown * Last measured latency in milliseconds or zero if unknown
*/ */
public final int latency() { public int getLatency() {
return latency; return latency;
} }
/** /**
* What trust hierarchy role does this device have? * What trust hierarchy role does this device have?
*/ */
public final PeerRole role() { public PeerRole getRole() {
return role; return role;
} }
/** /**
* Known network paths to peer * Known network paths to peer
*/ */
public final PeerPhysicalPath[] paths() { public PeerPhysicalPath[] getPaths() {
return paths; return paths;
} }
} }