Implemented MQ API for Android JNI
authorjihwan.seo <jihwan.seo@samsung.com>
Thu, 4 Aug 2016 00:59:35 +0000 (09:59 +0900)
committerAshok Babu Channa <ashok.channa@samsung.com>
Tue, 16 Aug 2016 11:30:20 +0000 (11:30 +0000)
connected from Android JNI to C++ API of OcResource.

Change-Id: I377d786dfa2bf7f06fb43a82b6bb1955f19dbcb1
Signed-off-by: jihwan.seo <jihwan.seo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/10003
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Ziran Sun <ziran.sun@samsung.com>
Reviewed-by: Jaehong Jo <jaehong.jo@samsung.com>
Reviewed-by: Ashok Babu Channa <ashok.channa@samsung.com>
android/android_api/base/jni/JniOcResource.cpp
android/android_api/base/jni/JniOcResource.h

index 3f597dba17b319d14321c434e280bbfb0dd6f453..60d3161dfb26d6669348b0c0874321583fcc39c1 100644 (file)
@@ -47,6 +47,10 @@ JniOcResource::~JniOcResource()
     m_onPostManager.removeAllListeners(env);
     m_onDeleteManager.removeAllListeners(env);
     m_onObserveManager.removeAllListeners(env);
+#ifdef WITH_MQ
+    m_onFoundTopicResourceManager.removeAllListeners(env);
+    m_onSubcribeTopicManager.removeAllListeners(env);
+#endif
 
     if (JNI_EDETACHED == envRet)
     {
@@ -400,6 +404,28 @@ void JniOcResource::removeOnObserveListener(JNIEnv* env, jobject jListener)
     this->m_onObserveManager.removeListener(env, jListener);
 }
 
+#ifdef WITH_MQ
+JniOnMQTopicFoundListener* JniOcResource::addOnTopicFoundListener(JNIEnv* env, jobject jListener)
+{
+    return this->m_onFoundTopicResourceManager.addListener(env, jListener, this);
+}
+
+void JniOcResource::removeOnTopicFoundListener(JNIEnv* env, jobject jListener)
+{
+    this->m_onFoundTopicResourceManager.removeListener(env, jListener);
+}
+
+JniOnMQSubscribeListener* JniOcResource::addOnMQTopicSubscribeListener(JNIEnv* env, jobject jListener)
+{
+    return this->m_onSubcribeTopicManager.addListener(env, jListener, this);
+}
+
+void JniOcResource::removeOnMQTopicSubscribeListener(JNIEnv* env, jobject jListener)
+{
+    this->m_onSubcribeTopicManager.removeListener(env, jListener);
+}
+#endif
+
 std::shared_ptr<OCResource> JniOcResource::getOCResource()
 {
     return this->m_sharedResource;
@@ -419,6 +445,90 @@ JniOcResource* JniOcResource::getJniOcResourcePtr(JNIEnv *env, jobject thiz)
     return resource;
 }
 
+#ifdef WITH_MQ
+OCStackResult JniOcResource::discoveryMQTopics(JNIEnv* env,
+    const QueryParamsMap &queryParametersMap, jobject jListener, QualityOfService QoS)
+{
+    JniOnMQTopicFoundListener *onTopicFoundListener = addOnTopicFoundListener(env, jListener);
+
+    MQTopicCallback findCallback = [onTopicFoundListener](const int& eCode,
+            const std::string& uri, std::shared_ptr<OCResource> resource)
+    {
+        onTopicFoundListener->foundTopicCallback(eCode, uri, resource);
+    };
+
+    return m_sharedResource->discoveryMQTopics(queryParametersMap, findCallback);
+}
+
+OCStackResult JniOcResource::createMQTopic(JNIEnv* env,
+    const OCRepresentation &representation, const std::string &targetUri,
+    const QueryParamsMap &queryParametersMap, jobject jListener, QualityOfService QoS)
+{
+    JniOnMQTopicFoundListener *onTopicCreatedListener = addOnTopicFoundListener(env, jListener);
+
+    MQTopicCallback createCallback = [onTopicCreatedListener](const int& eCode,
+            const std::string& uri, std::shared_ptr<OCResource> resource)
+    {
+        onTopicCreatedListener->createdTopicCallback(eCode, uri, resource);
+    };
+
+    return m_sharedResource->createMQTopic(representation, targetUri,
+                                           queryParametersMap,
+                                           createCallback);
+}
+#endif
+#ifdef MQ_SUBSCRIBER
+OCStackResult JniOcResource::subscribeMQTopic(JNIEnv* env,
+    const QueryParamsMap &queryParametersMap, jobject jListener, QualityOfService QoS)
+{
+    JniOnMQSubscribeListener *onSubscribeListener = addOnMQTopicSubscribeListener(env, jListener);
+
+    ObserveCallback subscribeCallback = [onSubscribeListener](const HeaderOptions& opts,
+        const OCRepresentation& rep, const int& eCode, const int& sequenceNumber)
+    {
+        onSubscribeListener->onSubscribeCallback(opts, rep, eCode, sequenceNumber);
+    };
+
+    return m_sharedResource->subscribeMQTopic(ObserveType::Observe, queryParametersMap,
+                                              subscribeCallback);
+}
+
+OCStackResult JniOcResource::unsubscribeMQTopic(QualityOfService QoS)
+{
+    return m_sharedResource->unsubscribeMQTopic();
+}
+
+OCStackResult JniOcResource::requestMQPublish(JNIEnv* env,
+    const QueryParamsMap &queryParametersMap, jobject jListener, QualityOfService QoS)
+{
+    JniOnPostListener *onPostListener = addOnPostListener(env, jListener);
+
+    PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
+        const OCRepresentation& rep, const int eCode)
+    {
+        onPostListener->onPostCallback(opts, rep, eCode);
+    };
+
+    return m_sharedResource->requestMQPublish(queryParametersMap, postCallback);
+}
+#endif
+#ifdef MQ_PUBLISHER
+OCStackResult JniOcResource::publishMQTopic(JNIEnv* env, const OCRepresentation &representation,
+    const QueryParamsMap &queryParametersMap, jobject jListener, QualityOfService QoS)
+{
+    JniOnPostListener *onPostListener = addOnPostListener(env, jListener);
+
+    PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
+        const OCRepresentation& rep, const int eCode)
+    {
+        onPostListener->onPostCallback(opts, rep, eCode);
+    };
+
+    return m_sharedResource->publishMQTopic(representation, queryParametersMap,
+                                            postCallback);
+}
+#endif
+
 /*
 * Class:     org_iotivity_base_OcResource
 * Method:    get
@@ -1636,8 +1746,24 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_discoveryMQTopicsImpl
     QueryParamsMap qpm;
     JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
 
-    //todo
-    // discoveryMQTopics call
+    try
+    {
+        OCStackResult result = resource->discoveryMQTopics(
+            env,
+            qpm,
+            jListener,
+            JniUtils::getQOS(env, static_cast<int>(jQoS)));
+
+        if (OC_STACK_OK != result)
+        {
+            ThrowOcException(result, "OcResource_discoveryMQTopicsImpl");
+        }
+    }
+    catch (OCException& e)
+    {
+        LOGE("%s", e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
+    }
 #else
     ThrowOcException(JNI_NO_SUPPORT, "not support");
 #endif
@@ -1696,8 +1822,26 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_createMQTopicImpl
     QueryParamsMap qpm;
     JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
 
-    //todo
-    // createMQTopic call
+    try
+    {
+        OCStackResult result = resource->createMQTopic(
+            env,
+            *representation,
+            targetUri,
+            qpm,
+            jListener,
+            JniUtils::getQOS(env, static_cast<int>(jQoS)));
+
+        if (OC_STACK_OK != result)
+        {
+            ThrowOcException(result, "OcResource_createMQTopicImpl");
+        }
+    }
+    catch (OCException& e)
+    {
+        LOGE("%s", e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
+    }
 #else
     ThrowOcException(JNI_NO_SUPPORT, "not support");
 #endif
@@ -1732,8 +1876,24 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_subscribeMQTopicImpl
     QueryParamsMap qpm;
     JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
 
-    //todo
-    // subscribeMQTopic call
+    try
+    {
+        OCStackResult result = resource->subscribeMQTopic(
+            env,
+            qpm,
+            jListener,
+            JniUtils::getQOS(env, static_cast<int>(jQoS)));
+
+        if (OC_STACK_OK != result)
+        {
+            ThrowOcException(result, "OcResource_subscribeMQTopicImpl");
+        }
+    }
+    catch (OCException& e)
+    {
+        LOGE("%s", e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
+    }
 #else
     ThrowOcException(JNI_NO_SUPPORT, "not support");
 #endif
@@ -1755,8 +1915,20 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_unsubscribeMQTopicImpl
         return;
     }
 
-    //todo
-    // unsubscribeMQTopic call
+    try
+    {
+        OCStackResult result = resource->unsubscribeMQTopic(
+                JniUtils::getQOS(env, static_cast<int>(jQoS)));
+        if (OC_STACK_OK != result)
+        {
+            ThrowOcException(result, "OcResource_unsubscribeMQTopicImpl");
+        }
+    }
+    catch (OCException& e)
+    {
+        LOGE("%s", e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
+    }
 #else
     ThrowOcException(JNI_NO_SUPPORT, "not support");
 #endif
@@ -1792,8 +1964,24 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_requestMQPublishImpl
     QueryParamsMap qpm;
     JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
 
-    //todo
-    // requestMQPublish call
+    try
+    {
+        OCStackResult result = resource->requestMQPublish(
+            env,
+            qpm,
+            jListener,
+            JniUtils::getQOS(env, static_cast<int>(jQoS)));
+
+        if (OC_STACK_OK != result)
+        {
+            ThrowOcException(result, "OcResource_requestMQPublishImpl");
+        }
+    }
+    catch (OCException& e)
+    {
+        LOGE("%s", e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
+    }
 #else
     ThrowOcException(JNI_NO_SUPPORT, "not support");
 #endif
@@ -1845,8 +2033,25 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_publishMQTopicImpl
     QueryParamsMap qpm;
     JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
 
-    //todo
-    // publishMQTopic call
+    try
+    {
+        OCStackResult result = resource->publishMQTopic(
+            env,
+            *representation,
+            qpm,
+            jListener,
+            JniUtils::getQOS(env, static_cast<int>(jQoS)));
+
+        if (OC_STACK_OK != result)
+        {
+            ThrowOcException(result, "OcResource_publishMQTopicImpl");
+        }
+    }
+    catch (OCException& e)
+    {
+        LOGE("%s", e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
+    }
 #else
     ThrowOcException(JNI_NO_SUPPORT, "not support");
 #endif
index 5911a96305f2c644d6679899d326235ad50676bc..7dfacd3278e65548ee083aaccc8ff832fd327c25 100644 (file)
 #include "JniOnDeleteListener.h"
 #include "JniOnObserveListener.h"
 #include "JniOcResourceIdentifier.h"
+#ifdef WITH_MQ
+#include "JniOnMQTopicFoundListener.h"
+#include "JniOnMQSubscribeListener.h"
+#endif
 
 #ifndef _Included_org_iotivity_base_OcResource
 #define _Included_org_iotivity_base_OcResource
@@ -84,8 +88,8 @@ public:
     std::string uri();
     OCConnectivityType connectivityType() const;
     bool isObservable();
-    std::vector< std::string >         getResourceTypes() const;
-    std::vector< std::string >         getResourceInterfaces(void) const;
+    std::vector< std::string >  getResourceTypes() const;
+    std::vector< std::string >  getResourceInterfaces(void) const;
     OCResourceIdentifier uniqueIdentifier() const;
     std::string sid() const;
 
@@ -105,12 +109,43 @@ public:
 
     static JniOcResource* getJniOcResourcePtr(JNIEnv *env, jobject thiz);
 
+#ifdef WITH_MQ
+    JniOnMQTopicFoundListener* addOnTopicFoundListener(JNIEnv* env, jobject jListener);
+    void removeOnTopicFoundListener(JNIEnv* env, jobject jListener);
+
+    JniOnMQSubscribeListener* addOnMQTopicSubscribeListener(JNIEnv* env, jobject jListener);
+    void removeOnMQTopicSubscribeListener(JNIEnv* env, jobject jListener);
+
+    OCStackResult discoveryMQTopics(JNIEnv* env, const QueryParamsMap &queryParametersMap,
+                                    jobject jListener, QualityOfService QoS);
+    OCStackResult createMQTopic(JNIEnv* env, const OCRepresentation &representation,
+                                const std::string &targetUri,
+                                const QueryParamsMap &queryParametersMap,
+                                jobject jListener, QualityOfService QoS);
+#endif
+#ifdef MQ_SUBSCRIBER
+    OCStackResult subscribeMQTopic(JNIEnv* env, const QueryParamsMap &queryParametersMap,
+                                   jobject jListener, QualityOfService QoS);
+    OCStackResult unsubscribeMQTopic(QualityOfService QoS);
+    OCStackResult requestMQPublish(JNIEnv* env, const QueryParamsMap &queryParametersMap,
+                                   jobject jListener, QualityOfService QoS);
+#endif
+#ifdef MQ_PUBLISHER
+    OCStackResult publishMQTopic(JNIEnv* env, const OCRepresentation &representation,
+                                 const QueryParamsMap &queryParametersMap,
+                                 jobject jListener, QualityOfService QoS);
+#endif
+
 private:
     JniListenerManager<JniOnGetListener> m_onGetManager;
     JniListenerManager<JniOnPutListener> m_onPutManager;
     JniListenerManager<JniOnPostListener> m_onPostManager;
     JniListenerManager<JniOnDeleteListener> m_onDeleteManager;
     JniListenerManager<JniOnObserveListener> m_onObserveManager;
+#ifdef WITH_MQ
+    JniListenerManager<JniOnMQTopicFoundListener> m_onFoundTopicResourceManager;
+    JniListenerManager<JniOnMQSubscribeListener> m_onSubcribeTopicManager;
+#endif
 
     std::shared_ptr<OCResource> m_sharedResource;
 };