From 46adf870a13a2fdb06a89c517d0cc2bd7cb35928 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Thu, 23 Apr 2015 21:39:07 -0700 Subject: [PATCH] move utility functions from anon namespace to ZT1_jniutils Signed-off-by: Grant Limberg --- java/jni/ZT1_jniutils.cpp | 178 ++++++++++++++++++++++++++ java/jni/ZT1_jniutils.h | 4 + java/jni/com_zerotierone_sdk_Node.cpp | 157 +---------------------- 3 files changed, 184 insertions(+), 155 deletions(-) diff --git a/java/jni/ZT1_jniutils.cpp b/java/jni/ZT1_jniutils.cpp index aae733268..8ec56acfd 100644 --- a/java/jni/ZT1_jniutils.cpp +++ b/java/jni/ZT1_jniutils.cpp @@ -4,7 +4,185 @@ extern "C" { #endif +jobject createResultObject(JNIEnv *env, ZT1_ResultCode code) +{ + // cache the class so we don't have to + // look it up every time we need to create a java + // ResultCode object + static jclass resultClass = NULL; + + jobject resultObject = NULL; + if(resultClass == NULL) + { + resultClass = env->FindClass("com/zerotierone/sdk/ResultCode"); + if(resultClass == NULL) + { + return NULL; // exception thrown + } + } + + std::string fieldName; + switch(code) + { + case ZT1_RESULT_OK: + fieldName = "ZT1_RESULT_OK"; + break; + case ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY: + fieldName = "ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY"; + break; + case ZT1_RESULT_FATAL_ERROR_DATA_STORE_FAILED: + fieldName = "ZT1_RESULT_FATAL_ERROR_DATA_STORE_FAILED"; + break; + case ZT1_RESULT_ERROR_NETWORK_NOT_FOUND: + fieldName = "ZT1_RESULT_ERROR_NETWORK_NOT_FOUND"; + break; + case ZT1_RESULT_FATAL_ERROR_INTERNAL: + default: + fieldName = "ZT1_RESULT_FATAL_ERROR_INTERNAL"; + break; + } + + jfieldID enumField = env->GetStaticFieldID(resultClass, fieldName.c_str(), "Lcom/zerotierone/sdk/ResultCode;"); + + resultObject = env->GetStaticObjectField(resultClass, enumField); + + return resultObject; +} + +ZT1_Node* findNode(uint64_t nodeId) +{ + NodeMap::iterator found = nodeMap.find(nodeId); + if(found != nodeMap.end()) + { + JniRef *ref = found->second; + return ref->node; + } + return NULL; +} + +jobject createVirtualNetworkStatus(JNIEnv *env, ZT1_VirtualNetworkStatus status) +{ + static jclass statusClass = NULL; + + jobject statusObject = NULL; + + if(statusClass == NULL) + { + statusClass = env->FindClass("com/zerotierone/sdk/VirtualNetworkStatus"); + if(statusClass == NULL) + { + return NULL; // exception thrown + } + } + + std::string fieldName; + switch(status) + { + case ZT1_NETWORK_STATUS_REQUESTING_CONFIGURATION: + fieldName = "NETWORK_STATUS_REQUESTING_CONFIGURATION"; + break; + case ZT1_NETWORK_STATUS_OK: + fieldName = "NETWORK_STATUS_OK"; + break; + case ZT1_NETWORK_STATUS_ACCESS_DENIED: + fieldName = "NETWORK_STATUS_ACCESS_DENIED"; + break; + case ZT1_NETWORK_STATUS_NOT_FOUND: + fieldName = "NETWORK_STATUS_NOT_FOUND"; + break; + case ZT1_NETWORK_STATUS_PORT_ERROR: + fieldName = "NETWORK_STATUS_PORT_ERROR"; + break; + case ZT1_NETWORK_STATUS_CLIENT_TOO_OLD: + fieldName = "NETWORK_STATUS_CLIENT_TOO_OLD"; + break; + } + + jfieldID enumField = env->GetStaticFieldID(statusClass, fieldName.c_str(), "Lcom/zerotierone/sdk/VirtualNetworkStatus;"); + + statusObject = env->GetStaticObjectField(statusClass, enumField); + + return statusObject; +} + +jobject createEvent(JNIEnv *env, ZT1_Event event) +{ + static jclass eventClass = NULL; + jobject eventObject = NULL; + + if(eventClass == NULL) + { + eventClass = env->FindClass("com/zerotierone/sdk/Event"); + if(eventClass == NULL) + { + return NULL; + } + } + + std::string fieldName; + switch(event) + { + case ZT1_EVENT_UP: + fieldName = "EVENT_UP"; + break; + case ZT1_EVENT_OFFLINE: + fieldName = "EVENT_OFFLINE"; + break; + case ZT1_EVENT_DOWN: + fieldName = "EVENT_DOWN"; + break; + case ZT1_EVENT_FATAL_ERROR_IDENTITY_COLLISION: + fieldName = "EVENT_FATAL_ERROR_IDENTITY_COLLISION"; + break; + case ZT1_EVENT_AUTHENTICATION_FAILURE: + fieldName = "EVENT_AUTHENTICATION_FAILURE"; + break; + case ZT1_EVENT_INVALID_PACKET: + fieldName = "EVENT_INVALID_PACKET"; + break; + case ZT1_EVENT_TRACE: + fieldName = "EVENT_TRACE"; + break; + } + + jfieldID enumField = env->GetStaticFieldID(eventClass, fieldName.c_str(), "Lcom/zerotierone/sdk/Event;"); + + eventObject = env->GetStaticObjectField(eventClass, enumField); + + return eventObject; +} + +jobject createVirtualNetworkType(JNIEnv *env, ZT1_VirtualNetworkType type) +{ + static jclass vntypeClass = NULL; + jobject vntypeObject = NULL; + + if(vntypeClass == NULL) + { + vntypeClass = env->FindClass("com/zerotierone/sdk/VirtualNetworkType"); + if(vntypeClass == NULL) + { + return NULL; + } + } + + std::string fieldName; + switch(type) + { + case ZT1_NETWORK_TYPE_PRIVATE: + fieldName = "NETWORK_TYPE_PRIVATE"; + break; + case ZT1_NETWORK_TYPE_PUBLIC: + fieldName = "NETWORK_TYPE_PUBLIC"; + break; + } + + jfieldID enumField = env->GetStaticFieldID(vntypeClass, fieldName.c_str(), "Lcom/zerotierone/sdk/VirtyalNetworkType;"); + vntypeObject = env->GetStaticObjectField(vntypeClass, enumField); + return vntypeObject; +} + jobject newArrayList(JNIEnv *env) { return NULL; diff --git a/java/jni/ZT1_jniutils.h b/java/jni/ZT1_jniutils.h index fbd27831b..b6e3c45bd 100644 --- a/java/jni/ZT1_jniutils.h +++ b/java/jni/ZT1_jniutils.h @@ -7,6 +7,10 @@ extern "C" { #endif +jobject createResultObject(JNIEnv *env, ZT1_ResultCode code); +jobject createVirtualNetworkStatus(JNIEnv *env, ZT1_VirtualNetworkStatus status); +jobject createEvent(JNIEnv *env, ZT1_Event event); + jobject newArrayList(JNIEnv *env); jobject appendItemToArrayList(JNIEnv *env, jobject object); diff --git a/java/jni/com_zerotierone_sdk_Node.cpp b/java/jni/com_zerotierone_sdk_Node.cpp index c5c0c9112..a94589f24 100644 --- a/java/jni/com_zerotierone_sdk_Node.cpp +++ b/java/jni/com_zerotierone_sdk_Node.cpp @@ -26,6 +26,7 @@ */ #include "com_zerotierone_sdk_Node.h" +#include "ZT1_jniutils.h" #include @@ -39,11 +40,6 @@ extern "C" { #endif namespace { - - jobject createResultObject(JNIEnv *env, ZT1_ResultCode code); - jobject createVirtualNetworkStatus(JNIEnv *env, ZT1_VirtualNetworkStatus status); - jobject createEvent(JNIEnv *env, ZT1_Event event); - struct JniRef { JniRef() @@ -126,156 +122,7 @@ namespace { } typedef std::map NodeMap; - static NodeMap nodeMap; - - jobject createResultObject(JNIEnv *env, ZT1_ResultCode code) - { - // cache the class so we don't have to - // look it up every time we need to create a java - // ResultCode object - static jclass resultClass = NULL; - - jobject resultObject = NULL; - - if(resultClass == NULL) - { - resultClass = env->FindClass("com/zerotierone/sdk/ResultCode"); - if(resultClass == NULL) - { - return NULL; // exception thrown - } - } - - std::string fieldName; - switch(code) - { - case ZT1_RESULT_OK: - fieldName = "ZT1_RESULT_OK"; - break; - case ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY: - fieldName = "ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY"; - break; - case ZT1_RESULT_FATAL_ERROR_DATA_STORE_FAILED: - fieldName = "ZT1_RESULT_FATAL_ERROR_DATA_STORE_FAILED"; - break; - case ZT1_RESULT_ERROR_NETWORK_NOT_FOUND: - fieldName = "ZT1_RESULT_ERROR_NETWORK_NOT_FOUND"; - break; - case ZT1_RESULT_FATAL_ERROR_INTERNAL: - default: - fieldName = "ZT1_RESULT_FATAL_ERROR_INTERNAL"; - break; - } - - jfieldID enumField = env->GetStaticFieldID(resultClass, fieldName.c_str(), "Lcom/zerotierone/sdk/ResultCode;"); - - resultObject = env->GetStaticObjectField(resultClass, enumField); - - return resultObject; - } - - ZT1_Node* findNode(uint64_t nodeId) - { - NodeMap::iterator found = nodeMap.find(nodeId); - if(found != nodeMap.end()) - { - JniRef *ref = found->second; - return ref->node; - } - return NULL; - } - - jobject createVirtualNetworkStatus(JNIEnv *env, ZT1_VirtualNetworkStatus status) - { - static jclass statusClass = NULL; - - jobject statusObject = NULL; - - if(statusClass == NULL) - { - statusClass = env->FindClass("com/zerotierone/sdk/VirtualNetworkStatus"); - if(statusClass == NULL) - { - return NULL; // exception thrown - } - } - - std::string fieldName; - switch(status) - { - case ZT1_NETWORK_STATUS_REQUESTING_CONFIGURATION: - fieldName = "NETWORK_STATUS_REQUESTING_CONFIGURATION"; - break; - case ZT1_NETWORK_STATUS_OK: - fieldName = "NETWORK_STATUS_OK"; - break; - case ZT1_NETWORK_STATUS_ACCESS_DENIED: - fieldName = "NETWORK_STATUS_ACCESS_DENIED"; - break; - case ZT1_NETWORK_STATUS_NOT_FOUND: - fieldName = "NETWORK_STATUS_NOT_FOUND"; - break; - case ZT1_NETWORK_STATUS_PORT_ERROR: - fieldName = "NETWORK_STATUS_PORT_ERROR"; - break; - case ZT1_NETWORK_STATUS_CLIENT_TOO_OLD: - fieldName = "NETWORK_STATUS_CLIENT_TOO_OLD"; - break; - } - - jfieldID enumField = env->GetStaticFieldID(statusClass, fieldName.c_str(), "Lcom/zerotierone/sdk/VirtualNetworkStatus;"); - - statusObject = env->GetStaticObjectField(statusClass, enumField); - - return statusObject; - } - - jobject createEvent(JNIEnv *env, ZT1_Event event) - { - static jclass eventClass = NULL; - jobject eventObject = NULL; - - if(eventClass == NULL) - { - eventClass = env->FindClass("com/zerotierone/sdk/Event"); - if(eventClass == NULL) - { - return NULL; - } - } - - std::string fieldName; - switch(event) - { - case ZT1_EVENT_UP: - fieldName = "EVENT_UP"; - break; - case ZT1_EVENT_OFFLINE: - fieldName = "EVENT_OFFLINE"; - break; - case ZT1_EVENT_DOWN: - fieldName = "EVENT_DOWN"; - break; - case ZT1_EVENT_FATAL_ERROR_IDENTITY_COLLISION: - fieldName = "EVENT_FATAL_ERROR_IDENTITY_COLLISION"; - break; - case ZT1_EVENT_AUTHENTICATION_FAILURE: - fieldName = "EVENT_AUTHENTICATION_FAILURE"; - break; - case ZT1_EVENT_INVALID_PACKET: - fieldName = "EVENT_INVALID_PACKET"; - break; - case ZT1_EVENT_TRACE: - fieldName = "EVENT_TRACE"; - break; - } - - jfieldID enumField = env->GetStaticFieldID(eventClass, fieldName.c_str(), "Lcom/zerotierone/sdk/Event;"); - - eventObject = env->GetStaticObjectField(eventClass, enumField); - - return eventObject; - } + static NodeMap nodeMap; } /*