Add api about non-iterated request get at cache.
authorKIM JungYong <jyong2.kim@samsung.com>
Mon, 7 Nov 2016 07:46:09 +0000 (16:46 +0900)
committerUze Choi <uzchoi@samsung.com>
Tue, 8 Nov 2016 08:28:53 +0000 (08:28 +0000)
Current data update logic on RE cache is subscribing and also polling get request to resource server.
but it is causes additional communication load.
Additional api will provide a disabled polling req for reducing communication loads.

void startCaching(CacheUpdatedCallback cb, CacheMode mode); // added api
if mode on param is OBSERVE_ONLY, polling req will be disabled.
otherwise, cache will polling req and subscribing.

And unused function is removed.

Change-Id: Ifa5fd5a21dc10a0e412a8cde0370413a362603ca
Signed-off-by: KIM JungYong <jyong2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/14065
Reviewed-by: Uze Choi <uzchoi@samsung.com>
Tested-by: Uze Choi <uzchoi@samsung.com>
service/resource-encapsulation/SConscript
service/resource-encapsulation/include/RCSRemoteResourceObject.h
service/resource-encapsulation/src/resourceCache/include/CacheTypes.h
service/resource-encapsulation/src/resourceCache/include/ObserveCache.h [new file with mode: 0644]
service/resource-encapsulation/src/resourceCache/include/ResourceCacheManager.h
service/resource-encapsulation/src/resourceCache/src/ObserveCache.cpp [new file with mode: 0644]
service/resource-encapsulation/src/resourceCache/src/ResourceCacheManager.cpp
service/resource-encapsulation/src/resourceCache/unittests/ResourceCacheTest.cpp
service/resource-encapsulation/src/resourceClient/RCSRemoteResourceObject.cpp

index 16e4f06..35c8d6d 100644 (file)
@@ -71,6 +71,10 @@ if target_os in ['android']:
     resourceClient_env.AppendUnique(CXXFLAGS = ['-frtti', '-fexceptions'])
     resourceClient_env.PrependUnique(LIBS = ['gnustl_shared', 'log'])
 
+if target_os in ['linux']:
+    if not env.get('RELEASE'):
+        resourceClient_env.PrependUnique(LIBS = ['gcov'])
+        resourceClient_env.AppendUnique(CXXFLAGS = ['--coverage'])
 
 ######################################################################
 # Source files and Targets
@@ -86,6 +90,7 @@ client_src = [
         BROKER_SRC_DIR + 'ResourceBroker.cpp',
         CACHE_SRC_DIR + 'DataCache.cpp',
         CACHE_SRC_DIR + 'ResourceCacheManager.cpp',
+        CACHE_SRC_DIR + 'ObserveCache.cpp',
         RESOURCECLIENT_DIR + 'RCSDiscoveryManager.cpp',
         RESOURCECLIENT_DIR + 'RCSDiscoveryManagerImpl.cpp',
         RESOURCECLIENT_DIR + 'RCSRemoteResourceObject.cpp'
index 5882c0f..935c66f 100644 (file)
@@ -66,6 +66,12 @@ namespace OIC
             LOST_SIGNAL, /**< Failed to reach the resource. */
         };
 
+        enum class CacheMode
+        {
+            OBSERVE_ONLY,
+            OBSERVE_WITH_POLLING
+        };
+
         /**
          * The states of monitoring.
          *
@@ -343,6 +349,7 @@ namespace OIC
              * updates the cached data accordingly.
              *
              * @param cb If non-empty function, it will be invoked whenever the cache updated.
+             * @param mode if CacheMode is OBSERVE_ONLY, it will be invoked when receive observe response only.
              *
              * @throws BadRequestException If caching is already started.
              *
@@ -355,7 +362,7 @@ namespace OIC
              * @see getCachedAttribute(const std::string&) const
              *
              */
-            void startCaching(CacheUpdatedCallback cb);
+            void startCaching(CacheUpdatedCallback cb, CacheMode mode = CacheMode::OBSERVE_WITH_POLLING);
 
             /**
              * Stops caching.
index fa08cc8..8c21cdc 100644 (file)
@@ -73,6 +73,12 @@ namespace OIC
             FREQUENCY
         };
 
+        enum class CACHE_METHOD
+        {
+            OBSERVE_ONLY,
+            ITERATED_GET
+        };
+
         typedef int CacheID;
 
         typedef std::function<OCStackResult(std::shared_ptr<PrimitiveResource>,
diff --git a/service/resource-encapsulation/src/resourceCache/include/ObserveCache.h b/service/resource-encapsulation/src/resourceCache/include/ObserveCache.h
new file mode 100644 (file)
index 0000000..a30e8b6
--- /dev/null
@@ -0,0 +1,93 @@
+//******************************************************************
+//
+// Copyright 2016 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#ifndef RCM_OBSERVECACHE_H_
+#define RCM_OBSERVECACHE_H_
+
+#include <list>
+#include <string>
+#include <memory>
+#include <mutex>
+#include <atomic>
+
+#include "CacheTypes.h"
+#include "PrimitiveResource.h"
+#include "ExpiryTimer.h"
+
+namespace OIC
+{
+    namespace Service
+    {
+        class ObserveCache : public std::enable_shared_from_this<ObserveCache>
+        {
+            public:
+                typedef std::function<OCStackResult(std::shared_ptr<PrimitiveResource>,
+                                            const RCSResourceAttributes &)> DataCacheCB;
+                typedef std::shared_ptr<ObserveCache> Ptr;
+
+            public:
+                ObserveCache(std::weak_ptr<PrimitiveResource> pResource);
+                ~ObserveCache() = default;
+
+                ObserveCache(const ObserveCache &) = delete;
+                ObserveCache(ObserveCache &&) = delete;
+                ObserveCache & operator = (const ObserveCache &) = delete;
+                ObserveCache & operator = (ObserveCache &&) = delete;
+
+                void startCache(DataCacheCB func);
+                void stopCache();
+
+                CACHE_STATE getCacheState() const;
+
+                RCSResourceAttributes getCachedData() const;
+
+                bool isCachedData() const;
+                bool isStartCache() const;
+
+            private:
+                typedef unsigned int TimerID;
+                typedef std::weak_ptr<PrimitiveResource> weakPrimitiveResource;
+                typedef std::weak_ptr<ObserveCache> weakDataCache;
+
+                // resource instance
+                weakPrimitiveResource m_wpResource;
+
+                // cached data info
+                RCSResourceAttributes m_attributes;
+                CACHE_STATE m_state;
+
+                DataCacheCB m_reportCB;
+
+                std::atomic<bool> m_isStart;
+
+                CacheID m_id;
+
+            private:
+                static void verifyObserveCB(const HeaderOptions &_hos,
+                                            const ResponseStatement &_rep, int _result,
+                                            unsigned int _seq, weakDataCache ptr);
+                void onObserve(const HeaderOptions &_hos,
+                               const ResponseStatement &_rep, int _result, unsigned int _seq);
+                bool convertOCResultToSuccess(OCStackResult ret);
+        };
+    } // namespace Service
+} // namespace OIC
+
+#endif /* RCM_DATACACHE_H_ */
index 23a3a45..ae02cac 100644 (file)
@@ -28,6 +28,7 @@
 
 #include "CacheTypes.h"
 #include "DataCache.h"
+#include "ObserveCache.h"
 
 namespace OIC
 {
@@ -36,12 +37,6 @@ namespace OIC
         class ResourceCacheManager
         {
             public:
-                class InvalidParameterException: public RCSException
-                {
-                    public:
-                        InvalidParameterException(std::string &&what)
-                            : RCSException { std::move(what) } {}
-                };
                 class HasNoCachedDataException: public RCSException
                 {
                     public:
@@ -54,26 +49,23 @@ namespace OIC
                 // throw InvalidParameterException;
                 CacheID requestResourceCache(
                     PrimitiveResourcePtr pResource, CacheCB func = NULL,
+                    CACHE_METHOD cm = CACHE_METHOD::ITERATED_GET,
                     REPORT_FREQUENCY rf = REPORT_FREQUENCY::NONE, long time = 0l);
 
                 // throw InvalidParameterException;
                 void cancelResourceCache(CacheID id);
 
                 // throw InvalidParameterException;
-                void updateResourceCache(PrimitiveResourcePtr pResource) const;
                 void updateResourceCache(CacheID id) const;
 
                 // throw InvalidParameterException;
                 // throw HasNoCachedDataException;
-                const RCSResourceAttributes getCachedData(PrimitiveResourcePtr pResource) const;
                 const RCSResourceAttributes getCachedData(CacheID id) const;
 
                 // throw InvalidParameterException;
-                CACHE_STATE getResourceCacheState(PrimitiveResourcePtr pResource) const;
                 CACHE_STATE getResourceCacheState(CacheID id) const;
 
                 // throw InvalidParameterException;
-                bool isCachedData(PrimitiveResourcePtr pResource) const;
                 bool isCachedData(CacheID id) const;
 
             private:
@@ -83,6 +75,9 @@ namespace OIC
                 static std::unique_ptr<std::list<DataCachePtr>> s_cacheDataList;
                 std::map<CacheID, DataCachePtr> cacheIDmap;
 
+                std::list<ObserveCache::Ptr> m_observeCacheList;
+                std::map<CacheID, ObserveCache::Ptr> observeCacheIDmap;
+
                 ResourceCacheManager() = default;
                 ~ResourceCacheManager();
                 ResourceCacheManager(const ResourceCacheManager &) = delete;
diff --git a/service/resource-encapsulation/src/resourceCache/src/ObserveCache.cpp b/service/resource-encapsulation/src/resourceCache/src/ObserveCache.cpp
new file mode 100644 (file)
index 0000000..132c01c
--- /dev/null
@@ -0,0 +1,148 @@
+//******************************************************************
+//
+// Copyright 2015 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#include "ObserveCache.h"
+#include "RCSException.h"
+
+namespace OIC
+{
+    namespace Service
+    {
+        ObserveCache::ObserveCache(std::weak_ptr<PrimitiveResource> pResource)
+        : m_wpResource(pResource), m_attributes(), m_state(CACHE_STATE::NONE),
+          m_reportCB(), m_isStart(false), m_id(0)
+        {
+        }
+
+        void ObserveCache::startCache(DataCacheCB func)
+        {
+            if (m_isStart)
+            {
+                throw RCSBadRequestException{ "Caching already started." };
+            }
+
+            m_reportCB = std::move(func);
+
+            auto resource = m_wpResource.lock();
+            if (resource == nullptr)
+            {
+                m_reportCB = nullptr;
+                throw RCSBadRequestException{ "Resource was not initialized." };
+            }
+
+            if (resource->isObservable())
+            {
+                resource->requestObserve(
+                        std::bind(&ObserveCache::verifyObserveCB,
+                                  std::placeholders::_1, std::placeholders::_2,
+                                  std::placeholders::_3, std::placeholders::_4,
+                                  shared_from_this()));
+            }
+            else
+            {
+                throw RCSBadRequestException{ "Can't observe, Never updated data." };
+            }
+
+            m_isStart = true;
+            m_state = CACHE_STATE::READY_YET;
+        }
+
+        void ObserveCache::stopCache()
+        {
+            auto resource = m_wpResource.lock();
+            if (resource != nullptr)
+            {
+                resource->cancelObserve();
+            }
+
+            m_reportCB = nullptr;
+            m_state = CACHE_STATE::NONE;
+
+            m_isStart = false;
+
+        }
+
+        CACHE_STATE ObserveCache::getCacheState() const
+        {
+            return m_state;
+        }
+
+        RCSResourceAttributes ObserveCache::getCachedData() const
+        {
+            return m_attributes;
+        }
+
+        bool ObserveCache::isCachedData() const
+        {
+            return !m_attributes.empty();
+        }
+
+        bool ObserveCache::isStartCache() const
+        {
+            return m_isStart;
+        }
+
+        void ObserveCache::onObserve(const HeaderOptions &,
+                       const ResponseStatement & rep, int _result, unsigned int)
+        {
+            if (!convertOCResultToSuccess((OCStackResult)_result))
+            {
+                return;
+            }
+
+            m_state = CACHE_STATE::READY;
+
+            if (m_attributes == rep.getAttributes())
+            {
+                return ;
+            }
+
+            m_attributes = rep.getAttributes();
+            m_reportCB(m_wpResource.lock(), m_attributes);
+        }
+
+        void ObserveCache::verifyObserveCB(const HeaderOptions &_hos,
+                                    const ResponseStatement &_rep, int _result,
+                                    unsigned int _seq, weakDataCache wPtr)
+        {
+            auto ptr = wPtr.lock();
+            if (ptr)
+            {
+                ptr->onObserve(_hos, _rep, _result, _seq);
+            }
+        }
+
+        bool ObserveCache::convertOCResultToSuccess(OCStackResult ret)
+        {
+            switch (ret)
+            {
+                case OC_STACK_OK:
+                case OC_STACK_RESOURCE_CREATED:
+                case OC_STACK_RESOURCE_DELETED:
+                case OC_STACK_PRESENCE_STOPPED:
+                case OC_STACK_CONTINUE:
+                case OC_STACK_RESOURCE_CHANGED:
+                    return true;
+                default:
+                    return false;
+            }
+        }
+    }
+}
index e53608b..c57c7f0 100644 (file)
@@ -19,6 +19,8 @@
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
 #include "ResourceCacheManager.h"
+#include "RCSException.h"
+#include "ocrandom.h"
 
 namespace OIC
 {
@@ -54,21 +56,43 @@ namespace OIC
         }
 
         CacheID ResourceCacheManager::requestResourceCache(
-            PrimitiveResourcePtr pResource, CacheCB func,
+            PrimitiveResourcePtr pResource, CacheCB func, CACHE_METHOD cm,
             REPORT_FREQUENCY rf, long reportTime)
         {
             if (pResource == nullptr)
             {
-                throw InvalidParameterException {"[requestResourceCache] Primitive Resource is invaild"};
+                throw RCSInvalidParameterException {"[requestResourceCache] Primitive Resource is invaild"};
             }
 
             CacheID retID = 0;
 
+            if (cm == CACHE_METHOD::OBSERVE_ONLY)
+            {
+                if (func == NULL || func == nullptr)
+                {
+                    throw RCSInvalidParameterException {"[requestResourceCache] CacheCB is invaild"};
+                }
+
+                std::lock_guard<std::mutex> lock(s_mutex);
+                retID = OCGetRandom();
+                while(observeCacheIDmap.find(retID) != observeCacheIDmap.end())
+                {
+                    retID = OCGetRandom();
+                }
+
+                auto newHandler = std::make_shared<ObserveCache>(pResource);
+                newHandler->startCache(std::move(func));
+                m_observeCacheList.push_back(newHandler);
+
+                observeCacheIDmap.insert(std::make_pair(retID, newHandler));
+                return retID;
+            }
+
             if (rf != REPORT_FREQUENCY::NONE)
             {
                 if (func == NULL || func == nullptr)
                 {
-                    throw InvalidParameterException {"[requestResourceCache] CacheCB is invaild"};
+                    throw RCSInvalidParameterException {"[requestResourceCache] CacheCB is invaild"};
                 }
                 if (!reportTime)
                 {
@@ -94,9 +118,20 @@ namespace OIC
 
         void ResourceCacheManager::cancelResourceCache(CacheID id)
         {
-            if (id == 0 || cacheIDmap.find(id) == cacheIDmap.end())
+            auto observeIns = observeCacheIDmap.find(id);
+            auto dataCacheIns = cacheIDmap.find(id);
+            if ((dataCacheIns == cacheIDmap.end() && observeIns == observeCacheIDmap.end())
+                || id == 0)
             {
-                throw InvalidParameterException {"[cancelResourceCache] CacheID is invaild"};
+                throw RCSInvalidParameterException {"[cancelResourceCache] CacheID is invaild"};
+            }
+
+            if (observeIns != observeCacheIDmap.end())
+            {
+                (observeIns->second)->stopCache();
+                (observeIns->second).reset();
+                observeCacheIDmap.erase(observeIns);
+                return;
             }
 
             DataCachePtr foundCacheHandler = findDataCache(id);
@@ -115,71 +150,38 @@ namespace OIC
             }
         }
 
-        void ResourceCacheManager::updateResourceCache(PrimitiveResourcePtr pResource) const
-        {
-            if (pResource == nullptr)
-            {
-                throw InvalidParameterException
-                {"[updateResourceCache] Primitive Resource is invaild"};
-            }
-
-            DataCachePtr foundCache = findDataCache(pResource);
-            if (foundCache == nullptr)
-            {
-                throw InvalidParameterException
-                {"[updateResourceCache] Primitive Resource is invaild"};
-            }
-            foundCache->requestGet();
-        }
-
         void ResourceCacheManager::updateResourceCache(CacheID updateId) const
         {
             if (updateId == 0)
             {
-                throw InvalidParameterException {"[getCachedData] CacheID is NULL"};
+                throw RCSInvalidParameterException {"[getCachedData] CacheID is NULL"};
             }
 
             DataCachePtr foundCache = findDataCache(updateId);
             if (foundCache == nullptr)
             {
-                throw InvalidParameterException {"[getCachedData] CacheID is invaild"};
+                throw RCSInvalidParameterException {"[getCachedData] CacheID is invaild"};
             }
             foundCache->requestGet();
         }
 
-        const RCSResourceAttributes ResourceCacheManager::getCachedData(
-            PrimitiveResourcePtr pResource) const
+        const RCSResourceAttributes ResourceCacheManager::getCachedData(CacheID id) const
         {
-            if (pResource == nullptr)
-            {
-                throw InvalidParameterException {"[getCachedData] Primitive Resource is nullptr"};
-            }
-
-            DataCachePtr handler = findDataCache(pResource);
-            if (handler == nullptr)
-            {
-                throw InvalidParameterException {"[getCachedData] Primitive Resource is invaild"};
-            }
-
-            if (handler->isCachedData() == false)
+            if (id == 0)
             {
-                throw HasNoCachedDataException {"[getCachedData] Cached Data is not stored"};
+                throw RCSInvalidParameterException {"[getCachedData] CacheID is NULL"};
             }
 
-            return handler->getCachedData();
-        }
-
-        const RCSResourceAttributes ResourceCacheManager::getCachedData(CacheID id) const
-        {
-            if (id == 0)
+            auto observePtr = observeCacheIDmap.find(id);
+            if (observePtr != observeCacheIDmap.end())
             {
-                throw InvalidParameterException {"[getCachedData] CacheID is NULL"};
+                return (observePtr->second)->getCachedData();
             }
 
             DataCachePtr handler = findDataCache(id);
             if (handler == nullptr)
             {
-                throw InvalidParameterException {"[getCachedData] CacheID is invaild"};
+                throw RCSInvalidParameterException {"[getCachedData] CacheID is invaild"};
             }
 
             if (handler->isCachedData() == false)
@@ -190,27 +192,17 @@ namespace OIC
             return handler->getCachedData();
         }
 
-        CACHE_STATE ResourceCacheManager::getResourceCacheState(
-            PrimitiveResourcePtr pResource) const
+        CACHE_STATE ResourceCacheManager::getResourceCacheState(CacheID id) const
         {
-            if (pResource == nullptr)
-            {
-                throw InvalidParameterException {"[getResourceCacheState] Primitive Resource is nullptr"};
-            }
-
-            DataCachePtr handler = findDataCache(pResource);
-            if (handler == nullptr)
+            if (id == 0)
             {
-                return CACHE_STATE::NONE;
+                throw RCSInvalidParameterException {"[getResourceCacheState] CacheID is NULL"};
             }
-            return handler->getCacheState();
-        }
 
-        CACHE_STATE ResourceCacheManager::getResourceCacheState(CacheID id) const
-        {
-            if (id == 0)
+            auto observePtr = observeCacheIDmap.find(id);
+            if (observePtr != observeCacheIDmap.end())
             {
-                throw InvalidParameterException {"[getResourceCacheState] CacheID is NULL"};
+                return (observePtr->second)->getCacheState();
             }
 
             DataCachePtr handler = findDataCache(id);
@@ -221,32 +213,23 @@ namespace OIC
             return handler->getCacheState();
         }
 
-        bool ResourceCacheManager::isCachedData(PrimitiveResourcePtr pResource) const
+        bool ResourceCacheManager::isCachedData(CacheID id) const
         {
-            if (pResource == nullptr)
-            {
-                throw InvalidParameterException {"[isCachedData] Primitive Resource is nullptr"};
-            }
-
-            DataCachePtr handler = findDataCache(pResource);
-            if (handler == nullptr)
+            if (id == 0)
             {
-                throw InvalidParameterException {"[isCachedData] Primitive Resource is invaild"};
+                throw RCSInvalidParameterException {"[isCachedData] CacheID is NULL"};
             }
-            return handler->isCachedData();
-        }
 
-        bool ResourceCacheManager::isCachedData(CacheID id) const
-        {
-            if (id == 0)
+            auto observePtr = observeCacheIDmap.find(id);
+            if (observePtr != observeCacheIDmap.end())
             {
-                throw InvalidParameterException {"[isCachedData] CacheID is NULL"};
+                return (observePtr->second)->isCachedData();
             }
 
             DataCachePtr handler = findDataCache(id);
             if (handler == nullptr)
             {
-                throw InvalidParameterException {"[isCachedData] CacheID is invaild"};
+                throw RCSInvalidParameterException {"[isCachedData] CacheID is invaild"};
             }
             return handler->isCachedData();
         }
index 43bfe41..b3a2bfd 100644 (file)
@@ -64,10 +64,11 @@ TEST_F(ResourceCacheManagerTest, requestResourceCache_resourceIsNULL)
 
     CacheCB func = cb;
     REPORT_FREQUENCY rf = REPORT_FREQUENCY::UPTODATE;
+    CACHE_METHOD cm = CACHE_METHOD::ITERATED_GET;
     long reportTime = 20l;
 
-    ASSERT_THROW(cacheInstance->requestResourceCache(NULL, func, rf, reportTime),
-                 ResourceCacheManager::InvalidParameterException);
+    ASSERT_THROW(cacheInstance->requestResourceCache(NULL, func, cm, rf, reportTime),
+                 RCSInvalidParameterException);
 }
 
 TEST_F(ResourceCacheManagerTest, requestResourceCache_cacheCBIsNULL)
@@ -75,10 +76,11 @@ TEST_F(ResourceCacheManagerTest, requestResourceCache_cacheCBIsNULL)
 
     CacheCB func = NULL;
     REPORT_FREQUENCY rf = REPORT_FREQUENCY::UPTODATE;
+    CACHE_METHOD cm = CACHE_METHOD::ITERATED_GET;
     long reportTime = 20l;
 
-    ASSERT_THROW(cacheInstance->requestResourceCache(pResource, func, rf, reportTime),
-                 ResourceCacheManager::InvalidParameterException);
+    ASSERT_THROW(cacheInstance->requestResourceCache(pResource, func, cm, rf, reportTime),
+                 RCSInvalidParameterException);
 }
 
 TEST_F(ResourceCacheManagerTest, requestResourceCache_reportTimeIsNULL)
@@ -91,8 +93,9 @@ TEST_F(ResourceCacheManagerTest, requestResourceCache_reportTimeIsNULL)
 
     CacheCB func = cb;
     REPORT_FREQUENCY rf = REPORT_FREQUENCY::UPTODATE;
+    CACHE_METHOD cm = CACHE_METHOD::ITERATED_GET;
 
-    id = cacheInstance->requestResourceCache(pResource, func, rf);
+    id = cacheInstance->requestResourceCache(pResource, func, cm, rf);
     cacheInstance->cancelResourceCache(id);
 
     ASSERT_NE(id, 0);
@@ -109,9 +112,10 @@ TEST_F(ResourceCacheManagerTest, requestResourceCache_normalCase)
 
     CacheCB func = cb;
     REPORT_FREQUENCY rf = REPORT_FREQUENCY::UPTODATE;
+    CACHE_METHOD cm = CACHE_METHOD::ITERATED_GET;
     long reportTime = 20l;
 
-    id = cacheInstance->requestResourceCache(pResource, func, rf, reportTime);
+    id = cacheInstance->requestResourceCache(pResource, func, cm, rf, reportTime);
     cacheInstance->cancelResourceCache(id);
 
     ASSERT_NE(id, 0);
@@ -121,7 +125,7 @@ TEST_F(ResourceCacheManagerTest, cancelResourceCache_cacheIDIsZero)
 {
 
     ASSERT_THROW(cacheInstance->cancelResourceCache(0),
-                 ResourceCacheManager::InvalidParameterException);
+                 RCSInvalidParameterException);
 }
 
 TEST_F(ResourceCacheManagerTest, cancelResourceCache_normalCase)
@@ -134,46 +138,10 @@ TEST_F(ResourceCacheManagerTest, cancelResourceCache_normalCase)
 
     CacheCB func = cb;
     REPORT_FREQUENCY rf = REPORT_FREQUENCY::UPTODATE;
+    CACHE_METHOD cm = CACHE_METHOD::ITERATED_GET;
     long reportTime = 20l;
 
-    id = cacheInstance->requestResourceCache(pResource, func, rf, reportTime);
-
-    cacheInstance->cancelResourceCache(id);
-}
-
-TEST_F(ResourceCacheManagerTest, updateResourceCachePrimitiveResource_resourceIsNULL)
-{
-
-    pResource = NULL;
-
-    ASSERT_THROW(cacheInstance->updateResourceCache(pResource),
-                 ResourceCacheManager::InvalidParameterException);
-}
-
-TEST_F(ResourceCacheManagerTest, updateResourceCachePrimitiveResource_cacheIsNULL)
-{
-
-    ASSERT_THROW(cacheInstance->updateResourceCache(pResource),
-                 ResourceCacheManager::InvalidParameterException);
-}
-
-TEST_F(ResourceCacheManagerTest, updateResourceCachePrimitiveResource_normalCase)
-{
-
-    mocks.OnCall(pResource.get(), PrimitiveResource::requestGet);
-    mocks.OnCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
-    mocks.OnCall(pResource.get(), PrimitiveResource::requestObserve);
-    mocks.OnCall(pResource.get(), PrimitiveResource::getUri).Return("testUri");
-    mocks.OnCall(pResource.get(), PrimitiveResource::getHost).Return("testHost");
-    mocks.OnCall(pResource.get(), PrimitiveResource::cancelObserve);
-
-    CacheCB func = cb;
-    REPORT_FREQUENCY rf = REPORT_FREQUENCY::UPTODATE;
-    long reportTime = 20l;
-
-    id = cacheInstance->requestResourceCache(pResource, func, rf, reportTime);
-
-    cacheInstance->updateResourceCache(pResource);
+    id = cacheInstance->requestResourceCache(pResource, func, cm, rf, reportTime);
 
     cacheInstance->cancelResourceCache(id);
 }
@@ -182,14 +150,14 @@ TEST_F(ResourceCacheManagerTest, updateResourceCacheCacheID_cacheIDIsZero)
 {
 
     ASSERT_THROW(cacheInstance->updateResourceCache(0),
-                 ResourceCacheManager::InvalidParameterException);
+                 RCSInvalidParameterException);
 }
 
 TEST_F(ResourceCacheManagerTest, updateResourceCacheCacheID_cacheIsNULL)
 {
 
     ASSERT_THROW(cacheInstance->updateResourceCache(id),
-                 ResourceCacheManager::InvalidParameterException);
+                 RCSInvalidParameterException);
 }
 
 TEST_F(ResourceCacheManagerTest, updateResourceCacheCacheID_normalCase)
@@ -204,85 +172,33 @@ TEST_F(ResourceCacheManagerTest, updateResourceCacheCacheID_normalCase)
 
     CacheCB func = cb;
     REPORT_FREQUENCY rf = REPORT_FREQUENCY::UPTODATE;
+    CACHE_METHOD cm = CACHE_METHOD::ITERATED_GET;
     long reportTime = 20l;
 
-    id = cacheInstance->requestResourceCache(pResource, func, rf, reportTime);
+    id = cacheInstance->requestResourceCache(pResource, func, cm, rf, reportTime);
 
     cacheInstance->updateResourceCache(id);
 
     cacheInstance->cancelResourceCache(id);
 }
 
-TEST_F(ResourceCacheManagerTest, getCachedDataPrimitiveResource_resourceIsNULL)
-{
-
-    pResource = NULL;
-
-    ASSERT_THROW(cacheInstance->getCachedData(pResource),
-                 ResourceCacheManager::InvalidParameterException);
-}
-
-TEST_F(ResourceCacheManagerTest, getCachedDataPrimitiveResource_handlerIsNULL)
-{
-
-    ASSERT_THROW(cacheInstance->getCachedData(pResource),
-                 ResourceCacheManager::InvalidParameterException);
-}
-
 TEST_F(ResourceCacheManagerTest, getCachedDataCachID_resourceIsNULL)
 {
 
-    ASSERT_THROW(cacheInstance->getCachedData(0), ResourceCacheManager::InvalidParameterException);
+    ASSERT_THROW(cacheInstance->getCachedData(0), RCSInvalidParameterException);
 }
 
 TEST_F(ResourceCacheManagerTest, getCachedDataCachID_handlerIsNULL)
 {
 
-    ASSERT_THROW(cacheInstance->getCachedData(id), ResourceCacheManager::InvalidParameterException);
-}
-
-TEST_F(ResourceCacheManagerTest, getResourceCacheStatePrimitiveResource_resourceIsNULL)
-{
-
-    pResource = NULL;
-
-    ASSERT_THROW(cacheInstance->getResourceCacheState(pResource),
-                 ResourceCacheManager::InvalidParameterException);
-}
-
-TEST_F(ResourceCacheManagerTest, getResourceCacheStatePrimitiveResource_handlerIsNULL)
-{
-
-    ASSERT_EQ(cacheInstance->getResourceCacheState(pResource), CACHE_STATE::NONE);
-}
-
-TEST_F(ResourceCacheManagerTest, getResourceCacheStatePrimitiveResource_normalCase)
-{
-
-    mocks.OnCall(pResource.get(), PrimitiveResource::requestGet);
-    mocks.OnCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
-    mocks.OnCall(pResource.get(), PrimitiveResource::requestObserve);
-    mocks.OnCall(pResource.get(), PrimitiveResource::getUri).Return("testUri");
-    mocks.OnCall(pResource.get(), PrimitiveResource::getHost).Return("testHost");
-    mocks.OnCall(pResource.get(), PrimitiveResource::cancelObserve);
-
-    CacheCB func = cb;
-    REPORT_FREQUENCY rf = REPORT_FREQUENCY::UPTODATE;
-    long reportTime = 20l;
-
-    id = cacheInstance->requestResourceCache(pResource, func, rf, reportTime);
-    CACHE_STATE state = cacheInstance->getResourceCacheState(pResource);
-
-    cacheInstance->cancelResourceCache(id);
-
-    ASSERT_EQ(state, CACHE_STATE::READY_YET);
+    ASSERT_THROW(cacheInstance->getCachedData(id), RCSInvalidParameterException);
 }
 
 TEST_F(ResourceCacheManagerTest, getResourceCacheStateCacheID_cacheIDIsZero)
 {
 
     ASSERT_THROW(cacheInstance->getResourceCacheState(0),
-                 ResourceCacheManager::InvalidParameterException);
+                 RCSInvalidParameterException);
 }
 
 TEST_F(ResourceCacheManagerTest, getResourceCacheStateCacheID_handlerIsNULL)
@@ -304,9 +220,10 @@ TEST_F(ResourceCacheManagerTest, getResourceCacheStateCacheID_normalCase)
 
     CacheCB func = cb;
     REPORT_FREQUENCY rf = REPORT_FREQUENCY::UPTODATE;
+    CACHE_METHOD cm = CACHE_METHOD::ITERATED_GET;
     long reportTime = 20l;
 
-    id = cacheInstance->requestResourceCache(pResource, func, rf, reportTime);
+    id = cacheInstance->requestResourceCache(pResource, func, cm, rf, reportTime);
     CACHE_STATE state = cacheInstance->getResourceCacheState(id);
 
     cacheInstance->cancelResourceCache(id);
index 4bb5b7b..e909261 100644 (file)
@@ -285,7 +285,7 @@ namespace OIC
             startCaching({ });
         }
 
-        void RCSRemoteResourceObject::startCaching(CacheUpdatedCallback cb)
+        void RCSRemoteResourceObject::startCaching(CacheUpdatedCallback cb, CacheMode mode)
         {
             SCOPE_LOG_F(DEBUG, TAG);
 
@@ -295,17 +295,28 @@ namespace OIC
                 throw RCSBadRequestException{ "Caching already started." };
             }
 
-            if (cb)
+            if (mode == CacheMode::OBSERVE_ONLY)
             {
                 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
                         m_primitiveResource,
                         std::bind(cachingCallback, std::placeholders::_1, std::placeholders::_2,
-                                std::move(cb)), REPORT_FREQUENCY::UPTODATE, 0);
+                                  std::move(cb)), CACHE_METHOD::OBSERVE_ONLY,
+                                  REPORT_FREQUENCY::UPTODATE, 0);
+            }
+
+            else if (cb)
+            {
+                m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
+                        m_primitiveResource,
+                        std::bind(cachingCallback, std::placeholders::_1, std::placeholders::_2,
+                                std::move(cb)), CACHE_METHOD::ITERATED_GET,
+                                REPORT_FREQUENCY::UPTODATE, 0);
             }
             else
             {
                 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
-                        m_primitiveResource, { }, REPORT_FREQUENCY::NONE, 0);
+                        m_primitiveResource, { }, CACHE_METHOD::ITERATED_GET,
+                        REPORT_FREQUENCY::NONE, 0);
             }
 
             OIC_LOG_V(DEBUG, TAG, "startCaching CACHE ID %d", m_cacheId);
@@ -335,7 +346,7 @@ namespace OIC
             }
 
             return convertCacheState(
-                    ResourceCacheManager::getInstance()->getResourceCacheState(m_primitiveResource));
+                    ResourceCacheManager::getInstance()->getResourceCacheState(m_cacheId));
         }
 
         bool RCSRemoteResourceObject::isCachedAvailable() const
@@ -362,7 +373,7 @@ namespace OIC
                 throw RCSBadRequestException{ "Cache data is not available." };
             }
 
-            return ResourceCacheManager::getInstance()->getCachedData(m_primitiveResource);
+            return ResourceCacheManager::getInstance()->getCachedData(m_cacheId);
         }
 
         RCSResourceAttributes::Value RCSRemoteResourceObject::getCachedAttribute(