add Event.fromInt

This commit is contained in:
Brenton Bostick 2023-01-31 12:04:21 -05:00
parent 82749e4442
commit 7c5f256d4a
5 changed files with 52 additions and 67 deletions

View File

@ -102,6 +102,7 @@ jmethodID VirtualNetworkRoute_ctor;
// Static methods
//
jmethodID Event_fromInt_method;
jmethodID InetAddress_getByAddress_method;
//
@ -158,14 +159,6 @@ jfieldID VirtualNetworkRoute_via_field;
// Static fields
//
jfieldID Event_EVENT_DOWN_field;
jfieldID Event_EVENT_FATAL_ERROR_IDENTITY_COLLISION_field;
jfieldID Event_EVENT_OFFLINE_field;
jfieldID Event_EVENT_ONLINE_field;
jfieldID Event_EVENT_REMOTE_TRACE_field;
jfieldID Event_EVENT_TRACE_field;
jfieldID Event_EVENT_UP_field;
jfieldID Event_EVENT_USER_MESSAGE_field;
jfieldID PeerRole_PEER_ROLE_LEAF_field;
jfieldID PeerRole_PEER_ROLE_MOON_field;
jfieldID PeerRole_PEER_ROLE_PLANET_field;
@ -265,6 +258,7 @@ void setupJNICache(JavaVM *vm) {
// Static methods
//
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;"));
//
@ -321,14 +315,6 @@ void setupJNICache(JavaVM *vm) {
// Static fields
//
EXCEPTIONANDNULLCHECK(Event_EVENT_DOWN_field = env->GetStaticFieldID(Event_class, "EVENT_DOWN", "Lcom/zerotier/sdk/Event;"));
EXCEPTIONANDNULLCHECK(Event_EVENT_FATAL_ERROR_IDENTITY_COLLISION_field = env->GetStaticFieldID(Event_class, "EVENT_FATAL_ERROR_IDENTITY_COLLISION", "Lcom/zerotier/sdk/Event;"));
EXCEPTIONANDNULLCHECK(Event_EVENT_OFFLINE_field = env->GetStaticFieldID(Event_class, "EVENT_OFFLINE", "Lcom/zerotier/sdk/Event;"));
EXCEPTIONANDNULLCHECK(Event_EVENT_ONLINE_field = env->GetStaticFieldID(Event_class, "EVENT_ONLINE", "Lcom/zerotier/sdk/Event;"));
EXCEPTIONANDNULLCHECK(Event_EVENT_REMOTE_TRACE_field = env->GetStaticFieldID(Event_class, "EVENT_REMOTE_TRACE", "Lcom/zerotier/sdk/Event;"));
EXCEPTIONANDNULLCHECK(Event_EVENT_TRACE_field = env->GetStaticFieldID(Event_class, "EVENT_TRACE", "Lcom/zerotier/sdk/Event;"));
EXCEPTIONANDNULLCHECK(Event_EVENT_UP_field = env->GetStaticFieldID(Event_class, "EVENT_UP", "Lcom/zerotier/sdk/Event;"));
EXCEPTIONANDNULLCHECK(Event_EVENT_USER_MESSAGE_field = env->GetStaticFieldID(Event_class, "EVENT_USER_MESSAGE", "Lcom/zerotier/sdk/Event;"));
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;"));

View File

@ -71,6 +71,7 @@ extern jmethodID VirtualNetworkRoute_ctor;
// Static methods
//
extern jmethodID Event_fromInt_method;
extern jmethodID InetAddress_getByAddress_method;
//
@ -127,14 +128,6 @@ extern jfieldID VirtualNetworkRoute_via_field;
// Static fields
//
extern jfieldID Event_EVENT_DOWN_field;
extern jfieldID Event_EVENT_FATAL_ERROR_IDENTITY_COLLISION_field;
extern jfieldID Event_EVENT_OFFLINE_field;
extern jfieldID Event_EVENT_ONLINE_field;
extern jfieldID Event_EVENT_REMOTE_TRACE_field;
extern jfieldID Event_EVENT_TRACE_field;
extern jfieldID Event_EVENT_UP_field;
extern jfieldID Event_EVENT_USER_MESSAGE_field;
extern jfieldID PeerRole_PEER_ROLE_LEAF_field;
extern jfieldID PeerRole_PEER_ROLE_MOON_field;
extern jfieldID PeerRole_PEER_ROLE_PLANET_field;

View File

@ -115,41 +115,12 @@ jobject createVirtualNetworkStatus(JNIEnv *env, ZT_VirtualNetworkStatus status)
jobject createEvent(JNIEnv *env, ZT_Event event)
{
jobject eventObject = NULL;
jfieldID field;
switch(event)
{
case ZT_EVENT_UP:
field = Event_EVENT_UP_field;
break;
case ZT_EVENT_OFFLINE:
field = Event_EVENT_OFFLINE_field;
break;
case ZT_EVENT_ONLINE:
field = Event_EVENT_ONLINE_field;
break;
case ZT_EVENT_DOWN:
field = Event_EVENT_DOWN_field;
break;
case ZT_EVENT_FATAL_ERROR_IDENTITY_COLLISION:
field = Event_EVENT_FATAL_ERROR_IDENTITY_COLLISION_field;
break;
case ZT_EVENT_TRACE:
field = Event_EVENT_TRACE_field;
break;
case ZT_EVENT_USER_MESSAGE:
field = Event_EVENT_USER_MESSAGE_field;
break;
case ZT_EVENT_REMOTE_TRACE:
field = Event_EVENT_REMOTE_TRACE_field;
break;
default:
break;
jobject eventObject = env->CallStaticObjectMethod(Event_class, Event_fromInt_method, event);
if (env->ExceptionCheck() || eventObject == NULL) {
LOGE("Error creating Event object");
return NULL;
}
eventObject = env->GetStaticObjectField(Event_class, field);
return eventObject;
}

View File

@ -201,7 +201,7 @@ namespace {
}
jobject eventObject = createEvent(env, event);
if (eventObject == NULL) {
if (env->ExceptionCheck() || eventObject == NULL) {
return;
}

View File

@ -27,6 +27,11 @@
package com.zerotier.sdk;
/**
* Status codes sent to status update callback when things happen
*
* Defined in ZeroTierOne.h as ZT_Event
*/
public enum Event {
/**
@ -35,19 +40,19 @@ public enum Event {
* This is the first event generated, and is always sent. It may occur
* before Node's constructor returns.
*/
EVENT_UP,
EVENT_UP(0),
/**
* Node is offline -- network does not seem to be reachable by any available strategy
*/
EVENT_OFFLINE,
EVENT_OFFLINE(1),
/**
* Node is online -- at least one upstream node appears reachable
*
* Meta-data: none
*/
EVENT_ONLINE,
EVENT_ONLINE(2),
/**
* Node is shutting down
@ -56,7 +61,7 @@ public enum Event {
* It's done for convenience, since cleaning up other state in the event
* handler may appear more idiomatic.</p>
*/
EVENT_DOWN,
EVENT_DOWN(3),
/**
* Your identity has collided with another node's ZeroTier address
@ -86,7 +91,7 @@ public enum Event {
* condition is a good way to make sure it never arises. It's like how
* umbrellas prevent rain and smoke detectors prevent fires. They do, right?</p>
*/
EVENT_FATAL_ERROR_IDENTITY_COLLISION,
EVENT_FATAL_ERROR_IDENTITY_COLLISION(4),
/**
* Trace (debugging) message
@ -95,7 +100,7 @@ public enum Event {
*
* <p>Meta-data: {@link String}, TRACE message</p>
*/
EVENT_TRACE,
EVENT_TRACE(5),
/**
* VERB_USER_MESSAGE received
@ -103,7 +108,7 @@ public enum Event {
* These are generated when a VERB_USER_MESSAGE packet is received via
* ZeroTier VL1.
*/
EVENT_USER_MESSAGE,
EVENT_USER_MESSAGE(6),
/**
* Remote trace received
@ -115,5 +120,35 @@ public enum Event {
* these, and controllers only save them if they pertain to networks
* with remote tracing enabled.
*/
EVENT_REMOTE_TRACE;
}
EVENT_REMOTE_TRACE(7);
@SuppressWarnings({"FieldCanBeLocal", "unused"})
private final int id;
Event(int id) {
this.id = id;
}
public static Event fromInt(int id) {
switch (id) {
case 0:
return EVENT_UP;
case 1:
return EVENT_OFFLINE;
case 2:
return EVENT_ONLINE;
case 3:
return EVENT_DOWN;
case 4:
return EVENT_FATAL_ERROR_IDENTITY_COLLISION;
case 5:
return EVENT_TRACE;
case 6:
return EVENT_USER_MESSAGE;
case 7:
return EVENT_REMOTE_TRACE;
default:
throw new RuntimeException("Unhandled value: " + id);
}
}
}