73ce388c61a0d9f776ad749285eb86b79e751718
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceCache / include / ObserveCache.h
1 //******************************************************************
2 //
3 // Copyright 2016 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 #ifndef RCM_OBSERVECACHE_H_
22 #define RCM_OBSERVECACHE_H_
23
24 #include <list>
25 #include <string>
26 #include <memory>
27 #include <mutex>
28 #include <atomic>
29
30 #include "CacheTypes.h"
31 #include "PrimitiveResource.h"
32 #include "ExpiryTimer.h"
33
34 namespace OIC
35 {
36     namespace Service
37     {
38         class ObserveCache : public std::enable_shared_from_this<ObserveCache>
39         {
40             public:
41                 typedef std::function<OCStackResult(std::shared_ptr<PrimitiveResource>,
42                             const RCSResourceAttributes &, int)> DataCacheCB;
43                 typedef std::shared_ptr<ObserveCache> Ptr;
44
45             public:
46                 ObserveCache(std::weak_ptr<PrimitiveResource> pResource);
47                 ~ObserveCache() = default;
48
49                 ObserveCache(const ObserveCache &) = delete;
50                 ObserveCache(ObserveCache &&) = delete;
51                 ObserveCache & operator = (const ObserveCache &) = delete;
52                 ObserveCache & operator = (ObserveCache &&) = delete;
53
54                 void startCache(DataCacheCB func);
55                 void stopCache();
56
57                 CACHE_STATE getCacheState() const;
58
59                 RCSResourceAttributes getCachedData() const;
60
61                 bool isCachedData() const;
62                 bool isStartCache() const;
63
64             private:
65                 typedef unsigned int TimerID;
66                 typedef std::weak_ptr<PrimitiveResource> weakPrimitiveResource;
67                 typedef std::weak_ptr<ObserveCache> weakDataCache;
68
69                 // resource instance
70                 weakPrimitiveResource m_wpResource;
71
72                 // cached data info
73                 RCSResourceAttributes m_attributes;
74                 CACHE_STATE m_state;
75
76                 std::mutex m_cbMutex;
77                 DataCacheCB m_reportCB;
78
79                 std::atomic<bool> m_isStart;
80
81                 CacheID m_id;
82
83             private:
84                 static void verifyObserveCB(const HeaderOptions &_hos,
85                                             const ResponseStatement &_rep, int _result,
86                                             unsigned int _seq, weakDataCache ptr);
87                 void onObserve(const HeaderOptions &_hos,
88                                const ResponseStatement &_rep, int _result, unsigned int _seq);
89                 bool convertOCResultToSuccess(OCStackResult ret);
90         };
91     } // namespace Service
92 } // namespace OIC
93
94 #endif /* RCM_DATACACHE_H_ */