replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceCache / include / DataCache.h
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 #ifndef RCM_DATACACHE_H_
22 #define RCM_DATACACHE_H_
23
24 #include <list>
25 #include <string>
26 #include <memory>
27 #include <mutex>
28
29 #include "CacheTypes.h"
30 #include "ExpiryTimer.h"
31
32 namespace OIC
33 {
34     namespace Service
35     {
36         class DataCache : public std::enable_shared_from_this<DataCache>
37         {
38             public:
39                 typedef unsigned int TimerID;
40                 typedef std::function<void(TimerID)> TimerCB;
41
42             public:
43                 DataCache();
44                 ~DataCache();
45
46                 DataCache(const DataCache &) = default;
47                 DataCache(DataCache &&) = default;
48                 DataCache & operator = (const DataCache &) = default;
49                 DataCache & operator = (DataCache &&) = default;
50
51                 void initializeDataCache(PrimitiveResourcePtr pResource);
52
53                 CacheID addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatTime);
54                 CacheID deleteSubscriber(CacheID id);
55
56                 CACHE_STATE getCacheState() const;
57                 const RCSResourceAttributes getCachedData() const;
58                 const PrimitiveResourcePtr getPrimitiveResource() const;
59
60                 void requestGet();
61                 bool isEmptySubscriber() const;
62                 bool isCachedData() const;
63
64             private:
65                 // resource instance
66                 PrimitiveResourcePtr sResource;
67
68                 // cached data info
69                 RCSResourceAttributes attributes;
70                 CACHE_STATE state;
71                 CACHE_MODE mode;
72                 bool isReady;
73
74                 // subscriber info
75                 std::unique_ptr<SubscriberInfo> subscriberList;
76                 mutable std::mutex m_mutex;
77                 mutable std::mutex att_mutex;
78
79                 ExpiryTimer networkTimer;
80                 ExpiryTimer pollingTimer;
81                 TimerID networkTimeOutHandle;
82                 TimerID pollingHandle;
83
84                 ObserveCB pObserveCB;
85                 GetCB pGetCB;
86                 TimerCB pTimerCB;
87                 TimerCB pPollingCB;
88
89                 unsigned int lastSequenceNum;
90
91             public:
92                 void onObserve(const HeaderOptions &_hos,
93                                const ResponseStatement &_rep, int _result, unsigned int _seq);
94                 void onGet(const HeaderOptions &_hos, const ResponseStatement &_rep, int _result);
95             private:
96                 void onTimeOut(const unsigned int timerID);
97                 void onPollingOut(const unsigned int timerID);
98
99                 CacheID generateCacheID();
100                 SubscriberInfoPair findSubscriber(CacheID id);
101                 void notifyObservers(const RCSResourceAttributes Att, int eCode);
102         };
103     } // namespace Service
104 } // namespace OIC
105
106 #endif /* RCM_DATACACHE_H_ */