make NodeStatus a plain data class

This commit is contained in:
Brenton Bostick 2023-01-31 13:40:17 -05:00
parent acf5b3579b
commit 4861ec5a40
6 changed files with 75 additions and 63 deletions

View File

@ -114,10 +114,6 @@ jmethodID VirtualNetworkType_fromInt_method;
// Instance fields
//
jfieldID NodeStatus_address_field;
jfieldID NodeStatus_online_field;
jfieldID NodeStatus_publicIdentity_field;
jfieldID NodeStatus_secretIdentity_field;
jfieldID Node_configListener_field;
jfieldID Node_eventListener_field;
jfieldID Node_frameListener_field;
@ -218,7 +214,7 @@ void setupJNICache(JavaVM *vm) {
EXCEPTIONANDNULLCHECK(InetSocketAddress_ctor = env->GetMethodID(InetSocketAddress_class, "<init>", "(Ljava/net/InetAddress;I)V"));
EXCEPTIONANDNULLCHECK(InetSocketAddress_getAddress_method = env->GetMethodID(InetSocketAddress_class, "getAddress", "()Ljava/net/InetAddress;"));
EXCEPTIONANDNULLCHECK(InetSocketAddress_getPort_method = env->GetMethodID(InetSocketAddress_class, "getPort", "()I"));
EXCEPTIONANDNULLCHECK(NodeStatus_ctor = env->GetMethodID(NodeStatus_class, "<init>", "()V"));
EXCEPTIONANDNULLCHECK(NodeStatus_ctor = env->GetMethodID(NodeStatus_class, "<init>", "(JLjava/lang/String;Ljava/lang/String;Z)V"));
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;"));
@ -247,10 +243,6 @@ void setupJNICache(JavaVM *vm) {
// Instance fields
//
EXCEPTIONANDNULLCHECK(NodeStatus_address_field = env->GetFieldID(NodeStatus_class, "address", "J"));
EXCEPTIONANDNULLCHECK(NodeStatus_online_field = env->GetFieldID(NodeStatus_class, "online", "Z"));
EXCEPTIONANDNULLCHECK(NodeStatus_publicIdentity_field = env->GetFieldID(NodeStatus_class, "publicIdentity", "Ljava/lang/String;"));
EXCEPTIONANDNULLCHECK(NodeStatus_secretIdentity_field = env->GetFieldID(NodeStatus_class, "secretIdentity", "Ljava/lang/String;"));
EXCEPTIONANDNULLCHECK(Node_configListener_field = env->GetFieldID(Node_class, "configListener", "Lcom/zerotier/sdk/VirtualNetworkConfigListener;"));
EXCEPTIONANDNULLCHECK(Node_eventListener_field = env->GetFieldID(Node_class, "eventListener", "Lcom/zerotier/sdk/EventListener;"));
EXCEPTIONANDNULLCHECK(Node_frameListener_field = env->GetFieldID(Node_class, "frameListener", "Lcom/zerotier/sdk/VirtualNetworkFrameListener;"));

View File

@ -83,10 +83,6 @@ extern jmethodID VirtualNetworkType_fromInt_method;
// Instance fields
//
extern jfieldID NodeStatus_address_field;
extern jfieldID NodeStatus_online_field;
extern jfieldID NodeStatus_publicIdentity_field;
extern jfieldID NodeStatus_secretIdentity_field;
extern jfieldID Node_configListener_field;
extern jfieldID Node_eventListener_field;
extern jfieldID Node_frameListener_field;

View File

@ -428,3 +428,34 @@ jobject newVirtualNetworkDNS(JNIEnv *env, const ZT_VirtualNetworkDNS &dns)
}
return NULL;
}
jobject newNodeStatus(JNIEnv *env, const ZT_NodeStatus &status) {
jstring pubIdentStr = env->NewStringUTF(status.publicIdentity);
if(env->ExceptionCheck() || pubIdentStr == NULL)
{
LOGE("Exception creating new string");
return NULL;
}
jstring secIdentStr = env->NewStringUTF(status.secretIdentity);
if(env->ExceptionCheck() || secIdentStr == NULL)
{
LOGE("Exception creating new string");
return NULL;
}
jobject nodeStatusObj = env->NewObject(
NodeStatus_class,
NodeStatus_ctor,
status.address,
pubIdentStr,
secIdentStr,
status.online);
if(env->ExceptionCheck() || nodeStatusObj == NULL) {
LOGE("Exception creating new NodeStatus");
return NULL;
}
return nodeStatusObj;
}

View File

@ -96,4 +96,6 @@ jobject newVirtualNetworkRoute(JNIEnv *env, const ZT_VirtualNetworkRoute &route)
jobject newVirtualNetworkDNS(JNIEnv *env, const ZT_VirtualNetworkDNS &dns);
jobject newNodeStatus(JNIEnv *env, const ZT_NodeStatus &status);
#endif // ZT_jniutils_h_

View File

@ -1126,41 +1126,11 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_status
{
int64_t nodeId = (int64_t) id;
ZT_Node *node = findNode(nodeId);
if(node == NULL)
{
// cannot find valid node. We should never get here.
return 0;
}
// create a com.zerotier.sdk.NodeStatus object
jobject nodeStatusObj = env->NewObject(NodeStatus_class, NodeStatus_ctor);
if(nodeStatusObj == NULL)
{
return NULL;
}
ZT_NodeStatus nodeStatus;
ZT_Node_status(node, &nodeStatus);
env->SetLongField(nodeStatusObj, NodeStatus_address_field, nodeStatus.address);
jstring pubIdentStr = env->NewStringUTF(nodeStatus.publicIdentity);
if(pubIdentStr == NULL)
{
return NULL; // out of memory
}
env->SetObjectField(nodeStatusObj, NodeStatus_publicIdentity_field, pubIdentStr);
jstring secIdentStr = env->NewStringUTF(nodeStatus.secretIdentity);
if(secIdentStr == NULL)
{
return NULL; // out of memory
}
env->SetObjectField(nodeStatusObj, NodeStatus_secretIdentity_field, secIdentStr);
env->SetBooleanField(nodeStatusObj, NodeStatus_online_field, nodeStatus.online);
return nodeStatusObj;
return newNodeStatus(env, nodeStatus);
}
/*

View File

@ -27,43 +27,64 @@
package com.zerotier.sdk;
public final class NodeStatus {
private long address;
private String publicIdentity;
private String secretIdentity;
private boolean online;
import com.zerotier.sdk.util.StringUtils;
private NodeStatus() {}
/**
* Current node status
*
* Defined in ZeroTierOne.h as ZT_NodeStatus
*/
public class NodeStatus {
private final long address;
private final String publicIdentity;
private final String secretIdentity;
private final boolean online;
public NodeStatus(long address, String publicIdentity, String secretIdentity, boolean online) {
this.address = address;
this.publicIdentity = publicIdentity;
this.secretIdentity = secretIdentity;
this.online = online;
}
@Override
public String toString() {
return "NodeStatus(" + StringUtils.addressToString(address) + ", " + publicIdentity + ", " + secretIdentity + ", " + online + ")";
}
/**
* 40-bit ZeroTier address of this node
*/
public final long getAddress() {
return address;
}
public long getAddress() {
return address;
}
/**
* Public identity in string-serialized form (safe to send to others)
*
* <p>This identity will remain valid as long as the node exists.</p>
*/
public final String getPublicIdentity() {
return publicIdentity;
}
public String getPublicIdentity() {
return publicIdentity;
}
/**
* Full identity including secret key in string-serialized form
*
* <p>This identity will remain valid as long as the node exists.</p>
*/
public final String getSecretIdentity() {
return secretIdentity;
}
public String getSecretIdentity() {
return secretIdentity;
}
/**
* True if some kind of connectivity appears available
*/
public final boolean isOnline() {
return online;
}
}
public boolean isOnline() {
return online;
}
}