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 NS_LOG_V(DEBUG, "cache type : %d", type);
107 if (type == NS_CONSUMER_CACHE_MESSAGE)
109 pthread_mutex_unlock(mutex);
111 return NSConsumerCacheWriteMessage(list, newObj);
113 else if (type == NS_CONSUMER_CACHE_PROVIDER)
115 pthread_mutex_unlock(mutex);
117 return NSConsumerCacheWriteProvider(list, newObj);
120 NS_LOG (ERROR, "Not Supported Type");
121 pthread_mutex_unlock(mutex);
126 NSResult NSStorageDelete(NSCacheList * list, const char * delId)
128 pthread_mutex_t * mutex = NSGetCacheMutex();
130 pthread_mutex_lock(mutex);
132 NSCacheType type = list->cacheType;
136 pthread_mutex_unlock(mutex);
137 NS_LOG (ERROR, "Failed to Delete Cache");
141 NSCacheElement * prev = list->head;
142 NSCacheElement * del = list->head;
144 if (NSConsumerCompareIdCacheData(type, del->data, delId))
146 if (del == list->head)
148 if (del == list->tail)
149 list->tail = del->next;
150 list->head = del->next;
152 if (type == NS_CONSUMER_CACHE_MESSAGE)
154 NSRemoveMessage((NSMessage_consumer *) del->data);
156 else if (type == NS_CONSUMER_CACHE_PROVIDER)
158 NSRemoveProvider((NSProvider_internal *) del->data);
161 pthread_mutex_unlock(mutex);
170 if (NSConsumerCompareIdCacheData(type, del->data, delId))
172 if (del == list->tail)
175 prev->next = del->next;
176 if (type == NS_CONSUMER_CACHE_MESSAGE)
178 NSRemoveMessage((NSMessage_consumer *) del->data);
180 else if (type == NS_CONSUMER_CACHE_PROVIDER)
182 NSRemoveProvider((NSProvider_internal *) del->data);
185 pthread_mutex_unlock(mutex);
193 pthread_mutex_unlock(mutex);
197 NSResult NSConsumerCacheWriteMessage(NSCacheList * list, NSCacheElement * newObj)
199 pthread_mutex_t * mutex = NSGetCacheMutex();
201 pthread_mutex_lock(mutex);
205 pthread_mutex_unlock(mutex);
206 NS_LOG (ERROR, "Failed to Write Message Cache");
210 NSMessage_consumer * newMsgObj = (NSMessage_consumer *) newObj->data;
212 pthread_mutex_unlock(mutex);
213 char msgId[NS_DEVICE_ID_LENGTH] = {0, };
214 snprintf(msgId, NS_DEVICE_ID_LENGTH, "%lld", (long long int)newMsgObj->messageId);
215 NSCacheElement * it = NSStorageRead(list, msgId);
216 pthread_mutex_lock(mutex);
220 NSMessage_consumer * msgObj = (NSMessage_consumer *) it->data;
221 if(msgObj->type == newMsgObj->type)
223 NS_LOG (DEBUG, "Already receive message");
224 pthread_mutex_unlock(mutex);
229 it->data = (void *) NSCopyMessage(newMsgObj);
232 NS_LOG (ERROR, "Failed to CopyMessage");
233 it->data = (void *) msgObj;
234 pthread_mutex_unlock(mutex);
238 NSRemoveMessage(msgObj);
239 pthread_mutex_unlock(mutex);
244 NSCacheElement * obj = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
247 NS_LOG(ERROR, "Fail to Create New Object");
248 pthread_mutex_unlock(mutex);
252 obj->data = (void *) NSCopyMessage(newMsgObj);
255 NS_LOG (ERROR, "Failed to CopyMessage");
256 pthread_mutex_unlock(mutex);
266 pthread_mutex_unlock(mutex);
271 (list->tail)->next = obj;
273 pthread_mutex_unlock(mutex);
278 NSResult NSConsumerCacheWriteProvider(NSCacheList * list, NSCacheElement * newObj)
280 pthread_mutex_t * mutex = NSGetCacheMutex();
282 pthread_mutex_lock(mutex);
284 NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1");
285 NSProvider_internal * prov = (NSProvider_internal *)newObj->data;
286 NS_LOG_V (DEBUG, "%s", prov->providerId);
287 NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1");
291 pthread_mutex_unlock(mutex);
292 NS_LOG (ERROR, "Failed to Write Provider Cache");
296 NSProvider_internal * newProvObj = (NSProvider_internal *) newObj->data;
298 pthread_mutex_unlock(mutex);
299 NSCacheElement * it = NSStorageRead(list, newProvObj->providerId);
300 pthread_mutex_lock(mutex);
304 NSProvider_internal * provObj = (NSProvider_internal *) it->data;
306 NSProviderConnectionInfo * infos = provObj->connection;
307 NSProviderConnectionInfo * lastConn = infos->next;
311 lastConn = lastConn->next;
313 infos->next = NSCopyProviderConnections(newProvObj->connection);
315 pthread_mutex_unlock(mutex);
320 NSCacheElement * obj = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
323 NS_LOG(ERROR, "Fail to Create New Object");
324 pthread_mutex_unlock(mutex);
328 NS_LOG_V(DEBUG, "New Object address : %s:%d", newProvObj->connection->addr->addr, newProvObj->connection->addr->port);
329 obj->data = (void *) NSCopyProvider(newProvObj);
331 NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2");
332 prov = (NSProvider_internal *)obj->data;
333 NS_LOG_V (DEBUG, "%s", prov->providerId);
334 NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2");
338 NS_LOG (ERROR, "Failed to CopyProvider");
339 pthread_mutex_unlock(mutex);
350 NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!3");
351 prov = (NSProvider_internal *)list->tail->data;
352 NS_LOG_V (DEBUG, "%s", prov->providerId);
353 NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!3");
355 pthread_mutex_unlock(mutex);
360 (list->tail)->next = obj;
363 NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!4");
364 prov = (NSProvider_internal *)list->tail->data;
365 NS_LOG_V (DEBUG, "%s", prov->providerId);
366 NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!4");
368 pthread_mutex_unlock(mutex);
373 NSResult NSStorageDestroy(NSCacheList * list)
375 pthread_mutex_t * mutex = NSGetCacheMutex();
377 pthread_mutex_lock(mutex);
379 NSCacheElement * iter = list->head;
380 NSCacheElement * next = NULL;
382 NSCacheType type = list->cacheType;
384 if (type == NS_CONSUMER_CACHE_MESSAGE)
388 next = (NSCacheElement *) iter->next;
390 NSRemoveMessage((NSMessage_consumer *) iter->data);
398 else if (type == NS_CONSUMER_CACHE_PROVIDER)
402 next = (NSCacheElement *) iter->next;
404 NSRemoveProvider((NSProvider_internal *) iter->data);
413 pthread_mutex_unlock(mutex);
418 bool NSConsumerCompareIdCacheData(NSCacheType type, void * data, const char * id)
425 if (type == NS_CONSUMER_CACHE_MESSAGE)
427 NSMessage_consumer * msg = (NSMessage_consumer *) data;
429 char msgId[NS_DEVICE_ID_LENGTH] = {0, };
430 snprintf(msgId, NS_DEVICE_ID_LENGTH, "%lld", (long long int)msg->messageId);
431 if (!strcmp(msgId, id))
438 else if (type == NS_CONSUMER_CACHE_PROVIDER)
440 NSProvider_internal * prov = (NSProvider_internal *) data;
442 if (!strcmp(prov->providerId, id))