f56568ffba09ff254db65c49e53926e90ca8c80e
[platform/upstream/iotivity.git] / service / notification / src / consumer / cache / linux / NSConsumerMemoryCache.c
1 //******************************************************************
2 //
3 // Copyright 2016 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 "NSConsumerMemoryCache.h"
22 #include "oic_malloc.h"
23 #include "oic_string.h"
24
25 pthread_mutex_t * NSGetCacheMutex()
26 {
27     static pthread_mutex_t NSCacheMutex;
28     return & NSCacheMutex;
29 }
30
31 void NSSetCacheMutex(pthread_mutex_t mutex)
32 {
33     *(NSGetCacheMutex()) = mutex;
34 }
35
36 NSCacheList * NSStorageCreate()
37 {
38     pthread_mutex_t * mutex = (pthread_mutex_t *) OICMalloc(sizeof(pthread_mutex_t));
39     pthread_mutex_init(mutex, NULL);
40     NSSetCacheMutex(*mutex);
41     mutex = NSGetCacheMutex();
42
43     pthread_mutex_lock(mutex);
44
45     NSCacheList * newList = (NSCacheList *) OICMalloc(sizeof(NSCacheList));
46     if (!newList)
47     {
48         pthread_mutex_unlock(mutex);
49         NS_LOG (ERROR, "Failed to Create Cache");
50         return NULL;
51     }
52
53     newList->head = newList->tail = NULL;
54
55     pthread_mutex_unlock(mutex);
56
57     return newList;
58 }
59
60 NSCacheElement * NSStorageRead(NSCacheList * list, const char * findId)
61 {
62     pthread_mutex_t * mutex = NSGetCacheMutex();
63
64     pthread_mutex_lock(mutex);
65
66     NSCacheElement * iter = list->head;
67     NSCacheElement * next = NULL;
68     NSCacheType type = list->cacheType;
69
70     while (iter)
71     {
72         next = iter->next;
73
74         pthread_mutex_unlock(mutex);
75
76         if (NSConsumerCompareIdCacheData(type, iter->data, findId))
77         {
78             pthread_mutex_unlock(mutex);
79
80             return iter;
81         }
82
83         iter = next;
84     }
85
86     pthread_mutex_unlock(mutex);
87     NS_LOG (DEBUG, "No Cache Element");
88     return NULL;
89 }
90
91 NSResult NSStorageWrite(NSCacheList * list, NSCacheElement * newObj)
92 {
93     pthread_mutex_t * mutex = NSGetCacheMutex();
94
95     pthread_mutex_lock(mutex);
96
97     NSCacheType type = list->cacheType;
98
99     if (!newObj)
100     {
101         pthread_mutex_unlock(mutex);
102         NS_LOG (ERROR, "Failed to Write Cache");
103         return NS_ERROR;
104     }
105
106     if (type == NS_CONSUMER_CACHE_MESSAGE)
107     {
108         pthread_mutex_unlock(mutex);
109
110         return NSConsumerCacheWriteMessage(list, newObj);
111     }
112     else if (type == NS_CONSUMER_CACHE_PROVIDER)
113     {
114         pthread_mutex_unlock(mutex);
115
116         return NSConsumerCacheWriteProvider(list, newObj);
117     }
118
119     NS_LOG (ERROR, "Not Supported Type");
120     pthread_mutex_unlock(mutex);
121
122     return NS_ERROR;
123 }
124
125 NSResult NSStorageDelete(NSCacheList * list, const char * delId)
126 {
127     pthread_mutex_t * mutex = NSGetCacheMutex();
128
129     pthread_mutex_lock(mutex);
130
131     NSCacheType type = list->cacheType;
132
133     if (!delId)
134     {
135         pthread_mutex_unlock(mutex);
136         NS_LOG (ERROR, "Failed to Delete Cache");
137         return NS_ERROR;
138     }
139
140     NSCacheElement * prev = list->head;
141     NSCacheElement * del = list->head;
142
143     if (NSConsumerCompareIdCacheData(type, del->data, delId))
144     {
145         if (del == list->head)
146         {
147             if (del == list->tail)
148                 list->tail = del->next;
149             list->head = del->next;
150
151             if (type == NS_CONSUMER_CACHE_MESSAGE)
152             {
153                 NSRemoveMessage((NSMessage_consumer *) del->data);
154             }
155             else if (type == NS_CONSUMER_CACHE_PROVIDER)
156             {
157                 NSRemoveProvider((NSProvider_internal *) del->data);
158             }
159             NSOICFree(del);
160             pthread_mutex_unlock(mutex);
161
162             return NS_OK;
163         }
164     }
165
166     del = del->next;
167     while (del)
168     {
169         if (NSConsumerCompareIdCacheData(type, del->data, delId))
170         {
171             if (del == list->tail)
172                 list->tail = prev;
173
174             prev->next = del->next;
175             if (type == NS_CONSUMER_CACHE_MESSAGE)
176             {
177                 NSRemoveMessage((NSMessage_consumer *) del->data);
178             }
179             else if (type == NS_CONSUMER_CACHE_PROVIDER)
180             {
181                 NSRemoveProvider((NSProvider_internal *) del->data);
182             }
183             NSOICFree(del);
184             pthread_mutex_unlock(mutex);
185
186             return NS_OK;
187         }
188
189         prev = del;
190         del = del->next;
191     }
192     pthread_mutex_unlock(mutex);
193     return NS_OK;
194 }
195
196 NSResult NSConsumerCacheWriteMessage(NSCacheList * list, NSCacheElement * newObj)
197 {
198     pthread_mutex_t * mutex = NSGetCacheMutex();
199
200     pthread_mutex_lock(mutex);
201
202     if (!newObj)
203     {
204         pthread_mutex_unlock(mutex);
205         NS_LOG (ERROR, "Failed to Write Message Cache");
206         return NS_ERROR;
207     }
208
209     NSMessage_consumer * newMsgObj = (NSMessage_consumer *) newObj->data;
210
211     pthread_mutex_unlock(mutex);
212     char msgId[NS_DEVICE_ID_LENGTH] = {0, };
213     snprintf(msgId, NS_DEVICE_ID_LENGTH, "%llu", newMsgObj->messageId);
214     NSCacheElement * it = NSStorageRead(list, msgId);
215     pthread_mutex_lock(mutex);
216
217     if (it)
218     {
219         NSMessage_consumer * msgObj = (NSMessage_consumer *) it->data;
220         it->data = (void *) NSCopyMessage(newMsgObj);
221         if (!it->data)
222         {
223             NS_LOG (ERROR, "Failed to CopyMessage");
224             it->data = (void *) msgObj;
225             pthread_mutex_unlock(mutex);
226
227             return NS_ERROR;
228         }
229         NSRemoveMessage(msgObj);
230         pthread_mutex_unlock(mutex);
231
232         return NS_OK;
233     }
234
235     NSCacheElement * obj = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
236     if (!obj)
237     {
238         NS_LOG(ERROR, "Fail to Create New Object");
239         pthread_mutex_unlock(mutex);
240
241         return NS_ERROR;
242     }
243     obj->data = (void *) NSCopyMessage(newMsgObj);
244     if (!obj->data)
245     {
246         NS_LOG (ERROR, "Failed to CopyMessage");
247         pthread_mutex_unlock(mutex);
248
249         return NS_ERROR;
250     }
251     obj->next = NULL;
252
253     if (!list->head)
254     {
255         list->head = obj;
256         list->tail = obj;
257         pthread_mutex_unlock(mutex);
258
259         return NS_OK;
260     }
261
262     (list->tail)->next = obj;
263     list->tail = obj;
264     pthread_mutex_unlock(mutex);
265
266     return NS_OK;
267 }
268
269 NSResult NSConsumerCacheWriteProvider(NSCacheList * list, NSCacheElement * newObj)
270 {
271     pthread_mutex_t * mutex = NSGetCacheMutex();
272
273     pthread_mutex_lock(mutex);
274
275     NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1");
276     NSProvider_internal * prov = (NSProvider_internal *)newObj->data;
277     NS_LOG_V (DEBUG, "%s", prov->providerId);
278     NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1");
279
280     if (!newObj)
281     {
282         pthread_mutex_unlock(mutex);
283         NS_LOG (ERROR, "Failed to Write Provider Cache");
284         return NS_ERROR;
285     }
286
287     NSProvider_internal * newProvObj = (NSProvider_internal *) newObj->data;
288
289     pthread_mutex_unlock(mutex);
290     NSCacheElement * it = NSStorageRead(list, newProvObj->providerId);
291     pthread_mutex_lock(mutex);
292
293     if (it)
294     {
295         NSProvider_internal * provObj = (NSProvider_internal *) it->data;
296         it->data = (void *) NSCopyProvider(newProvObj);
297         if (!it->data)
298         {
299             NS_LOG (ERROR, "Failed to CopyProvider");
300             it->data = (void *) provObj;
301             pthread_mutex_unlock(mutex);
302
303             return NS_ERROR;
304         }
305         NSRemoveProvider(provObj);
306         pthread_mutex_unlock(mutex);
307
308         return NS_OK;
309     }
310
311     NSCacheElement * obj = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
312     if (!obj)
313     {
314         NS_LOG(ERROR, "Fail to Create New Object");
315         pthread_mutex_unlock(mutex);
316
317         return NS_ERROR;
318     }
319     obj->data = (void *) NSCopyProvider(newProvObj);
320
321     NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2");
322     prov = (NSProvider_internal *)obj->data;
323     NS_LOG_V (DEBUG, "%s", prov->providerId);
324     NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2");
325
326     if (!obj->data)
327     {
328         NS_LOG (ERROR, "Failed to CopyProvider");
329         pthread_mutex_unlock(mutex);
330
331         return NS_ERROR;
332     }
333     obj->next = NULL;
334
335     if (!list->head)
336     {
337         list->head = obj;
338         list->tail = obj;
339         pthread_mutex_unlock(mutex);
340
341         NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!3");
342         prov = (NSProvider_internal *)list->tail->data;
343         NS_LOG_V (DEBUG, "%s", prov->providerId);
344         NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!3");
345
346         return NS_OK;
347     }
348
349     (list->tail)->next = obj;
350     list->tail = obj;
351
352     NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!4");
353     prov = (NSProvider_internal *)list->tail->data;
354     NS_LOG_V (DEBUG, "%s", prov->providerId);
355     NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!4");
356
357     pthread_mutex_unlock(mutex);
358
359     return NS_OK;
360 }
361
362 NSResult NSStorageDestroy(NSCacheList * list)
363 {
364     pthread_mutex_t * mutex = NSGetCacheMutex();
365
366     pthread_mutex_lock(mutex);
367
368     NSCacheElement * iter = list->head;
369     NSCacheElement * next = NULL;
370
371     NSCacheType type = list->cacheType;
372
373     if (type == NS_CONSUMER_CACHE_MESSAGE)
374     {
375         while (iter)
376         {
377             next = (NSCacheElement *) iter->next;
378
379             NSRemoveMessage((NSMessage_consumer *) iter->data);
380             NSOICFree(iter);
381
382             iter = next;
383         }
384
385         NSOICFree(list);
386     }
387     else if (type == NS_CONSUMER_CACHE_PROVIDER)
388     {
389         while (iter)
390         {
391             next = (NSCacheElement *) iter->next;
392
393             NSRemoveProvider((NSProvider_internal *) iter->data);
394             NSOICFree(iter);
395
396             iter = next;
397         }
398
399         NSOICFree(list);
400     }
401
402     pthread_mutex_unlock(mutex);
403
404     return NS_OK;
405 }
406
407 bool NSConsumerCompareIdCacheData(NSCacheType type, void * data, const char * id)
408 {
409     if (data == NULL)
410     {
411         return false;
412     }
413
414     if (type == NS_CONSUMER_CACHE_MESSAGE)
415     {
416         NSMessage_consumer * msg = (NSMessage_consumer *) data;
417
418         char msgId[NS_DEVICE_ID_LENGTH] = {0, };
419         snprintf(msgId, NS_DEVICE_ID_LENGTH, "%llu", msg->messageId);
420         if (!strcmp(msgId, id))
421         {
422             return true;
423         }
424
425         return false;
426     }
427     else if (type == NS_CONSUMER_CACHE_PROVIDER)
428     {
429         NSProvider_internal * prov = (NSProvider_internal *) data;
430
431         if (!strcmp(prov->providerId, id))
432         {
433             return true;
434         }
435
436         return false;
437     }
438
439     return false;
440 }