replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceCache / src / ObserveCache.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "ObserveCache.h"
22 #include "RCSException.h"
23
24 namespace OIC
25 {
26     namespace Service
27     {
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)
31         {
32         }
33
34         void ObserveCache::startCache(DataCacheCB func)
35         {
36             if (m_isStart)
37             {
38                 throw RCSBadRequestException{ "Caching already started." };
39             }
40
41             m_reportCB = std::move(func);
42
43             auto resource = m_wpResource.lock();
44             if (resource == nullptr)
45             {
46                 m_reportCB = nullptr;
47                 throw RCSBadRequestException{ "Resource was not initialized." };
48             }
49
50             if (resource->isObservable())
51             {
52                 resource->requestObserve(
53                         std::bind(&ObserveCache::verifyObserveCB,
54                                   std::placeholders::_1, std::placeholders::_2,
55                                   std::placeholders::_3, std::placeholders::_4,
56                                   shared_from_this()));
57             }
58             else
59             {
60                 throw RCSBadRequestException{ "Can't observe, Never updated data." };
61             }
62
63             m_isStart = true;
64             m_state = CACHE_STATE::READY_YET;
65         }
66
67         void ObserveCache::stopCache()
68         {
69             auto resource = m_wpResource.lock();
70             if (resource != nullptr)
71             {
72                 resource->cancelObserve();
73             }
74
75             m_reportCB = nullptr;
76             m_state = CACHE_STATE::NONE;
77
78             m_isStart = false;
79
80         }
81
82         CACHE_STATE ObserveCache::getCacheState() const
83         {
84             return m_state;
85         }
86
87         RCSResourceAttributes ObserveCache::getCachedData() const
88         {
89             return m_attributes;
90         }
91
92         bool ObserveCache::isCachedData() const
93         {
94             return !m_attributes.empty();
95         }
96
97         bool ObserveCache::isStartCache() const
98         {
99             return m_isStart;
100         }
101
102         void ObserveCache::onObserve(const HeaderOptions &,
103                        const ResponseStatement & rep, int _result, unsigned int)
104         {
105             m_state = CACHE_STATE::READY;
106
107             if (m_attributes == rep.getAttributes() &&
108                     convertOCResultToSuccess((OCStackResult)_result))
109             {
110                 return ;
111             }
112
113             if (m_reportCB)
114             {
115                 m_attributes = rep.getAttributes();
116                 m_reportCB(m_wpResource.lock(), m_attributes, _result);
117             }
118         }
119
120         void ObserveCache::verifyObserveCB(const HeaderOptions &_hos,
121                                     const ResponseStatement &_rep, int _result,
122                                     unsigned int _seq, weakDataCache wPtr)
123         {
124             auto ptr = wPtr.lock();
125             if (ptr)
126             {
127                 ptr->onObserve(_hos, _rep, _result, _seq);
128             }
129         }
130
131         bool ObserveCache::convertOCResultToSuccess(OCStackResult ret)
132         {
133             switch (ret)
134             {
135                 case OC_STACK_OK:
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:
141                     return true;
142                 default:
143                     return false;
144             }
145         }
146     }
147 }