Clean the warning message in notification service .
[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, const 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 \r
54         if (NSProviderCompareIdCacheData(type, iter->data, findId))\r
55         {\r
56             pthread_mutex_unlock(&NSCacheMutex);\r
57             return iter;\r
58         }\r
59 \r
60         iter = next;\r
61     }\r
62 \r
63     pthread_mutex_unlock(&NSCacheMutex);\r
64 \r
65     return NULL;\r
66 }\r
67 \r
68 NSResult NSCacheUpdateSubScriptionState(NSCacheList * list, NSCacheSubData * subData)\r
69 {\r
70     pthread_mutex_lock(&NSCacheMutex);\r
71 \r
72     printf("NS_ NSCacheUpdateSubScriptionState\n");\r
73 \r
74     if (subData == NULL)\r
75     {\r
76         printf("NS_ NSCacheWrite - newObj is NULL\n");\r
77         pthread_mutex_unlock(&NSCacheMutex);\r
78         return NS_ERROR;\r
79     }\r
80 \r
81     printf("NS_ findId0 - 0 = %s\n", subData->id);\r
82     pthread_mutex_unlock(&NSCacheMutex);\r
83     NSCacheElement * it = NSCacheRead(list, subData->id);\r
84     pthread_mutex_lock(&NSCacheMutex);\r
85     printf("NS_ findId0 -1 = %s\n", subData->id);\r
86     if (it)\r
87     {\r
88         printf("NS_ findId1 = %s\n", subData->id);\r
89         NSCacheSubData * itData = (NSCacheSubData *) it->data;\r
90         printf("NS_ findId2 = %s\n", subData->id);\r
91         if (strcmp(itData->id, subData->id) == 0)\r
92         {\r
93             printf("NS_ findId3_subdata_id = %s\n", subData->id);\r
94             printf("NS_ findId3_subdata_messageobId = %d\n", subData->messageObId);\r
95             printf("NS_ findId3_subdata_syncobId = %d\n", subData->syncObId);\r
96             printf("NS_ findId3_subdata_isWhite = %d\n", subData->isWhite);\r
97 \r
98             printf("NS_ findId3_itdata_id = %s\n", itData->id);\r
99             printf("NS_ findId3_itdata_messageobId = %d\n", itData->messageObId);\r
100             printf("NS_ findId3_itdata_isWhite = %d\n", itData->isWhite);\r
101 \r
102             itData->isWhite = subData->isWhite;\r
103 \r
104             printf("_NS : PROVIDER_CACHE_SUBSCRIBER UPDATE\n");\r
105 \r
106             pthread_mutex_unlock(&NSCacheMutex);\r
107             return NS_OK;\r
108 \r
109         }\r
110     }\r
111     pthread_mutex_unlock(&NSCacheMutex);\r
112     return NS_OK;\r
113 }\r
114 \r
115 NSResult NSCacheWrite(NSCacheList * list, NSCacheElement * newObj)\r
116 {\r
117     pthread_mutex_lock(&NSCacheMutex);\r
118 \r
119     NSCacheType type = list->cacheType;\r
120 \r
121     printf("NS_ NSCacheWrite\n");\r
122 \r
123     if (newObj == NULL)\r
124     {\r
125         printf("NS_ NSCacheWrite - newObj is NULL\n");\r
126         pthread_mutex_unlock(&NSCacheMutex);\r
127         return NS_ERROR;\r
128     }\r
129 \r
130     if (type == NS_PROVIDER_CACHE_SUBSCRIBER)\r
131     {\r
132         NSCacheSubData * subData = (NSCacheSubData *) newObj->data;\r
133 \r
134         printf("NS_ findId0 - 0 = %s\n", subData->id);\r
135         pthread_mutex_unlock(&NSCacheMutex);\r
136         NSCacheElement * it = NSCacheRead(list, subData->id);\r
137         pthread_mutex_lock(&NSCacheMutex);\r
138         printf("NS_ findId0 -1 = %s\n", subData->id);\r
139         if (it)\r
140         {\r
141             printf("NS_ findId1 = %s\n", subData->id);\r
142             NSCacheSubData * itData = (NSCacheSubData *) it->data;\r
143             printf("NS_ findId2 = %s\n", subData->id);\r
144             if (strcmp(itData->id, subData->id) == 0)\r
145             {\r
146                 printf("NS_ findId3_subdata_id = %s\n", subData->id);\r
147                 printf("NS_ findId3_subdata_messageobId = %d\n", subData->messageObId);\r
148                 printf("NS_ findId3_subdata_syncobId = %d\n", subData->syncObId);\r
149                 printf("NS_ findId3_subdata_isWhite = %d\n", subData->isWhite);\r
150 \r
151                 printf("NS_ findId3_itdata_id = %s\n", itData->id);\r
152                 printf("NS_ findId3_itdata_messageobId = %d\n", itData->messageObId);\r
153                 printf("NS_ findId3_itdata_isWhite = %d\n", itData->isWhite);\r
154                 if (itData->messageObId == 0)\r
155                 {\r
156                     itData->messageObId = subData->messageObId;\r
157                 }\r
158 \r
159                 if (itData->syncObId == 0)\r
160                 {\r
161                     itData->syncObId = subData->syncObId;\r
162                 }\r
163 \r
164                 //itData->isWhite = subData->isWhite;\r
165 \r
166                 printf("_NS : PROVIDER_CACHE_SUBSCRIBER UPDATE\n");\r
167 \r
168                 pthread_mutex_unlock(&NSCacheMutex);\r
169                 return NS_OK;\r
170 \r
171             }\r
172         }\r
173 \r
174     }\r
175     else if (type == NS_PROVIDER_CACHE_MESSAGE)\r
176     {\r
177         NSCacheMsgData * msgData = (NSCacheMsgData *) newObj->data;\r
178 \r
179         NSCacheElement * it = NSCacheRead(list, msgData->id);\r
180         if (it)\r
181         {\r
182             NSCacheMsgData * itData = (NSCacheMsgData *) it->data;\r
183 \r
184             if (strcmp(itData->id, msgData->id) == 0)\r
185             {\r
186 \r
187                 itData->messageType = msgData->messageType;\r
188                 printf("_NS : PROVIDER_CACHE_MESSAGE UPDATE\n");\r
189                 pthread_mutex_unlock(&NSCacheMutex);\r
190                 return NS_OK;\r
191 \r
192             }\r
193         }\r
194     }\r
195 \r
196     printf("NS_ berfore list->head == NULL\n");\r
197     if (list->head == NULL)\r
198     {\r
199         list->head = list->tail = newObj;\r
200         printf("NS_ list->head == NULL\n");\r
201         pthread_mutex_unlock(&NSCacheMutex);\r
202         return NS_OK;\r
203     }\r
204 \r
205     list->tail = list->tail->next = newObj;\r
206     printf("NS_ list->head == not NULL\n");\r
207     pthread_mutex_unlock(&NSCacheMutex);\r
208     return NS_OK;\r
209 }\r
210 \r
211 NSResult NSCacheDelete(NSCacheList * list, const char * delId)\r
212 {\r
213     pthread_mutex_lock(&NSCacheMutex);\r
214     NSCacheElement * prev = list->head;\r
215     NSCacheElement * del = list->head;\r
216 \r
217     NSCacheType type = list->cacheType;\r
218 \r
219     if (NSProviderCompareIdCacheData(type, del->data, delId))\r
220     {\r
221         if (del == list->head) // first object\r
222         {\r
223             if (del == list->tail) // first object (one object)\r
224                 list->tail = del->next;\r
225 \r
226             list->head = del->next;\r
227 \r
228             NSProviderDeleteCacheData(type, del->data);\r
229             OICFree(del);\r
230             pthread_mutex_unlock(&NSCacheMutex);\r
231             return NS_OK;\r
232         }\r
233     }\r
234 \r
235     del = del->next;\r
236     while (del)\r
237     {\r
238         if (NSProviderCompareIdCacheData(type, del->data, delId))\r
239         {\r
240             if (del == list->tail) // delete object same to last object\r
241                 list->tail = prev;\r
242 \r
243             prev->next = del->next;\r
244             NSProviderDeleteCacheData(type, del->data);\r
245             OICFree(del);\r
246             pthread_mutex_unlock(&NSCacheMutex);\r
247             return NS_OK;\r
248         }\r
249 \r
250         prev = del;\r
251         del = del->next;\r
252     }\r
253     pthread_mutex_unlock(&NSCacheMutex);\r
254     return NS_OK;\r
255 }\r
256 \r
257 NSResult NSCacheDestroy(NSCacheList * list)\r
258 {\r
259     NSCacheElement * iter = list->head;\r
260     NSCacheElement * next = NULL;\r
261 \r
262     NSCacheType type = list->cacheType;\r
263 \r
264     while (iter)\r
265     {\r
266         next = (NSCacheElement *) iter->next;\r
267 \r
268         NSProviderDeleteCacheData(type, iter->data);\r
269         OICFree(iter);\r
270 \r
271         iter = next;\r
272     }\r
273 \r
274     OICFree(list);\r
275 \r
276     return NS_OK;\r
277 }\r
278 \r
279 bool NSProviderCompareIdCacheData(NSCacheType type, void * data, const char * id)\r
280 {\r
281     if (data == NULL)\r
282     {\r
283         return false;\r
284     }\r
285 \r
286     if (type == NS_PROVIDER_CACHE_SUBSCRIBER)\r
287     {\r
288         NSCacheSubData * subData = (NSCacheSubData *) data;\r
289 \r
290         printf("NS_ subdata->id = %s\n", subData->id);\r
291         printf("NS_ id = %s\n", id);\r
292 \r
293         if (strcmp(subData->id, id) == 0)\r
294         {\r
295             return true;\r
296         }\r
297         return false;\r
298 \r
299     }\r
300     else if (type == NS_PROVIDER_CACHE_MESSAGE)\r
301     {\r
302         NSCacheMsgData * msgData = (NSCacheMsgData *) data;\r
303 \r
304         if (strcmp(msgData->id, id) == 0)\r
305         {\r
306             return true;\r
307         }\r
308 \r
309         return false;\r
310     }\r
311 \r
312     return false;\r
313 }\r
314 \r
315 NSResult NSProviderDeleteCacheData(NSCacheType type, void * data)\r
316 {\r
317     if (data == NULL)\r
318     {\r
319         return NS_OK;\r
320     }\r
321 \r
322     if (type == NS_PROVIDER_CACHE_SUBSCRIBER)\r
323     {\r
324         NSCacheSubData * subData = (NSCacheSubData *) data;\r
325         if (subData->id)\r
326         {\r
327             OICFree(subData->id);\r
328             subData->id = NULL;\r
329         }\r
330 \r
331         OICFree(subData);\r
332 \r
333         return NS_OK;\r
334     }\r
335     else if (type == NS_PROVIDER_CACHE_MESSAGE)\r
336     {\r
337         NSCacheMsgData * msgData = (NSCacheMsgData *) data;\r
338 \r
339         if (msgData->id)\r
340         {\r
341             OICFree(msgData->id);\r
342             msgData->id = NULL;\r
343         }\r
344 \r
345         if (msgData->nsMessage)\r
346         {\r
347             NSFreeMessage(msgData->nsMessage);\r
348         }\r
349 \r
350         OICFree(msgData);\r
351 \r
352         return NS_OK;\r
353     }\r
354 \r
355     return NS_OK;\r
356 }\r
357 \r