replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceClient / RCSRemoteResourceObject.cpp
index e909261..bc95aa9 100644 (file)
@@ -92,12 +92,40 @@ namespace
     }
 
     OCStackResult cachingCallback(std::shared_ptr< PrimitiveResource >,
-            const RCSResourceAttributes& data,
-            RCSRemoteResourceObject::CacheUpdatedCallback onCacheUpdated)
+            const RCSResourceAttributes& data, int eCode,
+            RCSRemoteResourceObject::CacheUpdatedCallback onCacheUpdated,
+            std::weak_ptr<RCSRemoteResourceObject> resourcePtr)
     {
         SCOPE_LOG_F(DEBUG, TAG);
 
-        onCacheUpdated(data);
+        //If error code is failure then RE Cache module should
+        //do clean up for caching flags, maps etc.
+        if(eCode > 4)
+        {
+            OIC_LOG_V(ERROR, TAG, "Error code: %d",eCode);
+            try
+            {
+                std::shared_ptr<RCSRemoteResourceObject> resource = resourcePtr.lock();
+                if(resource)
+                {
+                    resource->stopCaching();
+                }
+                else
+                {
+                    OIC_LOG(ERROR, TAG, "Resource object is null");
+                }
+            }
+            catch(...)
+            {
+                //Exception will be thrown: stack will return OC_STACK_ERROR
+                // if it already stopped observe. This call is reqired for clearing
+                //Cache manager.
+                OIC_LOG(DEBUG, TAG, "Cleared Cache");
+            }
+        }
+
+        //Calling application callback
+        onCacheUpdated(data, eCode);
         return OC_STACK_OK;
     }
 
@@ -211,7 +239,7 @@ namespace OIC
 
             OC::OCResource::Ptr ocResource = OC::OCPlatform::constructResourceObject(rcsResource->getAddress(),
                 rcsResource->getUri(),
-                CT_DEFAULT,
+                rcsResource->m_primitiveResource->getConnectivityType(),
                 rcsResource->isObservable(),
                 rcsResource->getTypes(),
                 rcsResource->getInterfaces());
@@ -299,8 +327,9 @@ namespace OIC
             {
                 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
                         m_primitiveResource,
-                        std::bind(cachingCallback, std::placeholders::_1, std::placeholders::_2,
-                                  std::move(cb)), CACHE_METHOD::OBSERVE_ONLY,
+                        std::bind(cachingCallback, std::placeholders::_1,
+                                  std::placeholders::_2, std::placeholders::_3,
+                                  std::move(cb), shared_from_this()), CACHE_METHOD::OBSERVE_ONLY,
                                   REPORT_FREQUENCY::UPTODATE, 0);
             }
 
@@ -308,8 +337,9 @@ namespace OIC
             {
                 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
                         m_primitiveResource,
-                        std::bind(cachingCallback, std::placeholders::_1, std::placeholders::_2,
-                                std::move(cb)), CACHE_METHOD::ITERATED_GET,
+                        std::bind(cachingCallback, std::placeholders::_1,
+                                std::placeholders::_2, std::placeholders::_3,
+                                std::move(cb), shared_from_this()), CACHE_METHOD::ITERATED_GET,
                                 REPORT_FREQUENCY::UPTODATE, 0);
             }
             else
@@ -332,7 +362,19 @@ namespace OIC
                 return;
             }
 
-            ResourceCacheManager::getInstance()->cancelResourceCache(m_cacheId);
+            try
+            {
+                ResourceCacheManager::getInstance()->cancelResourceCache(m_cacheId);
+            }
+            catch (const RCSInvalidParameterException &)
+            {
+                throw;
+            }
+            catch (...)
+            {
+                m_cacheId = 0;
+                throw;
+            }
             m_cacheId = 0;
         }