Add Resource Cache feature that updates cache data periodically for the non-observabl...
authorYounghyunJoo <yh_.joo@samsung.com>
Tue, 30 Jun 2015 01:40:16 +0000 (10:40 +0900)
committerUze Choi <uzchoi@samsung.com>
Tue, 30 Jun 2015 05:10:09 +0000 (05:10 +0000)
Add Primitive Timer instance which checks the frequency and alarms
Add onTimer function to update cache data when Primitive timer alarms.

Change-Id: Ic5c9c0076058344829075e84084c75adb295eaad
Signed-off-by: YounghyunJoo <yh_.joo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1449
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/resource-manipulation/modules/resourceCache/SConscript
service/resource-manipulation/modules/resourceCache/include/DataCache.h
service/resource-manipulation/modules/resourceCache/src/DataCache.cpp

index f2d263e..55bd996 100644 (file)
@@ -43,7 +43,8 @@ target_os = env.get('TARGET_OS')
 ######################################################################
 resourcecache_env.AppendUnique(CPPPATH = ['include'])
 resourcecache_env.AppendUnique(CPPPATH = ['../common/primitiveResource/include'])
-resourcecache_env.PrependUnique(LIBS = ['service_common', 'oc', 'octbstack', 'oc_logger', 'connectivity_abstraction', 'libcoap'])
+resourcecache_env.AppendUnique(CPPPATH = ['../common/primitiveTimer/include'])
+resourcecache_env.PrependUnique(LIBS = ['service_common', 'PrimitiveTimer', 'oc', 'octbstack', 'oc_logger', 'connectivity_abstraction', 'libcoap'])
 
 if target_os not in ['windows', 'winrt']:
        resourcecache_env.AppendUnique(CXXFLAGS = ['-O2', '-g', '-Wall', '-fmessage-length=0', '-std=c++11'])
index 45f4898..8029b24 100755 (executable)
@@ -29,6 +29,7 @@
 #include "logger.h"
 
 #include "CacheTypes.h"
+#include "PrimitiveTimer.h"
 
 class DataCache
 {
@@ -66,21 +67,19 @@ private:
     // subscriber info
     std::unique_ptr<SubscriberInfo> subscriberList;
 
+    PrimitiveTimer *timerInstance;
+
     // for requestCB from base
     void onObserve(const HeaderOptions& _hos,
             const ResponseStatement& _rep, int _result, int _seq);
     void onGet(const HeaderOptions& _hos, const ResponseStatement& _rep, int _result);
-
+    void *onTimer(const unsigned int timerID);
     ObserveCB pObserveCB;
     GetCB pGetCB;
+    TimerCB pTimerCB;
 
     OCStackResult updateCacheData();
 
-    // for timer
-    void setUpdateTime();
-    void onTimer();
-    bool isAvailable();
-
 };
 
 #endif /* DATACACHE_H_ */
index 531b5d3..c7fcace 100755 (executable)
@@ -31,6 +31,7 @@
 
 #include "ResponseStatement.h"
 #include "ResourceAttributes.h"
+#include "PrimitiveTimer.h"
 
 DataCache::DataCache(
             PrimitiveResourcePtr pResource,
@@ -41,6 +42,7 @@ DataCache::DataCache(
 {
     subscriberList = std::unique_ptr<SubscriberInfo>(new SubscriberInfo());
 
+    timerInstance = new PrimitiveTimer;
     state = CACHE_STATE::READY_YET;
     updateTime = 0l;
 
@@ -49,15 +51,17 @@ DataCache::DataCache(
             std::placeholders::_3, std::placeholders::_4));
     pGetCB = (GetCB)(std::bind(&DataCache::onGet, this,
             std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
+    pTimerCB = (TimerCB)(std::bind(&DataCache::onTimer, this, std::placeholders::_1));
 
+    pResource->requestGet(pGetCB);
     if(pResource->isObservable())
     {
         pResource->requestObserve(pObserveCB);
-        pResource->requestGet(pGetCB);
     }
     else
     {
         // TODO set timer
+        TimerID timerId = timerInstance->requestTimer(repeatTime, pTimerCB);
     }
 }
 
@@ -181,7 +185,16 @@ void DataCache::onGet(const HeaderOptions& _hos,
     }
     else
     {
+        attributes = _rep.getAttributes();
 
+        ResourceAttributes retAtt = attributes;
+        for(auto & i : * subscriberList)
+        {
+            if(i.second.first.rf != REPORT_FREQUENCY::NONE)
+            {
+                i.second.second(this->sResource, retAtt);
+            }
+        }
     }
 }
 
@@ -189,3 +202,9 @@ CACHE_STATE DataCache::getCacheState() const
 {
     return state;
 }
+
+void *DataCache::onTimer(const unsigned int timerID)
+{
+    sResource->requestGet(pGetCB);
+    TimerID timerId = timerInstance->requestTimer(5l, pTimerCB);
+}
\ No newline at end of file