2 * //******************************************************************
4 * // Copyright 2015 Intel Corporation.
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8 * // Licensed under the Apache License, Version 2.0 (the "License");
9 * // you may not use this file except in compliance with the License.
10 * // You may obtain a copy of the License at
12 * // http://www.apache.org/licenses/LICENSE-2.0
14 * // Unless required by applicable law or agreed to in writing, software
15 * // distributed under the License is distributed on an "AS IS" BASIS,
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * // See the License for the specific language governing permissions and
18 * // limitations under the License.
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 #include "JniOnPresenceListener.h"
25 JniOnPresenceListener::JniOnPresenceListener(JNIEnv *env, jobject jListener,
26 RemoveListenerCallback removeListenerCallback)
28 m_jwListener = env->NewWeakGlobalRef(jListener);
29 m_removeListenerCallback = removeListenerCallback;
32 JniOnPresenceListener::~JniOnPresenceListener()
34 LOGD("~JniOnPresenceListener");
38 JNIEnv *env = GetJNIEnv(ret);
39 if (nullptr == env) return;
41 env->DeleteWeakGlobalRef(m_jwListener);
42 m_jwListener = nullptr;
44 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
48 void JniOnPresenceListener::onPresenceCallback(OCStackResult result, const unsigned int nonce,
49 const std::string& hostAddress)
51 LOGI("JniOnPresenceListener::onPresenceCallback");
52 if (!m_jwListener) return;
55 JNIEnv *env = GetJNIEnv(ret);
56 if (nullptr == env) return;
58 if (OC_STACK_OK != result && OC_STACK_PRESENCE_STOPPED != result &&
59 OC_STACK_PRESENCE_TIMEOUT != result && OC_STACK_PRESENCE_DO_NOT_HANDLE != result)
61 ThrowOcException(result, "onPresenceCallback: stack failure");
62 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
66 std::string enumField = JniUtils::stackResultToStr(result);
67 if (enumField.empty())
69 ThrowOcException(JNI_INVALID_VALUE, "Unexpected OCStackResult value");
70 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
74 jobject jPresenceStatus = env->CallStaticObjectMethod(g_cls_OcPresenceStatus,
75 g_mid_OcPresenceStatus_get, env->NewStringUTF(enumField.c_str()));
78 checkExAndRemoveListener(env);
79 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
83 jobject jListener = env->NewLocalRef(m_jwListener);
86 checkExAndRemoveListener(env);
87 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
91 jclass clsL = env->GetObjectClass(jListener);
94 checkExAndRemoveListener(env);
95 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
98 jmethodID midL = env->GetMethodID(clsL, "onPresence",
99 "(Lorg/iotivity/base/OcPresenceStatus;ILjava/lang/String;)V");
102 checkExAndRemoveListener(env);
103 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
107 env->CallVoidMethod(jListener, midL, jPresenceStatus,
108 (jint)nonce, env->NewStringUTF(hostAddress.c_str()));
109 if (env->ExceptionCheck())
111 LOGE("Java exception is thrown");
112 checkExAndRemoveListener(env);
114 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
117 void JniOnPresenceListener::checkExAndRemoveListener(JNIEnv* env)
119 if (env->ExceptionCheck())
121 jthrowable ex = env->ExceptionOccurred();
122 env->ExceptionClear();
123 m_removeListenerCallback(env, m_jwListener);
124 env->Throw((jthrowable)ex);
128 m_removeListenerCallback(env, m_jwListener);
132 jweak JniOnPresenceListener::getJWListener()
134 return this->m_jwListener;