From 45053652a06c2c7939af3433d59d4dc6a5eedb26 Mon Sep 17 00:00:00 2001 From: Senthil Kumar G S Date: Fri, 26 Jul 2019 18:45:21 +0530 Subject: [PATCH] Observation without caching. Added support to receive all observe notifications. Background:- RE ignores the observe notification if there is no change in the representation. This patch introduces a default argument in"RCSRemoteResourceObject::startCaching" to let users decide whether all notifications need to be reported or not. API Usage: startCaching(std::move(cb), CacheMode::OBSERVE_ONLY, CacheReport::REPORT_ALL); Note: Existing functionalities/usages are not affected. https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/pull/378/commits/8be52525d302ef933fda2ca4032fbd716b40582b (cherry-picked from 8be52525d302ef933fda2ca4032fbd716b40582b) Change-Id: Ib96e91da07edcdeb9bcedd4cf733f7966bd83e50 Signed-off-by: Senthil Kumar G S Signed-off-by: Sudipto --- .../include/RCSRemoteResourceObject.h | 15 ++++++++++++++- .../src/resourceCache/include/CacheTypes.h | 3 ++- .../src/resourceCache/include/ObserveCache.h | 6 +++--- .../src/resourceCache/src/DataCache.cpp | 13 +++++++++---- .../src/resourceCache/src/ObserveCache.cpp | 15 +++++++++------ .../src/resourceCache/src/ResourceCacheManager.cpp | 2 +- .../src/resourceClient/RCSRemoteResourceObject.cpp | 17 ++++++++++++++--- 7 files changed, 52 insertions(+), 19 deletions(-) diff --git a/service/resource-encapsulation/include/RCSRemoteResourceObject.h b/service/resource-encapsulation/include/RCSRemoteResourceObject.h index e5af9a2..05b189c 100644 --- a/service/resource-encapsulation/include/RCSRemoteResourceObject.h +++ b/service/resource-encapsulation/include/RCSRemoteResourceObject.h @@ -88,6 +88,17 @@ namespace OIC DESTROYED /**< The resource is deleted. */ }; + /** + * The options available to report cache. + * + * @see startCaching + */ + enum class CacheReport + { + REPORT_CHANGES, /**< Report only when there is any change in the representation.*/ + REPORT_ALL /**< Report all irrespective of the representation.*/ + }; + class PrimitiveResource; /** @@ -351,6 +362,7 @@ namespace OIC * * @param cb If non-empty function, it will be invoked whenever the cache updated. * @param mode if CacheMode is OBSERVE_ONLY, it will be invoked when receive observe response only. + * @param reportType if CacheReport is REPORT_CHANGES, it will be invoked when observe response has state changes. * * @throws BadRequestException If caching is already started. * @@ -363,7 +375,8 @@ namespace OIC * @see getCachedAttribute(const std::string&) const * */ - void startCaching(CacheUpdatedCallback cb, CacheMode mode = CacheMode::OBSERVE_WITH_POLLING); + void startCaching(CacheUpdatedCallback cb, CacheMode mode = CacheMode::OBSERVE_WITH_POLLING, + CacheReport reportType = CacheReport::REPORT_CHANGES); /** * Stops caching. diff --git a/service/resource-encapsulation/src/resourceCache/include/CacheTypes.h b/service/resource-encapsulation/src/resourceCache/include/CacheTypes.h index 737edf1..91a11a7 100644 --- a/service/resource-encapsulation/src/resourceCache/include/CacheTypes.h +++ b/service/resource-encapsulation/src/resourceCache/include/CacheTypes.h @@ -46,7 +46,8 @@ namespace OIC { NONE = 0, UPTODATE, - PERIODICTY + PERIODICTY, + WHENEVER_NOTIFIED }; struct Report_Info diff --git a/service/resource-encapsulation/src/resourceCache/include/ObserveCache.h b/service/resource-encapsulation/src/resourceCache/include/ObserveCache.h index 73ce388..1c19f90 100644 --- a/service/resource-encapsulation/src/resourceCache/include/ObserveCache.h +++ b/service/resource-encapsulation/src/resourceCache/include/ObserveCache.h @@ -51,7 +51,7 @@ namespace OIC ObserveCache & operator = (const ObserveCache &) = delete; ObserveCache & operator = (ObserveCache &&) = delete; - void startCache(DataCacheCB func); + void startCache(DataCacheCB func, bool reportAll = false); void stopCache(); CACHE_STATE getCacheState() const; @@ -83,9 +83,9 @@ namespace OIC private: static void verifyObserveCB(const HeaderOptions &_hos, const ResponseStatement &_rep, int _result, - unsigned int _seq, weakDataCache ptr); + unsigned int _seq, weakDataCache ptr, bool reportAll); void onObserve(const HeaderOptions &_hos, - const ResponseStatement &_rep, int _result, unsigned int _seq); + const ResponseStatement &_rep, int _result, unsigned int _seq, bool reportAll); bool convertOCResultToSuccess(OCStackResult ret); }; } // namespace Service diff --git a/service/resource-encapsulation/src/resourceCache/src/DataCache.cpp b/service/resource-encapsulation/src/resourceCache/src/DataCache.cpp index 23ffd30..b397243 100644 --- a/service/resource-encapsulation/src/resourceCache/src/DataCache.cpp +++ b/service/resource-encapsulation/src/resourceCache/src/DataCache.cpp @@ -255,19 +255,24 @@ namespace OIC void DataCache::notifyObservers(const RCSResourceAttributes Att, int eCode) { + bool same = true; { std::lock_guard lock(att_mutex); - if (attributes == Att) + if (attributes != Att) { - return; + same = false; + attributes = Att; } - attributes = Att; } std::lock_guard lock(m_mutex); for (auto &i : * subscriberList) { - if (i.second.first.rf == REPORT_FREQUENCY::UPTODATE) + if (i.second.first.rf == REPORT_FREQUENCY::WHENEVER_NOTIFIED) + { + i.second.second(this->sResource, Att, eCode); + } + else if (i.second.first.rf == REPORT_FREQUENCY::UPTODATE && !same) { i.second.second(this->sResource, Att, eCode); } diff --git a/service/resource-encapsulation/src/resourceCache/src/ObserveCache.cpp b/service/resource-encapsulation/src/resourceCache/src/ObserveCache.cpp index c1b7a8f..cf0e128 100644 --- a/service/resource-encapsulation/src/resourceCache/src/ObserveCache.cpp +++ b/service/resource-encapsulation/src/resourceCache/src/ObserveCache.cpp @@ -31,7 +31,7 @@ namespace OIC { } - void ObserveCache::startCache(DataCacheCB func) + void ObserveCache::startCache(DataCacheCB func, bool reportAll) { if (m_isStart) { @@ -53,7 +53,7 @@ namespace OIC std::bind(&ObserveCache::verifyObserveCB, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, - shared_from_this())); + shared_from_this(), reportAll)); } else { @@ -100,12 +100,15 @@ namespace OIC } void ObserveCache::onObserve(const HeaderOptions &, - const ResponseStatement & rep, int _result, unsigned int) + const ResponseStatement & rep, int _result, unsigned int, bool reportAll) { + + std::cout << "onObserve (observecache.cpp) reportAll: " << reportAll << std::endl; + std::lock_guard lock(m_cbMutex); m_state = CACHE_STATE::READY; - if (m_attributes == rep.getAttributes() && + if (!reportAll && m_attributes == rep.getAttributes() && convertOCResultToSuccess((OCStackResult)_result)) { return; @@ -120,12 +123,12 @@ namespace OIC void ObserveCache::verifyObserveCB(const HeaderOptions &_hos, const ResponseStatement &_rep, int _result, - unsigned int _seq, weakDataCache wPtr) + unsigned int _seq, weakDataCache wPtr, bool reportAll) { auto ptr = wPtr.lock(); if (ptr) { - ptr->onObserve(_hos, _rep, _result, _seq); + ptr->onObserve(_hos, _rep, _result, _seq, reportAll); } } diff --git a/service/resource-encapsulation/src/resourceCache/src/ResourceCacheManager.cpp b/service/resource-encapsulation/src/resourceCache/src/ResourceCacheManager.cpp index 8a3f020..812c52e 100644 --- a/service/resource-encapsulation/src/resourceCache/src/ResourceCacheManager.cpp +++ b/service/resource-encapsulation/src/resourceCache/src/ResourceCacheManager.cpp @@ -87,7 +87,7 @@ namespace OIC } auto newHandler = std::make_shared(pResource); - newHandler->startCache(std::move(func)); + newHandler->startCache(std::move(func), (rf == REPORT_FREQUENCY::WHENEVER_NOTIFIED)); observeCacheIDmap.insert(std::make_pair(retID, newHandler)); return retID; diff --git a/service/resource-encapsulation/src/resourceClient/RCSRemoteResourceObject.cpp b/service/resource-encapsulation/src/resourceClient/RCSRemoteResourceObject.cpp index bc95aa9..26d83f9 100644 --- a/service/resource-encapsulation/src/resourceClient/RCSRemoteResourceObject.cpp +++ b/service/resource-encapsulation/src/resourceClient/RCSRemoteResourceObject.cpp @@ -313,7 +313,8 @@ namespace OIC startCaching({ }); } - void RCSRemoteResourceObject::startCaching(CacheUpdatedCallback cb, CacheMode mode) + void RCSRemoteResourceObject::startCaching(CacheUpdatedCallback cb, + CacheMode mode, CacheReport reportType) { SCOPE_LOG_F(DEBUG, TAG); @@ -323,6 +324,16 @@ namespace OIC throw RCSBadRequestException{ "Caching already started." }; } + REPORT_FREQUENCY freq; + if (reportType == CacheReport::REPORT_CHANGES) + { + freq = REPORT_FREQUENCY::UPTODATE; + } + else if(reportType == CacheReport::REPORT_ALL) + { + freq = REPORT_FREQUENCY::WHENEVER_NOTIFIED; + } + if (mode == CacheMode::OBSERVE_ONLY) { m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache( @@ -330,7 +341,7 @@ namespace OIC std::bind(cachingCallback, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::move(cb), shared_from_this()), CACHE_METHOD::OBSERVE_ONLY, - REPORT_FREQUENCY::UPTODATE, 0); + freq, 0); } else if (cb) @@ -340,7 +351,7 @@ namespace OIC std::bind(cachingCallback, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::move(cb), shared_from_this()), CACHE_METHOD::ITERATED_GET, - REPORT_FREQUENCY::UPTODATE, 0); + freq, 0); } else { -- 2.7.4