Add WITH_TCP test in simpleserver/simpleclient for android
authorjihwan.seo <jihwan.seo@samsung.com>
Wed, 17 Aug 2016 08:41:02 +0000 (17:41 +0900)
committerAshok Babu Channa <ashok.channa@samsung.com>
Fri, 26 Aug 2016 11:52:41 +0000 (11:52 +0000)
Change-Id: Ib9e8cf1939525e125f26edcbdcf234fc24681bf3
Signed-off-by: jihwan.seo <jihwan.seo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/10547
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Ashok Babu Channa <ashok.channa@samsung.com>
android/android_api/base/jni/JniListenerManager.h
android/android_api/base/jni/JniOnObserveListener.cpp
android/examples/simpleclient/src/main/java/org/iotivity/base/examples/SimpleClient.java
android/examples/simpleserver/src/main/java/org/iotivity/base/examples/Light.java

index a1a2083..8965e85 100644 (file)
@@ -148,7 +148,6 @@ public:
                     T* listener = refPair.first;
                     delete listener;
                     m_listenerMap.erase(it);
-
                     LOGI("OnEventListener is removed");
                 }
                 break;
@@ -160,6 +159,7 @@ public:
     void removeAllListeners(JNIEnv* env)
     {
         m_mapMutex.lock();
+        LOGI("All listeners are removed");
 
         for (auto& pair : m_listenerMap)
         {
index cbf1d2d..1bb3c23 100644 (file)
@@ -27,6 +27,8 @@
 #include "JniOcAccountManager.h"
 #endif
 
+#define CA_OBSERVE_MAX_SEQUENCE_NUMBER 0xFFFFFF
+
 JniOnObserveListener::JniOnObserveListener(JNIEnv *env, jobject jListener, JniOcResource* owner)
     : m_ownerResource(owner)
 {
@@ -76,6 +78,16 @@ void JniOnObserveListener::onObserveCallback(const HeaderOptions headerOptions,
         return;
     }
 
+    if (nullptr == m_jwListener)
+    {
+        LOGE("listener is not available");
+        if (JNI_EDETACHED == envRet)
+        {
+            g_jvm->DetachCurrentThread();
+        }
+        return;
+    }
+
     jobject jListener = env->NewLocalRef(m_jwListener);
     if (!jListener)
     {
@@ -86,9 +98,11 @@ void JniOnObserveListener::onObserveCallback(const HeaderOptions headerOptions,
         }
         return;
     }
+
     jclass clsL = env->GetObjectClass(jListener);
     if (!clsL)
     {
+        env->DeleteLocalRef(jListener);
         checkExAndRemoveListener(env);
         if (JNI_EDETACHED == envRet)
         {
@@ -103,22 +117,14 @@ void JniOnObserveListener::onObserveCallback(const HeaderOptions headerOptions,
         jobject ex = GetOcException(eCode, "stack error in onObserveCallback");
         if (!ex)
         {
-            checkExAndRemoveListener(env);
-            if (JNI_EDETACHED == envRet)
-            {
-                g_jvm->DetachCurrentThread();
-            }
-            return;
+            goto JNI_EXIT;
         }
+
         jmethodID midL = env->GetMethodID(clsL, "onObserveFailed", "(Ljava/lang/Throwable;)V");
         if (!midL)
         {
-            checkExAndRemoveListener(env);
-            if (JNI_EDETACHED == envRet)
-            {
-                g_jvm->DetachCurrentThread();
-            }
-            return;
+            env->DeleteLocalRef(ex);
+            goto JNI_EXIT;
         }
         env->CallVoidMethod(jListener, midL, ex);
     }
@@ -127,12 +133,7 @@ void JniOnObserveListener::onObserveCallback(const HeaderOptions headerOptions,
         jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env, headerOptions);
         if (!jHeaderOptionList)
         {
-            checkExAndRemoveListener(env);
-            if (JNI_EDETACHED == envRet)
-            {
-                g_jvm->DetachCurrentThread();
-            }
-            return;
+            goto JNI_EXIT;
         }
 
         OCRepresentation * rep = new OCRepresentation(ocRepresentation);
@@ -142,24 +143,17 @@ void JniOnObserveListener::onObserveCallback(const HeaderOptions headerOptions,
         if (!jRepresentation)
         {
             delete rep;
-            checkExAndRemoveListener(env);
-            if (JNI_EDETACHED == envRet)
-            {
-                g_jvm->DetachCurrentThread();
-            }
-            return;
+            env->DeleteLocalRef(jHeaderOptionList);
+            goto JNI_EXIT;
         }
 
         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->DeleteLocalRef(jRepresentation);
+            env->DeleteLocalRef(jHeaderOptionList);
+            goto JNI_EXIT;
         }
 
         env->CallVoidMethod(jListener, midL, jHeaderOptionList, jRepresentation,
@@ -168,18 +162,33 @@ void JniOnObserveListener::onObserveCallback(const HeaderOptions headerOptions,
         {
             LOGE("Java exception is thrown");
             delete rep;
+            env->DeleteLocalRef(jRepresentation);
+            env->DeleteLocalRef(jHeaderOptionList);
             jthrowable ex = env->ExceptionOccurred();
             env->ExceptionClear();
             m_ownerResource->removeOnObserveListener(env, m_jwListener);
             env->Throw((jthrowable)ex);
         }
 
-        if (OC_OBSERVE_DEREGISTER == sequenceNumber)
+        if (CA_OBSERVE_MAX_SEQUENCE_NUMBER + 1 == sequenceNumber)
         {
-            checkExAndRemoveListener(env);
+            LOGI("Observe De-registration action is successful");
+            goto JNI_EXIT;
         }
     }
 
+    env->DeleteLocalRef(clsL);
+    env->DeleteLocalRef(jListener);
+    if (JNI_EDETACHED == envRet)
+    {
+        g_jvm->DetachCurrentThread();
+    }
+    return;
+
+JNI_EXIT:
+    env->DeleteLocalRef(clsL);
+    env->DeleteLocalRef(jListener);
+    checkExAndRemoveListener(env);
     if (JNI_EDETACHED == envRet)
     {
         g_jvm->DetachCurrentThread();
@@ -188,6 +197,7 @@ void JniOnObserveListener::onObserveCallback(const HeaderOptions headerOptions,
 
 void JniOnObserveListener::checkExAndRemoveListener(JNIEnv* env)
 {
+    LOGI("checkExAndRemoveListener");
     if (env->ExceptionCheck())
     {
         jthrowable ex = env->ExceptionOccurred();
index 1ed9123..849cdc1 100644 (file)
@@ -70,12 +70,19 @@ public class SimpleClient extends Activity implements
     private OcResource mFoundLightResource = null;
     //local representation of a server's light resource
     private Light mLight = new Light();
+    //variables related observer
+    private int maxSequenceNumber = 0xFFFFFF;
+    private OcConnectivityType adapterFlag = OcConnectivityType.CT_ADAPTER_IP;
+    //flags related TCP transport test
+    private boolean isRequestFlag = false;
+    private boolean isTCPContained = false;
 
     /**
      * A local method to configure and initialize platform, and then search for the light resources.
      */
-    private void startSimpleClient() {
+    private void startSimpleClient(OcConnectivityType type) {
         Context context = this;
+        adapterFlag = type;
 
         PlatformConfig platformConfig = new PlatformConfig(
                 this,
@@ -138,8 +145,19 @@ public class SimpleClient extends Activity implements
         }
 
         if (null != mFoundLightResource) {
+            if (ocResource.getUri().equals("/a/light")) {
+                if (ocResource.getConnectivityTypeSet().contains(OcConnectivityType.CT_ADAPTER_TCP)) {
+                    msg("Found resource which has TCP transport");
+                    if (isTCPContained == false)
+                    {
+                        isTCPContained = true;
+                        return;
+                    }
+                }
+            }
             msg("Found another resource, ignoring");
             return;
+
         }
         // Get the resource URI
         String resourceUri = ocResource.getUri();
@@ -166,10 +184,24 @@ public class SimpleClient extends Activity implements
         if (resourceUri.equals("/a/light")) {
             //Assign resource reference to a global variable to keep it from being
             //destroyed by the GC when it is out of scope.
-            mFoundLightResource = ocResource;
-
-            // Call a local method which will internally invoke "get" API on the foundLightResource
-            getLightResourceRepresentation();
+            if (OcConnectivityType.CT_ADAPTER_TCP == adapterFlag)
+            {
+                if (ocResource.getConnectivityTypeSet().contains(OcConnectivityType.CT_ADAPTER_TCP))
+                {
+                    msg("set mFoundLightResource which has TCP transport");
+                    mFoundLightResource = ocResource;
+                    // Call a local method which will internally invoke "get" API
+                    getLightResourceRepresentation();
+                    return;
+                }
+            }
+            else
+            {
+                msg("set mFoundLightResource which has UDP transport");
+                mFoundLightResource = ocResource;
+                // Call a local method which will internally invoke "get" API on the foundLightResource
+                getLightResourceRepresentation();
+            }
         }
     }
 
@@ -478,37 +510,46 @@ public class SimpleClient extends Activity implements
     public synchronized void onObserveCompleted(List<OcHeaderOption> list,
                                                 OcRepresentation ocRepresentation,
                                                 int sequenceNumber) {
-        if (OcResource.OnObserveListener.REGISTER == sequenceNumber) {
-            msg("Observe registration action is successful:");
-        } else if (OcResource.OnObserveListener.DEREGISTER == sequenceNumber) {
-            msg("Observe De-registration action is successful");
-        } else if (OcResource.OnObserveListener.NO_OPTION == sequenceNumber) {
-            msg("Observe registration or de-registration action is failed");
-        }
-
-        msg("OBSERVE Result:");
-        msg("\tSequenceNumber:" + sequenceNumber);
-        try {
-            mLight.setOcRepresentation(ocRepresentation);
-        } catch (OcException e) {
-            Log.e(TAG, e.toString());
-            msg("Failed to get the attribute values");
-        }
-        msg(mLight.toString());
 
-        if ((++mObserveCount) == 11) {
-            msg("Cancelling Observe...");
+        if (sequenceNumber != maxSequenceNumber + 1)
+        {
+            msg("OBSERVE Result:");
+            msg("\tSequenceNumber:" + sequenceNumber);
             try {
-                mFoundLightResource.cancelObserve();
+                mLight.setOcRepresentation(ocRepresentation);
             } catch (OcException e) {
                 Log.e(TAG, e.toString());
-                msg("Error occurred while invoking \"cancelObserve\" API");
+                msg("Failed to get the attribute values");
             }
-            msg("DONE");
+            msg(mLight.toString());
+
+            if ((++mObserveCount) == 11) {
+                msg("Cancelling Observe...");
+                try {
+                    mFoundLightResource.cancelObserve(QualityOfService.HIGH);
+                } catch (OcException e) {
+                    Log.e(TAG, e.toString());
+                    msg("Error occurred while invoking \"cancelObserve\" API");
+                }
 
-            //prepare for the next restart of the SimpleClient
-            resetGlobals();
-            enableStartButton();
+                sleep(10);
+                resetGlobals();
+                if (true == isTCPContained && false == isRequestFlag)
+                {
+                    msg("Start TCP test...");
+                    startSimpleClient(OcConnectivityType.CT_ADAPTER_TCP);
+                    isRequestFlag = true;
+                    return;
+                } else if (true == isRequestFlag)
+                {
+                    msg("End TCP test...");
+                    isRequestFlag = false;
+                }
+
+                msg("DONE");
+                //prepare for the next restart of the SimpleClient
+                enableStartButton();
+            }
         }
     }
 
@@ -555,7 +596,8 @@ public class SimpleClient extends Activity implements
                     button.setEnabled(false);
                     new Thread(new Runnable() {
                         public void run() {
-                            startSimpleClient();
+                            isTCPContained = false;
+                            startSimpleClient(OcConnectivityType.CT_ADAPTER_IP);
                         }
                     }).start();
                 }
index 23ee5db..bb7107e 100644 (file)
@@ -286,6 +286,7 @@ public class Light implements OcPlatform.EntityHandler {
                 ErrorCode errorCode = e.getErrorCode();
                 if (ErrorCode.NO_OBSERVERS == errorCode) {
                     msg("No more observers, stopping notifications");
+                    mObserverNotifier = null;
                 }
                 return;
             }