1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "NSConsumerMemoryCache.h"
22 #include "oic_malloc.h"
23 #include "oic_string.h"
25 pthread_mutex_t * NSGetCacheMutex()
27 static pthread_mutex_t NSCacheMutex;
28 return & NSCacheMutex;
31 void NSSetCacheMutex(pthread_mutex_t mutex)
33 *(NSGetCacheMutex()) = mutex;
36 NSCacheList * NSStorageCreate()
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();
43 pthread_mutex_lock(mutex);
45 NSCacheList * newList = (NSCacheList *) OICMalloc(sizeof(NSCacheList));
48 pthread_mutex_unlock(mutex);
49 NS_LOG (ERROR, "Failed to Create Cache");
53 newList->head = newList->tail = NULL;
55 pthread_mutex_unlock(mutex);
60 NSCacheElement * NSStorageRead(NSCacheList * list, const char * findId)
62 pthread_mutex_t * mutex = NSGetCacheMutex();
64 pthread_mutex_lock(mutex);
66 NSCacheElement * iter = list->head;
67 NSCacheElement * next = NULL;
68 NSCacheType type = list->cacheType;
74 pthread_mutex_unlock(mutex);
76 if (NSConsumerCompareIdCacheData(type, iter->data, findId))
78 pthread_mutex_unlock(mutex);
86 pthread_mutex_unlock(mutex);
87 NS_LOG (DEBUG, "No Cache Element");
91 NSResult NSStorageWrite(NSCacheList * list, NSCacheElement * newObj)
93 pthread_mutex_t * mutex = NSGetCacheMutex();
95 pthread_mutex_lock(mutex);
97 NSCacheType type = list->cacheType;
101 pthread_mutex_unlock(mutex);
102 NS_LOG (ERROR, "Failed to Write Cache");
106 if (type == NS_CONSUMER_CACHE_MESSAGE)
108 pthread_mutex_unlock(mutex);
110 return NSConsumerCacheWriteMessage(list, newObj);
112 else if (type == NS_CONSUMER_CACHE_PROVIDER)
114 pthread_mutex_unlock(mutex);
116 return NSConsumerCacheWriteProvider(list, newObj);
119 NS_LOG (ERROR, "Not Supported Type");
120 pthread_mutex_unlock(mutex);
125 NSResult NSStorageDelete(NSCacheList * list, const char * delId)
127 pthread_mutex_t * mutex = NSGetCacheMutex();
129 pthread_mutex_lock(mutex);
131 NSCacheType type = list->cacheType;
135 pthread_mutex_unlock(mutex);
136 NS_LOG (ERROR, "Failed to Delete Cache");
140 NSCacheElement * prev = list->head;
141 NSCacheElement * del = list->head;
143 if (NSConsumerCompareIdCacheData(type, del->data, delId))
145 if (del == list->head)
147 if (del == list->tail)
148 list->tail = del->next;
149 list->head = del->next;
151 if (type == NS_CONSUMER_CACHE_MESSAGE)
153 NSRemoveMessage((NSMessage_consumer *) del->data);
155 else if (type == NS_CONSUMER_CACHE_PROVIDER)
157 NSRemoveProvider((NSProvider_internal *) del->data);
160 pthread_mutex_unlock(mutex);
169 if (NSConsumerCompareIdCacheData(type, del->data, delId))
171 if (del == list->tail)
174 prev->next = del->next;
175 if (type == NS_CONSUMER_CACHE_MESSAGE)
177 NSRemoveMessage((NSMessage_consumer *) del->data);
179 else if (type == NS_CONSUMER_CACHE_PROVIDER)
181 NSRemoveProvider((NSProvider_internal *) del->data);
184 pthread_mutex_unlock(mutex);
192 pthread_mutex_unlock(mutex);
196 NSResult NSConsumerCacheWriteMessage(NSCacheList * list, NSCacheElement * newObj)
198 pthread_mutex_t * mutex = NSGetCacheMutex();
200 pthread_mutex_lock(mutex);
204 pthread_mutex_unlock(mutex);
205 NS_LOG (ERROR, "Failed to Write Message Cache");
209 NSMessage_consumer * newMsgObj = (NSMessage_consumer *) newObj->data;
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);
219 NSMessage_consumer * msgObj = (NSMessage_consumer *) it->data;
220 it->data = (void *) NSCopyMessage(newMsgObj);
223 NS_LOG (ERROR, "Failed to CopyMessage");
224 it->data = (void *) msgObj;
225 pthread_mutex_unlock(mutex);
229 NSRemoveMessage(msgObj);
230 pthread_mutex_unlock(mutex);
235 NSCacheElement * obj = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
238 NS_LOG(ERROR, "Fail to Create New Object");
239 pthread_mutex_unlock(mutex);
243 obj->data = (void *) NSCopyMessage(newMsgObj);
246 NS_LOG (ERROR, "Failed to CopyMessage");
247 pthread_mutex_unlock(mutex);
257 pthread_mutex_unlock(mutex);
262 (list->tail)->next = obj;
264 pthread_mutex_unlock(mutex);
269 NSResult NSConsumerCacheWriteProvider(NSCacheList * list, NSCacheElement * newObj)
271 pthread_mutex_t * mutex = NSGetCacheMutex();
273 pthread_mutex_lock(mutex);
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");
282 pthread_mutex_unlock(mutex);
283 NS_LOG (ERROR, "Failed to Write Provider Cache");
287 NSProvider_internal * newProvObj = (NSProvider_internal *) newObj->data;
289 pthread_mutex_unlock(mutex);
290 NSCacheElement * it = NSStorageRead(list, newProvObj->providerId);
291 pthread_mutex_lock(mutex);
295 NSProvider_internal * provObj = (NSProvider_internal *) it->data;
296 it->data = (void *) NSCopyProvider(newProvObj);
299 NS_LOG (ERROR, "Failed to CopyProvider");
300 it->data = (void *) provObj;
301 pthread_mutex_unlock(mutex);
305 NSRemoveProvider(provObj);
306 pthread_mutex_unlock(mutex);
311 NSCacheElement * obj = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
314 NS_LOG(ERROR, "Fail to Create New Object");
315 pthread_mutex_unlock(mutex);
319 obj->data = (void *) NSCopyProvider(newProvObj);
321 NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2");
322 prov = (NSProvider_internal *)obj->data;
323 NS_LOG_V (DEBUG, "%s", prov->providerId);
324 NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2");
328 NS_LOG (ERROR, "Failed to CopyProvider");
329 pthread_mutex_unlock(mutex);
339 pthread_mutex_unlock(mutex);
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");
349 (list->tail)->next = obj;
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");
357 pthread_mutex_unlock(mutex);
362 NSResult NSStorageDestroy(NSCacheList * list)
364 pthread_mutex_t * mutex = NSGetCacheMutex();
366 pthread_mutex_lock(mutex);
368 NSCacheElement * iter = list->head;
369 NSCacheElement * next = NULL;
371 NSCacheType type = list->cacheType;
373 if (type == NS_CONSUMER_CACHE_MESSAGE)
377 next = (NSCacheElement *) iter->next;
379 NSRemoveMessage((NSMessage_consumer *) iter->data);
387 else if (type == NS_CONSUMER_CACHE_PROVIDER)
391 next = (NSCacheElement *) iter->next;
393 NSRemoveProvider((NSProvider_internal *) iter->data);
402 pthread_mutex_unlock(mutex);
407 bool NSConsumerCompareIdCacheData(NSCacheType type, void * data, const char * id)
414 if (type == NS_CONSUMER_CACHE_MESSAGE)
416 NSMessage_consumer * msg = (NSMessage_consumer *) data;
418 char msgId[NS_DEVICE_ID_LENGTH] = {0, };
419 snprintf(msgId, NS_DEVICE_ID_LENGTH, "%llu", msg->messageId);
420 if (!strcmp(msgId, id))
427 else if (type == NS_CONSUMER_CACHE_PROVIDER)
429 NSProvider_internal * prov = (NSProvider_internal *) data;
431 if (!strcmp(prov->providerId, id))