[IOT-826] Android: Fixed failure on cancelObserve() with High QoS
authorTim Kourt <tim.a.kourt@intel.com>
Tue, 1 Dec 2015 23:26:06 +0000 (15:26 -0800)
committerJon A. Cruz <jonc@osg.samsung.com>
Mon, 7 Dec 2015 20:00:11 +0000 (20:00 +0000)
In addition it propagates the actual error codes
from the C++ exceptions

Change-Id: I49ab61c42c5dade8e021cf1f1ae6fed83f1fee88
Signed-off-by: Tim Kourt <tim.a.kourt@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/4387
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jon A. Cruz <jonc@osg.samsung.com>
android/android_api/base/jni/JniOcPlatform.cpp
android/android_api/base/jni/JniOcResource.cpp
android/android_api/base/jni/JniOcResource.h
android/android_api/base/jni/JniOnObserveListener.cpp
android/android_api/base/src/main/java/org/iotivity/base/OcPlatform.java
android/android_api/base/src/main/java/org/iotivity/base/OcResource.java

index e3a6cd5..93029c9 100644 (file)
@@ -347,6 +347,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_configure
     {
         port = static_cast<uint16_t>(jPort);
     }
+
     PlatformConfig cfg{
         JniUtils::getServiceType(env, jServiceType),
         JniUtils::getModeType(env, jModeType),
@@ -355,7 +356,6 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_configure
         JniUtils::getQOS(env, static_cast<int>(jQOS)),
         JniOcSecurity::getOCPersistentStorage()
     };
-
     OCPlatform::Configure(cfg);
 }
 
@@ -392,7 +392,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyAllObservers0
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
     }
 }
 
@@ -433,7 +433,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyAllObservers1
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
     }
 }
 
@@ -506,7 +506,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyListOfObservers2(
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
     }
 }
 
@@ -581,7 +581,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyListOfObservers3(
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
     }
 }
 
@@ -639,7 +639,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResource0(
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
     }
 }
 
@@ -698,7 +698,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResource1(
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
     }
 }
 
@@ -755,7 +755,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getDeviceInfo0(
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
     }
 }
 
@@ -814,7 +814,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getDeviceInfo1(
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
     }
 }
 
@@ -871,7 +871,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getPlatformInfo0(
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
     }
 }
 
@@ -930,7 +930,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getPlatformInfo1(
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
     }
 }
 
@@ -966,7 +966,7 @@ JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_registerResource0(
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
         return nullptr;
     }
     JniOcResourceHandle* jniHandle = new JniOcResourceHandle(resourceHandle);
@@ -1045,7 +1045,7 @@ jobject jListener, jint jResourceProperty)
     {
         LOGE("%s", e.reason().c_str());
         delete entityHandler;
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
         return nullptr;
     }
 
@@ -1106,7 +1106,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerDeviceInfo0(
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
     }
 }
 
@@ -1237,7 +1237,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerPlatformInfo0(
         catch (OCException& e)
         {
             LOGE("Error is due to %s", e.reason().c_str());
-            ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+            ThrowOcException(e.code(), e.reason().c_str());
         }
 }
 
@@ -1274,7 +1274,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unregisterResource0(
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
     }
 }
 
@@ -1323,7 +1323,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindResource0
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
     }
 }
 
@@ -1395,7 +1395,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindResources0(
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
     }
 }
 
@@ -1450,7 +1450,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unbindResource0(
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
     }
 }
 
@@ -1521,7 +1521,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unbindResources0(
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
     }
 }
 
@@ -1567,7 +1567,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindTypeToResource0(
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
     }
 }
 
@@ -1613,7 +1613,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindInterfaceToResource
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
     }
 }
 
@@ -1639,7 +1639,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_startPresence0(
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
     }
 }
 
@@ -1665,7 +1665,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_stopPresence0(
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
     }
 }
 
@@ -1720,7 +1720,7 @@ JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_subscribePresence0(
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
         return nullptr;
     }
 
@@ -1794,7 +1794,7 @@ JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_subscribePresence1(
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
         return nullptr;
     }
 
@@ -1853,7 +1853,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unsubscribePresence0(
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
     }
 }
 
@@ -1965,6 +1965,6 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_sendResponse0(
     catch (OCException& e)
     {
         LOGE("%s", e.reason().c_str());
-        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
     }
 }
index 29edcc6..9e6925a 100644 (file)
@@ -284,17 +284,12 @@ OCStackResult JniOcResource::observe(JNIEnv* env, ObserveType observeType,
     return m_sharedResource->observe(observeType, queryParametersMap, observeCallback, QoS);
 }
 
-OCStackResult JniOcResource::cancelObserve(JNIEnv* env)
-{
-    //this->m_onObserveManager.removeAllListeners(env);
-    return m_sharedResource->cancelObserve();
-}
-
 OCStackResult JniOcResource::cancelObserve(JNIEnv* env, QualityOfService qos)
 {
-    //TODO confirm behavior
-    //add removal of java listeners by qos
-    //this->m_onObserveManager.removeAllListeners(env);
+    if (QualityOfService::HighQos != qos)
+    {
+        this->m_onObserveManager.removeAllListeners(env);
+    }
     return m_sharedResource->cancelObserve(qos);
 }
 
@@ -1279,34 +1274,6 @@ jobject jListener, jint jQoS)
 
 /*
 * Class:     org_iotivity_base_OcResource
-* Method:    cancelObserve
-* Signature: ()V
-*/
-JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_cancelObserve
-(JNIEnv *env, jobject thiz)
-{
-    LOGD("OcResource_cancelObserve");
-    JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
-    if (!resource) return;
-
-    try
-    {
-        OCStackResult result = resource->cancelObserve(env);
-
-        if (OC_STACK_OK != result)
-        {
-            ThrowOcException(result, "OcResource_cancelObserve");
-        }
-    }
-    catch (OCException& e)
-    {
-        LOGE("%s", e.reason().c_str());
-        ThrowOcException(e.code(), e.reason().c_str());
-    }
-}
-
-/*
-* Class:     org_iotivity_base_OcResource
 * Method:    cancelObserve1
 * Signature: I)V
 */
index cb36691..a038312 100644 (file)
@@ -251,14 +251,6 @@ extern "C" {
 
     /*
     * Class:     org_iotivity_base_OcResource
-    * Method:    cancelObserve
-    * Signature: ()V
-    */
-    JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_cancelObserve
-        (JNIEnv *, jobject);
-
-    /*
-    * Class:     org_iotivity_base_OcResource
     * Method:    cancelObserve1
     * Signature: (I)V
     */
index 0b2c28f..3ad1d11 100644 (file)
@@ -127,6 +127,11 @@ void JniOnObserveListener::onObserveCallback(const HeaderOptions headerOptions,
             m_ownerResource->removeOnObserveListener(env, m_jwListener);
             env->Throw((jthrowable)ex);
         }
+
+        if (OC_OBSERVE_DEREGISTER == sequenceNumber)
+        {
+            checkExAndRemoveListener(env);
+        }
     }
 
     if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
index 5b2388d..8733878 100644 (file)
@@ -70,6 +70,7 @@ public final class OcPlatform {
     public static final String PRESENCE_URI = "/oic/ad";
 
     private static volatile boolean sIsPlatformInitialized = false;
+    private static QualityOfService sPlatformQualityOfService = QualityOfService.NA;
 
     private OcPlatform() {
     }
@@ -86,6 +87,8 @@ public final class OcPlatform {
         if (!sIsPlatformInitialized) {
             CaInterface.initialize(platformConfig.getContext());
 
+            sPlatformQualityOfService = platformConfig.getQualityOfService();
+
             OcPlatform.configure(
                     platformConfig.getServiceType().getValue(),
                     platformConfig.getModeType().getValue(),
@@ -463,7 +466,7 @@ public final class OcPlatform {
 
     /**
      * This API registers a resource with the server
-     * <P>
+     * <p/>
      * Note: This API applies to server & client side.
      * </P>
      *
@@ -482,9 +485,10 @@ public final class OcPlatform {
 
     /**
      * This API registers a resource with the server NOTE: This API applies to server side only.
-     * <P>
+     * <p/>
      * Note: This API applies to server side only.
      * </P>
+     *
      * @param resourceUri         The URI of the resource. Example: "a/light"
      * @param resourceTypeName    The resource type. Example: "light"
      * @param resourceInterface   The resource interface (whether it is collection etc).
@@ -933,4 +937,14 @@ public final class OcPlatform {
                     "OcPlatform.Configure before any other API calls are permitted");
         }
     }
+
+    /**
+     * Gets platform quality of service
+     *
+     * @return quality of service
+     */
+    public static QualityOfService getPlatformQualityOfService() {
+        OcPlatform.initCheck();
+        return sPlatformQualityOfService;
+    }
 }
index 333c5b2..bb22465 100644 (file)
@@ -411,7 +411,9 @@ public class OcResource {
      *
      * @throws OcException
      */
-    public native void cancelObserve() throws OcException;
+    public void cancelObserve() throws OcException{
+        this.cancelObserve(OcPlatform.getPlatformQualityOfService());
+    }
 
     /**
      * Method to cancel the observation on the resource