From 13ded2aed7e1910f69896cedbc2bcd6b9f2dd2b3 Mon Sep 17 00:00:00 2001 From: "jihwan.seo" Date: Thu, 4 Aug 2016 09:13:08 +0900 Subject: [PATCH] Added Android java API for Cloud MQ Client. - added Native Caller for MQ API Change-Id: I7a15877cd041b8321088c9f8ec530fe77b390b2f Signed-off-by: jihwan.seo Reviewed-on: https://gerrit.iotivity.org/gerrit/9999 Tested-by: jenkins-iotivity Reviewed-by: Jaehong Jo Reviewed-by: Jon A. Cruz --- android/android_api/base/jni/JniOcResource.cpp | 248 +++++++++++++++++++++ android/android_api/base/jni/JniOcResource.h | 52 ++++- android/android_api/base/jni/JniOcStack.h | 1 + .../main/java/org/iotivity/base/OcResource.java | 170 ++++++++++++++ 4 files changed, 470 insertions(+), 1 deletion(-) diff --git a/android/android_api/base/jni/JniOcResource.cpp b/android/android_api/base/jni/JniOcResource.cpp index 743c1bc..3f597db 100644 --- a/android/android_api/base/jni/JniOcResource.cpp +++ b/android/android_api/base/jni/JniOcResource.cpp @@ -1603,3 +1603,251 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_dispose JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz); delete resource; } + +/* +* Class: org_iotivity_base_OcResource +* Method: discoveryMQTopicsImpl +* Signature: (Ljava/util/Map;Lorg/iotivity/base/OcPlatform/OnResourceFoundListener;I)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_discoveryMQTopicsImpl +(JNIEnv *env, jobject thiz, jobject jQueryParamsMap, jobject jListener, jint jQoS) +{ + LOGD("OcResource_discoveryMQTopicsImpl"); + +#ifdef WITH_MQ + if (!jQueryParamsMap) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null"); + return; + } + + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "listener cannot be null"); + return; + } + + JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz); + if (!resource) + { + return; + } + + QueryParamsMap qpm; + JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm); + + //todo + // discoveryMQTopics call +#else + ThrowOcException(JNI_NO_SUPPORT, "not support"); +#endif +} + +/* +* Class: org_iotivity_base_OcResource +* Method: createMQTopicImpl +* Signature: (Lorg/iotivity/base/OcRepresentation;Ljava/util/String;Ljava/util/Map +* ;Lorg/iotivity/base/OcPlatform/OnMQTopicCreatedListener;I)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_createMQTopicImpl +(JNIEnv *env, jobject thiz, jobject jRepresentation, jstring jUri, + jobject jQueryParamsMap, jobject jListener, jint jQoS) +{ + LOGD("OcResource_createMQTopicImpl"); + +#ifdef WITH_MQ + if (!jQueryParamsMap) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null"); + return; + } + + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "listener cannot be null"); + return; + } + + if (!jRepresentation) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "representation null"); + return; + } + + JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz); + if (!resource) + { + return; + } + + std::string targetUri; + if (jUri) + { + targetUri = env->GetStringUTFChars(jUri, nullptr); + } + + OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env, + jRepresentation); + if (!representation) + { + return; + } + + QueryParamsMap qpm; + JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm); + + //todo + // createMQTopic call +#else + ThrowOcException(JNI_NO_SUPPORT, "not support"); +#endif +} + +/* +* Class: org_iotivity_base_OcResource +* Method: subscribeMQTopic +* Signature: (Ljava/util/Map;Lorg/iotivity/base/OcResource/OnObserveListener;I)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_subscribeMQTopicImpl +(JNIEnv *env, jobject thiz, jobject jQueryParamsMap, jobject jListener, jint jQoS) +{ + LOGD("OcResource_subscribeMQTopicImpl"); +#ifdef MQ_SUBSCRIBER + if (!jQueryParamsMap) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null"); + return; + } + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "onObserveListener cannot be null"); + return; + } + JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz); + if (!resource) + { + return; + } + + QueryParamsMap qpm; + JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm); + + //todo + // subscribeMQTopic call +#else + ThrowOcException(JNI_NO_SUPPORT, "not support"); +#endif +} + +/* +* Class: org_iotivity_base_OcResource +* Method: unsubscribeMQTopicImpl +* Signature: (I)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_unsubscribeMQTopicImpl +(JNIEnv *env, jobject thiz, jint jQoS) +{ + LOGD("OcResource_unsubscribeMQTopicImpl"); +#ifdef MQ_SUBSCRIBER + JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz); + if (!resource) + { + return; + } + + //todo + // unsubscribeMQTopic call +#else + ThrowOcException(JNI_NO_SUPPORT, "not support"); +#endif +} + +/* +* Class: org_iotivity_base_OcResource +* Method: requestMQPublishImpl +* Signature: (Ljava/util/Map;Lorg/iotivity/base/OcResource/OnPostListener;I)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_requestMQPublishImpl +(JNIEnv *env, jobject thiz, jobject jQueryParamsMap, jobject jListener, jint jQoS) +{ + LOGD("OcResource_requestMQPublishImpl"); +#ifdef MQ_SUBSCRIBER + if (!jQueryParamsMap) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null"); + return; + } + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "onPostListener cannot be null"); + return; + } + + JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz); + if (!resource) + { + return; + } + + QueryParamsMap qpm; + JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm); + + //todo + // requestMQPublish call +#else + ThrowOcException(JNI_NO_SUPPORT, "not support"); +#endif +} + +/* +* Class: org_iotivity_base_OcResource +* Method: publishMQTopicImpl +* Signature: (Lorg/iotivity/base/OcRepresentation;Ljava/util/Map; +* Lorg/iotivity/base/OcResource/OnPostListener;I)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_publishMQTopicImpl +(JNIEnv *env, jobject thiz, jobject jRepresentation, jobject jQueryParamsMap, + jobject jListener, jint jQoS) +{ + LOGD("OcResource_publishMQTopicImpl"); +#ifdef MQ_PUBLISHER + if (!jRepresentation) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "representation cannot be null"); + return; + } + + if (!jQueryParamsMap) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null"); + return; + } + + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "onPostListener cannot be null"); + return; + } + + JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz); + if (!resource) + { + return; + } + + OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env, + jRepresentation); + if (!representation) + { + return; + } + + QueryParamsMap qpm; + JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm); + + //todo + // publishMQTopic call +#else + ThrowOcException(JNI_NO_SUPPORT, "not support"); +#endif +} diff --git a/android/android_api/base/jni/JniOcResource.h b/android/android_api/base/jni/JniOcResource.h index a038312..5911a96 100644 --- a/android/android_api/base/jni/JniOcResource.h +++ b/android/android_api/base/jni/JniOcResource.h @@ -345,7 +345,57 @@ extern "C" { JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_dispose (JNIEnv *, jobject); + /* + * Class: org_iotivity_base_OcResource + * Method: subscribeMQTopicImpl + * Signature: (Ljava/util/Map;Lorg/iotivity/base/OcResource/OnObserveListener;I)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_subscribeMQTopicImpl + (JNIEnv *, jobject, jobject, jobject, jint); + + /* + * Class: org_iotivity_base_OcResource + * Method: unsubscribeMQTopicImpl + * Signature: (I)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_unsubscribeMQTopicImpl + (JNIEnv *, jobject, jint); + + /* + * Class: org_iotivity_base_OcResource + * Method: requestMQPublishImpl + * Signature: (Ljava/util/Map;Lorg/iotivity/base/OcResource/OnPostListener;I)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_requestMQPublishImpl + (JNIEnv *, jobject, jobject, jobject, jint); + + /* + * Class: org_iotivity_base_OcResource + * Method: publishMQTopicImpl + * Signature: (Lorg/iotivity/base/OcRepresentation;Ljava/util/Map + * ;Lorg/iotivity/base/OcResource/OnPostListener;I)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_publishMQTopicImpl + (JNIEnv *, jobject, jobject, jobject, jobject, jint); + + /* + * Class: org_iotivity_base_OcResource + * Method: discoveryMQTopicsImpl + * Signature: (Ljava/util/Map;Lorg/iotivity/base/OcResource/OnMQTopicFoundListener;I)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_discoveryMQTopicsImpl + (JNIEnv *, jobject, jobject, jobject, jint); + + /* + * Class: org_iotivity_base_OcResource + * Method: createMQTopicImpl + * Signature: (Lorg/iotivity/base/OcRepresentation;Ljava/util/String;Ljava/util/Map + * ;Lorg/iotivity/base/OcPlatform/OnMQTopicCreatedListener;I)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_createMQTopicImpl + (JNIEnv *, jobject, jobject, jstring, jobject, jobject, jint); + #ifdef __cplusplus } #endif -#endif \ No newline at end of file +#endif diff --git a/android/android_api/base/jni/JniOcStack.h b/android/android_api/base/jni/JniOcStack.h index 8b7bc6f..273a6ac 100644 --- a/android/android_api/base/jni/JniOcStack.h +++ b/android/android_api/base/jni/JniOcStack.h @@ -38,6 +38,7 @@ #define JNI_NO_NATIVE_POINTER 1001 #define JNI_INVALID_VALUE 1002 #define JNI_NO_SUCH_KEY 1003 +#define JNI_NO_SUPPORT 1004 jobject getOcException(JNIEnv* env, const char* file, const char* functionName, const int line, const int code, const char* message); diff --git a/android/android_api/base/src/main/java/org/iotivity/base/OcResource.java b/android/android_api/base/src/main/java/org/iotivity/base/OcResource.java index b74c2e5..3270d69 100644 --- a/android/android_api/base/src/main/java/org/iotivity/base/OcResource.java +++ b/android/android_api/base/src/main/java/org/iotivity/base/OcResource.java @@ -591,4 +591,174 @@ public class OcResource { private native void dispose(); private long mNativeHandle; + + /** + * Method to discovery Topics + * + * @param queryParamsMap map which can have the query parameter name and value + * @param onTopicFoundListener event handler The handler method will be invoked with a map + * of attribute name and values. + * @param qualityOfService the quality of communication. + * @throws OcException + */ + public void discoveryMQTopics(Map queryParamsMap, + OnMQTopicFoundListener onTopicFoundListener, + QualityOfService qualityOfService) throws OcException { + this.discoveryMQTopicsImpl(queryParamsMap, onTopicFoundListener, + qualityOfService.getValue()); + } + + private synchronized native void discoveryMQTopicsImpl( + Map queryParamsMap, + OnMQTopicFoundListener onTopicFoundListener, + int qualityOfService) throws OcException; + + /** + * Method to create Topic into MQ Brober. + * + * @param ocRepresentation representation of the MQ Broker. + * @param uri new MQ Topic uri which want to create. + * @param queryParamsMap map which can have the query parameter name and value. + * @param onTopicCreatedListener event handler The handler method will be invoked with a map + * of attribute name and values. + * @param qualityOfService the quality of communication. + * @throws OcException + */ + public void createMQTopic(OcRepresentation ocRepresentation, + String uri, + Map queryParamsMap, + OnMQTopicCreatedListener onTopicCreatedListener, + QualityOfService qualityOfService) throws OcException { + this.createMQTopicImpl(ocRepresentation, uri, queryParamsMap, + onTopicCreatedListener, qualityOfService.getValue()); + } + + private synchronized native void createMQTopicImpl( + OcRepresentation ocRepresentation, + String uri, + Map queryParamsMap, + OnMQTopicCreatedListener onTopicCreatedListener, + int qualityOfService) throws OcException; + + /** + * Method to set subscribe on the Topic. + * + * @param queryParamsMap map which can have the query parameter name and value. + * @param onObserveListener event handler The handler method will be invoked with a map + * of attribute name and values. + * @param qualityOfService the quality of communication. + * @throws OcException + */ + public void subscribeMQTopic(Map queryParamsMap, + OnObserveListener onObserveListener, + QualityOfService qualityOfService) throws OcException { + this.subscribeMQTopicImpl(queryParamsMap, + onObserveListener, + qualityOfService.getValue()); + } + + private synchronized native void subscribeMQTopicImpl(Map queryParamsMap, + OnObserveListener onObserveListener, + int qualityOfService) throws OcException; + + /** + * Method to cancel the observation on the Topic. + * + * @param qualityOfService the quality of communication. + * @throws OcException + */ + public void unsubscribeMQTopic(QualityOfService qualityOfService) throws OcException{ + this.unsubscribeMQTopicImpl(qualityOfService.getValue()); + } + + private native void unsubscribeMQTopicImpl( + int qualityOfService) throws OcException; + + /** + * Method to requestMQPublish on a Topic + * + * @param queryParamsMap Map which can have the query parameter name and value + * @param onPostListener event handler The event handler will be invoked with a map of + * attribute name and values. + * @param qualityOfService the quality of communication. + * @throws OcException + */ + public void requestMQPublish(Map queryParamsMap, + OnPostListener onPostListener, + QualityOfService qualityOfService) throws OcException { + this.requestMQPublishImpl(queryParamsMap, + onPostListener, + qualityOfService.getValue()); + } + + private native void requestMQPublishImpl(Map queryParamsMap, + OnPostListener onPostListener, + int qualityOfService) throws OcException; + + /** + * Method to publishMQTopic on a Topic + * + * @param ocRepresentation representation of the resource + * @param queryParamsMap Map which can have the query parameter name and value + * @param onPostListener event handler The event handler will be invoked with a map of + * attribute name and values. + * @param qualityOfService the quality of communication. + * @throws OcException + */ + public void publishMQTopic(OcRepresentation ocRepresentation, + Map queryParamsMap, + OnPostListener onPostListener, + QualityOfService qualityOfService) throws OcException { + this.publishMQTopicImpl(ocRepresentation, + queryParamsMap, + onPostListener, + qualityOfService.getValue()); + } + + private native void publishMQTopicImpl(OcRepresentation ocRepresentation, + Map queryParamsMap, + OnPostListener onPostListener, + int qualityOfService) throws OcException; + + /** + * An OnMQTopicFoundListener can be registered via the OcResource.discoveryMQTopics call. + * Event listeners are notified asynchronously + */ + public interface OnMQTopicFoundListener { + public void onTopicDiscoveried(OcResource resource); + public void onDiscoveryTopicFailed(Throwable ex, String uri); + } + + /** + * An OnMQTopicCreatedListener can be registered via the OcResource.createMQTopic call. + * Event listeners are notified asynchronously + */ + public interface OnMQTopicCreatedListener { + public void onTopicResourceCreated(OcResource resource); + public void onCreateTopicFailed(Throwable ex, String uri); + } + + /** + * An OnMQTopicSubscribeListener can be registered via the OcResource.subscribeMQTopic call. + * Event listeners are notified asynchronously + */ + public interface OnMQTopicSubscribeListener { + /** + * To Subscriber. + */ + public static final int SUBSCRIBER = 0; + /** + * To Unrubscriber. + */ + public static final int UNSUBSCRIBER = 1; + /** + * Others. + */ + public static final int NO_OPTION = 2; + public void onSubScribeCompleted(List headerOptionList, + OcRepresentation ocRepresentation, + int sequenceNumber); + + public void onSubScribeFailed(Throwable ex); + } } -- 2.7.4