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);
44 env->DeleteWeakGlobalRef(m_jwListener);
45 m_jwListener = nullptr;
47 if (JNI_EDETACHED == ret)
49 g_jvm->DetachCurrentThread();
54 void JniOnPresenceListener::onPresenceCallback(OCStackResult result, const unsigned int nonce,
55 const std::string& hostAddress)
57 LOGI("JniOnPresenceListener::onPresenceCallback");
64 JNIEnv *env = GetJNIEnv(ret);
70 if (OC_STACK_OK != result && OC_STACK_PRESENCE_STOPPED != result &&
71 OC_STACK_PRESENCE_TIMEOUT != result && OC_STACK_PRESENCE_DO_NOT_HANDLE != result)
73 ThrowOcException(result, "onPresenceCallback: stack failure");
74 if (JNI_EDETACHED == ret)
76 g_jvm->DetachCurrentThread();
81 std::string enumField = JniUtils::stackResultToStr(result);
82 if (enumField.empty())
84 ThrowOcException(JNI_INVALID_VALUE, "Unexpected OCStackResult value");
85 if (JNI_EDETACHED == ret)
87 g_jvm->DetachCurrentThread();
92 jobject jPresenceStatus = env->CallStaticObjectMethod(g_cls_OcPresenceStatus,
93 g_mid_OcPresenceStatus_get, env->NewStringUTF(enumField.c_str()));
96 checkExAndRemoveListener(env);
97 if (JNI_EDETACHED == ret)
99 g_jvm->DetachCurrentThread();
104 jobject jListener = env->NewLocalRef(m_jwListener);
107 checkExAndRemoveListener(env);
108 if (JNI_EDETACHED == ret)
110 g_jvm->DetachCurrentThread();
115 jclass clsL = env->GetObjectClass(jListener);
118 checkExAndRemoveListener(env);
119 if (JNI_EDETACHED == ret)
121 g_jvm->DetachCurrentThread();
125 jmethodID midL = env->GetMethodID(clsL, "onPresence",
126 "(Lorg/iotivity/base/OcPresenceStatus;ILjava/lang/String;)V");
129 checkExAndRemoveListener(env);
130 if (JNI_EDETACHED == ret)
132 g_jvm->DetachCurrentThread();
137 env->CallVoidMethod(jListener, midL, jPresenceStatus,
138 (jint)nonce, env->NewStringUTF(hostAddress.c_str()));
139 if (env->ExceptionCheck())
141 LOGE("Java exception is thrown");
142 checkExAndRemoveListener(env);
145 if (JNI_EDETACHED == ret)
147 g_jvm->DetachCurrentThread();
151 void JniOnPresenceListener::checkExAndRemoveListener(JNIEnv* env)
153 if (env->ExceptionCheck())
155 jthrowable ex = env->ExceptionOccurred();
156 env->ExceptionClear();
157 m_removeListenerCallback(env, m_jwListener);
158 env->Throw((jthrowable)ex);
162 m_removeListenerCallback(env, m_jwListener);
166 jweak JniOnPresenceListener::getJWListener()
168 return this->m_jwListener;