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>
void requestGet();
bool isEmptySubscriber() const;
+ bool isCachedData() const;
private:
// resource instance
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;
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;
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)
{
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)
{
networkTimeOutHandle = 0;
pollingHandle = 0;
lastSequenceNum = 0;
+ isReady = false;
}
DataCache::~DataCache()
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)
if (state != CACHE_STATE::READY)
{
state = CACHE_STATE::READY;
+ isReady = true;
}
if (mode != CACHE_MODE::OBSERVE)
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())
if (state != CACHE_STATE::READY)
{
state = CACHE_STATE::READY;
+ isReady = true;
}
if (mode != CACHE_MODE::OBSERVE)
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)
{
return state;
}
- void DataCache::onTimeOut(unsigned int timerID)
+ void DataCache::onTimeOut(unsigned int /*timerID*/)
{
if(mode == CACHE_MODE::OBSERVE)
{
state = CACHE_STATE::LOST_SIGNAL;
}
- void DataCache::onPollingOut(const unsigned int timerID)
+ void DataCache::onPollingOut(const unsigned int /*timerID*/)
{
if (sResource != nullptr)
{
{
throw InvalidParameterException {"[getCachedData] Primitive Resource is invaild"};
}
+
+ if(handler->isCachedData() == false)
+ {
+ throw HasNoCachedDataException {"[getCachedData] Cached Data is not stored"};
+ }
+
return handler->getCachedData();
}
{
throw InvalidParameterException {"[getCachedData] CacheID is invaild"};
}
+
+ if(handler->isCachedData() == false)
+ {
+ throw HasNoCachedDataException {"[getCachedData] Cached Data is not stored"};
+ }
+
return handler->getCachedData();
}
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);