From 63f70ba4659d61bac0d800a23b01b07b16c2f14c Mon Sep 17 00:00:00 2001 From: Brenton Bostick Date: Tue, 31 Jan 2023 18:05:08 -0500 Subject: [PATCH] make PeerPhysicalPath a plain data class --- java/jni/ZT_jnicache.cpp | 10 +--- java/jni/ZT_jnicache.h | 4 -- java/jni/ZT_jniutils.cpp | 25 +++++----- .../com/zerotier/sdk/PeerPhysicalPath.java | 48 ++++++++++++++----- 4 files changed, 48 insertions(+), 39 deletions(-) diff --git a/java/jni/ZT_jnicache.cpp b/java/jni/ZT_jnicache.cpp index 764f7f6c1..13087ae2e 100644 --- a/java/jni/ZT_jnicache.cpp +++ b/java/jni/ZT_jnicache.cpp @@ -114,10 +114,6 @@ jmethodID VirtualNetworkType_fromInt_method; // Instance fields // -jfieldID PeerPhysicalPath_address_field; -jfieldID PeerPhysicalPath_lastReceive_field; -jfieldID PeerPhysicalPath_lastSend_field; -jfieldID PeerPhysicalPath_preferred_field; jfieldID Peer_address_field; jfieldID Peer_latency_field; jfieldID Peer_paths_field; @@ -211,7 +207,7 @@ void setupJNICache(JavaVM *vm) { EXCEPTIONANDNULLCHECK(PacketSender_onSendPacketRequested_method = env->GetMethodID(PacketSender_class, "onSendPacketRequested", "(JLjava/net/InetSocketAddress;[BI)I")); 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, "", "()V")); + EXCEPTIONANDNULLCHECK(PeerPhysicalPath_ctor = env->GetMethodID(PeerPhysicalPath_class, "", "(Ljava/net/InetSocketAddress;JJZZ)V")); EXCEPTIONANDNULLCHECK(Peer_ctor = env->GetMethodID(Peer_class, "", "()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")); @@ -236,10 +232,6 @@ void setupJNICache(JavaVM *vm) { // Instance fields // - EXCEPTIONANDNULLCHECK(PeerPhysicalPath_address_field = env->GetFieldID(PeerPhysicalPath_class, "address", "Ljava/net/InetSocketAddress;")); - EXCEPTIONANDNULLCHECK(PeerPhysicalPath_lastReceive_field = env->GetFieldID(PeerPhysicalPath_class, "lastReceive", "J")); - EXCEPTIONANDNULLCHECK(PeerPhysicalPath_lastSend_field = env->GetFieldID(PeerPhysicalPath_class, "lastSend", "J")); - EXCEPTIONANDNULLCHECK(PeerPhysicalPath_preferred_field = env->GetFieldID(PeerPhysicalPath_class, "preferred", "Z")); 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;")); diff --git a/java/jni/ZT_jnicache.h b/java/jni/ZT_jnicache.h index 8dd176ab4..1c4ea210b 100644 --- a/java/jni/ZT_jnicache.h +++ b/java/jni/ZT_jnicache.h @@ -83,10 +83,6 @@ extern jmethodID VirtualNetworkType_fromInt_method; // Instance fields // -extern jfieldID PeerPhysicalPath_address_field; -extern jfieldID PeerPhysicalPath_lastReceive_field; -extern jfieldID PeerPhysicalPath_lastSend_field; -extern jfieldID PeerPhysicalPath_preferred_field; extern jfieldID Peer_address_field; extern jfieldID Peer_latency_field; extern jfieldID Peer_paths_field; diff --git a/java/jni/ZT_jniutils.cpp b/java/jni/ZT_jniutils.cpp index 5f4b1f702..d6faafeb2 100644 --- a/java/jni/ZT_jniutils.cpp +++ b/java/jni/ZT_jniutils.cpp @@ -201,26 +201,23 @@ jobject newPeerPhysicalPath(JNIEnv *env, const ZT_PeerPhysicalPath &ppp) { LOGV("newPeerPhysicalPath Called"); - jobject pppObject = env->NewObject(PeerPhysicalPath_class, PeerPhysicalPath_ctor); - if(env->ExceptionCheck() || pppObject == NULL) - { - LOGE("Error creating PPP object"); - return NULL; // out of memory - } - jobject addressObject = newInetSocketAddress(env, ppp.address); if(env->ExceptionCheck() || addressObject == NULL) { LOGE("Error creating InetSocketAddress object"); return NULL; } - env->SetObjectField(pppObject, PeerPhysicalPath_address_field, addressObject); - env->SetLongField(pppObject, PeerPhysicalPath_lastSend_field, ppp.lastSend); - env->SetLongField(pppObject, PeerPhysicalPath_lastReceive_field, ppp.lastReceive); - env->SetBooleanField(pppObject, PeerPhysicalPath_preferred_field, ppp.preferred); - - if(env->ExceptionCheck()) { - LOGE("Exception assigning fields to PeerPhysicalPath object"); + jobject pppObject = env->NewObject( + PeerPhysicalPath_class, + PeerPhysicalPath_ctor, + addressObject, + ppp.lastSend, + ppp.lastReceive, + ppp.preferred); + if(env->ExceptionCheck() || pppObject == NULL) + { + LOGE("Error creating PPP object"); + return NULL; } return pppObject; diff --git a/java/src/com/zerotier/sdk/PeerPhysicalPath.java b/java/src/com/zerotier/sdk/PeerPhysicalPath.java index 4aee98776..078d11509 100644 --- a/java/src/com/zerotier/sdk/PeerPhysicalPath.java +++ b/java/src/com/zerotier/sdk/PeerPhysicalPath.java @@ -31,48 +31,72 @@ import java.net.InetSocketAddress; /** * Physical network path to a peer + * + * Defined in ZeroTierOne.h as ZT_PeerPhysicalPath */ -public final class PeerPhysicalPath { - private InetSocketAddress address; - private long lastSend; - private long lastReceive; - private boolean fixed; - private boolean preferred; +public class PeerPhysicalPath { - private PeerPhysicalPath() {} + private final InetSocketAddress address; + + private final long lastSend; + + private final long lastReceive; + + private final boolean fixed; + + private final boolean preferred; + + public PeerPhysicalPath(InetSocketAddress address, long lastSend, long lastReceive, boolean fixed, boolean preferred) { + this.address = address; + if (lastSend < 0) { + throw new RuntimeException("lastSend < 0: " + lastSend); + } + this.lastSend = lastSend; + if (lastReceive < 0) { + throw new RuntimeException("lastReceive < 0: " + lastReceive); + } + this.lastReceive = lastReceive; + this.fixed = fixed; + this.preferred = preferred; + } + + @Override + public String toString() { + return "PeerPhysicalPath(" + address + ", " + lastSend + ", " + lastReceive + ", " + fixed + ", " + preferred + ")"; + } /** * Address of endpoint */ - public final InetSocketAddress address() { + public InetSocketAddress getAddress() { return address; } /** * Time of last send in milliseconds or 0 for never */ - public final long lastSend() { + public long getLastSend() { return lastSend; } /** * Time of last receive in milliseconds or 0 for never */ - public final long lastReceive() { + public long getLastReceive() { return lastReceive; } /** * Is path fixed? (i.e. not learned, static) */ - public final boolean isFixed() { + public boolean isFixed() { return fixed; } /** * Is path preferred? */ - public final boolean isPreferred() { + public boolean isPreferred() { return preferred; } }