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 <larry.j.sachs@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/17713
Reviewed-by: George Nash <george.nash@intel.com>
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: jihwan seo <jihwan.seo@samsung.com>
Reviewed-by: Rick Bell <richard.s.bell@intel.com>
int qualityOfService) throws OcException;
/**
+ * API for Service and Resource Discovery
+ * <p>
+ * Note: This API is for client side only.
+ * </p>
+ *
+ * @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<OcConnectivityType> 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.
+ * <p>
+ * Note: This API is for client side only.
+ * </p>
+ *
+ * @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<OcConnectivityType> 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.
}
/**
+ * 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
*/
int qualityOfService) throws OcException;
/**
+ * API for Service and Resource Discovery
+ * <p>
+ * Note: This API is for client side only.
+ * </p>
+ *
+ * @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<OcConnectivityType> 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.
+ * <p>
+ * Note: This API is for client side only.
+ * </p>
+ *
+ * @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<OcConnectivityType> 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.
}
/**
+ * 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
*/
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<jobject,
+ std::pair<JniOnResourcesFoundListener*, int >>(
+ jgListener,
+ std::pair<JniOnResourcesFoundListener*, int>(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;
}
/*
+* 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<std::shared_ptr<OC::OCResource>> 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<OCConnectivityType>(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<std::shared_ptr<OC::OCResource>> 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<OCConnectivityType>(jConnectivityType),
+ findCallback,
+ findErrorCallback,
+ JniUtils::getQOS(env, static_cast<int>(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
*/
#include "JniOcStack.h"
#include "JniOnResourceFoundListener.h"
+#include "JniOnResourcesFoundListener.h"
#include "JniOnDeviceInfoListener.h"
#include "JniOnPlatformInfoListener.h"
#include "JniOnDPDevicesFoundListener.h"
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);
void RemoveOnDirectPairingListener(JNIEnv* env, jobject jListener);
std::map<jobject, std::pair<JniOnResourceFoundListener*, int>> onResourceFoundListenerMap;
+std::map<jobject, std::pair<JniOnResourcesFoundListener*, int>> onResourcesFoundListenerMap;
std::map<jobject, std::pair<JniOnDeviceInfoListener*, int>> onDeviceInfoListenerMap;
std::map<jobject, std::pair<JniOnPlatformInfoListener*, int>> onPlatformInfoListenerMap;
std::map<jobject, std::pair<JniOnPresenceListener*, int>> onPresenceListenerMap;
std::map<jobject, std::pair<JniOnDirectPairingListener*, int>> directPairingListenerMap;
std::mutex resourceFoundMapLock;
+std::mutex resourcesFoundMapLock;
std::mutex deviceInfoMapLock;
std::mutex platformInfoMapLock;
std::mutex presenceMapLock;
/*
* 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
*/
--- /dev/null
+/*
+* //******************************************************************
+* //
+* // 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<std::shared_ptr<OC::OCResource>>& 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);
+ }
+}
--- /dev/null
+/*
+* //******************************************************************
+* //
+* // 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 <jni.h>
+#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<std::shared_ptr<OC::OCResource>>& resources);
+ void findResourcesErrorCallback(const std::string& uri, const int eCode);
+
+private:
+ RemoveListenerCallback m_removeListenerCallback;
+ jweak m_jwListener;
+ void checkExAndRemoveListener(JNIEnv* env);
+};
+
+#endif
#include "JniUtils.h"
#include "JniOcRepresentation.h"
+#include "JniOcResource.h"
jobject JniUtils::convertStrVectorToJavaStrList(JNIEnv *env, std::vector<std::string> &vector)
{
return repArr;
}
+
+jobjectArray JniUtils::convertResourceVectorToJavaArray(JNIEnv *env,
+ const std::vector<std::shared_ptr<OC::OCResource>>& resourceVector)
+{
+ jsize len = static_cast<jsize>(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<JniOcResource>(env, jResource, resource);
+ if (!jResource)
+ {
+ return nullptr;
+ }
+
+ env->SetObjectArrayElement(resourceArray, i, jResource);
+ if (env->ExceptionCheck())
+ {
+ return nullptr;
+ }
+ env->DeleteLocalRef(jResource);
+ }
+ return resourceArray;
+}
std::vector<OC::OCRepresentation>& representationVector);
static jobjectArray convertRepresentationVectorToJavaArray(JNIEnv *env,
const std::vector<OC::OCRepresentation>& representationVector);
+ static jobjectArray convertResourceVectorToJavaArray(JNIEnv *env,
+ const std::vector<std::shared_ptr<OC::OCResource>>& resourceVector);
static OC::ServiceType getServiceType(JNIEnv *env, int type)
{
'JniUtils.cpp',
'JniEntityHandler.cpp',
'JniOnResourceFoundListener.cpp',
+ 'JniOnResourcesFoundListener.cpp',
'JniOnDeviceInfoListener.cpp',
'JniOnPlatformInfoListener.cpp',
'JniOnPresenceListener.cpp',