From: Larry Sachs Date: Mon, 6 Mar 2017 23:02:40 +0000 (-0800) Subject: [IOT-1829] findResourceList is not implemented in Java API X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=dedfc9ebe08c85f3263dc15aaf3bac89decb3db4;p=contrib%2Fiotivity.git [IOT-1829] findResourceList is not implemented in Java API Added jni calls to support new c sdk api to return a list of resources. OcPlatform.java now has findResources() methods that take a OnResourcesFoundListener as a callback which has a method for the returned array of found resources. Change-Id: Ia874294d0a73b35f7bd23b15d9c122df2622cfdc Signed-off-by: Larry Sachs Reviewed-on: https://gerrit.iotivity.org/gerrit/17713 Reviewed-by: George Nash Tested-by: jenkins-iotivity Reviewed-by: jihwan seo Reviewed-by: Rick Bell --- diff --git a/java/iotivity-android/src/main/java/org/iotivity/base/OcPlatform.java b/java/iotivity-android/src/main/java/org/iotivity/base/OcPlatform.java index 34b0026..fd11235 100644 --- a/java/iotivity-android/src/main/java/org/iotivity/base/OcPlatform.java +++ b/java/iotivity-android/src/main/java/org/iotivity/base/OcPlatform.java @@ -357,6 +357,93 @@ public final class OcPlatform { int qualityOfService) throws OcException; /** + * API for Service and Resource Discovery + *

+ * Note: This API is for client side only. + *

+ * + * @param host Host Address of a service to direct resource discovery query. + * If empty, performs multicast resource discovery query + * @param resourceUri name of the resource. If null or empty, performs search for all + * resource names + * @param connectivityTypeSet Set of types of connectivity. Example: IP + * @param onResourcesFoundListener Handles events, success states and failure states. + * @throws OcException if failure + */ + public static void findResources( + String host, + String resourceUri, + EnumSet connectivityTypeSet, + OnResourcesFoundListener onResourcesFoundListener) throws OcException { + OcPlatform.initCheck(); + + int connTypeInt = 0; + + for (OcConnectivityType connType : OcConnectivityType.values()) { + if (connectivityTypeSet.contains(connType)) + connTypeInt |= connType.getValue(); + } + + OcPlatform.findResources0( + host, + resourceUri, + connTypeInt, + onResourcesFoundListener + ); + } + + private static native void findResources0( + String host, + String resourceUri, + int connectivityType, + OnResourcesFoundListener onResourcesFoundListener) throws OcException; + + /** + * API for Service and Resource Discovery. + *

+ * Note: This API is for client side only. + *

+ * + * @param host Host IP Address of a service to direct resource discovery query. + * If empty, performs multicast resource discovery query + * @param resourceUri name of the resource. If null or empty, performs search for all + * resource names + * @param connectivityTypeSet Set of types of connectivity. Example: IP + * @param onResourcesFoundListener Handles events, success states and failure states. + * @param qualityOfService the quality of communication + * @throws OcException if failure + */ + public static void findResources( + String host, + String resourceUri, + EnumSet connectivityTypeSet, + OnResourcesFoundListener onResourcesFoundListener, + QualityOfService qualityOfService) throws OcException { + OcPlatform.initCheck(); + + int connTypeInt = 0; + + for (OcConnectivityType connType : OcConnectivityType.values()) { + if (connectivityTypeSet.contains(connType)) + connTypeInt |= connType.getValue(); + } + + OcPlatform.findResources1(host, + resourceUri, + connTypeInt, + onResourcesFoundListener, + qualityOfService.getValue() + ); + } + + private static native void findResources1( + String host, + String resourceUri, + int connectivityType, + OnResourcesFoundListener onResourcesFoundListener, + int qualityOfService) throws OcException; + + /** * API for Device Discovery * * @param host Host IP Address. If null or empty, Multicast is performed. @@ -1134,6 +1221,15 @@ public final class OcPlatform { } /** + * An OnResourcesFoundListener can be registered via the OcPlatform.findResources call. + * Event listeners are notified asynchronously + */ + public interface OnResourcesFoundListener { + public void onResourcesFound(OcResource[] resources); + public void onFindResourcesFailed(Throwable ex, String uri); + } + + /** * An OnDeviceFoundListener can be registered via the OcPlatform.getDeviceInfo call. * Event listeners are notified asynchronously */ diff --git a/java/iotivity-linux/src/main/java/org/iotivity/base/OcPlatform.java b/java/iotivity-linux/src/main/java/org/iotivity/base/OcPlatform.java index 58807e9..215c946 100644 --- a/java/iotivity-linux/src/main/java/org/iotivity/base/OcPlatform.java +++ b/java/iotivity-linux/src/main/java/org/iotivity/base/OcPlatform.java @@ -351,6 +351,93 @@ public final class OcPlatform { int qualityOfService) throws OcException; /** + * API for Service and Resource Discovery + *

+ * Note: This API is for client side only. + *

+ * + * @param host Host Address of a service to direct resource discovery query. + * If empty, performs multicast resource discovery query + * @param resourceUri name of the resource. If null or empty, performs search for all + * resource names + * @param connectivityTypeSet Set of types of connectivity. Example: IP + * @param onResourcesFoundListener Handles events, success states and failure states. + * @throws OcException if failure + */ + public static void findResources( + String host, + String resourceUri, + EnumSet connectivityTypeSet, + OnResourcesFoundListener onResourcesFoundListener) throws OcException { + OcPlatform.initCheck(); + + int connTypeInt = 0; + + for (OcConnectivityType connType : OcConnectivityType.values()) { + if (connectivityTypeSet.contains(connType)) + connTypeInt |= connType.getValue(); + } + + OcPlatform.findResources0( + host, + resourceUri, + connTypeInt, + onResourcesFoundListener + ); + } + + private static native void findResources0( + String host, + String resourceUri, + int connectivityType, + OnResourcesFoundListener onResourcesFoundListener) throws OcException; + + /** + * API for Service and Resource Discovery. + *

+ * Note: This API is for client side only. + *

+ * + * @param host Host IP Address of a service to direct resource discovery query. + * If empty, performs multicast resource discovery query + * @param resourceUri name of the resource. If null or empty, performs search for all + * resource names + * @param connectivityTypeSet Set of types of connectivity. Example: IP + * @param onResourcesFoundListener Handles events, success states and failure states. + * @param qualityOfService the quality of communication + * @throws OcException if failure + */ + public static void findResources( + String host, + String resourceUri, + EnumSet connectivityTypeSet, + OnResourcesFoundListener onResourcesFoundListener, + QualityOfService qualityOfService) throws OcException { + OcPlatform.initCheck(); + + int connTypeInt = 0; + + for (OcConnectivityType connType : OcConnectivityType.values()) { + if (connectivityTypeSet.contains(connType)) + connTypeInt |= connType.getValue(); + } + + OcPlatform.findResources1(host, + resourceUri, + connTypeInt, + onResourcesFoundListener, + qualityOfService.getValue() + ); + } + + private static native void findResources1( + String host, + String resourceUri, + int connectivityType, + OnResourcesFoundListener onResourcesFoundListener, + int qualityOfService) throws OcException; + + /** * API for Device Discovery * * @param host Host IP Address. If null or empty, Multicast is performed. @@ -1125,6 +1212,15 @@ public final class OcPlatform { } /** + * An OnResourcesFoundListener can be registered via the OcPlatform.findResources call. + * Event listeners are notified asynchronously + */ + public interface OnResourcesFoundListener { + public void onResourcesFound(OcResource[] resources); + public void onFindResourcesFailed(Throwable ex, String uri); + } + + /** * An OnDeviceFoundListener can be registered via the OcPlatform.getDeviceInfo call. * Event listeners are notified asynchronously */ diff --git a/java/jni/JniOcPlatform.cpp b/java/jni/JniOcPlatform.cpp index 5fd6685..291b471 100644 --- a/java/jni/JniOcPlatform.cpp +++ b/java/jni/JniOcPlatform.cpp @@ -108,6 +108,78 @@ void RemoveOnResourceFoundListener(JNIEnv* env, jobject jListener) resourceFoundMapLock.unlock(); } +JniOnResourcesFoundListener* AddOnResourcesFoundListener(JNIEnv* env, jobject jListener) +{ + JniOnResourcesFoundListener *onResourcesFoundListener = nullptr; + + resourcesFoundMapLock.lock(); + + for (auto it = onResourcesFoundListenerMap.begin(); + it != onResourcesFoundListenerMap.end(); + ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + onResourcesFoundListener = refPair.first; + refPair.second++; + it->second = refPair; + onResourcesFoundListenerMap.insert(*it); + LOGD("OnResourcesFoundListener: ref. count incremented"); + break; + } + } + + if (!onResourcesFoundListener) + { + onResourcesFoundListener = + new JniOnResourcesFoundListener(env, jListener, RemoveOnResourcesFoundListener); + jobject jgListener = env->NewGlobalRef(jListener); + + onResourcesFoundListenerMap.insert( + std::pair>( + jgListener, + std::pair(onResourcesFoundListener, + 1))); + LOGD("OnResourcesFoundListener: new listener"); + } + resourcesFoundMapLock.unlock(); + return onResourcesFoundListener; +} + +void RemoveOnResourcesFoundListener(JNIEnv* env, jobject jListener) +{ + resourcesFoundMapLock.lock(); + + for (auto it = onResourcesFoundListenerMap.begin(); + it != onResourcesFoundListenerMap.end(); + ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + if (refPair.second > 1) + { + refPair.second--; + it->second = refPair; + onResourcesFoundListenerMap.insert(*it); + LOGI("OnResourcesFoundListener: ref. count decremented"); + } + else + { + env->DeleteGlobalRef(it->first); + JniOnResourcesFoundListener* listener = refPair.first; + delete listener; + onResourcesFoundListenerMap.erase(it); + LOGI("OnResourcesFoundListener removed"); + } + break; + } + } + resourcesFoundMapLock.unlock(); +} + JniOnDeviceInfoListener* AddOnDeviceInfoListener(JNIEnv* env, jobject jListener) { JniOnDeviceInfoListener *onDeviceInfoListener = nullptr; @@ -985,6 +1057,175 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResource1( } /* +* Class: org_iotivity_base_OcPlatform +* Method: findResources0 +* Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnResourcesFoundListener;)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResources0( + JNIEnv *env, + jclass clazz, + jstring jHost, + jstring jResourceUri, + jint jConnectivityType, + jobject jListener) +{ + LOGD("OcPlatform_findResources0"); + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "onResourcesFoundListener cannot be null"); + return; + } + const char *host = nullptr; + if (jHost) + { + host = env->GetStringUTFChars(jHost, nullptr); + } + const char *resourceUri = nullptr; + if (jResourceUri) + { + resourceUri = env->GetStringUTFChars(jResourceUri, nullptr); + } + + JniOnResourcesFoundListener *onResourcesFoundListener = AddOnResourcesFoundListener(env, jListener); + + FindResListCallback findCallback = [onResourcesFoundListener](const std::vector> resources) + { + onResourcesFoundListener->foundResourcesCallback(resources); + }; + + FindErrorCallback findErrorCallback = [onResourcesFoundListener](const std::string& uri, const int eCode) + { + onResourcesFoundListener->findResourcesErrorCallback(uri, eCode); + }; + + try + { + OCStackResult result = OCPlatform::findResourceList( + host, + resourceUri, + static_cast(jConnectivityType), + findCallback, + findErrorCallback, + OC::QualityOfService::NaQos); + + if (jHost) + { + env->ReleaseStringUTFChars(jHost, host); + } + if (jResourceUri) + { + env->ReleaseStringUTFChars(jResourceUri, resourceUri); + } + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Find resources has failed"); + return; + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + + if (jHost) + { + env->ReleaseStringUTFChars(jHost, host); + } + if (jResourceUri) + { + env->ReleaseStringUTFChars(jResourceUri, resourceUri); + } + + ThrowOcException(e.code(), e.reason().c_str()); + } +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: findResources1 +* Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnResourcesFoundListener;I)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResources1( + JNIEnv *env, + jclass clazz, + jstring jHost, + jstring jResourceUri, + jint jConnectivityType, + jobject jListener, + jint jQoS) +{ + LOGD("OcPlatform_findResources1"); + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "onResourcesFoundListener cannot be null"); + return; + } + const char *host = nullptr; + if (jHost) + { + host = env->GetStringUTFChars(jHost, nullptr); + } + const char *resourceUri = nullptr; + if (jResourceUri) + { + resourceUri = env->GetStringUTFChars(jResourceUri, nullptr); + } + + JniOnResourcesFoundListener *onResourcesFoundListener = AddOnResourcesFoundListener(env, jListener); + + FindResListCallback findCallback = [onResourcesFoundListener](std::vector> resources) + { + onResourcesFoundListener->foundResourcesCallback(resources); + }; + + FindErrorCallback findErrorCallback = [onResourcesFoundListener](const std::string& uri, const int eCode) + { + onResourcesFoundListener->findResourcesErrorCallback(uri, eCode); + }; + + try + { + OCStackResult result = OCPlatform::findResourceList( + host, + resourceUri, + static_cast(jConnectivityType), + findCallback, + findErrorCallback, + JniUtils::getQOS(env, static_cast(jQoS))); + + if (jHost) + { + env->ReleaseStringUTFChars(jHost, host); + } + if (jResourceUri) + { + env->ReleaseStringUTFChars(jResourceUri, resourceUri); + } + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Find resources has failed"); + return; + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + + if (jHost) + { + env->ReleaseStringUTFChars(jHost, host); + } + if (jResourceUri) + { + env->ReleaseStringUTFChars(jResourceUri, resourceUri); + } + + ThrowOcException(e.code(), e.reason().c_str()); + } +} + +/* * Class: org_iotivity_base_OcPlatform * Method: findDirectPairingDevices * Signature: (ILorg/iotivity/base/OcPlatform/FindDirectPairingListener;)V diff --git a/java/jni/JniOcPlatform.h b/java/jni/JniOcPlatform.h index 71cf66c..0b8106a 100644 --- a/java/jni/JniOcPlatform.h +++ b/java/jni/JniOcPlatform.h @@ -21,6 +21,7 @@ */ #include "JniOcStack.h" #include "JniOnResourceFoundListener.h" +#include "JniOnResourcesFoundListener.h" #include "JniOnDeviceInfoListener.h" #include "JniOnPlatformInfoListener.h" #include "JniOnDPDevicesFoundListener.h" @@ -38,6 +39,9 @@ using namespace OC; JniOnResourceFoundListener* AddOnResourceFoundListener(JNIEnv* env, jobject jListener); void RemoveOnResourceFoundListener(JNIEnv* env, jobject jListener); +JniOnResourcesFoundListener* AddOnResourcesFoundListener(JNIEnv* env, jobject jListener); +void RemoveOnResourcesFoundListener(JNIEnv* env, jobject jListener); + JniOnDeviceInfoListener* AddOnDeviceInfoListener(JNIEnv* env, jobject jListener); void RemoveOnDeviceInfoListener(JNIEnv* env, jobject jListener); @@ -57,6 +61,7 @@ JniOnDirectPairingListener* AddOnDirectPairingListener(JNIEnv* env, jobject jLis void RemoveOnDirectPairingListener(JNIEnv* env, jobject jListener); std::map> onResourceFoundListenerMap; +std::map> onResourcesFoundListenerMap; std::map> onDeviceInfoListenerMap; std::map> onPlatformInfoListenerMap; std::map> onPresenceListenerMap; @@ -65,6 +70,7 @@ std::map> onDPDevicesFound std::map> directPairingListenerMap; std::mutex resourceFoundMapLock; +std::mutex resourcesFoundMapLock; std::mutex deviceInfoMapLock; std::mutex platformInfoMapLock; std::mutex presenceMapLock; @@ -149,6 +155,22 @@ extern "C" { /* * Class: org_iotivity_base_OcPlatform + * Method: findResources0 + * Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnResourcesFoundListener;)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResources0 + (JNIEnv *, jclass, jstring, jstring, jint, jobject); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: findResources1 + * Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnResourcesFoundListener;I)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResources1 + (JNIEnv *, jclass, jstring, jstring, jint, jobject, jint); + + /* + * Class: org_iotivity_base_OcPlatform * Method: getDeviceInfo0 * Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnDeviceFoundListener;)V */ diff --git a/java/jni/JniOnResourcesFoundListener.cpp b/java/jni/JniOnResourcesFoundListener.cpp new file mode 100644 index 0000000..d3517b3 --- /dev/null +++ b/java/jni/JniOnResourcesFoundListener.cpp @@ -0,0 +1,221 @@ +/* +* //****************************************************************** +* // +* // Copyright 2017 Intel Corporation. +* // +* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +* // +* // Licensed under the Apache License, Version 2.0 (the "License"); +* // you may not use this file except in compliance with the License. +* // You may obtain a copy of the License at +* // +* // http://www.apache.org/licenses/LICENSE-2.0 +* // +* // Unless required by applicable law or agreed to in writing, software +* // distributed under the License is distributed on an "AS IS" BASIS, +* // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* // See the License for the specific language governing permissions and +* // limitations under the License. +* // +* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +*/ +#include "JniOnResourcesFoundListener.h" +#include "JniOcResource.h" +#include "JniUtils.h" + +JniOnResourcesFoundListener::JniOnResourcesFoundListener(JNIEnv *env, jobject jListener, + RemoveListenerCallback removeListenerCallback) +{ + m_jwListener = env->NewWeakGlobalRef(jListener); + m_removeListenerCallback = removeListenerCallback; +} + +JniOnResourcesFoundListener::~JniOnResourcesFoundListener() +{ + LOGI("~JniOnResourcesFoundListener()"); + if (m_jwListener) + { + jint ret = JNI_ERR; + JNIEnv *env = GetJNIEnv(ret); + if (nullptr == env) + { + return; + } + env->DeleteWeakGlobalRef(m_jwListener); + m_jwListener = nullptr; + if (JNI_EDETACHED == ret) + { + g_jvm->DetachCurrentThread(); + } + } +} + +void JniOnResourcesFoundListener::foundResourcesCallback(const std::vector>& resources) +{ + jint ret = JNI_ERR; + JNIEnv *env = GetJNIEnv(ret); + if (nullptr == env) + { + return; + } + + jobject jListener = env->NewLocalRef(m_jwListener); + if (!jListener) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) + { + g_jvm->DetachCurrentThread(); + } + return; + } + + jobjectArray jResourceArray = JniUtils::convertResourceVectorToJavaArray(env, resources); + if (!jResourceArray) + { + env->DeleteLocalRef(jListener); + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) + { + g_jvm->DetachCurrentThread(); + } + return; + } + + if (env->ExceptionCheck()) + { + env->DeleteLocalRef(jResourceArray); + env->DeleteLocalRef(jListener); + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) + { + g_jvm->DetachCurrentThread(); + } + return; + } + jclass clsL = env->GetObjectClass(jListener); + if (!clsL) + { + env->DeleteLocalRef(jResourceArray); + env->DeleteLocalRef(jListener); + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) + { + g_jvm->DetachCurrentThread(); + } + return; + } + jmethodID midL = env->GetMethodID(clsL, "onResourcesFound", "([Lorg/iotivity/base/OcResource;)V"); + if (!midL) + { + env->DeleteLocalRef(clsL); + env->DeleteLocalRef(jResourceArray); + env->DeleteLocalRef(jListener); + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) + { + g_jvm->DetachCurrentThread(); + } + return; + } + env->CallVoidMethod(jListener, midL, jResourceArray); + if (env->ExceptionCheck()) + { + LOGE("Java exception is thrown"); + env->DeleteLocalRef(clsL); + env->DeleteLocalRef(jResourceArray); + env->DeleteLocalRef(jListener); + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) + { + g_jvm->DetachCurrentThread(); + } + return; + } + + env->DeleteLocalRef(clsL); + env->DeleteLocalRef(jResourceArray); + env->DeleteLocalRef(jListener); + if (JNI_EDETACHED == ret) + { + g_jvm->DetachCurrentThread(); + } +} + +void JniOnResourcesFoundListener::findResourcesErrorCallback(const std::string& uri, const int eCode) +{ + LOGI("findResources error callback value : %d", eCode); + jint ret = JNI_ERR; + JNIEnv *env = GetJNIEnv(ret); + if (nullptr == env) + { + return; + } + + jobject jListener = env->NewLocalRef(m_jwListener); + if (!jListener) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) + { + g_jvm->DetachCurrentThread(); + } + return; + } + + jclass clsL = env->GetObjectClass(jListener); + if (!clsL) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) + { + g_jvm->DetachCurrentThread(); + } + return; + } + + jobject ex = GetOcException(eCode, "stack error in onFindResourcesErrorCallback"); + if (!ex) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) + { + g_jvm->DetachCurrentThread(); + } + return; + } + + + jmethodID midL = env->GetMethodID(clsL, "onFindResourcesFailed", + "(Ljava/lang/Throwable;Ljava/lang/String;)V"); + if (!midL) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) + { + g_jvm->DetachCurrentThread(); + } + return; + } + env->CallVoidMethod(jListener, midL, ex, env->NewStringUTF(uri.c_str())); + + if (JNI_EDETACHED == ret) + { + g_jvm->DetachCurrentThread(); + } +} + +void JniOnResourcesFoundListener::checkExAndRemoveListener(JNIEnv* env) +{ + if (env->ExceptionCheck()) + { + jthrowable ex = env->ExceptionOccurred(); + env->ExceptionClear(); + m_removeListenerCallback(env, m_jwListener); + env->Throw((jthrowable)ex); + } + else + { + m_removeListenerCallback(env, m_jwListener); + } +} diff --git a/java/jni/JniOnResourcesFoundListener.h b/java/jni/JniOnResourcesFoundListener.h new file mode 100644 index 0000000..a88b90d --- /dev/null +++ b/java/jni/JniOnResourcesFoundListener.h @@ -0,0 +1,44 @@ +/* +* //****************************************************************** +* // +* // Copyright 2017 Intel Corporation. +* // +* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +* // +* // Licensed under the Apache License, Version 2.0 (the "License"); +* // you may not use this file except in compliance with the License. +* // You may obtain a copy of the License at +* // +* // http://www.apache.org/licenses/LICENSE-2.0 +* // +* // Unless required by applicable law or agreed to in writing, software +* // distributed under the License is distributed on an "AS IS" BASIS, +* // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* // See the License for the specific language governing permissions and +* // limitations under the License. +* // +* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +*/ +#include +#include "JniOcStack.h" + +#ifndef _Included_org_iotivity_base_OcPlatform_OnResourcesFoundListener +#define _Included_org_iotivity_base_OcPlatform_OnResourcesFoundListener + +class JniOnResourcesFoundListener +{ +public: + JniOnResourcesFoundListener(JNIEnv *env, jobject jListener, + RemoveListenerCallback removeListenerCallback); + ~JniOnResourcesFoundListener(); + + void foundResourcesCallback(const std::vector>& resources); + void findResourcesErrorCallback(const std::string& uri, const int eCode); + +private: + RemoveListenerCallback m_removeListenerCallback; + jweak m_jwListener; + void checkExAndRemoveListener(JNIEnv* env); +}; + +#endif diff --git a/java/jni/JniUtils.cpp b/java/jni/JniUtils.cpp index 3ee3ada..a99a475 100644 --- a/java/jni/JniUtils.cpp +++ b/java/jni/JniUtils.cpp @@ -22,6 +22,7 @@ #include "JniUtils.h" #include "JniOcRepresentation.h" +#include "JniOcResource.h" jobject JniUtils::convertStrVectorToJavaStrList(JNIEnv *env, std::vector &vector) { @@ -264,3 +265,35 @@ jobjectArray JniUtils::convertRepresentationVectorToJavaArray(JNIEnv *env, return repArr; } + +jobjectArray JniUtils::convertResourceVectorToJavaArray(JNIEnv *env, + const std::vector>& resourceVector) +{ + jsize len = static_cast(resourceVector.size()); + jobjectArray resourceArray = env->NewObjectArray(len, g_cls_OcResource, NULL); + + if (!resourceArray) + { + return nullptr; + } + + for (jsize i = 0; i < len; ++i) + { + JniOcResource *resource = new JniOcResource(resourceVector[i]); + jobject jResource = env->NewObject(g_cls_OcResource, g_mid_OcResource_ctor); + + SetHandle(env, jResource, resource); + if (!jResource) + { + return nullptr; + } + + env->SetObjectArrayElement(resourceArray, i, jResource); + if (env->ExceptionCheck()) + { + return nullptr; + } + env->DeleteLocalRef(jResource); + } + return resourceArray; +} diff --git a/java/jni/JniUtils.h b/java/jni/JniUtils.h index 18a4fdb..5237866 100644 --- a/java/jni/JniUtils.h +++ b/java/jni/JniUtils.h @@ -44,6 +44,8 @@ public: std::vector& representationVector); static jobjectArray convertRepresentationVectorToJavaArray(JNIEnv *env, const std::vector& representationVector); + static jobjectArray convertResourceVectorToJavaArray(JNIEnv *env, + const std::vector>& resourceVector); static OC::ServiceType getServiceType(JNIEnv *env, int type) { diff --git a/java/jni/SConscript b/java/jni/SConscript index 1e5e5be..ac245ec 100644 --- a/java/jni/SConscript +++ b/java/jni/SConscript @@ -112,6 +112,7 @@ ocstack_files = [ 'JniUtils.cpp', 'JniEntityHandler.cpp', 'JniOnResourceFoundListener.cpp', + 'JniOnResourcesFoundListener.cpp', 'JniOnDeviceInfoListener.cpp', 'JniOnPlatformInfoListener.cpp', 'JniOnPresenceListener.cpp',