Change occlient to cancel observe on every registration.
authorMandeep Shetty <mandeep.shetty@intel.com>
Tue, 6 Oct 2015 00:02:47 +0000 (17:02 -0700)
committerJon A. Cruz <jonc@osg.samsung.com>
Wed, 28 Oct 2015 08:09:40 +0000 (08:09 +0000)
In the case when there are multiple servers, occlient sends out observer
requests to all of them. On reaching 15 notifications, only one of the
observation registrations were cancelled.
As the handle for cancellation was maintained in a global variable, none
of the others were cancelled and the client continued to get
notifications and this led to some failed tests.
Changed client behavior to cancel observe wit the handle present in the
response.

This can be cherry-picked to 1.0.0-dev

Change-Id: I51d28620b140dbb4f28baef7aa3be93276f6de5f
Signed-off-by: Mandeep Shetty <mandeep.shetty@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/3535
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jon A. Cruz <jonc@osg.samsung.com>
resource/csdk/stack/samples/linux/SimpleClientServer/occlient.cpp

index d1ea32e..dca270d 100644 (file)
@@ -60,8 +60,6 @@ static std::string coapServerResource = "/a/light";
 
 void StripNewLineChar(char* str);
 
-// The handle for the observe registration
-OCDoHandle gObserveDoHandle;
 #ifdef WITH_PRESENCE
 // The handle for observe registration
 OCDoHandle gPresenceHandle;
@@ -162,10 +160,6 @@ OCStackResult InvokeOCDoResource(std::ostringstream &query,
     {
         OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
     }
-    else if (method == OC_REST_OBSERVE || method == OC_REST_OBSERVE_ALL)
-    {
-        gObserveDoHandle = handle;
-    }
 #ifdef WITH_PRESENCE
     else if (method == OC_REST_PRESENCE)
     {
@@ -279,7 +273,7 @@ OCStackApplicationResult getReqCB(void* ctx, OCDoHandle /*handle*/,
     return OC_STACK_DELETE_TRANSACTION;
 }
 
-OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle /*handle*/,
+OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle handle,
                                   OCClientResponse * clientResponse)
 {
     if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
@@ -296,11 +290,12 @@ OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle /*handle*/,
         OC_LOG_PAYLOAD(INFO, clientResponse->payload);
         OC_LOG(INFO, TAG, ("=============> Obs Response"));
         gNumObserveNotifies++;
-        if (gNumObserveNotifies == 15) //large number to test observing in DELETE case.
+        if (gNumObserveNotifies > 15) //large number to test observing in DELETE case.
         {
             if (TestCase == TEST_OBS_REQ_NON || TestCase == TEST_OBS_REQ_CON)
             {
-                if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
+                OC_LOG(ERROR, TAG, "Cancelling with LOW QOS");
+                if (OCCancel (handle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
                 {
                     OC_LOG(ERROR, TAG, "Observe cancel error");
                 }
@@ -308,7 +303,8 @@ OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle /*handle*/,
             }
             else if (TestCase == TEST_OBS_REQ_NON_CANCEL_IMM)
             {
-                if (OCCancel (gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
+                OC_LOG(ERROR, TAG, "Cancelling with HIGH QOS");
+                if (OCCancel (handle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
                 {
                     OC_LOG(ERROR, TAG, "Observe cancel error");
                 }