added cacheadapter interface and memorycache of notification.
[platform/upstream/iotivity.git] / service / notification / src / provider / cache / linux / NSProviderMemoryCache.c
1 //******************************************************************\r
2 //\r
3 // Copyright 2016 Samsung Electronics All Rights Reserved.\r
4 //\r
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
6 //\r
7 // Licensed under the Apache License, Version 2.0 (the "License");\r
8 // you may not use this file except in compliance with the License.\r
9 // You may obtain a copy of the License at\r
10 //\r
11 //      http://www.apache.org/licenses/LICENSE-2.0\r
12 //\r
13 // Unless required by applicable law or agreed to in writing, software\r
14 // distributed under the License is distributed on an "AS IS" BASIS,\r
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16 // See the License for the specific language governing permissions and\r
17 // limitations under the License.\r
18 //\r
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
20 #include "NSProviderMemoryCache.h"\r
21 \r
22 NSCacheList * NSCacheCreate()\r
23 {\r
24     pthread_mutex_lock(&NSCacheMutex);\r
25     NSCacheList * newList = (NSCacheList *) OICMalloc(sizeof(NSCacheList));\r
26     if (!newList)\r
27     {\r
28         pthread_mutex_unlock(&NSCacheMutex);\r
29         return NULL;\r
30     }\r
31 \r
32     newList->head = newList->tail = NULL;\r
33 \r
34     pthread_mutex_unlock(&NSCacheMutex);\r
35 \r
36     return newList;\r
37 }\r
38 \r
39 NSCacheElement * NSCacheRead(NSCacheList * list, char * findId)\r
40 {\r
41     pthread_mutex_lock(&NSCacheMutex);\r
42     NSCacheElement * iter = list->head;\r
43     NSCacheElement * next = NULL;\r
44     NSCacheType type = list->cacheType;\r
45 \r
46     printf("NS_ findId = %s\n", findId);\r
47 \r
48     while (iter)\r
49     {\r
50         next = iter->next;\r
51 \r
52         printf("NS_ findId2 = %s\n", findId);\r
53         pthread_mutex_unlock(&NSCacheMutex);\r
54 \r
55         if (NSProviderCompareIdCacheData(type, iter->data, findId))\r
56         {\r
57             pthread_mutex_unlock(&NSCacheMutex);\r
58             return iter;\r
59         }\r
60 \r
61         iter = next;\r
62     }\r
63 \r
64     pthread_mutex_unlock(&NSCacheMutex);\r
65 \r
66     return NULL;\r
67 }\r
68 \r
69 NSResult NSCacheUpdateSubScriptionState(NSCacheList * list, NSCacheSubData * subData)\r
70 {\r
71     pthread_mutex_lock(&NSCacheMutex);\r
72 \r
73     NSCacheType type = list->cacheType;\r
74 \r
75     printf("NS_ NSCacheWrite\n");\r
76 \r
77     if (subData == NULL)\r
78     {\r
79         printf("NS_ NSCacheWrite - newObj is NULL\n");\r
80         pthread_mutex_unlock(&NSCacheMutex);\r
81         return NS_ERROR;\r
82     }\r
83 \r
84     printf("NS_ findId0 - 0 = %s\n", subData->id);\r
85     pthread_mutex_unlock(&NSCacheMutex);\r
86     NSCacheElement * it = NSCacheRead(list, subData->id);\r
87     pthread_mutex_lock(&NSCacheMutex);\r
88     printf("NS_ findId0 -1 = %s\n", subData->id);\r
89     if (it)\r
90     {\r
91         printf("NS_ findId1 = %s\n", subData->id);\r
92         NSCacheSubData * itData = (NSCacheSubData *) it->data;\r
93         printf("NS_ findId2 = %s\n", subData->id);\r
94         if (strcmp(itData->id, subData->id) == 0)\r
95         {\r
96             printf("NS_ findId3_subdata_id = %s\n", subData->id);\r
97             printf("NS_ findId3_subdata_messageobId = %d\n", subData->messageObId);\r
98             printf("NS_ findId3_subdata_syncobId = %d\n", subData->syncObId);\r
99             printf("NS_ findId3_subdata_isWhite = %d\n", subData->isWhite);\r
100 \r
101             printf("NS_ findId3_itdata_id = %s\n", itData->id);\r
102             printf("NS_ findId3_itdata_messageobId = %d\n", itData->messageObId);\r
103             printf("NS_ findId3_itdata_isWhite = %d\n", itData->isWhite);\r
104 \r
105             itData->isWhite = subData->isWhite;\r
106 \r
107             printf("_NS : PROVIDER_CACHE_SUBSCRIBER UPDATE\n");\r
108 \r
109             pthread_mutex_unlock(&NSCacheMutex);\r
110             return NS_OK;\r
111 \r
112         }\r
113     }\r
114     pthread_mutex_unlock(&NSCacheMutex);\r
115     return NS_OK;\r
116 }\r
117 \r
118 NSResult NSCacheWrite(NSCacheList * list, NSCacheElement * newObj)\r
119 {\r
120     pthread_mutex_lock(&NSCacheMutex);\r
121 \r
122     NSCacheType type = list->cacheType;\r
123 \r
124     printf("NS_ NSCacheWrite\n");\r
125 \r
126     if (newObj == NULL)\r
127     {\r
128         printf("NS_ NSCacheWrite - newObj is NULL\n");\r
129         pthread_mutex_unlock(&NSCacheMutex);\r
130         return NS_ERROR;\r
131     }\r
132 \r
133     if (type == NS_PROVIDER_CACHE_SUBSCRIBER)\r
134     {\r
135         NSCacheSubData * subData = (NSCacheSubData *) newObj->data;\r
136 \r
137         printf("NS_ findId0 - 0 = %s\n", subData->id);\r
138         pthread_mutex_unlock(&NSCacheMutex);\r
139         NSCacheElement * it = NSCacheRead(list, subData->id);\r
140         pthread_mutex_lock(&NSCacheMutex);\r
141         printf("NS_ findId0 -1 = %s\n", subData->id);\r
142         if (it)\r
143         {\r
144             printf("NS_ findId1 = %s\n", subData->id);\r
145             NSCacheSubData * itData = (NSCacheSubData *) it->data;\r
146             printf("NS_ findId2 = %s\n", subData->id);\r
147             if (strcmp(itData->id, subData->id) == 0)\r
148             {\r
149                 printf("NS_ findId3_subdata_id = %s\n", subData->id);\r
150                 printf("NS_ findId3_subdata_messageobId = %d\n", subData->messageObId);\r
151                 printf("NS_ findId3_subdata_syncobId = %d\n", subData->syncObId);\r
152                 printf("NS_ findId3_subdata_isWhite = %d\n", subData->isWhite);\r
153 \r
154                 printf("NS_ findId3_itdata_id = %s\n", itData->id);\r
155                 printf("NS_ findId3_itdata_messageobId = %d\n", itData->messageObId);\r
156                 printf("NS_ findId3_itdata_isWhite = %d\n", itData->isWhite);\r
157                 if (itData->messageObId == 0)\r
158                 {\r
159                     itData->messageObId = subData->messageObId;\r
160                 }\r
161 \r
162                 if (itData->syncObId == 0)\r
163                 {\r
164                     itData->syncObId = subData->syncObId;\r
165                 }\r
166 \r
167                 //itData->isWhite = subData->isWhite;\r
168 \r
169                 printf("_NS : PROVIDER_CACHE_SUBSCRIBER UPDATE\n");\r
170 \r
171                 pthread_mutex_unlock(&NSCacheMutex);\r
172                 return NS_OK;\r
173 \r
174             }\r
175         }\r
176 \r
177     }\r
178     else if (type == NS_PROVIDER_CACHE_MESSAGE)\r
179     {\r
180         NSCacheMsgData * msgData = (NSCacheMsgData *) newObj->data;\r
181 \r
182         NSCacheElement * it = NSCacheRead(list, msgData->id);\r
183         if (it)\r
184         {\r
185             NSCacheMsgData * itData = (NSCacheMsgData *) it->data;\r
186 \r
187             if (strcmp(itData->id, msgData->id) == 0)\r
188             {\r
189 \r
190                 itData->messageType = msgData->messageType;\r
191                 printf("_NS : PROVIDER_CACHE_MESSAGE UPDATE\n");\r
192                 pthread_mutex_unlock(&NSCacheMutex);\r
193                 return NS_OK;\r
194 \r
195             }\r
196         }\r
197     }\r
198 \r
199     printf("NS_ berfore list->head == NULL\n");\r
200     if (list->head == NULL)\r
201     {\r
202         list->head = list->tail = newObj;\r
203         printf("NS_ list->head == NULL\n");\r
204         pthread_mutex_unlock(&NSCacheMutex);\r
205         return NS_OK;\r
206     }\r
207 \r
208     list->tail = list->tail->next = newObj;\r
209     printf("NS_ list->head == not NULL\n");\r
210     pthread_mutex_unlock(&NSCacheMutex);\r
211     return NS_OK;\r
212 }\r
213 \r
214 NSResult NSCacheDelete(NSCacheList * list, char * delId)\r
215 {\r
216 \r
217     pthread_mutex_lock(&NSCacheMutex);\r
218     NSCacheElement * prev = list->head;\r
219     NSCacheElement * del = list->head;\r
220 \r
221     NSCacheType type = list->cacheType;\r
222 \r
223     if (NSProviderCompareIdCacheData(type, del->data, delId))\r
224     {\r
225         if (del == list->head) // first object\r
226         {\r
227             if (del == list->tail) // first object (one object)\r
228                 list->tail = del->next;\r
229 \r
230             list->head = del->next;\r
231 \r
232             NSProviderDeleteCacheData(type, del->data);\r
233             OICFree(del);\r
234             pthread_mutex_unlock(&NSCacheMutex);\r
235             return NS_OK;\r
236         }\r
237     }\r
238 \r
239     del = del->next;\r
240     while (del)\r
241     {\r
242         if (NSProviderCompareIdCacheData(type, del->data, delId))\r
243         {\r
244             if (del == list->tail) // delete object same to last object\r
245                 list->tail = prev;\r
246 \r
247             prev->next = del->next;\r
248             NSProviderDeleteCacheData(type, del->data);\r
249             OICFree(del);\r
250             pthread_mutex_unlock(&NSCacheMutex);\r
251             return NS_OK;\r
252         }\r
253 \r
254         prev = del;\r
255         del = del->next;\r
256     }\r
257     pthread_mutex_unlock(&NSCacheMutex);\r
258     return NS_OK;\r
259 }\r
260 \r
261 NSResult NSCacheDestroy(NSCacheList * list)\r
262 {\r
263     NSCacheElement * iter = list->head;\r
264     NSCacheElement * next = NULL;\r
265 \r
266     NSCacheType type = list->cacheType;\r
267 \r
268     while (iter)\r
269     {\r
270         next = (NSCacheElement *) iter->next;\r
271 \r
272         NSProviderDeleteCacheData(type, iter->data);\r
273         OICFree(iter);\r
274 \r
275         iter = next;\r
276     }\r
277 \r
278     OICFree(list);\r
279 \r
280     return NS_OK;\r
281 }\r
282 \r
283 bool NSProviderCompareIdCacheData(NSCacheType type, void * data, char * id)\r
284 {\r
285     if (data == NULL)\r
286     {\r
287         return false;\r
288     }\r
289 \r
290     if (type == NS_PROVIDER_CACHE_SUBSCRIBER)\r
291     {\r
292         NSCacheSubData * subData = (NSCacheSubData *) data;\r
293 \r
294         printf("NS_ subdata->id = %s\n", subData->id);\r
295         printf("NS_ id = %s\n", id);\r
296 \r
297         if (strcmp(subData->id, id) == 0)\r
298         {\r
299             return true;\r
300         }\r
301         return false;\r
302 \r
303     }\r
304     else if (type == NS_PROVIDER_CACHE_MESSAGE)\r
305     {\r
306         NSCacheMsgData * msgData = (NSCacheMsgData *) data;\r
307 \r
308         if (strcmp(msgData->id, id) == 0)\r
309         {\r
310             return true;\r
311         }\r
312 \r
313         return false;\r
314     }\r
315 \r
316     return false;\r
317 }\r
318 \r
319 NSResult NSProviderDeleteCacheData(NSCacheType type, void * data)\r
320 {\r
321     if (data == NULL)\r
322     {\r
323         return NS_OK;\r
324     }\r
325 \r
326     if (type == NS_PROVIDER_CACHE_SUBSCRIBER)\r
327     {\r
328         NSCacheSubData * subData = (NSCacheSubData *) data;\r
329         if (subData->id)\r
330         {\r
331             OICFree(subData->id);\r
332             subData->id = NULL;\r
333         }\r
334 \r
335         OICFree(subData);\r
336 \r
337         return NS_OK;\r
338     }\r
339     else if (type == NS_PROVIDER_CACHE_MESSAGE)\r
340     {\r
341         NSCacheMsgData * msgData = (NSCacheMsgData *) data;\r
342 \r
343         if (msgData->id)\r
344         {\r
345             OICFree(msgData->id);\r
346             msgData->id = NULL;\r
347         }\r
348 \r
349         if (msgData->nsMessage)\r
350         {\r
351             NSFreeMessage(msgData->nsMessage);\r
352         }\r
353 \r
354         OICFree(msgData);\r
355 \r
356         return NS_OK;\r
357     }\r
358 \r
359     return NS_OK;\r
360 }\r
361 \r