add namespace OIC::Service.
change parameter and return type of interfaces.
add exception handle in interfaces.
arrange duplicated code and logic.
add updateResourceCache().
Change-Id: I71302bc7cc0351626dd67790e2b4bf787c5b881e
Signed-off-by: jyong2.kim <jyong2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1558
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
resourcecache_env.AppendUnique(CPPPATH = ['include'])
resourcecache_env.AppendUnique(CPPPATH = ['../common/primitiveResource/include'])
resourcecache_env.AppendUnique(CPPPATH = ['../common/expiryTimer/include'])
+resourcecache_env.AppendUnique(CPPPATH = ['../common/expiryTimer/src'])
resourcecache_env.PrependUnique(LIBS = ['service_common', 'ExpiryTimer', 'oc', 'octbstack', 'oc_logger', 'connectivity_abstraction', 'libcoap'])
if target_os not in ['windows', 'winrt']:
resourcecache_env.InstallTarget(resourcecachesdk, 'libResouceCache')
# Go to build sample apps
-SConscript('SampleApp/SConscript')
+#SConscript('SampleApp/SConscript')
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-#ifndef CACHETYPES_H
-#define CACHETYPES_H
+#ifndef RCM_CACHETYPES_H
+#define RCM_CACHETYPES_H
#include <functional>
#include <map>
#include <memory>
#include <string>
-#include "ocstack.h"
-#include "OCResource.h"
+#include "logger.h"
#include "PrimitiveResource.h"
#include "ResourceAttributes.h"
-#define CACHE_TAG PCF("CACHE")
-#define DEFAULT_REPORT_TIME 30
-#define DEFAULT_EXPIRED_TIME 15l
+namespace OIC
+{
+ namespace Service
+ {
-using namespace OIC::Service;
+ class DataCache;
-class DataCache;
+ #define CACHE_TAG PCF("CACHE")
+ #define DEFAULT_REPORT_TIME 10l
+ #define DEFAULT_EXPIRED_TIME 15l
-enum class REPORT_FREQUENCY
-{
- NONE = 0,
- UPTODATE,
- PERIODICTY
-};
+ enum class REPORT_FREQUENCY
+ {
+ NONE = 0,
+ UPTODATE,
+ PERIODICTY
+ };
-struct Report_Info
-{
- REPORT_FREQUENCY rf;
- int reportID;
- long repeatTime;
- unsigned int timerID;
-};
+ struct Report_Info
+ {
+ REPORT_FREQUENCY rf;
+ int reportID;
+ long repeatTime;
+ unsigned int timerID;
+ };
-enum class CACHE_STATE
-{
- READY = 0,
- READY_YET,
- LOST_SIGNAL,
- DESTROYED,
- UPDATING,
- NONE
-};
+ enum class CACHE_STATE
+ {
+ READY = 0,
+ READY_YET,
+ LOST_SIGNAL,
+ DESTROYED,
+ UPDATING,
+ NONE
+ };
-typedef int CacheID;
+ typedef int CacheID;
-typedef std::function<OCStackResult(std::shared_ptr<PrimitiveResource>, const ResourceAttributes &)> CacheCB;
-typedef std::map<int, std::pair<Report_Info, CacheCB>> SubscriberInfo;
-typedef std::pair<int, std::pair<Report_Info, CacheCB>> SubscriberInfoPair;
+ typedef std::function<OCStackResult(std::shared_ptr<PrimitiveResource>,
+ const ResourceAttributes &)> 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 PrimitiveResource::GetCallback GetCB;
-typedef PrimitiveResource::ObserveCallback ObserveCB;
+ typedef OC::OCResource BaseResource;
+ typedef PrimitiveResource::GetCallback GetCB;
+ typedef PrimitiveResource::ObserveCallback ObserveCB;
-typedef std::shared_ptr<DataCache> DataCachePtr;
-typedef std::shared_ptr<PrimitiveResource> PrimitiveResourcePtr;
+ typedef std::shared_ptr<DataCache> DataCachePtr;
+ typedef std::shared_ptr<PrimitiveResource> PrimitiveResourcePtr;
+ } // namespace Service
+} // namespace OIC
-#endif
+#endif /* RCM_CACHETYPES_H */
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-#ifndef DATACACHE_H_
-#define DATACACHE_H_
+#ifndef RCM_DATACACHE_H_
+#define RCM_DATACACHE_H_
#include <list>
#include <string>
#include <memory>
-#include "OCResource.h"
-#include "logger.h"
-
#include "CacheTypes.h"
#include "ExpiryTimer.h"
-class DataCache
+namespace OIC
{
-public:
- DataCache(
- PrimitiveResourcePtr pResource,
- CacheCB func,
- REPORT_FREQUENCY rf,
- long repeatTime);
- ~DataCache();
-
- CacheID addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatTime);
- CacheID deleteSubscriber(CacheID id);
-
- CACHE_STATE getCacheState() const;
- const ResourceAttributes getCachedData() const;
- const PrimitiveResourcePtr getPrimitiveResource() const;
-
- SubscriberInfoPair findSubscriber(CacheID id);
-
-private:
- // resource instance
- PrimitiveResourcePtr sResource;
- std::shared_ptr<BaseResource> baseHandler;
-
- // cached data info
- ResourceAttributes attributes;
- CACHE_STATE state;
-
- // subscriber info
- std::unique_ptr<SubscriberInfo> subscriberList;
-
- // timer info
- ExpiryTimer *timerInstance;
- TimerID expiredTimerId;
-
- // 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();
-
-};
-
-#endif /* DATACACHE_H_ */
+ namespace Service
+ {
+ class DataCache
+ {
+ public:
+ DataCache();
+ ~DataCache();
+
+ void initializeDataCache(PrimitiveResourcePtr pResource);
+
+ CacheID addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatTime);
+ CacheID deleteSubscriber(CacheID id);
+
+ CACHE_STATE getCacheState() const;
+ const ResourceAttributes getCachedData() const;
+ const PrimitiveResourcePtr getPrimitiveResource() const;
+
+ void requestGet();
+
+ private:
+ // resource instance
+ PrimitiveResourcePtr sResource;
+ std::shared_ptr<BaseResource> baseHandler;
+
+ // cached data info
+ ResourceAttributes attributes;
+ CACHE_STATE state;
+
+ // subscriber info
+ std::unique_ptr<SubscriberInfo> subscriberList;
+
+ ExpiryTimer networkTimer;
+ ExpiryTimer pollingTimer;
+ TimerID networkTimeOutHandle;
+ TimerID pollingHandle;
+
+ ObserveCB pObserveCB;
+ GetCB pGetCB;
+ TimerCB pTimerCB;
+ TimerCB pPollingCB;
+
+ // 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 *onTimeOut(const unsigned int timerID);
+ void *onPollingOut(const unsigned int timerID);
+
+ CacheID generateCacheID();
+ SubscriberInfoPair findSubscriber(CacheID id);
+ void notifyObservers(ResourceAttributes Att);
+ };
+ } // namespace Service
+} // namespace OIC
+
+#endif /* RCM_DATACACHE_H_ */
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-#ifndef RESOURCECACHEMANAGER_H_
-#define RESOURCECACHEMANAGER_H_
+#ifndef RCM_RESOURCECACHEMANAGER_H_
+#define RCM_RESOURCECACHEMANAGER_H_
#include <list>
#include <string>
#include <mutex>
+#include <map>
-#include "OCPlatform.h"
#include "CacheTypes.h"
#include "DataCache.h"
-#define CACHE_TAG PCF("CACHE")
-
-class ResourceCacheManager
+namespace OIC
{
-public:
- static ResourceCacheManager * getInstance();
+ namespace Service
+ {
+ class ResourceCacheManager
+ {
+ public:
+ class InvalidParameter: public PrimitiveException
+ {
+ public:
+ InvalidParameter(std::string&& what) : PrimitiveException{ std::move(what) } {}
+ };
+
+ static ResourceCacheManager * getInstance();
+
+ CacheID requestResourceCache(
+ PrimitiveResourcePtr pResource, CacheCB func = NULL,
+ REPORT_FREQUENCY rf = REPORT_FREQUENCY::NONE, long time = 0l);
+ void cancelResourceCache(CacheID id);
+
+ void updateResourceCache(PrimitiveResourcePtr pResource) const;
+ void updateResourceCache(CacheID id) const;
- CacheID requestResourceCache(
- PrimitiveResourcePtr pResource,
- CacheCB func = NULL, REPORT_FREQUENCY rf = REPORT_FREQUENCY::NONE, long time = 0l);
- CacheID cancelResourceCache(CacheID id);
- OCStackResult updateResourceCache(PrimitiveResourcePtr pResource);
+ const ResourceAttributes getCachedData(PrimitiveResourcePtr pResource) const;
+ const ResourceAttributes getCachedData(CacheID id) const;
- const ResourceAttributes getCachedData(PrimitiveResourcePtr pResource) const;
- const ResourceAttributes getCachedData(CacheID id) const;
+ CACHE_STATE getResourceCacheState(PrimitiveResourcePtr pResource) const;
+ CACHE_STATE getResourceCacheState(CacheID id) const;
- CACHE_STATE getResourceCacheState(PrimitiveResourcePtr pResource) const;
- CACHE_STATE getResourceCacheState(CacheID id) const;
+ private:
+ static ResourceCacheManager * s_instance;
+ static std::mutex s_mutexForCreation;
+ static std::unique_ptr<std::list<DataCachePtr>> s_cacheDataList;
+ std::map<CacheID, DataCachePtr> cacheIDmap;
- ~ResourceCacheManager();
-private:
- ResourceCacheManager();
+ ResourceCacheManager() = default;
+ ~ResourceCacheManager();
+ ResourceCacheManager(const ResourceCacheManager&) = delete;
+ ResourceCacheManager(ResourceCacheManager&&) = delete;
- static ResourceCacheManager * s_instance;
- static std::mutex s_mutexForCreation;
- static std::unique_ptr<std::list<DataCachePtr>> s_cacheDataList;
+ ResourceCacheManager& operator=(const ResourceCacheManager&) const = delete;
+ ResourceCacheManager& operator=(ResourceCacheManager&&) const = delete;
- DataCachePtr findDataCache(PrimitiveResourcePtr pResource) const;
- DataCachePtr findDataCache(CacheID id) const;
-};
+ static void initializeResourceCacheManager();
+ DataCachePtr findDataCache(PrimitiveResourcePtr pResource) const;
+ DataCachePtr findDataCache(CacheID id) const;
+ };
+ } // namespace Service
+} // namespace OIC
-#endif /* RESOURCECACHEMANAGER_H_ */
+#endif /* RCM_RESOURCECACHEMANAGER_H_ */
#include <utility>
#include <ctime>
-#include "OCApi.h"
+//#include "OCApi.h"
#include "DataCache.h"
#include "ResourceAttributes.h"
#include "ExpiryTimer.h"
-DataCache::DataCache(
- PrimitiveResourcePtr pResource,
- CacheCB func,
- REPORT_FREQUENCY rf,
- long repeatTime
- ):sResource(pResource)
+namespace OIC
{
- subscriberList = std::unique_ptr<SubscriberInfo>(new SubscriberInfo());
-
- timerInstance = new ExpiryTimer;
- state = CACHE_STATE::READY_YET;
-
- pObserveCB = (ObserveCB)(std::bind(&DataCache::onObserve, this,
- std::placeholders::_1, std::placeholders::_2,
- 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())
+ namespace Service
{
- pResource->requestObserve(pObserveCB);
- expiredTimerId = timerInstance->requestTimer(DEFAULT_EXPIRED_TIME, pTimerCB);
- }
-}
+ DataCache::DataCache()
+ {
+ subscriberList = std::unique_ptr<SubscriberInfo>(new SubscriberInfo());
-DataCache::~DataCache()
-{
- // TODO Auto-generated destructor stub
+ sResource = nullptr;
- // TODO delete all node!!
- subscriberList->clear();
- subscriberList.release();
-}
+ state = CACHE_STATE::READY_YET;
-CacheID DataCache::addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatTime)
-{
- Report_Info newItem;
- newItem.rf = rf;
- newItem.repeatTime = repeatTime;
- newItem.timerID = 0;
+ pObserveCB = (ObserveCB)(std::bind(&DataCache::onObserve, this,
+ std::placeholders::_1, std::placeholders::_2,
+ 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::onTimeOut, this, std::placeholders::_1));
+ pPollingCB = (TimerCB)(std::bind(&DataCache::onPollingOut, this, std::placeholders::_1));
- srand(time(NULL));
- newItem.reportID = rand();
+ }
- while(1)
- {
- if(findSubscriber(newItem.reportID).first != 0 || newItem.reportID == 0)
+ DataCache::~DataCache()
{
- newItem.reportID = rand();
+ state = CACHE_STATE::DESTROYED;
+ if(subscriberList != nullptr)
+ {
+ subscriberList->clear();
+ subscriberList.release();
+ }
}
- else
+
+ void DataCache::initializeDataCache(PrimitiveResourcePtr pResource)
{
- break;
+ sResource = pResource;
+
+ sResource->requestGet(pGetCB);
+ if(sResource->isObservable())
+ {
+ sResource->requestObserve(pObserveCB);
+ }
+ networkTimeOutHandle = networkTimer.requestTimer(DEFAULT_EXPIRED_TIME, pTimerCB);
}
- }
- TimerID timerId = timerInstance->requestTimer(repeatTime, pTimerCB);
- newItem.timerID = timerId;
+ CacheID DataCache::addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatTime)
+ {
+ Report_Info newItem;
+ newItem.rf = rf;
+ newItem.repeatTime = repeatTime;
+ newItem.timerID = 0;
- subscriberList->insert(std::make_pair(newItem.reportID, std::make_pair(newItem, func)));
+ newItem.reportID = generateCacheID();
+ if(subscriberList != nullptr)
+ {
+ subscriberList->insert(std::make_pair(newItem.reportID, std::make_pair(newItem, func)));
+ }
- return newItem.reportID;
-}
+ return newItem.reportID;
+ }
-CacheID DataCache::deleteSubscriber(CacheID id)
-{
- CacheID ret = 0;
+ CacheID DataCache::deleteSubscriber(CacheID id)
+ {
+ CacheID ret = 0;
- SubscriberInfoPair pair = findSubscriber(id);
- if(pair.first != 0)
- {
- ret = pair.first;
- subscriberList->erase(pair.first);
- }
+ SubscriberInfoPair pair = findSubscriber(id);
+ if(pair.first != 0)
+ {
+ ret = pair.first;
+ subscriberList->erase(pair.first);
+ }
- return ret;
-}
+ return ret;
+ }
-SubscriberInfoPair DataCache::findSubscriber(CacheID id)
-{
- SubscriberInfoPair ret;
+ SubscriberInfoPair DataCache::findSubscriber(CacheID id)
+ {
+ SubscriberInfoPair ret;
- for(auto & i : *subscriberList)
- {
- if(i.first == id)
+ for(auto & i : *subscriberList)
+ {
+ if(i.first == id)
+ {
+ ret = std::make_pair(i.first, std::make_pair((Report_Info)i.second.first,
+ (CacheCB)i.second.second));
+ break;
+ }
+ }
+
+ return ret;
+ }
+
+ const PrimitiveResourcePtr DataCache::getPrimitiveResource() const
{
- ret = std::make_pair(i.first, std::make_pair((Report_Info)i.second.first, (CacheCB)i.second.second));
+ return (sResource!=nullptr)?sResource:nullptr;
}
- }
- return ret;
-}
+ const ResourceAttributes DataCache::getCachedData() const
+ {
+ if(state != CACHE_STATE::READY || attributes.empty())
+ {
+ return ResourceAttributes();
+ }
+ const ResourceAttributes retAtt = attributes;
+ return retAtt;
+ }
-const PrimitiveResourcePtr DataCache::getPrimitiveResource() const
-{
- return sResource;
-}
+ void DataCache::onObserve(
+ const HeaderOptions& _hos, const ResponseStatement& _rep, int _result, int _seq)
+ {
-const ResourceAttributes DataCache::getCachedData() const
-{
- if(state != CACHE_STATE::READY)
- {
- return ResourceAttributes();
- }
- const ResourceAttributes retAtt = attributes;
+ if(_result != OC_STACK_OK || _rep.getAttributes().empty())
+ {
+ return;
+ }
- return retAtt;
-}
+ if(state != CACHE_STATE::READY)
+ {
+ state = CACHE_STATE::READY;
+ }
-void DataCache::onObserve(
- const HeaderOptions& _hos, const ResponseStatement& _rep, int _result, int _seq)
-{
+ networkTimer.cancelTimer(networkTimeOutHandle);
+ networkTimeOutHandle = networkTimer.requestTimer(DEFAULT_EXPIRED_TIME, pTimerCB);
- if(_result != OC_STACK_OK)
- {
- // TODO handle error
- return;
- }
+ notifyObservers(_rep.getAttributes());
+ }
- if(_rep.getAttributes().empty())
- {
- return;
- }
+ void DataCache::onGet(const HeaderOptions& _hos,
+ const ResponseStatement& _rep, int _result)
+ {
+ if(_result != OC_STACK_OK || _rep.getAttributes().empty())
+ {
+ return;
+ }
- state = CACHE_STATE::READY;
+ if(state != CACHE_STATE::READY)
+ {
+ state = CACHE_STATE::READY;
+ }
- attributes = _rep.getAttributes();
+ if(!sResource->isObservable())
+ {
+ networkTimer.cancelTimer(networkTimeOutHandle);
+ networkTimeOutHandle = networkTimer.requestTimer(DEFAULT_EXPIRED_TIME, pTimerCB);
- if(sResource->isObservable())
- {
- timerInstance->cancelTimer(expiredTimerId);
- expiredTimerId = timerInstance->requestTimer(DEFAULT_EXPIRED_TIME, pTimerCB);
- }
+ pollingHandle = pollingTimer.requestTimer(DEFAULT_REPORT_TIME, pPollingCB);
+ }
- // notify!!
- ResourceAttributes retAtt = attributes;
- for(auto & i : * subscriberList)
- {
- if(i.second.first.rf == REPORT_FREQUENCY::UPTODATE)
- {
- i.second.second(this->sResource, retAtt);
+ notifyObservers(_rep.getAttributes());
}
- }
-}
-void DataCache::onGet(const HeaderOptions& _hos,
- const ResponseStatement& _rep, int _result)
-{
- if(state == CACHE_STATE::READY_YET)
- {
- state = CACHE_STATE::READY;
- attributes = _rep.getAttributes();
- if(sResource->isObservable())
+ void DataCache::notifyObservers(ResourceAttributes Att)
{
- timerInstance->cancelTimer(expiredTimerId);
- expiredTimerId = timerInstance->requestTimer(DEFAULT_EXPIRED_TIME, pTimerCB);
+ if(attributes == Att)
+ {
+ return;
+ }
+
+ attributes = Att;
+
+ ResourceAttributes retAtt = Att;
+ for(auto & i : * subscriberList)
+ {
+ if(i.second.first.rf == REPORT_FREQUENCY::UPTODATE)
+ {
+ i.second.second(this->sResource, retAtt);
+ }
+ }
}
- }
- else
- {
- attributes = _rep.getAttributes();
- if(sResource->isObservable())
+
+ CACHE_STATE DataCache::getCacheState() const
{
- timerInstance->cancelTimer(expiredTimerId);
- expiredTimerId = timerInstance->requestTimer(DEFAULT_EXPIRED_TIME, pTimerCB);
+ return state;
}
- ResourceAttributes retAtt = attributes;
- for(auto & i : * subscriberList)
+ void *DataCache::onTimeOut(unsigned int timerID)
+ {
+ state = CACHE_STATE::LOST_SIGNAL;
+ return NULL;
+ }
+ void *DataCache::onPollingOut(const unsigned int timerID)
{
- if(i.second.first.rf != REPORT_FREQUENCY::NONE)
+ if(sResource != nullptr)
{
- i.second.second(this->sResource, retAtt);
+ sResource->requestGet(pGetCB);
}
+ return NULL;
}
- }
-}
-CACHE_STATE DataCache::getCacheState() const
-{
- return state;
-}
+ CacheID DataCache::generateCacheID()
+ {
+ CacheID retID = 0;
+ srand(time(NULL));
-void *DataCache::onTimer(const unsigned int timerID)
-{
- sResource->requestGet(pGetCB);
- if(sResource->isObservable())
- {
- expiredTimerId = timerInstance->requestTimer(DEFAULT_EXPIRED_TIME, pTimerCB);
- }
- else
- {
- for(auto & i : * subscriberList)
+ while(1)
+ {
+ if(findSubscriber(retID).first == 0 && retID != 0)
+ {
+ break;
+ }
+ retID = rand();
+ }
+
+ return retID;
+ }
+
+ void DataCache::requestGet()
{
- if(i.second.first.timerID == timerID)
+ state = CACHE_STATE::UPDATING;
+ if(sResource != nullptr)
{
- TimerID timerId = timerInstance->requestTimer(i.second.first.repeatTime, pTimerCB);
- i.second.first.timerID = timerId;
- break;
+ sResource->requestGet(pGetCB);
}
}
- }
-}
\ No newline at end of file
+ } // namespace Service
+} // namespace OIC
#include "ResourceCacheManager.h"
-ResourceCacheManager * ResourceCacheManager::s_instance = NULL;
-std::mutex ResourceCacheManager::s_mutexForCreation;
-std::unique_ptr<std::list<DataCachePtr>> ResourceCacheManager::s_cacheDataList(nullptr);
-
-ResourceCacheManager::ResourceCacheManager()
+namespace OIC
{
- // TODO Auto-generated constructor stub
- if(!s_cacheDataList)
+ namespace Service
{
- s_cacheDataList = std::unique_ptr<std::list<DataCachePtr>>(new std::list<DataCachePtr>);
- }
-}
+ ResourceCacheManager * ResourceCacheManager::s_instance = NULL;
+ std::mutex ResourceCacheManager::s_mutexForCreation;
+ std::unique_ptr<std::list<DataCachePtr>> ResourceCacheManager::s_cacheDataList(nullptr);
-ResourceCacheManager::~ResourceCacheManager()
-{
- // TODO Auto-generated destructor stub
- if(s_cacheDataList)
- {
- s_cacheDataList->clear();
- }
-}
+ ResourceCacheManager::~ResourceCacheManager()
+ {
+ if(s_cacheDataList != nullptr)
+ {
+ s_cacheDataList->clear();
+ }
+ }
-ResourceCacheManager * ResourceCacheManager::getInstance()
-{
- if(s_instance == nullptr)
- {
- s_mutexForCreation.lock();
- if(s_instance == nullptr)
+ ResourceCacheManager * ResourceCacheManager::getInstance()
{
- s_instance = new ResourceCacheManager();
+ if(s_instance == nullptr)
+ {
+ s_mutexForCreation.lock();
+ if(s_instance == nullptr)
+ {
+ s_instance = new ResourceCacheManager();
+ s_instance->initializeResourceCacheManager();
+ }
+ s_mutexForCreation.unlock();
+ }
+ return s_instance;
}
- s_mutexForCreation.unlock();
- }
- return s_instance;
-}
-
-CacheID ResourceCacheManager::requestResourceCache(
- PrimitiveResourcePtr pResource, CacheCB func,
- REPORT_FREQUENCY rf, long reportTime)
-{
- CacheID retID = 0;
- if(rf != REPORT_FREQUENCY::NONE)
- {
- if(func == NULL)
+ CacheID ResourceCacheManager::requestResourceCache(
+ PrimitiveResourcePtr pResource, CacheCB func,
+ REPORT_FREQUENCY rf, long reportTime)
{
+ if(pResource == nullptr)
+ {
+ throw InvalidParameter{"[requestResourceCache] Primitive Resource is invaild"};
+ }
+
+ CacheID retID = 0;
+
+ if(rf != REPORT_FREQUENCY::NONE)
+ {
+ if(func == NULL || func == nullptr)
+ {
+ throw InvalidParameter{"[requestResourceCache] CacheCB is invaild"};
+ }
+ if(!reportTime)
+ {
+ // default setting
+ reportTime = DEFAULT_REPORT_TIME;
+ }
+ }
+
+ DataCachePtr newHandler = findDataCache(pResource);
+ if(newHandler == nullptr)
+ {
+ newHandler.reset(new DataCache());
+ newHandler->initializeDataCache(pResource);
+ s_cacheDataList->push_back(newHandler);
+ }
+ retID = newHandler->addSubscriber(func, rf, reportTime);
+
+ cacheIDmap.insert(std::make_pair(retID, newHandler));
+
return retID;
}
- if(!reportTime)
+
+ void ResourceCacheManager::cancelResourceCache(CacheID id)
{
- // default setting
- reportTime = DEFAULT_REPORT_TIME;
+ if(id == 0 || cacheIDmap.find(id) == cacheIDmap.end())
+ {
+ throw InvalidParameter{"[cancelResourceCache] CacheID is invaild"};
+ }
+
+ DataCachePtr foundCacheHandler = findDataCache(id);
+ if(foundCacheHandler != nullptr)
+ {
+ CacheID retID = foundCacheHandler->deleteSubscriber(id);
+ if(retID == id)
+ {
+ cacheIDmap.erase(id);
+ }
+ }
}
- }
-
- DataCachePtr newHandler = findDataCache(pResource);
- if(newHandler == nullptr)
- {
- newHandler = std::make_shared<DataCache>(pResource, func, rf, reportTime);
- s_cacheDataList->push_back(newHandler);
- }
- retID = newHandler->addSubscriber(func, rf, reportTime);
-
- return retID;
-}
-
-CacheID ResourceCacheManager::cancelResourceCache(CacheID id)
-{
- CacheID retID = 0;
- if(id == 0)
- {
- return retID;
- }
-
- DataCachePtr foundCacheHandler = findDataCache(id);
- if(foundCacheHandler == nullptr)
- {
- return retID;
- }
- else
- {
- retID = foundCacheHandler->deleteSubscriber(id);
- }
-
- return retID;
-}
-DataCachePtr ResourceCacheManager::findDataCache(PrimitiveResourcePtr pResource) const
-{
- DataCachePtr retHandler = nullptr;
- for (auto & i : * s_cacheDataList)
- {
- if(i->getPrimitiveResource()->getUri() == pResource->getUri() &&
- i->getPrimitiveResource()->getHost() == pResource->getHost())
+ void ResourceCacheManager::updateResourceCache(PrimitiveResourcePtr pResource) const
+ {
+ if(pResource == nullptr)
+ {
+ throw InvalidParameter{"[updateResourceCache] Primitive Resource is invaild"};
+ }
+
+ DataCachePtr foundCache = findDataCache(pResource);
+ if(foundCache == nullptr)
+ {
+ throw InvalidParameter{"[updateResourceCache] Primitive Resource is invaild"};
+ }
+ foundCache->requestGet();
+ }
+ void ResourceCacheManager::updateResourceCache(CacheID updateId) const
{
- retHandler = i;
- break;
+ if(updateId == 0)
+ {
+ throw InvalidParameter{"[getCachedData] CacheID is invaild"};
+ }
+
+ DataCachePtr foundCache = findDataCache(updateId);
+ if(foundCache == nullptr)
+ {
+ throw InvalidParameter{"[getCachedData] CacheID is invaild"};
+ }
+ foundCache->requestGet();
}
- }
- return retHandler;
-}
-DataCachePtr ResourceCacheManager::findDataCache(CacheID id) const
-{
- DataCachePtr retHandler = nullptr;
- for (auto & i : * s_cacheDataList)
- {
- SubscriberInfoPair pair = i->findSubscriber(id);
- if(pair.first != 0)
+ const ResourceAttributes ResourceCacheManager::getCachedData(
+ PrimitiveResourcePtr pResource) const
{
- retHandler = i;
- break;
+ if(pResource == nullptr)
+ {
+ throw InvalidParameter{"[getCachedData] Primitive Resource is invaild"};
+ }
+
+ DataCachePtr handler = findDataCache(pResource);
+ if(handler == nullptr)
+ {
+ throw InvalidParameter{"[getCachedData] Primitive Resource is invaild"};
+ }
+ return handler->getCachedData();
}
- }
- return retHandler;
-}
-OCStackResult ResourceCacheManager::updateResourceCache(PrimitiveResourcePtr pResource)
-{
- OCStackResult ret = OC_STACK_ERROR;
+ const ResourceAttributes ResourceCacheManager::getCachedData(CacheID id) const
+ {
+ if(id == 0)
+ {
+ throw InvalidParameter{"[getCachedData] CacheID is invaild"};
+ }
+
+ DataCachePtr handler = findDataCache(id);
+ if(handler == nullptr)
+ {
+ throw InvalidParameter{"[getCachedData] CacheID is invaild"};
+ }
+ return handler->getCachedData();
+ }
- // TODO update now (request get)
+ CACHE_STATE ResourceCacheManager::getResourceCacheState(
+ PrimitiveResourcePtr pResource) const
+ {
+ if(pResource == nullptr)
+ {
+ throw InvalidParameter{"[getResourceCacheState] Primitive Resource is invaild"};
+ }
+
+ DataCachePtr handler = findDataCache(pResource);
+ if(handler == nullptr)
+ {
+ return CACHE_STATE::NONE;
+ }
+ return handler->getCacheState();
+ }
- return ret;
-}
+ CACHE_STATE ResourceCacheManager::getResourceCacheState(CacheID id) const
+ {
+ if(id == 0)
+ {
+ throw InvalidParameter{"[getResourceCacheState] CacheID is invaild"};
+ }
+
+ DataCachePtr handler = findDataCache(id);
+ if(handler == nullptr)
+ {
+ return CACHE_STATE::NONE;
+ }
+ return handler->getCacheState();
+ }
-const ResourceAttributes ResourceCacheManager::getCachedData(PrimitiveResourcePtr pResource) const
-{
- DataCachePtr handler = findDataCache(pResource);
- if(handler == nullptr)
- {
- return ResourceAttributes();
- }
- return handler->getCachedData();
-}
+ void ResourceCacheManager::initializeResourceCacheManager()
+ {
+ if(s_cacheDataList == nullptr)
+ {
+ s_cacheDataList
+ = std::unique_ptr<std::list<DataCachePtr>>(new std::list<DataCachePtr>);
+ }
+ }
-const ResourceAttributes ResourceCacheManager::getCachedData(CacheID id) const
-{
- DataCachePtr handler = findDataCache(id);
- if(handler == nullptr)
- {
- return ResourceAttributes();
- }
- return handler->getCachedData();
-}
+ DataCachePtr ResourceCacheManager::findDataCache(PrimitiveResourcePtr pResource) const
+ {
+ DataCachePtr retHandler = nullptr;
+ for (auto & i : * s_cacheDataList)
+ {
+ if(i->getPrimitiveResource()->getUri() == pResource->getUri() &&
+ i->getPrimitiveResource()->getHost() == pResource->getHost())
+ {
+ retHandler = i;
+ break;
+ }
+ }
+ return retHandler;
+ }
-CACHE_STATE ResourceCacheManager::getResourceCacheState(PrimitiveResourcePtr pResource) const
-{
- DataCachePtr handler = findDataCache(pResource);
- if(handler == nullptr)
- {
- return CACHE_STATE::NONE;
- }
- return handler->getCacheState();
-}
-CACHE_STATE ResourceCacheManager::getResourceCacheState(CacheID id) const
-{
- DataCachePtr handler = findDataCache(id);
- if(handler == nullptr)
- {
- return CACHE_STATE::NONE;
- }
- return handler->getCacheState();
-}
+ DataCachePtr ResourceCacheManager::findDataCache(CacheID id) const
+ {
+ DataCachePtr retHandler = nullptr;
+ for(auto it : cacheIDmap)
+ {
+ if(it.first == id)
+ {
+ retHandler = it.second;
+ break;
+ }
+ }
+
+ return retHandler;
+ }
+ } // namespace Service
+} // namespace OIC