Resource Manipulation: resolve Jira issue IOT-641.
[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
28 #include "CacheTypes.h"
29 #include "ExpiryTimer.h"
30
31 namespace OIC
32 {
33     namespace Service
34     {
35         class DataCache
36         {
37             public:
38                 typedef unsigned int TimerID;
39                 typedef std::function<void(TimerID)> TimerCB;
40
41             public:
42                 DataCache();
43                 ~DataCache();
44
45                 void initializeDataCache(PrimitiveResourcePtr pResource);
46
47                 CacheID addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatTime);
48                 CacheID deleteSubscriber(CacheID id);
49
50                 CACHE_STATE getCacheState() const;
51                 const RCSResourceAttributes getCachedData() const;
52                 const PrimitiveResourcePtr getPrimitiveResource() const;
53
54                 void requestGet();
55                 bool isEmptySubscriber() const;
56
57             private:
58                 // resource instance
59                 PrimitiveResourcePtr sResource;
60 //                std::shared_ptr<BaseResource> baseHandler;
61
62                 // cached data info
63                 RCSResourceAttributes attributes;
64                 CACHE_STATE state;
65                 CACHE_MODE mode;
66
67                 // subscriber info
68                 std::unique_ptr<SubscriberInfo> subscriberList;
69
70                 ExpiryTimer networkTimer;
71                 ExpiryTimer pollingTimer;
72                 TimerID networkTimeOutHandle;
73                 TimerID pollingHandle;
74
75                 ObserveCB pObserveCB;
76                 GetCB pGetCB;
77                 TimerCB pTimerCB;
78                 TimerCB pPollingCB;
79
80                 // for requestCB from base
81                 void onObserve(const HeaderOptions &_hos,
82                                const ResponseStatement &_rep, int _result, int _seq);
83                 void onGet(const HeaderOptions &_hos, const ResponseStatement &_rep, int _result);
84                 void onTimeOut(const unsigned int timerID);
85                 void onPollingOut(const unsigned int timerID);
86
87                 CacheID generateCacheID();
88                 SubscriberInfoPair findSubscriber(CacheID id);
89                 void notifyObservers(RCSResourceAttributes Att);
90         };
91     } // namespace Service
92 } // namespace OIC
93
94 #endif /* RCM_DATACACHE_H_ */