void JniCacheUpdateListener::cacheUpdatedCallback(OIC::Service::RCSResourceAttributes attributes) { LOGI("JniDiscoverResourceListener::cacheUpdatedCallback enter"); jint ret; JNIEnv *env = GetREJNIEnv(ret); if (NULL == 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; } RCSResourceAttributes *attributesObj = new RCSResourceAttributes(attributes); jlong handle = reinterpret_cast<jlong>(attributesObj); jobject jAttributes = env->NewObject(g_cls_RCSRemoteResourceAttributesObject, g_mid_RCSRemoteResourceAttributesObject_ctor, handle, true); if (!jAttributes) { delete attributesObj; checkExAndRemoveListener(env); if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); return; } jmethodID midL = env->GetMethodID(clsL, "onCacheUpdatedCallback", "(Lorg/iotivity/ResourceEncapsulation/common/RCSResourceAttributes;)V"); if (!midL) { delete attributesObj; checkExAndRemoveListener(env); if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); return; } env->CallVoidMethod(jListener, midL, jAttributes); if (env->ExceptionCheck()) { LOGE("Java exception is thrown"); delete attributesObj; } if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); LOGI("JniDiscoverResourceListener::cacheUpdatedCallback exit"); }
void JniOnDeviceInfoListener::foundDeviceCallback(const OC::OCRepresentation& ocRepresentation) { jint ret; JNIEnv *env = GetJNIEnv(ret); if (nullptr == env) return; jobject jListener = env->NewLocalRef(m_jwListener); if (!jListener) { LOGI("Java onDeviceInfoListener object is already destroyed, quiting"); checkExAndRemoveListener(env); if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); return; } OCRepresentation* rep = new OCRepresentation(ocRepresentation); jlong handle = reinterpret_cast<jlong>(rep); jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool, handle, true); if (!jRepresentation) { delete rep; checkExAndRemoveListener(env); if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); return; } jclass clsL = env->GetObjectClass(jListener); if (!clsL) { delete rep; checkExAndRemoveListener(env); if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); return; } jmethodID midL = env->GetMethodID(clsL, "onDeviceFound", "(Lorg/iotivity/base/OcRepresentation;)V"); if (!midL) { delete rep; checkExAndRemoveListener(env); if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); return; } env->CallVoidMethod(jListener, midL, jRepresentation); if (env->ExceptionCheck()) { LOGE("Java exception is thrown"); delete rep; checkExAndRemoveListener(env); } if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); }
void JniOnPresenceListener::onPresenceCallback(OCStackResult result, const unsigned int nonce, const std::string& hostAddress) { LOGI("JniOnPresenceListener::onPresenceCallback"); if (!m_jwListener) return; jint ret; JNIEnv *env = GetJNIEnv(ret); if (NULL == env) return; if (OC_STACK_OK != result && OC_STACK_PRESENCE_STOPPED != result && OC_STACK_PRESENCE_TIMEOUT != result && OC_STACK_PRESENCE_DO_NOT_HANDLE != result) { ThrowOcException(result, "onPresenceCallback: stack failure"); if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); return; } std::string enumField = JniUtils::stackResultToStr(result); if (enumField.empty()) { ThrowOcException(JNI_INVALID_VALUE, "Unexpected OCStackResult value"); if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); return; } jobject jPresenceStatus = env->CallStaticObjectMethod(g_cls_OcPresenceStatus, g_mid_OcPresenceStatus_get, env->NewStringUTF(enumField.c_str())); if (!jPresenceStatus) { checkExAndRemoveListener(env); if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); 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; } jmethodID midL = env->GetMethodID(clsL, "onPresence", "(Lorg/iotivity/base/OcPresenceStatus;ILjava/lang/String;)V"); if (!midL) { checkExAndRemoveListener(env); if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); return; } env->CallVoidMethod(jListener, midL, jPresenceStatus, (jint)nonce, env->NewStringUTF(hostAddress.c_str())); if (env->ExceptionCheck()) { LOGE("Java exception is thrown"); checkExAndRemoveListener(env); } if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); }
void JniOnGetListener::onGetCallback(const HeaderOptions& headerOptions, const OCRepresentation& ocRepresentation, const int eCode) { jint envRet = JNI_ERR; JNIEnv *env = GetJNIEnv(envRet); if (nullptr == env) { return; } jobject jListener = env->NewLocalRef(m_jwListener); if (!jListener) { checkExAndRemoveListener(env); if (JNI_EDETACHED == envRet) { g_jvm->DetachCurrentThread(); } return; } jclass clsL = env->GetObjectClass(jListener); if (!clsL) { checkExAndRemoveListener(env); if (JNI_EDETACHED == envRet) { g_jvm->DetachCurrentThread(); } return; } if (OC_STACK_OK != eCode) { jobject ex = GetOcException(eCode, "stack error in onGetCallback"); if (!ex) { checkExAndRemoveListener(env); if (JNI_EDETACHED == envRet) { g_jvm->DetachCurrentThread(); } return; } jmethodID midL = env->GetMethodID(clsL, "onGetFailed", "(Ljava/lang/Throwable;)V"); if (!midL) { checkExAndRemoveListener(env); if (JNI_EDETACHED == envRet) { g_jvm->DetachCurrentThread(); } return; } env->CallVoidMethod(jListener, midL, ex); } else { jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env, headerOptions); if (!jHeaderOptionList) { checkExAndRemoveListener(env); if (JNI_EDETACHED == envRet) { g_jvm->DetachCurrentThread(); } return; } OCRepresentation* rep = new OCRepresentation(ocRepresentation); jlong handle = reinterpret_cast<jlong>(rep); jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool, handle, true); if (!jRepresentation) { delete rep; checkExAndRemoveListener(env); if (JNI_EDETACHED == envRet) { g_jvm->DetachCurrentThread(); } return; } jmethodID midL = env->GetMethodID(clsL, "onGetCompleted", "(Ljava/util/List;Lorg/iotivity/base/OcRepresentation;)V"); if (!midL) { delete rep; checkExAndRemoveListener(env); if (JNI_EDETACHED == envRet) { g_jvm->DetachCurrentThread(); } return; } env->CallVoidMethod(jListener, midL, jHeaderOptionList, jRepresentation); if (env->ExceptionCheck()) { LOGE("Java exception is thrown"); delete rep; } } checkExAndRemoveListener(env); if (JNI_EDETACHED == envRet) { g_jvm->DetachCurrentThread(); } }
OIC::Service::RCSGetResponse JniGetRequestListener::onGetRequestCallback( OIC::Service::RCSRequest request, OIC::Service::RCSResourceAttributes attributes) { LOGI("JniGetRequestListener::onGetRequestCallback enter"); jint ret; JNIEnv *env = GetREJNIEnv(ret); if (NULL == env) LOGE("env is NULL"); jobject jListener = env->NewLocalRef(m_jwListener); if (!jListener) { checkExAndRemoveListener(env); if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); LOGE("jListener is NULL"); } jclass clsL = env->GetObjectClass(jListener); if (!clsL) { checkExAndRemoveListener(env); if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); LOGE("clsL is NULL"); } //constructing java attribute object RCSResourceAttributes *attributesObj = new RCSResourceAttributes(attributes); jlong handle = reinterpret_cast<jlong>(attributesObj); jobject jAttributes = env->NewObject(g_cls_RCSRemoteResourceAttributesObject, g_mid_RCSRemoteResourceAttributesObject_ctor, handle, true); if (!jAttributes) { delete attributesObj; checkExAndRemoveListener(env); if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); LOGI("onGetRequestCallback: jAttributes == null"); } //constructing java RCSRequest object std::string resourceUri = request.getResourceUri(); jstring javaUri = env->NewStringUTF(resourceUri.c_str()); g_mid_RCSRequest_ctor = env->GetMethodID(g_cls_RCSRequest, "<init>", "(Ljava/lang/String;)V"); jobject javaRCSRequestObject = env->NewObject( g_cls_RCSRequest, g_mid_RCSRequest_ctor, javaUri); //getting the method ID jmethodID midL = env->GetMethodID(clsL, "onGetRequest", "(Lorg/iotivity/ResourceEncapsulation/server/RCSRequest;Lorg/iotivity/ResourceEncapsulation/common/RCSResourceAttributes;)Lorg/iotivity/ResourceEncapsulation/server/RCSGetResponse;"); if (!midL) { delete attributesObj; checkExAndRemoveListener(env); if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); LOGE(" midL is null"); } //calling the Java method jobject javaGetResponse = env->CallObjectMethod(jListener, midL, javaRCSRequestObject, jAttributes); //getting the information related to Response jclass GetResponseClass = env->GetObjectClass(javaGetResponse); jmethodID midGetTypeOfGetResponse = env->GetMethodID(GetResponseClass, "getTypeOfGetResponse", "()I"); jint type = env->CallIntMethod(javaGetResponse, midGetTypeOfGetResponse); if (0 == type) { return OIC::Service::RCSGetResponse::defaultAction(); } else if (1 == type) { LOGI("Type == 1"); jmethodID midGetResult = env->GetMethodID(GetResponseClass, "getResult", "()I"); jmethodID midGetErrorCode = env->GetMethodID(GetResponseClass, "getErrorCode", "()I"); jint handlerResult = env->CallIntMethod(javaGetResponse, midGetResult); jint errorCode = env->CallIntMethod(javaGetResponse, midGetErrorCode); OCEntityHandlerResult nativeHandlerResult = convertIntToOCEntityHandlerResult(handlerResult); return OIC::Service::RCSGetResponse::create(nativeHandlerResult, errorCode); } else if (2 == type) { LOGI("Type == 2"); jmethodID midGetAttributes = env->GetMethodID(GetResponseClass, "getAttributes", "()Lorg/iotivity/ResourceEncapsulation/common/RCSResourceAttributes;"); jobject attributes = env->CallObjectMethod(javaGetResponse, midGetAttributes); RCSResourceAttributes *attributesObj = JniResourceAttributes::getResourceAttributePtr(env, attributes); return OIC::Service::RCSGetResponse::create(*attributesObj); } else if (3 == type) { LOGI("Type == 3"); jmethodID midGetResult = env->GetMethodID(GetResponseClass, "getResult", "()I"); jmethodID midGetErrorCode = env->GetMethodID(GetResponseClass, "getErrorCode", "()I"); jint handlerResult = env->CallIntMethod(javaGetResponse, midGetResult); jint errorCode = env->CallIntMethod(javaGetResponse, midGetErrorCode); OCEntityHandlerResult nativeHandlerResult = convertIntToOCEntityHandlerResult(handlerResult); jmethodID midGetAttributes = env->GetMethodID(GetResponseClass, "getAttributes", "()Lorg/iotivity/ResourceEncapsulation/common/RCSResourceAttributes;"); jobject attributes = env->CallObjectMethod(javaGetResponse, midGetAttributes); RCSResourceAttributes *attributesObj = JniResourceAttributes::getResourceAttributePtr(env, attributes); return OIC::Service::RCSGetResponse::create(*attributesObj, nativeHandlerResult, errorCode); } else { LOGI("defaultAction"); return OIC::Service::RCSGetResponse::defaultAction(); } if (env->ExceptionCheck()) { LOGE("Java exception is thrown"); delete attributesObj; } checkExAndRemoveListener(env); if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); LOGI("JniGetRequestListener::onGetRequestCallback exit"); }
void JniOnObserveListener::onObserveCallback(const HeaderOptions headerOptions, const OCRepresentation& ocRepresentation, const int& eCode, const int& sequenceNumber) { jint envRet; JNIEnv *env = GetJNIEnv(envRet); if (NULL == env) return; jobject jListener = env->NewLocalRef(m_jwListener); if (!jListener) { checkExAndRemoveListener(env); if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread(); return; } jclass clsL = env->GetObjectClass(jListener); if (!clsL) { checkExAndRemoveListener(env); if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread(); return; } if (OC_STACK_OK != eCode && OC_STACK_RESOURCE_CREATED != eCode && OC_STACK_RESOURCE_DELETED != eCode) { jobject ex = GetOcException(eCode, "stack error in onObserveCallback"); if (!ex) { checkExAndRemoveListener(env); if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread(); return; } jmethodID midL = env->GetMethodID(clsL, "onObserveFailed", "(Ljava/lang/Throwable;)V"); if (!midL) { checkExAndRemoveListener(env); if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread(); return; } env->CallVoidMethod(jListener, midL, ex); } else { jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env, headerOptions); if (!jHeaderOptionList) { checkExAndRemoveListener(env); if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread(); return; } OCRepresentation * rep = new OCRepresentation(ocRepresentation); jlong handle = reinterpret_cast<jlong>(rep); jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool, handle, true); if (!jRepresentation) { delete rep; checkExAndRemoveListener(env); if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread(); return; } jmethodID midL = env->GetMethodID(clsL, "onObserveCompleted", "(Ljava/util/List;Lorg/iotivity/base/OcRepresentation;I)V"); if (!midL) { checkExAndRemoveListener(env); if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread(); return; } env->CallVoidMethod(jListener, midL, jHeaderOptionList, jRepresentation, static_cast<jint>(sequenceNumber)); if (env->ExceptionCheck()) { LOGE("Java exception is thrown"); delete rep; jthrowable ex = env->ExceptionOccurred(); env->ExceptionClear(); m_ownerResource->removeOnObserveListener(env, m_jwListener); env->Throw((jthrowable)ex); } } if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread(); }