From 5f314f209e890fe38d974927b3b63ebb0ecb24b6 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Mon, 20 Apr 2015 20:32:29 -0700 Subject: [PATCH] Updated Java Node class native methods to pass the node ID This is so that we know which C-based ZT1_Node struct is being operated on Signed-off-by: Grant Limberg --- java/jni/Android.mk | 7 ++ java/jni/com_zerotierone_sdk_Node.h | 44 ++++++--- java/src/com/zerotierone/sdk/Node.java | 119 ++++++++++++++++++++----- 3 files changed, 134 insertions(+), 36 deletions(-) diff --git a/java/jni/Android.mk b/java/jni/Android.mk index 8bc63e7cf..74be8d4f7 100644 --- a/java/jni/Android.mk +++ b/java/jni/Android.mk @@ -3,6 +3,9 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := ZeroTierOneJNI +LOCAL_C_INCLUDES := $(ZT1)/include + +# ZeroTierOne SDK source files LOCAL_SRC_FILES := \ $(ZT1)/ext/lz4/lz4.c \ $(ZT1)/ext/json-parser/json.c \ @@ -31,4 +34,8 @@ LOCAL_SRC_FILES := \ $(ZT1)/osdep/Http.cpp \ $(ZT1)/osdep/OSUtils.cpp +# JNI Files +LOCAL_SRC_FILES += \ + com_zerotierone_sdk_Node.cpp + include $(BUILD_SHARED_LIBRARY) \ No newline at end of file diff --git a/java/jni/com_zerotierone_sdk_Node.h b/java/jni/com_zerotierone_sdk_Node.h index 6dc0f35e0..706e7bcd7 100644 --- a/java/jni/com_zerotierone_sdk_Node.h +++ b/java/jni/com_zerotierone_sdk_Node.h @@ -7,61 +7,77 @@ #ifdef __cplusplus extern "C" { #endif +/* + * Class: com_zerotierone_sdk_Node + * Method: node_init + * Signature: (J)Lcom/zerotierone/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_node_1init + (JNIEnv *, jobject, jlong); + +/* + * Class: com_zerotierone_sdk_Node + * Method: node_delete + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_com_zerotierone_sdk_Node_node_1delete + (JNIEnv *, jobject, jlong); + /* * Class: com_zerotierone_sdk_Node * Method: processVirtualNetworkFrame - * Signature: (JJJJIILjava/nio/ByteBuffer;ILjava/lang/Long;)Lcom/zerotierone/sdk/ResultCode; + * Signature: (JJJJJIILjava/nio/ByteBuffer;ILjava/lang/Long;)Lcom/zerotierone/sdk/ResultCode; */ JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_processVirtualNetworkFrame - (JNIEnv *, jobject, jlong, jlong, jlong, jlong, jint, jint, jobject, jint, jobject); + (JNIEnv *, jobject, jlong, jlong, jlong, jlong, jlong, jint, jint, jobject, jint, jobject); /* * Class: com_zerotierone_sdk_Node * Method: processBackgroundTasks - * Signature: (JLjava/lang/Long;)Lcom/zerotierone/sdk/ResultCode; + * Signature: (JJLjava/lang/Long;)Lcom/zerotierone/sdk/ResultCode; */ JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_processBackgroundTasks - (JNIEnv *, jobject, jlong, jobject); + (JNIEnv *, jobject, jlong, jlong, jobject); /* * Class: com_zerotierone_sdk_Node * Method: join - * Signature: (J)Lcom/zerotierone/sdk/ResultCode; + * Signature: (JJ)Lcom/zerotierone/sdk/ResultCode; */ JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_join - (JNIEnv *, jobject, jlong); + (JNIEnv *, jobject, jlong, jlong); /* * Class: com_zerotierone_sdk_Node * Method: leave - * Signature: (J)Lcom/zerotierone/sdk/ResultCode; + * Signature: (JJ)Lcom/zerotierone/sdk/ResultCode; */ JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_leave - (JNIEnv *, jobject, jlong); + (JNIEnv *, jobject, jlong, jlong); /* * Class: com_zerotierone_sdk_Node * Method: multicastSubscribe - * Signature: (JJJ)Lcom/zerotierone/sdk/ResultCode; + * Signature: (JJJJ)Lcom/zerotierone/sdk/ResultCode; */ JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_multicastSubscribe - (JNIEnv *, jobject, jlong, jlong, jlong); + (JNIEnv *, jobject, jlong, jlong, jlong, jlong); /* * Class: com_zerotierone_sdk_Node * Method: multicastUnsubscribe - * Signature: (JJJ)Lcom/zerotierone/sdk/ResultCode; + * Signature: (JJJJ)Lcom/zerotierone/sdk/ResultCode; */ JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_multicastUnsubscribe - (JNIEnv *, jobject, jlong, jlong, jlong); + (JNIEnv *, jobject, jlong, jlong, jlong, jlong); /* * Class: com_zerotierone_sdk_Node * Method: address - * Signature: ()J + * Signature: (J)J */ JNIEXPORT jlong JNICALL Java_com_zerotierone_sdk_Node_address - (JNIEnv *, jobject); + (JNIEnv *, jobject, jlong); #ifdef __cplusplus } diff --git a/java/src/com/zerotierone/sdk/Node.java b/java/src/com/zerotierone/sdk/Node.java index e3786162d..ca84270cb 100644 --- a/java/src/com/zerotierone/sdk/Node.java +++ b/java/src/com/zerotierone/sdk/Node.java @@ -35,12 +35,26 @@ public class Node { System.loadLibrary("ZeroTierOneJNI"); } - private final DataStoreGetListener getListener; - private final DataStorePutListener putListener; - private final PacketSender sender; - private final VirtualNetworkFrameListener frameListener; - private final VirtualNetworkConfigListener configListener; - + private static final String TAG = "NODE"; + + /** + * Node ID for JNI purposes. + * Currently set to the now value passed in at the constructor + * + * -1 if the node has already been closed + */ + private final long nodeId; + + private final DataStoreGetListener getListener; + private final DataStorePutListener putListener; + private final PacketSender sender; + private final VirtualNetworkFrameListener frameListener; + private final VirtualNetworkConfigListener configListener; + + + private native ResultCode node_init(long now); + private native void node_delete(long nodeId); + public Node(long now, DataStoreGetListener getListener, DataStorePutListener putListener, @@ -48,33 +62,78 @@ public class Node { VirtualNetworkFrameListener frameListener, VirtualNetworkConfigListener configListener) { - this.getListener = getListener; - this.putListener = putListener; - this.sender = sender; - this.frameListener = frameListener; - this.configListener = configListener; + this.nodeId = now; + + this.getListener = getListener; + this.putListener = putListener; + this.sender = sender; + this.frameListener = frameListener; + this.configListener = configListener; + + ResultCode rc = node_init(now); + if(rc.getValue() != ResultCode.RESULT_OK) + { + // TODO: Throw Exception + } } - public native ResultCode processVirtualNetworkFrame( + public void close() { + if(nodeId != -1) { + node_delete(nodeId); + nodeId = -1; + } + } + + private native ResultCode processVirtualNetworkFrame( + long nodeId, long now, long nwid, long sourceMac, long destMac, - int etherTYpe, + int etherType, int vlanId, ByteBuffer frameData, int frameLength, Long nextBackgroundTaskDeadline); - public native ResultCode processBackgroundTasks( + public ResultCode processVirtualNetworkFrame( + long now, + long nwid, + long sourceMac, + long destMac, + int etherType, + int vlanId, + ByteBuffer frameData, + int frameLength, + Long nextBackgroundTaskDeadline) { + return processVirtualNetworkFrame( + nodeId, now, nwid, sourceMac, destMac, etherType, vlanId, + frameData, frameLength, nextBackgroundTaskDeadline); + } + + private native ResultCode processBackgroundTasks( + long nodeId, long now, Long nextBackgroundTaskDeadline); - public native ResultCode join(long nwid); + public ResultCode processBackgroundTasks(long now, long nextBackgroundTaskDeadline) { + return processBackgroundTasks(nodeId, now, nextBackgroundTaskDeadline); + } - public native ResultCode leave(long nwid); + private native ResultCode join(long nodeId, long nwid); - public native ResultCode multicastSubscribe( + public ResultCode join(long nwid) { + return join(nodeId, nwid); + } + + private native ResultCode leave(long nodeId, long nwid); + + public ResultCode leave(long nwid) { + return leave(nodeId, nwid); + } + + private native ResultCode multicastSubscribe( + long nodeId, long nwid, long multicastGroup, long multicastAdi); @@ -82,10 +141,18 @@ public class Node { public ResultCode multicastSubscribe( long nwid, long multicastGroup) { - return multicastSubscribe(nwid, multicastGroup, 0); + return multicastSubscribe(nodeId, nwid, multicastGroup, 0); } - public native ResultCode multicastUnsubscribe( + public ResultCode multicastSubscribe( + long nwid, + long multicastGroup, + long multicastAdi) { + return multicastSubscribe(nodeId, nwid, multicastGroup, multicastAdi); + } + + private native ResultCode multicastUnsubscribe( + long nodeId, long nwid, long multicastGroup, long multicastAdi); @@ -93,11 +160,19 @@ public class Node { public ResultCode multicastUnsubscribe( long nwid, long multicastGroup) { - return multicastUnsubscribe(nwid, multicastGroup, 0); + return multicastUnsubscribe(nodeId, nwid, multicastGroup, 0); } - public native long address(); - + public ResultCode multicastUnsubscribe( + long nwid, + long multicastGroup, + long multicastAdi) { + return multicastUnsubscribe(nodeId, nwid, multicastGroup, multicastAdi); + } + private native long address(long nodeId); + public long address() { + return address(nodeId); + } } \ No newline at end of file