[Resource encapsulation]Add check logic about whether cached resource exists or not.
authorjyong2.kim <jyong2.kim@samsung.com>
Fri, 31 Jul 2015 12:05:15 +0000 (21:05 +0900)
committerUze Choi <uzchoi@samsung.com>
Fri, 31 Jul 2015 15:47:25 +0000 (15:47 +0000)
Throw exception when getCachedData() is requested for non-cached resource.
Add interface for checking cached resource existence.
Mutex for attributes is moved into the class member variable.

Change-Id: I25a163bf6900dd23f14158f28808873059798845
Signed-off-by: jyong2.kim <jyong2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2024
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/resource-encapsulation/src/resourceCache/include/DataCache.h
service/resource-encapsulation/src/resourceCache/include/ResourceCacheManager.h
service/resource-encapsulation/src/resourceCache/src/DataCache.cpp
service/resource-encapsulation/src/resourceCache/src/ResourceCacheManager.cpp

index bf18718..b1429c6 100644 (file)
@@ -54,6 +54,7 @@ namespace OIC
 
                 void requestGet();
                 bool isEmptySubscriber() const;
+                bool isCachedData() const;
 
             private:
                 // resource instance
@@ -63,10 +64,12 @@ namespace OIC
                 RCSResourceAttributes attributes;
                 CACHE_STATE state;
                 CACHE_MODE mode;
+                bool isReady;
 
                 // subscriber info
                 std::unique_ptr<SubscriberInfo> subscriberList;
                 mutable std::mutex m_mutex;
+                mutable std::mutex att_mutex;
 
                 ExpiryTimer networkTimer;
                 ExpiryTimer pollingTimer;
index 7deb7df..23a3a45 100644 (file)
@@ -42,23 +42,40 @@ namespace OIC
                         InvalidParameterException(std::string &&what)
                             : RCSException { std::move(what) } {}
                 };
+                class HasNoCachedDataException: public RCSException
+                {
+                    public:
+                        HasNoCachedDataException(std::string &&what)
+                            : RCSException { std::move(what) } {}
+                };
 
                 static ResourceCacheManager *getInstance();
 
+                // throw InvalidParameterException;
                 CacheID requestResourceCache(
                     PrimitiveResourcePtr pResource, CacheCB func = NULL,
                     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:
                 static ResourceCacheManager *s_instance;
                 static std::mutex s_mutex;
index a6b5874..ac2f1e2 100644 (file)
@@ -38,13 +38,10 @@ namespace OIC
 
         namespace
         {
-            std::mutex cbMutex;
-
             void verifyObserveCB(
                 const HeaderOptions &_hos, const ResponseStatement &_rep,
                 int _result, int _seq, std::weak_ptr<DataCache> rpPtr)
             {
-                std::lock_guard<std::mutex> lock(cbMutex);
                 std::shared_ptr<DataCache> Ptr = rpPtr.lock();
                 if(Ptr)
                 {
@@ -63,7 +60,6 @@ namespace OIC
                     const HeaderOptions &_hos, const ResponseStatement &_rep,
                     int _result, std::weak_ptr<DataCache> rpPtr)
             {
-                std::lock_guard<std::mutex> lock(cbMutex);
                 std::shared_ptr<DataCache> Ptr = rpPtr.lock();
                 if(Ptr)
                 {
@@ -91,6 +87,7 @@ namespace OIC
             networkTimeOutHandle = 0;
             pollingHandle = 0;
             lastSequenceNum = 0;
+            isReady = false;
         }
 
         DataCache::~DataCache()
@@ -191,16 +188,21 @@ namespace OIC
 
         const RCSResourceAttributes DataCache::getCachedData() const
         {
-            if (state != CACHE_STATE::READY || attributes.empty())
+            std::lock_guard<std::mutex> lock(att_mutex);
+            if (state != CACHE_STATE::READY)
             {
                 return RCSResourceAttributes();
             }
-            const RCSResourceAttributes retAtt = attributes;
-            return retAtt;
+            return attributes;
+        }
+
+        bool DataCache::isCachedData() const
+        {
+            return isReady;
         }
 
-        void DataCache::onObserve(
-            const HeaderOptions &_hos, const ResponseStatement &_rep, int _result, int _seq)
+        void DataCache::onObserve(const HeaderOptions & /*_hos*/,
+                const ResponseStatement & _rep, int _result, int _seq)
         {
 
             if (_result != OC_STACK_OK || _rep.getAttributes().empty() || lastSequenceNum > _seq)
@@ -215,6 +217,7 @@ namespace OIC
             if (state != CACHE_STATE::READY)
             {
                 state = CACHE_STATE::READY;
+                isReady = true;
             }
 
             if (mode != CACHE_MODE::OBSERVE)
@@ -228,7 +231,7 @@ namespace OIC
             notifyObservers(_rep.getAttributes());
         }
 
-        void DataCache::onGet(const HeaderOptions &_hos,
+        void DataCache::onGet(const HeaderOptions & /*_hos*/,
                               const ResponseStatement &_rep, int _result)
         {
             if (_result != OC_STACK_OK || _rep.getAttributes().empty())
@@ -239,6 +242,7 @@ namespace OIC
             if (state != CACHE_STATE::READY)
             {
                 state = CACHE_STATE::READY;
+                isReady = true;
             }
 
             if (mode != CACHE_MODE::OBSERVE)
@@ -255,13 +259,15 @@ namespace OIC
 
         void DataCache::notifyObservers(const RCSResourceAttributes Att)
         {
-            if (attributes == Att)
             {
-                return;
+                std::lock_guard<std::mutex> lock(att_mutex);
+                if (attributes == Att)
+                {
+                    return;
+                }
+                attributes = Att;
             }
 
-            attributes = Att;
-
             std::lock_guard<std::mutex> lock(m_mutex);
             for (auto &i : * subscriberList)
             {
@@ -277,7 +283,7 @@ namespace OIC
             return state;
         }
 
-        void DataCache::onTimeOut(unsigned int timerID)
+        void DataCache::onTimeOut(unsigned int /*timerID*/)
         {
             if(mode == CACHE_MODE::OBSERVE)
             {
@@ -294,7 +300,7 @@ namespace OIC
 
             state = CACHE_STATE::LOST_SIGNAL;
         }
-        void DataCache::onPollingOut(const unsigned int timerID)
+        void DataCache::onPollingOut(const unsigned int /*timerID*/)
         {
             if (sResource != nullptr)
             {
index f6724ee..8018c3e 100644 (file)
@@ -160,6 +160,12 @@ namespace OIC
             {
                 throw InvalidParameterException {"[getCachedData] Primitive Resource is invaild"};
             }
+
+            if(handler->isCachedData() == false)
+            {
+                throw HasNoCachedDataException {"[getCachedData] Cached Data is not stored"};
+            }
+
             return handler->getCachedData();
         }
 
@@ -175,6 +181,12 @@ namespace OIC
             {
                 throw InvalidParameterException {"[getCachedData] CacheID is invaild"};
             }
+
+            if(handler->isCachedData() == false)
+            {
+                throw HasNoCachedDataException {"[getCachedData] Cached Data is not stored"};
+            }
+
             return handler->getCachedData();
         }
 
@@ -209,6 +221,36 @@ namespace OIC
             return handler->getCacheState();
         }
 
+        bool ResourceCacheManager::isCachedData(PrimitiveResourcePtr pResource) const
+        {
+            if (pResource == nullptr)
+            {
+                throw InvalidParameterException {"[isCachedData] Primitive Resource is nullptr"};
+            }
+
+            DataCachePtr handler = findDataCache(pResource);
+            if (handler == nullptr)
+            {
+                throw InvalidParameterException {"[isCachedData] Primitive Resource is invaild"};
+            }
+            return handler->isCachedData();
+        }
+
+        bool ResourceCacheManager::isCachedData(CacheID id) const
+        {
+            if (id == 0)
+            {
+                throw InvalidParameterException {"[isCachedData] CacheID is NULL"};
+            }
+
+            DataCachePtr handler = findDataCache(id);
+            if (handler == nullptr)
+            {
+                throw InvalidParameterException {"[isCachedData] CacheID is invaild"};
+            }
+            return handler->isCachedData();
+        }
+
         void ResourceCacheManager::initializeResourceCacheManager()
         {
             std::lock_guard<std::mutex> lock(s_mutex);