From: Harish Kumara Marappa Date: Wed, 12 Aug 2015 09:39:27 +0000 (+0530) Subject: JNI methods implementation for sending GET, PUT, POST and observe X-Git-Tag: 1.2.0+RC1~764^2~39 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8181c3a282993935b76377108b9dea126fc216a3;p=platform%2Fupstream%2Fiotivity.git JNI methods implementation for sending GET, PUT, POST and observe requests. Change-Id: If4d5ac51b237a3d4d2934493bc3e0cf0641349db Signed-off-by: Harish Kumara Marappa Reviewed-on: https://gerrit.iotivity.org/gerrit/2135 Tested-by: jenkins-iotivity Reviewed-by: Madan Lanka --- diff --git a/service/simulator/java/jni/simulator_manager_jni.h b/service/simulator/java/jni/simulator_manager_jni.h index 410aa71..93595d8 100644 --- a/service/simulator/java/jni/simulator_manager_jni.h +++ b/service/simulator/java/jni/simulator_manager_jni.h @@ -52,11 +52,11 @@ Java_org_oic_simulator_SimulatorManagerNativeInterface_setLogger (JNIEnv *env, jclass object, jobject logger); JNIEXPORT jint JNICALL -Java_org_iotivity_simulator_SimulatorManagerNativeInterface_findResource +Java_org_oic_simulator_SimulatorManagerNativeInterface_findResource (JNIEnv *env, jobject interfaceObject, jstring jResourceType, jobject jListener); JNIEXPORT jobject JNICALL -Java_org_iotivity_simulator_SimulatorManagerNativeInterface_getFoundResources +Java_org_oic_simulator_SimulatorManagerNativeInterface_getFoundResources (JNIEnv *env, jobject interfaceObject, jstring jResourceType); #ifdef __cplusplus diff --git a/service/simulator/java/jni/simulator_remote_resource_jni.cpp b/service/simulator/java/jni/simulator_remote_resource_jni.cpp index 03ad55c..e0fddca 100644 --- a/service/simulator/java/jni/simulator_remote_resource_jni.cpp +++ b/service/simulator/java/jni/simulator_remote_resource_jni.cpp @@ -20,42 +20,525 @@ #include "simulator_remote_resource_jni.h" #include "simulator_common_jni.h" +#include "simulator_error_codes.h" +#include "simulator_resource_jni_util.h" +#include "simulator_resource_model_jni.h" -JniSimulatorRemoteResource::JniSimulatorRemoteResource -(SimulatorRemoteResourcePtr &resource) : m_sharedResource(resource) -{} +extern SimulatorClassRefs gSimulatorClassRefs; -JniSimulatorRemoteResource *JniSimulatorRemoteResource::getJniSimulatorResourcePtr(JNIEnv *env, - jobject thiz) +SimulatorRemoteResourcePtr JniSimulatorRemoteResource::getResourceHandle +(JNIEnv *env, jobject object) { - JniSimulatorRemoteResource *resource = GetHandle(env, thiz); - if (env->ExceptionCheck()) - { - std::cout << "Failed to get native handle from SimulatorRemoteResource"; - } - if (!resource) + JniSimulatorRemoteResource *jniResource = GetHandle(env, object); + if (env->ExceptionCheck() || !jniResource) { return nullptr; } - return resource; + + return jniResource->m_resource; } -std::string JniSimulatorRemoteResource::getURI() +class JNIOnObserveListener { - return m_sharedResource->getURI(); -} + public: + void setJavaOnObserveListener(JNIEnv *env, jobject listener) + { + m_listener = env->NewWeakGlobalRef(listener); + } + + void onObserveCallback(const int eCode, + const SimulatorResourceModel &representation, + const int sequenceNumber) + { + JNIEnv *env = getEnv(); + if (nullptr == env) + return; + + jobject onObserveListener = env->NewLocalRef(m_listener); + if (!onObserveListener) + { + releaseEnv(); + return; + } + + jclass onObserveCls = env->GetObjectClass(onObserveListener); + if (!onObserveCls) + { + releaseEnv(); + return; + } + + if (OC_STACK_OK != eCode && OC_STACK_RESOURCE_CREATED != eCode + && OC_STACK_RESOURCE_DELETED != eCode) + { + jmethodID midL = env->GetMethodID(onObserveCls, "onObserveFailed", "(Ljava/lang/Throwable;)V"); + if (!midL) + { + releaseEnv(); + return; + } + env->CallVoidMethod(onObserveListener, midL); + } + else + { + SimulatorResourceModel *rep = new SimulatorResourceModel(representation); + jlong handle = reinterpret_cast(rep); + jobject jRepresentation = env->NewObject(gSimulatorClassRefs.classSimulatorResourceModel, + gSimulatorClassRefs.classSimulatorResourceModelId, handle, true); + if (!jRepresentation) + { + releaseEnv(); + return; + } + + jmethodID midL = env->GetMethodID(onObserveCls, "onObserveCompleted", + "(Lorg/oic/simulator/SimulatorResourceModel;I)V"); + if (!midL) + { + releaseEnv(); + return; + } + + env->CallVoidMethod(onObserveListener, midL, jRepresentation, + static_cast(sequenceNumber)); + if (env->ExceptionCheck()) + { + releaseEnv(); + } + } + } + + private: + jweak m_listener; +}; + +class JNIOnGetListener +{ + public: + void setJavaOnGetListener(JNIEnv *env, jobject listener) + { + m_listener = env->NewWeakGlobalRef(listener); + } + + void onGetCallback(const int eCode, + const SimulatorResourceModel &representation) + { + JNIEnv *env = getEnv(); + if (nullptr == env) + return; + + jobject onGetListener = env->NewLocalRef(m_listener); + if (!onGetListener) + { + releaseEnv(); + return; + } + + jclass onGetCls = env->GetObjectClass(onGetListener); + if (!onGetCls) + { + releaseEnv(); + return; + } + + if (OC_STACK_OK != eCode && OC_STACK_RESOURCE_CREATED != eCode + && OC_STACK_RESOURCE_DELETED != eCode) + { + jmethodID midL = env->GetMethodID(onGetCls, "onGetFailed", "(Ljava/lang/Throwable;)V"); + if (!midL) + { + releaseEnv(); + return; + } + env->CallVoidMethod(onGetListener, midL); + } + else + { + SimulatorResourceModel *rep = new SimulatorResourceModel(representation); + jlong handle = reinterpret_cast(rep); + jobject jRepresentation = env->NewObject(gSimulatorClassRefs.classSimulatorResourceModel, + gSimulatorClassRefs.classSimulatorResourceModelId, handle, true); + if (!jRepresentation) + { + releaseEnv(); + return; + } + + jmethodID midL = env->GetMethodID(onGetCls, "onGetCompleted", + "(Lorg/oic/simulator/SimulatorResourceModel;)V"); + if (!midL) + { + releaseEnv(); + return; + } + + env->CallVoidMethod(onGetListener, midL, jRepresentation); + if (env->ExceptionCheck()) + { + releaseEnv(); + } + } + } + + private: + jweak m_listener; +}; + +class JNIOnPutListener +{ + public: + void setJavaOnPutListener(JNIEnv *env, jobject listener) + { + m_listener = env->NewWeakGlobalRef(listener); + } + + void onPutCallback(const int eCode, + const SimulatorResourceModel &representation) + { + JNIEnv *env = getEnv(); + if (nullptr == env) + return; + + jobject onPutListener = env->NewLocalRef(m_listener); + if (!onPutListener) + { + releaseEnv(); + return; + } + + jclass onGetCls = env->GetObjectClass(onPutListener); + if (!onGetCls) + { + releaseEnv(); + return; + } + + if (OC_STACK_OK != eCode && OC_STACK_RESOURCE_CREATED != eCode + && OC_STACK_RESOURCE_DELETED != eCode) + { + jmethodID midL = env->GetMethodID(onGetCls, "onPutFailed", "(Ljava/lang/Throwable;)V"); + if (!midL) + { + releaseEnv(); + return; + } + env->CallVoidMethod(onPutListener, midL); + } + else + { + SimulatorResourceModel *rep = new SimulatorResourceModel(representation); + jlong handle = reinterpret_cast(rep); + jobject jRepresentation = env->NewObject(gSimulatorClassRefs.classSimulatorResourceModel, + gSimulatorClassRefs.classSimulatorResourceModelId, handle, true); + if (!jRepresentation) + { + releaseEnv(); + return; + } + jmethodID midL = env->GetMethodID(onGetCls, "onPutCompleted", + "(Lorg/oic/simulator/SimulatorResourceModel;)V"); + if (!midL) + { + releaseEnv(); + return; + } -JNIEXPORT jstring JNICALL -Java_org_iotivity_simulator_SimulatorRemoteResource_getURI + env->CallVoidMethod(onPutListener, midL, jRepresentation); + if (env->ExceptionCheck()) + { + releaseEnv(); + } + } + } + + private: + jweak m_listener; +}; + +class JNIOnPostListener +{ + public: + void setJavaOnPostListener(JNIEnv *env, jobject listener) + { + m_listener = env->NewWeakGlobalRef(listener); + } + + void onPostCallback(const int eCode, + const SimulatorResourceModel &representation) + { + JNIEnv *env = getEnv(); + if (nullptr == env) + return; + + jobject onPostListener = env->NewLocalRef(m_listener); + if (!onPostListener) + { + releaseEnv(); + return; + } + + jclass onGetCls = env->GetObjectClass(onPostListener); + if (!onGetCls) + { + releaseEnv(); + return; + } + + if (OC_STACK_OK != eCode && OC_STACK_RESOURCE_CREATED != eCode + && OC_STACK_RESOURCE_DELETED != eCode) + { + jmethodID midL = env->GetMethodID(onGetCls, "onPostFailed", "(Ljava/lang/Throwable;)V"); + if (!midL) + { + releaseEnv(); + return; + } + env->CallVoidMethod(onPostListener, midL); + } + else + { + SimulatorResourceModel *rep = new SimulatorResourceModel(representation); + jlong handle = reinterpret_cast(rep); + jobject jRepresentation = env->NewObject(gSimulatorClassRefs.classSimulatorResourceModel, + gSimulatorClassRefs.classSimulatorResourceModelId, handle, true); + if (!jRepresentation) + { + releaseEnv(); + return; + } + + jmethodID midL = env->GetMethodID(onGetCls, "onPostCompleted", + "(Lorg/oic/simulator/SimulatorResourceModel;)V"); + if (!midL) + { + releaseEnv(); + return; + } + + env->CallVoidMethod(onPostListener, midL, jRepresentation); + if (env->ExceptionCheck()) + { + releaseEnv(); + } + } + } + + private: + jweak m_listener; + +}; + + +JNIEXPORT jint JNICALL +Java_org_oic_simulator_SimulatorRemoteResource_observe +(JNIEnv *env, jobject thiz, jint observeType, jobject jQueryParamsMap, jobject jListener) +{ + if (!jListener) + return SIMULATOR_BAD_INPUT; + + SimulatorRemoteResourcePtr resource = JniSimulatorRemoteResource::getResourceHandle(env, + thiz); + if (nullptr == resource) + return SIMULATOR_RESOURCE_NOT_FOUND; + + std::map queryParams; + if (jQueryParamsMap) + convertJavaMapToQueryParamsMap(env, jQueryParamsMap, queryParams); + + SimulatorRemoteResource::ObserveType type = SimulatorRemoteResource::OBSERVE; + if (1 == observeType) + type = SimulatorRemoteResource::OBSERVE_ALL; + + JNIOnObserveListener *onObserveListener = new JNIOnObserveListener(); + onObserveListener->setJavaOnObserveListener(env, jListener); + + return resource->observe(type, queryParams, + std::bind(&JNIOnObserveListener::onObserveCallback, + onObserveListener, std::placeholders::_1, + std::placeholders::_2, std::placeholders::_3)); +} + +JNIEXPORT jint JNICALL +Java_org_oic_simulator_SimulatorRemoteResource_cancelObserve (JNIEnv *env, jobject thiz) { - std::cout << "SimulatorRemoteResource_getURI Entry"; - JniSimulatorRemoteResource *resource = JniSimulatorRemoteResource::getJniSimulatorResourcePtr(env, + SimulatorRemoteResourcePtr resource = JniSimulatorRemoteResource::getResourceHandle(env, thiz); + if (nullptr == resource) + return SIMULATOR_RESOURCE_NOT_FOUND; - if (!resource) - return nullptr; + return resource->cancelObserve(); +} + +JNIEXPORT jint JNICALL +Java_org_oic_simulator_SimulatorRemoteResource_get +(JNIEnv *env, jobject thiz, jstring jResourceType, jstring jResourceInterface, + jobject jQueryParamsMap, jobject jListener) +{ + if (!jListener) + return SIMULATOR_BAD_INPUT; + + SimulatorRemoteResourcePtr resource = JniSimulatorRemoteResource::getResourceHandle(env, + thiz); + if (nullptr == resource) + return SIMULATOR_RESOURCE_NOT_FOUND; + + // Resource type + const char *typeCStr = NULL; + std::string resourceType; + if (jResourceType) + { + typeCStr = env->GetStringUTFChars(jResourceType, NULL); + resourceType = typeCStr; + } + + // Interface type + const char *interfaceCStr = NULL; + std::string interfaceType; + if (jResourceInterface) + { + interfaceCStr = env->GetStringUTFChars(jResourceInterface, NULL); + interfaceType = interfaceCStr; + } + + // Query parameters + std::map queryParams; + if (jQueryParamsMap) + convertJavaMapToQueryParamsMap(env, jQueryParamsMap, queryParams); + + // Create listener + JNIOnGetListener *onGetListener = new JNIOnGetListener(); + onGetListener->setJavaOnGetListener(env, jListener); + + SimulatorResult result = resource->get(resourceType, interfaceType, + queryParams, + std::bind(&JNIOnGetListener::onGetCallback, + onGetListener, std::placeholders::_1, + std::placeholders::_2)); + + if (typeCStr) + env->ReleaseStringUTFChars(jResourceType, typeCStr); + + if (interfaceCStr) + env->ReleaseStringUTFChars(jResourceInterface, interfaceCStr); + + return result; +} + +JNIEXPORT jint JNICALL +Java_org_oic_simulator_SimulatorRemoteResource_put +(JNIEnv *env, jobject thiz, jstring jResourceType, jstring jResourceInterface, + jobject jRepresentation, jobject jQueryParamsMap, jobject jListener) +{ + if (!jListener) + return SIMULATOR_BAD_INPUT; + + SimulatorRemoteResourcePtr resource = JniSimulatorRemoteResource::getResourceHandle(env, + thiz); + if (nullptr == resource) + return SIMULATOR_RESOURCE_NOT_FOUND; + + // Resource type + const char *typeCStr = NULL; + std::string resourceType; + if (jResourceType) + { + typeCStr = env->GetStringUTFChars(jResourceType, NULL); + resourceType = typeCStr; + } + + // Interface type + const char *interfaceCStr = NULL; + std::string interfaceType; + if (jResourceInterface) + { + interfaceCStr = env->GetStringUTFChars(jResourceInterface, NULL); + interfaceType = interfaceCStr; + } + + // Query parameters + std::map queryParams; + if (jQueryParamsMap) + convertJavaMapToQueryParamsMap(env, jQueryParamsMap, queryParams); + + SimulatorResourceModel resourceModel; + JniSimulatorResourceModel::getResourceModel(env, jRepresentation, resourceModel); + + // Create listener + JNIOnPutListener *onPutListener = new JNIOnPutListener(); + onPutListener->setJavaOnPutListener(env, jListener); + + SimulatorResult result = resource->put(resourceType, interfaceType, + resourceModel, queryParams, + std::bind(&JNIOnPutListener::onPutCallback, + onPutListener, std::placeholders::_1, + std::placeholders::_2)); + + if (typeCStr) + env->ReleaseStringUTFChars(jResourceType, typeCStr); + + if (interfaceCStr) + env->ReleaseStringUTFChars(jResourceInterface, interfaceCStr); + + return result; +} + +JNIEXPORT jint JNICALL +Java_org_oic_simulator_SimulatorRemoteResource_post +(JNIEnv *env, jobject thiz, jstring jResourceType, jstring jResourceInterface, + jobject jRepresentation, jobject jQueryParamsMap, jobject jListener) +{ + if (!jListener) + return SIMULATOR_BAD_INPUT; + + SimulatorRemoteResourcePtr resource = JniSimulatorRemoteResource::getResourceHandle(env, + thiz); + if (nullptr == resource) + return SIMULATOR_RESOURCE_NOT_FOUND; + + // Resource type + const char *typeCStr = NULL; + std::string resourceType; + if (jResourceType) + { + typeCStr = env->GetStringUTFChars(jResourceType, NULL); + resourceType = typeCStr; + } + + // Interface type + const char *interfaceCStr = NULL; + std::string interfaceType; + if (jResourceInterface) + { + interfaceCStr = env->GetStringUTFChars(jResourceInterface, NULL); + interfaceType = interfaceCStr; + } + + // Query parameters + std::map queryParams; + if (jQueryParamsMap) + convertJavaMapToQueryParamsMap(env, jQueryParamsMap, queryParams); + + SimulatorResourceModel resourceModel; + JniSimulatorResourceModel::getResourceModel(env, jRepresentation, resourceModel); + + // Create listener + JNIOnPostListener *onPostListener = new JNIOnPostListener(); + onPostListener->setJavaOnPostListener(env, jListener); + + SimulatorResult result = resource->post(resourceType, interfaceType, + resourceModel, queryParams, + std::bind(&JNIOnPostListener::onPostCallback, + onPostListener, std::placeholders::_1, + std::placeholders::_2)); + + if (typeCStr) + env->ReleaseStringUTFChars(jResourceType, typeCStr); + + if (interfaceCStr) + env->ReleaseStringUTFChars(jResourceInterface, interfaceCStr); + + return result; +} - return env->NewStringUTF(resource->getURI().c_str()); -} \ No newline at end of file diff --git a/service/simulator/java/jni/simulator_remote_resource_jni.h b/service/simulator/java/jni/simulator_remote_resource_jni.h index 41105ca..8439f43 100644 --- a/service/simulator/java/jni/simulator_remote_resource_jni.h +++ b/service/simulator/java/jni/simulator_remote_resource_jni.h @@ -23,33 +23,48 @@ #include #include "simulator_remote_resource.h" -#include "simulator_client.h" class JniSimulatorRemoteResource { public: - - JniSimulatorRemoteResource(SimulatorRemoteResourcePtr &resource); - ~JniSimulatorRemoteResource(); - static JniSimulatorRemoteResource *getJniSimulatorResourcePtr(JNIEnv *env, jobject thiz); - std::string getURI(); - + JniSimulatorRemoteResource(SimulatorRemoteResourcePtr &resource) + : m_resource(resource) {}; + static SimulatorRemoteResourcePtr getResourceHandle(JNIEnv *env, jobject object); private: - std::shared_ptr m_sharedResource; + SimulatorRemoteResourcePtr m_resource; }; #ifdef __cplusplus extern "C" { #endif -JNIEXPORT jstring JNICALL -Java_org_iotivity_simulator_SimulatorRemoteResource_getURI -(JNIEnv *, jobject); +JNIEXPORT jint JNICALL +Java_org_oic_simulator_SimulatorRemoteResource_observe +(JNIEnv *env, jobject thiz, jint observeType, jobject jQueryParamsMap, jobject jListener); + +JNIEXPORT jint JNICALL +Java_org_oic_simulator_SimulatorRemoteResource_cancelObserve +(JNIEnv *env, jobject thiz); + +JNIEXPORT jint JNICALL +Java_org_oic_simulator_SimulatorRemoteResource_get +(JNIEnv *env, jobject thiz, jstring jResourceType, jstring jResourceInterface, + jobject jQueryParamsMap, jobject jListener); + +JNIEXPORT jint JNICALL +Java_org_oic_simulator_SimulatorRemoteResource_put +(JNIEnv *env, jobject thiz, jstring jResourceType, jstring jResourceInterface, + jobject jRepresentation, jobject jQueryParamsMap, jobject jListener); + +JNIEXPORT jint JNICALL +Java_org_oic_simulator_SimulatorRemoteResource_post +(JNIEnv *env, jobject thiz, jstring jResourceType, jstring jResourceInterface, + jobject jRepresentation, jobject jQueryParamsMap, jobject jListener); #ifdef __cplusplus } #endif -#endif //SIMULATOR_REMOTE_RESOURCE_JNI_H_ +#endif diff --git a/service/simulator/java/jni/simulator_resource_jni_util.cpp b/service/simulator/java/jni/simulator_resource_jni_util.cpp index a93998c..802aa33 100644 --- a/service/simulator/java/jni/simulator_resource_jni_util.cpp +++ b/service/simulator/java/jni/simulator_resource_jni_util.cpp @@ -18,6 +18,9 @@ * ******************************************************************/ #include "simulator_resource_jni_util.h" +#include "simulator_common_jni.h" + +extern SimulatorClassRefs gSimulatorClassRefs; std::vector convertIntegerVector(JNIEnv *env, jobject jVectorInt) { @@ -134,3 +137,32 @@ std::vector convertStringVector(JNIEnv *env, jobject jVectorString) return vectorString; } + +void convertJavaMapToQueryParamsMap(JNIEnv *env, jobject hashMap, + std::map &queryParams) +{ + if (!hashMap) return; + + jobject jEntrySet = env->CallObjectMethod(hashMap, gSimulatorClassRefs.classMapEntrySet); + jobject jIterator = env->CallObjectMethod(jEntrySet, gSimulatorClassRefs.classIteratorId); + if (!jEntrySet || !jIterator || env->ExceptionCheck()) return; + + while (env->CallBooleanMethod(jIterator, gSimulatorClassRefs.classHasNextId)) + { + jobject jEntry = env->CallObjectMethod(jIterator, gSimulatorClassRefs.classNextId); + if (!jEntry) return; + jstring jKey = (jstring)env->CallObjectMethod(jEntry, gSimulatorClassRefs.classMapGetKey); + if (!jKey) return; + jstring jValue = (jstring)env->CallObjectMethod(jEntry, gSimulatorClassRefs.classMapGetValue); + if (!jValue) return; + + queryParams.insert(std::make_pair(env->GetStringUTFChars(jKey, NULL), + env->GetStringUTFChars(jValue, NULL))); + + if (env->ExceptionCheck()) return; + env->DeleteLocalRef(jEntry); + env->DeleteLocalRef(jKey); + env->DeleteLocalRef(jValue); + } +} + diff --git a/service/simulator/java/jni/simulator_resource_jni_util.h b/service/simulator/java/jni/simulator_resource_jni_util.h index 98e72a1..3beb49b 100644 --- a/service/simulator/java/jni/simulator_resource_jni_util.h +++ b/service/simulator/java/jni/simulator_resource_jni_util.h @@ -33,6 +33,8 @@ #include #include +#include "simulator_remote_resource.h" + /** * Utility function for converting a Java Vector of Strings to CPP Vector of Strings * @@ -63,5 +65,8 @@ std::vector convertDoubleVector(JNIEnv *env, jobject jVectorDouble); */ std::vector convertStringVector(JNIEnv *env, jobject jVectorString); +void convertJavaMapToQueryParamsMap(JNIEnv *env, jobject hashMap, + std::map &map); + #endif //__SIMULATOR_RESOURCE_JNI_UTIL_H_