1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "ObserveCache.h"
22 #include "RCSException.h"
28 ObserveCache::ObserveCache(std::weak_ptr<PrimitiveResource> pResource)
29 : m_wpResource(pResource), m_attributes(), m_state(CACHE_STATE::NONE),
30 m_reportCB(), m_isStart(false), m_id(0)
34 void ObserveCache::startCache(DataCacheCB func)
38 throw RCSBadRequestException{ "Caching already started." };
41 m_reportCB = std::move(func);
43 auto resource = m_wpResource.lock();
44 if (resource == nullptr)
47 throw RCSBadRequestException{ "Resource was not initialized." };
50 if (resource->isObservable())
52 resource->requestObserve(
53 std::bind(&ObserveCache::verifyObserveCB,
54 std::placeholders::_1, std::placeholders::_2,
55 std::placeholders::_3, std::placeholders::_4,
60 throw RCSBadRequestException{ "Can't observe, Never updated data." };
64 m_state = CACHE_STATE::READY_YET;
67 void ObserveCache::stopCache()
69 auto resource = m_wpResource.lock();
70 if (resource != nullptr)
72 resource->cancelObserve();
76 m_state = CACHE_STATE::NONE;
82 CACHE_STATE ObserveCache::getCacheState() const
87 RCSResourceAttributes ObserveCache::getCachedData() const
92 bool ObserveCache::isCachedData() const
94 return !m_attributes.empty();
97 bool ObserveCache::isStartCache() const
102 void ObserveCache::onObserve(const HeaderOptions &,
103 const ResponseStatement & rep, int _result, unsigned int)
105 m_state = CACHE_STATE::READY;
107 if (m_attributes == rep.getAttributes() &&
108 convertOCResultToSuccess((OCStackResult)_result))
115 m_attributes = rep.getAttributes();
116 m_reportCB(m_wpResource.lock(), m_attributes, _result);
120 void ObserveCache::verifyObserveCB(const HeaderOptions &_hos,
121 const ResponseStatement &_rep, int _result,
122 unsigned int _seq, weakDataCache wPtr)
124 auto ptr = wPtr.lock();
127 ptr->onObserve(_hos, _rep, _result, _seq);
131 bool ObserveCache::convertOCResultToSuccess(OCStackResult ret)
136 case OC_STACK_RESOURCE_CREATED:
137 case OC_STACK_RESOURCE_DELETED:
138 case OC_STACK_PRESENCE_STOPPED:
139 case OC_STACK_CONTINUE:
140 case OC_STACK_RESOURCE_CHANGED: