2 * //******************************************************************
\r
4 * // Copyright 2015 Intel Corporation.
\r
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
8 * // Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * // you may not use this file except in compliance with the License.
\r
10 * // You may obtain a copy of the License at
\r
12 * // http://www.apache.org/licenses/LICENSE-2.0
\r
14 * // Unless required by applicable law or agreed to in writing, software
\r
15 * // distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * // See the License for the specific language governing permissions and
\r
18 * // limitations under the License.
\r
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
22 #include "JniOnPresenceListener.h"
\r
23 #include "JniUtils.h"
\r
25 JniOnPresenceListener::JniOnPresenceListener(JNIEnv *env, jobject jListener,
\r
26 RemoveListenerCallback removeListenerCallback)
\r
28 m_jwListener = env->NewWeakGlobalRef(jListener);
\r
29 m_removeListenerCallback = removeListenerCallback;
\r
32 JniOnPresenceListener::~JniOnPresenceListener()
\r
34 LOGD("~JniOnPresenceListener");
\r
38 JNIEnv *env = GetJNIEnv(ret);
\r
39 if (NULL == env) return;
\r
40 env->DeleteWeakGlobalRef(m_jwListener);
\r
41 m_jwListener = nullptr;
\r
42 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
\r
46 void JniOnPresenceListener::onPresenceCallback(OCStackResult result, const unsigned int nonce,
\r
47 const std::string& hostAddress)
\r
49 LOGI("JniOnPresenceListener::onPresenceCallback");
\r
50 if (!m_jwListener) return;
\r
53 JNIEnv *env = GetJNIEnv(ret);
\r
54 if (NULL == env) return;
\r
56 if (OC_STACK_OK != result && OC_STACK_PRESENCE_STOPPED != result &&
\r
57 OC_STACK_PRESENCE_TIMEOUT != result && OC_STACK_PRESENCE_DO_NOT_HANDLE != result)
\r
59 ThrowOcException(result, "onPresenceCallback: stack failure");
\r
60 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
\r
64 std::string enumField = JniUtils::stackResultToStr(result);
\r
65 if (enumField.empty())
\r
67 ThrowOcException(JNI_INVALID_VALUE, "Unexpected OCStackResult value");
\r
68 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
\r
72 jobject jPresenceStatus = env->CallStaticObjectMethod(g_cls_OcPresenceStatus,
\r
73 g_mid_OcPresenceStatus_get, env->NewStringUTF(enumField.c_str()));
\r
74 if (!jPresenceStatus)
\r
76 checkExAndRemoveListener(env);
\r
77 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
\r
81 jobject jListener = env->NewLocalRef(m_jwListener);
\r
84 checkExAndRemoveListener(env);
\r
85 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
\r
89 jclass clsL = env->GetObjectClass(jListener);
\r
92 checkExAndRemoveListener(env);
\r
93 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
\r
96 jmethodID midL = env->GetMethodID(clsL, "onPresence",
\r
97 "(Lorg/iotivity/base/OcPresenceStatus;ILjava/lang/String;)V");
\r
100 checkExAndRemoveListener(env);
\r
101 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
\r
105 env->CallVoidMethod(jListener, midL, jPresenceStatus,
\r
106 (jint)nonce, env->NewStringUTF(hostAddress.c_str()));
\r
107 if (env->ExceptionCheck())
\r
109 LOGE("Java exception is thrown");
\r
110 checkExAndRemoveListener(env);
\r
112 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
\r
115 void JniOnPresenceListener::checkExAndRemoveListener(JNIEnv* env)
\r
117 if (env->ExceptionCheck())
\r
119 jthrowable ex = env->ExceptionOccurred();
\r
120 env->ExceptionClear();
\r
121 m_removeListenerCallback(env, m_jwListener);
\r
122 env->Throw((jthrowable)ex);
\r
126 m_removeListenerCallback(env, m_jwListener);
\r
130 jweak JniOnPresenceListener::getJWListener()
\r
132 return this->m_jwListener;
\r