Imported Upstream version 0.9.2
[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                 void initializeDataCache(PrimitiveResourcePtr pResource);
47
48                 CacheID addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatTime);
49                 CacheID deleteSubscriber(CacheID id);
50
51                 CACHE_STATE getCacheState() const;
52                 const RCSResourceAttributes getCachedData() const;
53                 const PrimitiveResourcePtr getPrimitiveResource() const;
54
55                 void requestGet();
56                 bool isEmptySubscriber() const;
57                 bool isCachedData() const;
58
59             private:
60                 // resource instance
61                 PrimitiveResourcePtr sResource;
62
63                 // cached data info
64                 RCSResourceAttributes attributes;
65                 CACHE_STATE state;
66                 CACHE_MODE mode;
67                 bool isReady;
68
69                 // subscriber info
70                 std::unique_ptr<SubscriberInfo> subscriberList;
71                 mutable std::mutex m_mutex;
72                 mutable std::mutex att_mutex;
73
74                 ExpiryTimer networkTimer;
75                 ExpiryTimer pollingTimer;
76                 TimerID networkTimeOutHandle;
77                 TimerID pollingHandle;
78
79                 ObserveCB pObserveCB;
80                 GetCB pGetCB;
81                 TimerCB pTimerCB;
82                 TimerCB pPollingCB;
83
84                 unsigned int lastSequenceNum;
85
86             public:
87                 void onObserve(const HeaderOptions &_hos,
88                                const ResponseStatement &_rep, int _result, int _seq);
89                 void onGet(const HeaderOptions &_hos, const ResponseStatement &_rep, int _result);
90             private:
91                 void onTimeOut(const unsigned int timerID);
92                 void onPollingOut(const unsigned int timerID);
93
94                 CacheID generateCacheID();
95                 SubscriberInfoPair findSubscriber(CacheID id);
96                 void notifyObservers(const RCSResourceAttributes Att);
97         };
98     } // namespace Service
99 } // namespace OIC
100
101 #endif /* RCM_DATACACHE_H_ */