diff options
author | Grant Limberg <glimberg@gmail.com> | 2015-04-24 20:13:21 -0700 |
---|---|---|
committer | Grant Limberg <glimberg@gmail.com> | 2015-04-24 20:13:21 -0700 |
commit | 9c62ce9e6f1a755c21b3adcb50262499a25d6df4 (patch) | |
tree | a4448b35b38a9ac388571fcd7a51d7b2ba94467e /java | |
parent | f2eed5a65f21c8d5916defcf4b88d2665e38a192 (diff) | |
download | infinitytier-9c62ce9e6f1a755c21b3adcb50262499a25d6df4.tar.gz infinitytier-9c62ce9e6f1a755c21b3adcb50262499a25d6df4.zip |
implemented EventCallback
All callback methods are now implemented
Diffstat (limited to 'java')
-rw-r--r-- | java/jni/ZT1_jniutils.cpp | 78 | ||||
-rw-r--r-- | java/jni/ZT1_jniutils.h | 2 | ||||
-rw-r--r-- | java/jni/com_zerotierone_sdk_Node.cpp | 178 | ||||
-rw-r--r-- | java/src/com/zerotierone/sdk/EventListener.java | 11 | ||||
-rw-r--r-- | java/src/com/zerotierone/sdk/Node.java | 3 |
5 files changed, 201 insertions, 71 deletions
diff --git a/java/jni/ZT1_jniutils.cpp b/java/jni/ZT1_jniutils.cpp index 82f5dba8..8d1a25c4 100644 --- a/java/jni/ZT1_jniutils.cpp +++ b/java/jni/ZT1_jniutils.cpp @@ -879,6 +879,84 @@ jobject newNetworkConfig(JNIEnv *env, const ZT1_VirtualNetworkConfig &vnetConfig return vnetConfigObj; } +jobject newVersion(JNIEnv *env, int major, int minor, int rev, long featureFlags) +{ + // create a com.zerotierone.sdk.Version object + static jclass versionClass = NULL; + static jmethodID versionConstructor = NULL; + + if(versionClass == NULL) + { + versionClass = env->FindClass("com/zerotierone/sdk/Version"); + if(versionClass == NULL) + { + return NULL; + } + } + + if(versionConstructor == NULL) + { + versionConstructor = env->GetMethodID( + versionClass, "<init>", "()V"); + if(versionConstructor == NULL) + { + return NULL; + } + } + + jobject versionObj = env->NewObject(versionClass, versionConstructor); + if(versionObj == NULL) + { + return NULL; + } + + // copy data to Version object + static jfieldID majorField = NULL; + static jfieldID minorField = NULL; + static jfieldID revisionField = NULL; + static jfieldID featureFlagsField = NULL; + + if(majorField == NULL) + { + majorField = env->GetFieldID(versionClass, "major", "I"); + if(majorField = NULL) + { + return NULL; + } + } + + if(minorField == NULL) + { + minorField = env->GetFieldID(versionClass, "minor", "I"); + if(minorField == NULL) + { + return NULL; + } + } + + if(revisionField == NULL) + { + revisionField = env->GetFieldID(versionClass, "revision", "I"); + if(revisionField == NULL) + { + return NULL; + } + } + + if(featureFlagsField == NULL) + { + featureFlagsField = env->GetFieldID(versionClass, "featureFlags", "J"); + if(featureFlagsField == NULL) + { + return NULL; + } + } + + env->SetIntField(versionObj, majorField, (jint)major); + env->SetIntField(versionObj, minorField, (jint)minor); + env->SetIntField(versionObj, revisionField, (jint)rev); + env->SetLongField(versionObj, featureFlagsField, (jlong)featureFlags); +} #ifdef __cplusplus } diff --git a/java/jni/ZT1_jniutils.h b/java/jni/ZT1_jniutils.h index df22b0ca..a379ed05 100644 --- a/java/jni/ZT1_jniutils.h +++ b/java/jni/ZT1_jniutils.h @@ -26,6 +26,8 @@ jobject newPeerPhysicalPath(JNIEnv *env, const ZT1_PeerPhysicalPath &ppp); jobject newNetworkConfig(JNIEnv *env, const ZT1_VirtualNetworkConfig &config); +jobject newVersion(JNIEnv *env, int major, int minor, int rev, long featureFlags); + #ifdef __cplusplus } #endif diff --git a/java/jni/com_zerotierone_sdk_Node.cpp b/java/jni/com_zerotierone_sdk_Node.cpp index 72e9fe10..ec4f4a65 100644 --- a/java/jni/com_zerotierone_sdk_Node.cpp +++ b/java/jni/com_zerotierone_sdk_Node.cpp @@ -48,6 +48,7 @@ namespace { , dataStoreGetListener(NULL) , dataStorePutListener(NULL) , packetSender(NULL) + , eventListener(NULL) , frameListener(NULL) , configListener(NULL) {} @@ -60,6 +61,7 @@ namespace { jobject dataStoreGetListener; jobject dataStorePutListener; jobject packetSender; + jobject eventListener; jobject frameListener; jobject configListener; }; @@ -152,6 +154,99 @@ namespace { assert(ref->node == node); JNIEnv *env = ref->env; + + jclass eventListenerClass = NULL; + jmethodID onEventMethod = NULL; + jmethodID onOutOfDateMethod = NULL; + jmethodID onNetworkErrorMethod = NULL; + jmethodID onTraceMethod = NULL; + + eventListenerClass = env->GetObjectClass(ref->eventListener); + if(eventListenerClass == NULL) + { + return; + } + + onEventMethod = env->GetMethodID(eventListenerClass, + "onEvent", "(Lcom/zerotierone/sdk/Event;)V"); + if(onEventMethod == NULL) + { + return; + } + + onOutOfDateMethod = env->GetMethodID(eventListenerClass, + "onOutOfDate", "(Lcom/zerotierone/sdk/Version;)V"); + if(onOutOfDateMethod == NULL) + { + return; + } + + onNetworkErrorMethod = env->GetMethodID(eventListenerClass, + "onNetworkError", "(Lcom/zerotierone/sdk/Version;Ljava/net/InetAddress;)V"); + if(onNetworkErrorMethod == NULL) + { + return; + } + + onTraceMethod = env->GetMethodID(eventListenerClass, + "onTrace", "(Ljava/lang/String;)V"); + if(onTraceMethod == NULL) + { + return; + } + + jobject eventObject = createEvent(env, event); + if(eventObject == NULL) + { + return; + } + + switch(event) + { + case ZT1_EVENT_UP: + case ZT1_EVENT_OFFLINE: + case ZT1_EVENT_ONLINE: + case ZT1_EVENT_DOWN: + case ZT1_EVENT_FATAL_ERROR_IDENTITY_COLLISION: + { + // call onEvent() + env->CallVoidMethod(eventListenerClass, onEventMethod, eventObject); + } + break; + case ZT1_EVENT_SAW_MORE_RECENT_VERSION: + { + // call onOutOfDate() + if(data != NULL) + { + int *version = (int*)data; + jobject verisonObj = newVersion(env, version[0], version[1], version[2], 0); + env->CallVoidMethod(eventListenerClass, onEventMethod, verisonObj); + } + } + break; + case ZT1_EVENT_AUTHENTICATION_FAILURE: + case ZT1_EVENT_INVALID_PACKET: + { + // call onNetworkError() + if(data != NULL) + { + sockaddr_storage *addr = (sockaddr_storage*)data; + jobject addressObj = newInetAddress(env, *addr); + env->CallVoidMethod(eventListenerClass, onNetworkErrorMethod, addressObj); + } + } + case ZT1_EVENT_TRACE: + { + // call onTrace() + if(data != NULL) + { + const char* message = (const char*)data; + jstring messageStr = env->NewStringUTF(message); + env->CallVoidMethod(eventListenerClass, onTraceMethod); + } + } + break; + } } long DataStoreGetFunction(ZT1_Node *node,void *userData, @@ -412,6 +507,19 @@ JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_node_1init( return NULL; } + fid = env->GetFieldID( + cls, "eventListener", "Lcom/zerotierone/sdk/EventListener;"); + if(fid == NULL) + { + return NULL; + } + + ref->eventListener = env->GetObjectField(obj, fid); + if(ref->eventListener == NULL) + { + return NULL; + } + nodeMap.insert(std::make_pair(ref->id, ref)); return resultObject; @@ -923,26 +1031,6 @@ JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_networkConfig( JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_version( JNIEnv *env, jobject obj) { - // create a com.zerotierone.sdk.Version object - jclass versionClass = env->FindClass("com/zerotierone/sdk/Version"); - if(versionClass == NULL) - { - return NULL; - } - - jmethodID versionConstructor = env->GetMethodID( - versionClass, "<init>", "()V"); - if(versionConstructor == NULL) - { - return NULL; - } - - jobject versionObj = env->NewObject(versionClass, versionConstructor); - if(versionObj == NULL) - { - return NULL; - } - int major = 0; int minor = 0; int revision = 0; @@ -950,55 +1038,7 @@ JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_version( ZT1_version(&major, &minor, &revision, &featureFlags); - // copy data to Version object - static jfieldID majorField = NULL; - static jfieldID minorField = NULL; - static jfieldID revisionField = NULL; - static jfieldID featureFlagsField = NULL; - - if(majorField == NULL) - { - majorField = env->GetFieldID(versionClass, "major", "I"); - if(majorField = NULL) - { - return NULL; - } - } - - if(minorField == NULL) - { - minorField = env->GetFieldID(versionClass, "minor", "I"); - if(minorField == NULL) - { - return NULL; - } - } - - if(revisionField == NULL) - { - revisionField = env->GetFieldID(versionClass, "revision", "I"); - if(revisionField == NULL) - { - return NULL; - } - } - - if(featureFlagsField == NULL) - { - featureFlagsField = env->GetFieldID(versionClass, "featureFlags", "J"); - if(featureFlagsField == NULL) - { - return NULL; - } - } - - env->SetIntField(versionObj, majorField, (jint)major); - env->SetIntField(versionObj, minorField, (jint)minor); - env->SetIntField(versionObj, revisionField, (jint)revision); - env->SetLongField(versionObj, featureFlagsField, (jlong)featureFlags); - - - return versionObj; + return newVersion(env, major, minor, revision, featureFlags); } /* diff --git a/java/src/com/zerotierone/sdk/EventListener.java b/java/src/com/zerotierone/sdk/EventListener.java index 9e3e4d24..63030dfd 100644 --- a/java/src/com/zerotierone/sdk/EventListener.java +++ b/java/src/com/zerotierone/sdk/EventListener.java @@ -27,8 +27,15 @@ package com.zerotierone.sdk; -import java.nio.ByteBuffer; +import java.net.InetAddress; +import java.lang.String; public interface EventListener { - public void onEvent(Node node,Event event, ByteBuffer buffer); + public void onEvent(Event event); + + public void onNetworkError(Event event, InetAddress source); + + public void onOutOfDate(Version newVersion); + + public void onTrace(String message); } diff --git a/java/src/com/zerotierone/sdk/Node.java b/java/src/com/zerotierone/sdk/Node.java index 34a67cb7..649f84a8 100644 --- a/java/src/com/zerotierone/sdk/Node.java +++ b/java/src/com/zerotierone/sdk/Node.java @@ -50,6 +50,7 @@ public class Node { private final DataStoreGetListener getListener; private final DataStorePutListener putListener; private final PacketSender sender; + private final EventListener eventListener; private final VirtualNetworkFrameListener frameListener; private final VirtualNetworkConfigListener configListener; @@ -57,6 +58,7 @@ public class Node { DataStoreGetListener getListener, DataStorePutListener putListener, PacketSender sender, + EventListener eventListener, VirtualNetworkFrameListener frameListener, VirtualNetworkConfigListener configListener) { @@ -65,6 +67,7 @@ public class Node { this.getListener = getListener; this.putListener = putListener; this.sender = sender; + this.eventListener = eventListener; this.frameListener = frameListener; this.configListener = configListener; |