e5bcf2a00a4cf981f39d92560f810b88663ccee8
[platform/upstream/iotivity.git] / service / basis / resourceCache / src / ResourceCacheManager.cpp
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 #include "ResourceCacheManager.h"
22
23 ResourceCacheManager * ResourceCacheManager::s_instance = NULL;
24 std::mutex ResourceCacheManager::s_mutexForCreation;
25 std::list< DataCache * > * ResourceCacheManager::s_cacheDataList = NULL;
26
27 ResourceCacheManager::ResourceCacheManager()
28 {
29     // TODO Auto-generated constructor stub
30     if(!s_cacheDataList)
31     {
32         s_cacheDataList = new std::list< DataCache * >();
33     }
34 }
35
36 ResourceCacheManager::~ResourceCacheManager()
37 {
38     // TODO Auto-generated destructor stub
39     if(s_cacheDataList)
40     {
41         s_cacheDataList->clear();
42         delete s_cacheDataList;
43     }
44 }
45
46
47 ResourceCacheManager * ResourceCacheManager::getInstance()
48 {
49     if(!s_instance)
50     {
51         s_mutexForCreation.lock();
52         if(!s_instance)
53         {
54             s_instance = new ResourceCacheManager();
55         }
56         s_mutexForCreation.unlock();
57     }
58     return s_instance;
59 }
60
61 CacheID ResourceCacheManager::requestResourceCache(
62         PrimitiveResource & pResource, CacheCB func, REPORT_FREQUENCY rf, long reportTime)
63 {
64     CacheID ret = 0;
65
66     if(rt == NULL || pResource == NULL)
67     {
68         return ret;
69     }
70
71     if(rt != REPORT_FREQUENCY::NONE)
72     {
73         if(func == NULL)
74         {
75             return ret;
76         }
77         if(!reportTime)
78         {
79             // default setting
80             reportTime = 30;
81         }
82     }
83
84     DataCache * newHandler = findCacheHandler(pResource);
85     if(newHandler == nullptr)
86     {
87         DataCache * newHandler = new DataCache(pResource, func, rf, reportTime);
88         s_cacheDataList->push_back(newHandler);
89     }
90     ret = newHandler->addSubscriber(func, rf, reportTime);
91
92     return ret;
93 }
94
95 OCStackResult ResourceCacheManager::cancelResourceCache(PrimitiveResource & pResource, CacheID id)
96 {
97     OCStackResult ret = OC_STACK_ERROR;
98
99     if(pResource == NULL || id == 0)
100     {
101         return ret;
102     }
103
104     // TODO cancel cache
105     CacheID retID = 0;
106     DataCache * deleteCacheHandler = findCacheHandler(pResource);
107     if(deleteCacheHandler == nullptr)
108     {
109         return ret;
110     }
111     else
112     {
113         retID = deleteCacheHandler->deleteSubscriber(id);
114     }
115
116     if(retID == id)
117     {
118         ret = OC_STACK_OK;
119     }
120
121     return ret;
122 }
123
124 DataCache * ResourceCacheManager::findCacheHandler(PrimitiveResource & pResource)
125 {
126     DataCache * retHandler = nullptr;
127     for (auto i : s_cacheDataList)
128     {
129         if(i->getPrimitiveResource() == pResource)
130         {
131             retHandler = i;
132             break;
133         }
134     }
135     return retHandler;
136 }
137
138 OCStackResult ResourceCacheManager::updateResourceCache(PrimitiveResource & pResource)
139 {
140     OCStackResult ret = OC_STACK_ERROR;
141
142     // TODO update now (request get)
143
144     return ret;
145 }