Add ResourceCache Sconscript
authorjyong2.kim <jyong2.kim@samsung.com>
Wed, 10 Jun 2015 04:15:32 +0000 (13:15 +0900)
committerUze Choi <uzchoi@samsung.com>
Thu, 11 Jun 2015 12:48:33 +0000 (12:48 +0000)
fix build error and syntax error.
arrange the pointer.
fix ResponseStatement's error.

Change-Id: Iea690231a9c139867c80eed6dd63b3033285836a
Signed-off-by: jyong2.kim <jyong2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1230
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/basis/common/primitiveResource/include/ResponseStatement.h
service/basis/resourceCache/SConscript [new file with mode: 0644]
service/basis/resourceCache/include/CacheTypes.h
service/basis/resourceCache/include/DataCache.h
service/basis/resourceCache/include/ResourceCacheManager.h
service/basis/resourceCache/src/DataCache.cpp
service/basis/resourceCache/src/ResourceCacheManager.cpp

index 4878fe5..fe369aa 100755 (executable)
@@ -20,6 +20,8 @@
 #ifndef __RESPONSESTATEMENT_H\r
 #define __RESPONSESTATEMENT_H\r
 \r
+#include <ResourceAttributes.h>\r
+\r
 /**\r
  * TODO : design for future flexibility\r
  */\r
@@ -42,7 +44,7 @@ public:
     std::vector<std::string> getResourceTypes() const;\r
     std::vector<std::string> getResourceInterfaces() const;\r
 \r
-    ResourceAttributes getAttributes() const;\r
+    ResourceAttributes getAttributes() const{ return ResourceAttributes(); }\r
 \r
     ~ResponseStatement() {}\r
 };\r
diff --git a/service/basis/resourceCache/SConscript b/service/basis/resourceCache/SConscript
new file mode 100644 (file)
index 0000000..89a4062
--- /dev/null
@@ -0,0 +1,74 @@
+#******************************************************************
+#
+# 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.
+#
+#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+##
+# ResourceCache build script
+##
+
+Import('env')
+
+if env.get('RELEASE'):
+       env.AppendUnique(CCFLAGS = ['-Os'])
+       env.AppendUnique(CPPDEFINES = ['NDEBUG'])
+else:
+       env.AppendUnique(CCFLAGS = ['-g'])
+
+if env.get('LOGGING'):
+       env.AppendUnique(CPPDEFINES = ['TB_LOG'])
+
+lib_env = env.Clone()
+SConscript(env.get('SRC_DIR') + '/service/third_party_libs.scons', 'lib_env')
+resourcecache_env = lib_env.Clone()
+
+target_os = env.get('TARGET_OS')
+######################################################################
+# Build flags
+######################################################################
+resourcecache_env.AppendUnique(CPPPATH = ['include'])
+resourcecache_env.AppendUnique(CPPPATH = ['../common/primitiveResource/include'])
+resourcecache_env.PrependUnique(LIBS = ['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'])
+
+if target_os == 'linux':
+       resourcecache_env.AppendUnique(LIBS = ['pthread'])
+
+######################################################################
+# Source files and Targets
+######################################################################
+CACHE_SRC_DIR = 'src/'
+resourcecache_src = [
+        CACHE_SRC_DIR + 'DataCache.cpp',
+        CACHE_SRC_DIR + 'ResourceCacheManager.cpp',
+        CACHE_SRC_DIR + '../../common/primitiveResource/src/PrimitiveResource.cpp',
+        CACHE_SRC_DIR + '../../common/primitiveResource/src/ResourceAttributes.cpp'
+        ]
+
+if target_os in ['tizen','android'] :
+    resourcecachesdk = resourcecache_env.SharedLibrary('ResourceCache', resourcecache_src)
+else :
+    resourcecachesdk = resourcecache_env.StaticLibrary('ResourceCache', resourcecache_src)
+
+resourcecache_env.InstallTarget(resourcecachesdk, 'libResouceCache')
+
+# Go to build sample apps
+#SConscript('SampleApp/SConscript')
+
index 5f312e2..260038f 100644 (file)
@@ -33,6 +33,8 @@
 
 #define CACHE_TAG  PCF("CACHE")
 
+class DataCache;
+
 enum class REPORT_FREQUENCY
 {
     NONE = 0,
@@ -54,23 +56,23 @@ enum class CACHE_STATE
     READY_YET,
     LOST_SIGNAL,
     DESTROYED,
-    UPDATING
+    UPDATING,
+    NONE
 };
 
 typedef int CacheID;
 
-typedef std::map<std::string, std::string> CacheData;
-typedef std::function<OCStackResult(std::shared_ptr<PrimitiveResource>, CacheData)> CacheCB;
-typedef std::map<Report_Info, CacheCB> SubscriberInfo;
-typedef std::pair<Report_Info, CacheCB> SubscriberInfoPair;
+typedef std::map<std::string, std::string> CachedData;
+typedef std::function<OCStackResult(std::shared_ptr<PrimitiveResource>, std::shared_ptr<CachedData>)> CacheCB;
+typedef std::map<int, std::pair<Report_Info, CacheCB>> SubscriberInfo;
+typedef std::pair<int, std::pair<Report_Info, CacheCB>> SubscriberInfoPair;
 
 typedef OC::OCResource BaseResource;
-//typedef std::function<void(const OC::HeaderOptions&,
-//            const OC::OCRepresentation&, const int, const int)> ObserveCB;
-//typedef std::function<void(const OC::HeaderOptions&,
-//            const OC::OCRepresentation&, const int)> GetCB;
-
 typedef PrimitiveResource::GetCallback GetCB;
 typedef PrimitiveResource::ObserveCallback ObserveCB;
 
+typedef std::shared_ptr<DataCache> DataCachePtr;
+typedef std::shared_ptr<CachedData> CachedDataPtr;
+typedef std::shared_ptr<PrimitiveResource> PrimitiveResourcePtr;
+
 #endif
index 7c7d054..4d0ffb1 100644 (file)
@@ -22,7 +22,6 @@
 #define DATACACHE_H_
 
 #include <list>
-#include <memory>
 #include <string>
 #include <boost/progress.hpp>
 
@@ -35,7 +34,7 @@ class DataCache
 {
 public:
     DataCache(
-            PrimitiveResource & pResource,
+            PrimitiveResourcePtr pResource,
             CacheCB func,
             REPORT_FREQUENCY rf,
             long repeatTime);
@@ -44,8 +43,11 @@ public:
     CacheID addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatTime);
     CacheID deleteSubscriber(CacheID id);
 
-    std::shared_ptr<CacheData> getCachedData();
-    PrimitiveResource * getPrimitiveResource();
+    CachedDataPtr getCachedData();
+    CACHE_STATE getCacheState();
+    PrimitiveResourcePtr getPrimitiveResource();
+
+    SubscriberInfoPair findSubscriber(CacheID id);
 
 private:
     // origin resource info
@@ -53,19 +55,17 @@ private:
     std::string address;
 
     // resource instance
-    PrimitiveResource *sResource;
+    PrimitiveResourcePtr sResource;
     std::shared_ptr<BaseResource> baseHandler;
 
-    std::shared_ptr<ResourceAttributes> attributes;
-
     // cached data info
-    std::shared_ptr<CacheData> data;
+    CachedDataPtr data;
+    std::shared_ptr<ResourceAttributes> attributes;
     long updateTime;
     CACHE_STATE state;
 
     // subscriber info
-    SubscriberInfo *subscriberList;
-    SubscriberInfoPair findSubscriber(CacheID id);
+    std::unique_ptr<SubscriberInfo> subscriberList;
 
     // for requestCB from base
     void onObserve(const HeaderOptions& _hos,
@@ -75,7 +75,6 @@ private:
     ObserveCB pObserveCB;
     GetCB pGetCB;
 
-    //
     OCStackResult updateCacheData();
 
     // for timer
index bc36d7f..5c5bab0 100644 (file)
 class ResourceCacheManager
 {
 public:
-    ResourceCacheManager * getInstance();
+    static ResourceCacheManager * getInstance();
 
     CacheID requestResourceCache(
-            PrimitiveResource & pResource,
+            PrimitiveResourcePtr pResource,
                 CacheCB func = NULL, REPORT_FREQUENCY rf = REPORT_FREQUENCY::NONE, long time = 0l);
 
-//    OCStackResult cancelResourceCache(std::string address, std::string uri);
-    OCStackResult cancelResourceCache(PrimitiveResource & pResource, CacheID id);
+    OCStackResult cancelResourceCache(PrimitiveResourcePtr pResource, CacheID id);
 
-//    OCStackResult updateResourceCache(std::string address, std::string uri);
-    OCStackResult updateResourceCache(PrimitiveResource & pResource);
+    OCStackResult updateResourceCache(PrimitiveResourcePtr pResource);
 
-//    OCStackResult getResourceCache(std::string address, std::string uri);
-    OCStackResult getResourceCache(PrimitiveResource & pResource);
+    CachedDataPtr getCachedData(PrimitiveResourcePtr pResource);
+    CachedDataPtr getCachedData(CacheID id);
 
+    CACHE_STATE getResourceCacheState(PrimitiveResourcePtr pResource);
+    CACHE_STATE getResourceCacheState(CacheID id);
+
+    ~ResourceCacheManager();
 private:
     ResourceCacheManager();
-    ~ResourceCacheManager();
 
     static ResourceCacheManager * s_instance;
     static std::mutex s_mutexForCreation;
-    static std::list< DataCache * > * s_cacheDataList;
+    static std::unique_ptr<std::list<DataCachePtr>> s_cacheDataList;
 
-    DataCache * findCacheHandler(PrimitiveResource & pResource);
+    DataCachePtr findDataCache(PrimitiveResourcePtr pResource);
+    DataCachePtr findDataCache(CacheID id);
 };
 
 #endif /* RESOURCECACHEMANAGER_H_ */
index caed5b4..b452ba8 100644 (file)
@@ -18,8 +18,6 @@
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
-#include "CacheHandler.h"
-
 #include <memory>
 #include <cstdlib>
 #include <functional>
 
 #include "OCApi.h"
 
+#include "DataCache.h"
+
+#include "ResponseStatement.h"
+#include "ResourceAttributes.h"
+
 DataCache::DataCache(
-            PrimitiveResource & pResource,
+            PrimitiveResourcePtr pResource,
             CacheCB func,
             REPORT_FREQUENCY rf,
             long repeatTime
             ):sResource(pResource)
 {
-    subscriberList = new SubscriberInfo();
-    data = new CacheData();
+    subscriberList = std::unique_ptr<SubscriberInfo>(new SubscriberInfo());
+    data = std::make_shared<CachedData>();
 
     state = CACHE_STATE::READY_YET;
     updateTime = 0l;
@@ -48,9 +51,9 @@ DataCache::DataCache(
     pGetCB = (GetCB)(std::bind(&DataCache::onGet, this,
             std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
 
-    if(pResource.isObservable())
+    if(pResource->isObservable())
     {
-        pResource.requestObserve(pObserveCB);
+        pResource->requestObserve(pObserveCB);
     }
     else
     {
@@ -65,7 +68,7 @@ DataCache::~DataCache()
 
     // TODO delete all node!!
     subscriberList->clear();
-    delete subscriberList;
+    subscriberList.release();
 }
 
 CacheID DataCache::addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatTime)
@@ -80,7 +83,7 @@ CacheID DataCache::addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatT
 
     while(1)
     {
-        if(findSubscriber(newItem.reportID) != nullptr || newItem.reportID == 0)
+        if(findSubscriber(newItem.reportID).first == 0 || newItem.reportID == 0)
         {
             newItem.reportID = rand();
         }
@@ -90,7 +93,7 @@ CacheID DataCache::addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatT
         }
     }
 
-    subscriberList->insert(SubscriberInfoPair(newItem, func));
+    subscriberList->insert(std::make_pair(newItem.reportID, std::make_pair(newItem, func)));
 
     return newItem.reportID;
 }
@@ -100,9 +103,9 @@ CacheID DataCache::deleteSubscriber(CacheID id)
     CacheID ret = 0;
 
     SubscriberInfoPair pair = findSubscriber(id);
-    if(pair != nullptr)
+    if(pair.first != 0)
     {
-        ret = pair.first.reportID;
+        ret = pair.first;
         subscriberList->erase(pair.first);
     }
 
@@ -111,37 +114,31 @@ CacheID DataCache::deleteSubscriber(CacheID id)
 
 SubscriberInfoPair DataCache::findSubscriber(CacheID id)
 {
-    SubscriberInfoPair ret = nullptr;
+    SubscriberInfoPair ret;
 
-    for(auto i : subscriberList)
+    for(auto & i : *subscriberList)
     {
-        if(i->first.reportID == id)
+        if(i.first == id)
         {
-            ret = i;
+            ret = std::make_pair(i.first, std::make_pair((Report_Info)i.second.first, (CacheCB)i.second.second));
         }
     }
 
     return ret;
 }
 
-std::shared_ptr<CacheData> DataCache::getCachedData()
+CachedDataPtr DataCache::getCachedData()
 {
     if(state != CACHE_STATE::READY)
     {
-        return NULL;
+        return nullptr;
     }
     return data;
 }
 
-PrimitiveResource * DataCache::getPrimitiveResource()
+std::shared_ptr<PrimitiveResource> DataCache::getPrimitiveResource()
 {
-    PrimitiveResource ret = NULL;
-    if(sResource)
-    {
-        ret = sResource;
-    }
-
-    return ret;
+    return sResource;
 }
 
 void DataCache::onObserve(
@@ -153,7 +150,8 @@ void DataCache::onObserve(
         // TODO handle error
         return;
     }
-    if(_rep.getAttributes().isEmpty())
+
+    if(_rep.getAttributes().empty())
     {
         return;
     }
@@ -162,25 +160,34 @@ void DataCache::onObserve(
 
     // set data
     data->clear();
-    ResourceAttributes::iterator it = att.begin();
-    for(; att.end(); ++it)
-    {
-        std::string key = it->key();
-        // TODO change template or variant
-        std::string val = it->value();
 
-        data[key] = val;
+
+    for(auto & i : att)
+    {
+        const std::string &key = i.key();
+//        std::string value = i.value();
+        std::string val;
+        data->insert(CachedData::value_type(key, val));
     }
 
     // notify!!
-    std::map::iterator mapiter = subscriberList->begin();
-    for(; subscriberList->end(); ++mapiter)
+
+    for(auto & i : * subscriberList)
     {
-        if(mapiter->first().rf == REPORT_FREQUENCY::UPTODATE)
+        if(i.second.first.rf == REPORT_FREQUENCY::UPTODATE)
         {
-            // TODO update report time
-//            mapiter->first().latestReportTime = now;
-            mapiter->second()(this->sResource, data);
+            i.second.second(this->sResource, data);
         }
     }
 }
+
+void DataCache::onGet(const HeaderOptions& _hos,
+        const ResponseStatement& _rep, int _result)
+{
+
+}
+
+CACHE_STATE DataCache::getCacheState()
+{
+    return state;
+}
index e5bcf2a..a27480d 100644 (file)
 
 ResourceCacheManager * ResourceCacheManager::s_instance = NULL;
 std::mutex ResourceCacheManager::s_mutexForCreation;
-std::list< DataCache * > * ResourceCacheManager::s_cacheDataList = NULL;
+std::unique_ptr<std::list<DataCachePtr>> ResourceCacheManager::s_cacheDataList(nullptr);
 
 ResourceCacheManager::ResourceCacheManager()
 {
     // TODO Auto-generated constructor stub
     if(!s_cacheDataList)
     {
-        s_cacheDataList = new std::list< DataCache * >();
+        s_cacheDataList = std::unique_ptr<std::list<DataCachePtr>>(new std::list<DataCachePtr>);
     }
 }
 
@@ -39,19 +39,18 @@ ResourceCacheManager::~ResourceCacheManager()
     if(s_cacheDataList)
     {
         s_cacheDataList->clear();
-        delete s_cacheDataList;
     }
 }
 
 
 ResourceCacheManager * ResourceCacheManager::getInstance()
 {
-    if(!s_instance)
+    if(s_instance == nullptr)
     {
         s_mutexForCreation.lock();
-        if(!s_instance)
+        if(s_instance == nullptr)
         {
-            s_instance = new ResourceCacheManager();
+            s_instance = new ResourceCacheManager();;
         }
         s_mutexForCreation.unlock();
     }
@@ -59,16 +58,12 @@ ResourceCacheManager * ResourceCacheManager::getInstance()
 }
 
 CacheID ResourceCacheManager::requestResourceCache(
-        PrimitiveResource & pResource, CacheCB func, REPORT_FREQUENCY rf, long reportTime)
+        PrimitiveResourcePtr pResource, CacheCB func,
+        REPORT_FREQUENCY rf, long reportTime)
 {
     CacheID ret = 0;
 
-    if(rt == NULL || pResource == NULL)
-    {
-        return ret;
-    }
-
-    if(rt != REPORT_FREQUENCY::NONE)
+    if(rf != REPORT_FREQUENCY::NONE)
     {
         if(func == NULL)
         {
@@ -81,10 +76,10 @@ CacheID ResourceCacheManager::requestResourceCache(
         }
     }
 
-    DataCache * newHandler = findCacheHandler(pResource);
+    DataCachePtr newHandler = findDataCache(pResource);
     if(newHandler == nullptr)
     {
-        DataCache * newHandler = new DataCache(pResource, func, rf, reportTime);
+        DataCachePtr newHandler = std::make_shared<DataCache>(pResource, func, rf, reportTime);
         s_cacheDataList->push_back(newHandler);
     }
     ret = newHandler->addSubscriber(func, rf, reportTime);
@@ -92,18 +87,18 @@ CacheID ResourceCacheManager::requestResourceCache(
     return ret;
 }
 
-OCStackResult ResourceCacheManager::cancelResourceCache(PrimitiveResource & pResource, CacheID id)
+OCStackResult ResourceCacheManager::cancelResourceCache(PrimitiveResourcePtr pResource, CacheID id)
 {
     OCStackResult ret = OC_STACK_ERROR;
 
-    if(pResource == NULL || id == 0)
+    if(id == 0)
     {
         return ret;
     }
 
     // TODO cancel cache
     CacheID retID = 0;
-    DataCache * deleteCacheHandler = findCacheHandler(pResource);
+    DataCachePtr deleteCacheHandler = findDataCache(pResource);
     if(deleteCacheHandler == nullptr)
     {
         return ret;
@@ -121,12 +116,13 @@ OCStackResult ResourceCacheManager::cancelResourceCache(PrimitiveResource & pRes
     return ret;
 }
 
-DataCache * ResourceCacheManager::findCacheHandler(PrimitiveResource & pResource)
+DataCachePtr ResourceCacheManager::findDataCache(PrimitiveResourcePtr pResource)
 {
-    DataCache * retHandler = nullptr;
-    for (auto i : s_cacheDataList)
+    DataCachePtr retHandler = nullptr;
+    for (auto & i : * s_cacheDataList)
     {
-        if(i->getPrimitiveResource() == pResource)
+        if(i->getPrimitiveResource()->getUri() == pResource->getUri() &&
+                i->getPrimitiveResource()->getHost() == pResource->getHost())
         {
             retHandler = i;
             break;
@@ -135,7 +131,22 @@ DataCache * ResourceCacheManager::findCacheHandler(PrimitiveResource & pResource
     return retHandler;
 }
 
-OCStackResult ResourceCacheManager::updateResourceCache(PrimitiveResource & pResource)
+DataCachePtr ResourceCacheManager::findDataCache(CacheID id)
+{
+    DataCachePtr retHandler = nullptr;
+    for (auto & i : * s_cacheDataList)
+    {
+        SubscriberInfoPair pair = i->findSubscriber(id);
+        if(pair.first != 0)
+        {
+            retHandler = i;
+            break;
+        }
+    }
+    return retHandler;
+}
+
+OCStackResult ResourceCacheManager::updateResourceCache(PrimitiveResourcePtr pResource)
 {
     OCStackResult ret = OC_STACK_ERROR;
 
@@ -143,3 +154,42 @@ OCStackResult ResourceCacheManager::updateResourceCache(PrimitiveResource & pRes
 
     return ret;
 }
+
+CachedDataPtr ResourceCacheManager::getCachedData(PrimitiveResourcePtr pResource)
+{
+    DataCachePtr handler = findDataCache(pResource);
+    if(handler == nullptr)
+    {
+        return nullptr;
+    }
+    return handler->getCachedData();
+}
+
+CachedDataPtr ResourceCacheManager::getCachedData(CacheID id)
+{
+    DataCachePtr handler = findDataCache(id);
+    if(handler == nullptr)
+    {
+        return nullptr;
+    }
+    return handler->getCachedData();
+}
+
+CACHE_STATE ResourceCacheManager::getResourceCacheState(PrimitiveResourcePtr pResource)
+{
+    DataCachePtr handler = findDataCache(pResource);
+    if(handler == nullptr)
+    {
+        return CACHE_STATE::NONE;
+    }
+    return handler->getCacheState();
+}
+CACHE_STATE ResourceCacheManager::getResourceCacheState(CacheID id)
+{
+    DataCachePtr handler = findDataCache(id);
+    if(handler == nullptr)
+    {
+        return CACHE_STATE::NONE;
+    }
+    return handler->getCacheState();
+}