Observation without caching. 83/213983/1
authorSenthil Kumar G S <senthil.gs@samsung.com>
Fri, 26 Jul 2019 13:15:21 +0000 (18:45 +0530)
committerSudipto <sudipto.bal@samsung.com>
Mon, 16 Sep 2019 11:50:55 +0000 (17:20 +0530)
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 <senthil.gs@samsung.com>
Signed-off-by: Sudipto <sudipto.bal@samsung.com>
service/resource-encapsulation/include/RCSRemoteResourceObject.h
service/resource-encapsulation/src/resourceCache/include/CacheTypes.h
service/resource-encapsulation/src/resourceCache/include/ObserveCache.h
service/resource-encapsulation/src/resourceCache/src/DataCache.cpp
service/resource-encapsulation/src/resourceCache/src/ObserveCache.cpp
service/resource-encapsulation/src/resourceCache/src/ResourceCacheManager.cpp
service/resource-encapsulation/src/resourceClient/RCSRemoteResourceObject.cpp

index e5af9a2..05b189c 100644 (file)
@@ -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.
index 737edf1..91a11a7 100644 (file)
@@ -46,7 +46,8 @@ namespace OIC
         {
             NONE = 0,
             UPTODATE,
-            PERIODICTY
+            PERIODICTY,
+            WHENEVER_NOTIFIED
         };
 
         struct Report_Info
index 73ce388..1c19f90 100644 (file)
@@ -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
index 23ffd30..b397243 100644 (file)
@@ -255,19 +255,24 @@ namespace OIC
 
         void DataCache::notifyObservers(const RCSResourceAttributes Att, int eCode)
         {
+            bool same = true;
             {
                 std::lock_guard<std::mutex> lock(att_mutex);
-                if (attributes == Att)
+                if (attributes != Att)
                 {
-                    return;
+                    same = false;
+                    attributes = Att;
                 }
-                attributes = Att;
             }
 
             std::lock_guard<std::mutex> 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);
                 }
index c1b7a8f..cf0e128 100644 (file)
@@ -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<std::mutex> 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);
             }
         }
 
index 8a3f020..812c52e 100644 (file)
@@ -87,7 +87,7 @@ namespace OIC
                 }
 
                 auto newHandler = std::make_shared<ObserveCache>(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;
index bc95aa9..26d83f9 100644 (file)
@@ -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
             {