Add api about non-iterated request get at cache.
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceCache / include / ResourceCacheManager.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_RESOURCECACHEMANAGER_H_
22 #define RCM_RESOURCECACHEMANAGER_H_
23
24 #include <list>
25 #include <string>
26 #include <mutex>
27 #include <map>
28
29 #include "CacheTypes.h"
30 #include "DataCache.h"
31 #include "ObserveCache.h"
32
33 namespace OIC
34 {
35     namespace Service
36     {
37         class ResourceCacheManager
38         {
39             public:
40                 class HasNoCachedDataException: public RCSException
41                 {
42                     public:
43                         HasNoCachedDataException(std::string &&what)
44                             : RCSException { std::move(what) } {}
45                 };
46
47                 static ResourceCacheManager *getInstance();
48
49                 // throw InvalidParameterException;
50                 CacheID requestResourceCache(
51                     PrimitiveResourcePtr pResource, CacheCB func = NULL,
52                     CACHE_METHOD cm = CACHE_METHOD::ITERATED_GET,
53                     REPORT_FREQUENCY rf = REPORT_FREQUENCY::NONE, long time = 0l);
54
55                 // throw InvalidParameterException;
56                 void cancelResourceCache(CacheID id);
57
58                 // throw InvalidParameterException;
59                 void updateResourceCache(CacheID id) const;
60
61                 // throw InvalidParameterException;
62                 // throw HasNoCachedDataException;
63                 const RCSResourceAttributes getCachedData(CacheID id) const;
64
65                 // throw InvalidParameterException;
66                 CACHE_STATE getResourceCacheState(CacheID id) const;
67
68                 // throw InvalidParameterException;
69                 bool isCachedData(CacheID id) const;
70
71             private:
72                 static ResourceCacheManager *s_instance;
73                 static std::mutex s_mutex;
74                 static std::mutex s_mutexForCreation;
75                 static std::unique_ptr<std::list<DataCachePtr>> s_cacheDataList;
76                 std::map<CacheID, DataCachePtr> cacheIDmap;
77
78                 std::list<ObserveCache::Ptr> m_observeCacheList;
79                 std::map<CacheID, ObserveCache::Ptr> observeCacheIDmap;
80
81                 ResourceCacheManager() = default;
82                 ~ResourceCacheManager();
83                 ResourceCacheManager(const ResourceCacheManager &) = delete;
84                 ResourceCacheManager(ResourceCacheManager &&) = delete;
85
86                 ResourceCacheManager &operator=(const ResourceCacheManager &) const = delete;
87                 ResourceCacheManager &operator=(ResourceCacheManager && ) const = delete;
88
89                 static void initializeResourceCacheManager();
90                 DataCachePtr findDataCache(PrimitiveResourcePtr pResource) const;
91                 DataCachePtr findDataCache(CacheID id) const;
92         };
93     } // namespace Service
94 } // namespace OIC
95
96 #endif /* RCM_RESOURCECACHEMANAGER_H_ */