From 2f61b9654e1c96538941b75783aec28d043ec3c0 Mon Sep 17 00:00:00 2001 From: "jyong2.kim" Date: Wed, 10 Jun 2015 13:15:32 +0900 Subject: [PATCH] Add ResourceCache Sconscript fix build error and syntax error. arrange the pointer. fix ResponseStatement's error. Change-Id: Iea690231a9c139867c80eed6dd63b3033285836a Signed-off-by: jyong2.kim Reviewed-on: https://gerrit.iotivity.org/gerrit/1230 Tested-by: jenkins-iotivity Reviewed-by: Uze Choi --- .../primitiveResource/include/ResponseStatement.h | 4 +- service/basis/resourceCache/SConscript | 74 +++++++++++++++++ service/basis/resourceCache/include/CacheTypes.h | 22 ++--- service/basis/resourceCache/include/DataCache.h | 21 +++-- .../resourceCache/include/ResourceCacheManager.h | 24 +++--- service/basis/resourceCache/src/DataCache.cpp | 87 +++++++++++--------- .../resourceCache/src/ResourceCacheManager.cpp | 96 ++++++++++++++++------ 7 files changed, 232 insertions(+), 96 deletions(-) create mode 100644 service/basis/resourceCache/SConscript diff --git a/service/basis/common/primitiveResource/include/ResponseStatement.h b/service/basis/common/primitiveResource/include/ResponseStatement.h index 4878fe5..fe369aa 100755 --- a/service/basis/common/primitiveResource/include/ResponseStatement.h +++ b/service/basis/common/primitiveResource/include/ResponseStatement.h @@ -20,6 +20,8 @@ #ifndef __RESPONSESTATEMENT_H #define __RESPONSESTATEMENT_H +#include + /** * TODO : design for future flexibility */ @@ -42,7 +44,7 @@ public: std::vector getResourceTypes() const; std::vector getResourceInterfaces() const; - ResourceAttributes getAttributes() const; + ResourceAttributes getAttributes() const{ return ResourceAttributes(); } ~ResponseStatement() {} }; diff --git a/service/basis/resourceCache/SConscript b/service/basis/resourceCache/SConscript new file mode 100644 index 0000000..89a4062 --- /dev/null +++ b/service/basis/resourceCache/SConscript @@ -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') + diff --git a/service/basis/resourceCache/include/CacheTypes.h b/service/basis/resourceCache/include/CacheTypes.h index 5f312e2..260038f 100644 --- a/service/basis/resourceCache/include/CacheTypes.h +++ b/service/basis/resourceCache/include/CacheTypes.h @@ -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 CacheData; -typedef std::function, CacheData)> CacheCB; -typedef std::map SubscriberInfo; -typedef std::pair SubscriberInfoPair; +typedef std::map CachedData; +typedef std::function, std::shared_ptr)> CacheCB; +typedef std::map> SubscriberInfo; +typedef std::pair> SubscriberInfoPair; typedef OC::OCResource BaseResource; -//typedef std::function ObserveCB; -//typedef std::function GetCB; - typedef PrimitiveResource::GetCallback GetCB; typedef PrimitiveResource::ObserveCallback ObserveCB; +typedef std::shared_ptr DataCachePtr; +typedef std::shared_ptr CachedDataPtr; +typedef std::shared_ptr PrimitiveResourcePtr; + #endif diff --git a/service/basis/resourceCache/include/DataCache.h b/service/basis/resourceCache/include/DataCache.h index 7c7d054..4d0ffb1 100644 --- a/service/basis/resourceCache/include/DataCache.h +++ b/service/basis/resourceCache/include/DataCache.h @@ -22,7 +22,6 @@ #define DATACACHE_H_ #include -#include #include #include @@ -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 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 baseHandler; - std::shared_ptr attributes; - // cached data info - std::shared_ptr data; + CachedDataPtr data; + std::shared_ptr attributes; long updateTime; CACHE_STATE state; // subscriber info - SubscriberInfo *subscriberList; - SubscriberInfoPair findSubscriber(CacheID id); + std::unique_ptr subscriberList; // for requestCB from base void onObserve(const HeaderOptions& _hos, @@ -75,7 +75,6 @@ private: ObserveCB pObserveCB; GetCB pGetCB; - // OCStackResult updateCacheData(); // for timer diff --git a/service/basis/resourceCache/include/ResourceCacheManager.h b/service/basis/resourceCache/include/ResourceCacheManager.h index bc36d7f..5c5bab0 100644 --- a/service/basis/resourceCache/include/ResourceCacheManager.h +++ b/service/basis/resourceCache/include/ResourceCacheManager.h @@ -33,30 +33,32 @@ 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> s_cacheDataList; - DataCache * findCacheHandler(PrimitiveResource & pResource); + DataCachePtr findDataCache(PrimitiveResourcePtr pResource); + DataCachePtr findDataCache(CacheID id); }; #endif /* RESOURCECACHEMANAGER_H_ */ diff --git a/service/basis/resourceCache/src/DataCache.cpp b/service/basis/resourceCache/src/DataCache.cpp index caed5b4..b452ba8 100644 --- a/service/basis/resourceCache/src/DataCache.cpp +++ b/service/basis/resourceCache/src/DataCache.cpp @@ -18,8 +18,6 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -#include "CacheHandler.h" - #include #include #include @@ -29,15 +27,20 @@ #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(new SubscriberInfo()); + data = std::make_shared(); 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 DataCache::getCachedData() +CachedDataPtr DataCache::getCachedData() { if(state != CACHE_STATE::READY) { - return NULL; + return nullptr; } return data; } -PrimitiveResource * DataCache::getPrimitiveResource() +std::shared_ptr 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; +} diff --git a/service/basis/resourceCache/src/ResourceCacheManager.cpp b/service/basis/resourceCache/src/ResourceCacheManager.cpp index e5bcf2a..a27480d 100644 --- a/service/basis/resourceCache/src/ResourceCacheManager.cpp +++ b/service/basis/resourceCache/src/ResourceCacheManager.cpp @@ -22,14 +22,14 @@ ResourceCacheManager * ResourceCacheManager::s_instance = NULL; std::mutex ResourceCacheManager::s_mutexForCreation; -std::list< DataCache * > * ResourceCacheManager::s_cacheDataList = NULL; +std::unique_ptr> 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>(new std::list); } } @@ -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(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(); +} -- 2.7.4