1 /* ****************************************************************
3 * Copyright 2016 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 #include "JniOnMQSubscribeListener.h"
22 #include "JniOcResource.h"
23 #include "JniOcRepresentation.h"
26 JniOnMQSubscribeListener::JniOnMQSubscribeListener(JNIEnv *env,
28 RemoveListenerCallback removeListener)
29 : m_removeListener(removeListener)
31 m_jwListener = env->NewWeakGlobalRef(jListener);
34 JniOnMQSubscribeListener::~JniOnMQSubscribeListener()
39 JNIEnv *env = GetJNIEnv(ret);
45 env->DeleteWeakGlobalRef(m_jwListener);
46 m_jwListener = nullptr;
48 if (JNI_EDETACHED == ret)
50 g_jvm->DetachCurrentThread();
55 void JniOnMQSubscribeListener::onSubscribeCallback(const HeaderOptions headerOptions,
56 const OCRepresentation& ocRepresentation, const int& eCode, const int& sequenceNumber)
58 jint envRet = JNI_ERR;
59 JNIEnv *env = GetJNIEnv(envRet);
65 jobject jListener = env->NewLocalRef(m_jwListener);
68 checkExAndRemoveListener(env);
69 if (JNI_EDETACHED == envRet)
71 g_jvm->DetachCurrentThread();
75 jclass clsL = env->GetObjectClass(jListener);
78 env->DeleteLocalRef(jListener);
79 checkExAndRemoveListener(env);
80 if (JNI_EDETACHED == envRet)
82 g_jvm->DetachCurrentThread();
87 if (OC_STACK_OK != eCode && OC_STACK_RESOURCE_CREATED != eCode &&
88 OC_STACK_RESOURCE_DELETED != eCode && OC_STACK_RESOURCE_CHANGED != eCode)
90 jobject ex = GetOcException(eCode, "stack error in onSubScribeCallback");
95 jmethodID midL = env->GetMethodID(clsL, "onSubScribeFailed", "(Ljava/lang/Throwable;)V");
101 env->CallVoidMethod(jListener, midL, ex);
105 OCRepresentation * rep = new OCRepresentation(ocRepresentation);
106 jlong handle = reinterpret_cast<jlong>(rep);
107 jobject jRepresentation = env->NewObject(g_cls_OcRepresentation,
108 g_mid_OcRepresentation_N_ctor_bool, handle, true);
109 if (!jRepresentation)
115 if (sequenceNumber != 1 + MAX_SEQUENCE_NUMBER)
117 jmethodID midL = env->GetMethodID(clsL, "onSubScribeCompleted",
118 "(Ljava/util/List;Lorg/iotivity/base/OcRepresentation;I)V");
122 env->DeleteLocalRef(jRepresentation);
126 jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env,
128 if (!jHeaderOptionList)
131 env->DeleteLocalRef(jRepresentation);
135 env->CallVoidMethod(jListener, midL, jHeaderOptionList, jRepresentation,
136 static_cast<jint>(sequenceNumber));
137 if (env->ExceptionCheck())
139 LOGE("Java exception is thrown");
141 env->DeleteLocalRef(jRepresentation);
142 env->DeleteLocalRef(jHeaderOptionList);
143 jthrowable ex = env->ExceptionOccurred();
144 env->ExceptionClear();
145 m_ownerResource->removeOnMQTopicSubscribeListener(env, m_jwListener);
146 env->Throw((jthrowable)ex);
151 jmethodID midL = env->GetMethodID(clsL, "onUnsubScribeCompleted",
152 "(Lorg/iotivity/base/OcRepresentation;I)V");
156 env->DeleteLocalRef(jRepresentation);
160 env->CallVoidMethod(jListener, midL, jRepresentation,
161 static_cast<jint>(sequenceNumber));
162 if (env->ExceptionCheck())
164 LOGE("Java exception is thrown");
166 env->DeleteLocalRef(jRepresentation);
167 jthrowable ex = env->ExceptionOccurred();
168 env->ExceptionClear();
169 m_ownerResource->removeOnMQTopicSubscribeListener(env, m_jwListener);
170 env->Throw((jthrowable)ex);
173 checkExAndRemoveListener(env);
177 env->DeleteLocalRef(clsL);
178 env->DeleteLocalRef(jListener);
179 if (JNI_EDETACHED == envRet)
181 g_jvm->DetachCurrentThread();
186 env->DeleteLocalRef(clsL);
187 env->DeleteLocalRef(jListener);
188 checkExAndRemoveListener(env);
189 if (JNI_EDETACHED == envRet)
191 g_jvm->DetachCurrentThread();
195 void JniOnMQSubscribeListener::checkExAndRemoveListener(JNIEnv* env)
197 if (env->ExceptionCheck())
199 jthrowable ex = env->ExceptionOccurred();
200 env->ExceptionClear();
201 m_removeListener(env, m_jwListener);
202 env->Throw((jthrowable)ex);
206 m_removeListener(env, m_jwListener);