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 * g_NSCacheMutex = NULL;
28 if (g_NSCacheMutex == NULL)
30 g_NSCacheMutex = (pthread_mutex_t *) OICMalloc(sizeof(pthread_mutex_t));
31 NS_VERIFY_NOT_NULL(g_NSCacheMutex, NULL);
33 pthread_mutex_init(g_NSCacheMutex, NULL);
35 return g_NSCacheMutex;
38 NSCacheList * NSStorageCreate()
40 pthread_mutex_t * mutex = NSGetCacheMutex();
41 pthread_mutex_lock(mutex);
43 NSCacheList * newList = (NSCacheList *) OICMalloc(sizeof(NSCacheList));
44 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(newList, NULL, pthread_mutex_unlock(mutex));
49 pthread_mutex_unlock(mutex);
54 NSCacheElement * NSStorageRead(NSCacheList * list, const char * findId)
56 NS_VERIFY_NOT_NULL(list, NULL);
57 NS_VERIFY_NOT_NULL(findId, NULL);
59 pthread_mutex_t * mutex = NSGetCacheMutex();
60 pthread_mutex_lock(mutex);
62 NSCacheElement * iter = list->head;
63 NSCacheType type = list->cacheType;
67 if (NSConsumerCompareIdCacheData(type, iter->data, findId))
69 pthread_mutex_unlock(mutex);
76 NS_LOG (DEBUG, "No Cache Element");
77 pthread_mutex_unlock(mutex);
81 NSResult NSStorageWrite(NSCacheList * list, NSCacheElement * newObj)
83 NS_VERIFY_NOT_NULL(list, NS_ERROR);
84 NS_VERIFY_NOT_NULL(newObj, NS_ERROR);
86 NSCacheType type = list->cacheType;
87 NS_LOG_V(DEBUG, "cache type : %d", type);
89 if (type == NS_CONSUMER_CACHE_MESSAGE)
91 return NSConsumerCacheWriteMessage(list, newObj);
93 else if (type == NS_CONSUMER_CACHE_PROVIDER)
95 return NSConsumerCacheWriteProvider(list, newObj);
98 NS_LOG (ERROR, "Not Supported Type");
103 NSResult NSStorageDelete(NSCacheList * list, const char * delId)
105 NS_VERIFY_NOT_NULL(list, NS_ERROR);
106 NS_VERIFY_NOT_NULL(delId, NS_ERROR);
108 NSCacheType type = list->cacheType;
110 pthread_mutex_t * mutex = NSGetCacheMutex();
111 pthread_mutex_lock(mutex);
113 NSCacheElement * prev = list->head;
114 NSCacheElement * del = list->head;
115 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(del, NS_ERROR, pthread_mutex_unlock(mutex));
117 if (NSConsumerCompareIdCacheData(type, del->data, delId))
119 if (del == list->head)
121 if (del == list->tail)
123 list->tail = del->next;
126 list->head = del->next;
128 if (type == NS_CONSUMER_CACHE_MESSAGE)
130 NSRemoveMessage((NSMessage *) del->data);
132 else if (type == NS_CONSUMER_CACHE_PROVIDER)
134 NSRemoveProvider((NSProvider_internal *) del->data);
137 pthread_mutex_unlock(mutex);
146 if (NSConsumerCompareIdCacheData(type, del->data, delId))
148 if (del == list->tail)
153 prev->next = del->next;
154 if (type == NS_CONSUMER_CACHE_MESSAGE)
156 NSRemoveMessage((NSMessage *) del->data);
158 else if (type == NS_CONSUMER_CACHE_PROVIDER)
160 NSRemoveProvider((NSProvider_internal *) del->data);
163 pthread_mutex_unlock(mutex);
171 pthread_mutex_unlock(mutex);
175 void NSConsumerRemoveMessageStore(NSCacheElement * ele, NSStoreMessage * msg)
181 NSResult NSConsumerCacheWriteMessage(NSCacheList * list, NSCacheElement * newObj)
183 NS_VERIFY_NOT_NULL(list, NS_ERROR);
184 NS_VERIFY_NOT_NULL(newObj, NS_ERROR);
186 pthread_mutex_t * mutex = NSGetCacheMutex();
188 NSMessage * newMsgObj = ((NSStoreMessage *)newObj->data)->msg;
190 char msgId[NS_DEVICE_ID_LENGTH] = {0, };
191 snprintf(msgId, NS_DEVICE_ID_LENGTH, "%lld", (long long int)newMsgObj->messageId);
192 NSCacheElement * it = NSStorageRead(list, msgId);
196 NS_LOG(DEBUG, "Update message status.");
197 pthread_mutex_lock(mutex);
198 NSStoreMessage * sMsgObj = (NSStoreMessage *) it->data;
199 if(sMsgObj->status == ((NSStoreMessage *)newObj->data)->status)
201 NS_LOG (DEBUG, "Already receive message");
202 pthread_mutex_unlock(mutex);
206 sMsgObj->status = ((NSStoreMessage *)newObj->data)->status;
207 pthread_mutex_unlock(mutex);
211 NS_LOG(DEBUG, "Add message at storage.");
212 NSStoreMessage * sMsgObj = (NSStoreMessage *) OICMalloc(sizeof(NSStoreMessage));
213 NS_VERIFY_NOT_NULL(sMsgObj, NS_ERROR);
215 NSCacheElement * obj = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
216 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(obj, NS_ERROR, NSOICFree(sMsgObj));
218 sMsgObj->status = NS_SYNC_UNREAD;
219 sMsgObj->msg = (void *) NSCopyMessage(newMsgObj);
220 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(sMsgObj->msg, NS_ERROR,
221 NSConsumerRemoveMessageStore(obj, sMsgObj));
224 obj->data = (NSCacheData *) sMsgObj;
226 pthread_mutex_lock(mutex);
231 pthread_mutex_unlock(mutex);
235 (list->tail)->next = obj;
237 pthread_mutex_unlock(mutex);
242 NSResult NSConsumerCacheWriteProvider(NSCacheList * list, NSCacheElement * newObj)
244 NS_VERIFY_NOT_NULL(list, NS_ERROR);
245 NS_VERIFY_NOT_NULL(newObj, NS_ERROR);
247 pthread_mutex_t * mutex = NSGetCacheMutex();
249 NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1");
250 NSProvider_internal * prov = (NSProvider_internal *)newObj->data;
251 NS_LOG_V (DEBUG, "%s", prov->providerId);
252 NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1");
254 NSProvider_internal * newProvObj = (NSProvider_internal *) newObj->data;
256 NSCacheElement * it = NSStorageRead(list, newProvObj->providerId);
257 pthread_mutex_lock(mutex);
261 NSProvider_internal * provObj = (NSProvider_internal *) it->data;
263 NSProviderConnectionInfo * infos = provObj->connection;
264 NSProviderConnectionInfo * lastConn = infos->next;
268 lastConn = lastConn->next;
270 infos->next = NSCopyProviderConnections(newProvObj->connection);
272 pthread_mutex_unlock(mutex);
277 NSCacheElement * obj = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
278 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(obj, NS_ERROR, pthread_mutex_unlock(mutex));
280 NS_LOG_V(DEBUG, "New Object address : %s:%d", newProvObj->connection->addr->addr, newProvObj->connection->addr->port);
281 obj->data = (void *) NSCopyProvider(newProvObj);
283 NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2");
284 prov = (NSProvider_internal *)obj->data;
285 NS_LOG_V (DEBUG, "%s", prov->providerId);
286 NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2");
290 NS_LOG (ERROR, "Failed to CopyProvider");
291 pthread_mutex_unlock(mutex);
302 NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!3");
303 prov = (NSProvider_internal *)list->tail->data;
304 NS_LOG_V (DEBUG, "%s", prov->providerId);
305 NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!3");
307 pthread_mutex_unlock(mutex);
312 (list->tail)->next = obj;
315 NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!4");
316 prov = (NSProvider_internal *)list->tail->data;
317 NS_LOG_V (DEBUG, "%s", prov->providerId);
318 NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!4");
320 pthread_mutex_unlock(mutex);
325 NSCacheElement * NSPopProviderCacheList(NSCacheList * list)
327 NS_VERIFY_NOT_NULL(list, NULL);
329 pthread_mutex_t * mutex = NSGetCacheMutex();
330 pthread_mutex_lock(mutex);
332 NSCacheElement * head = list->head;
335 if (list->tail == head)
340 list->head = head->next;
344 pthread_mutex_unlock(mutex);
349 NSResult NSStorageDestroy(NSCacheList * list)
351 NS_VERIFY_NOT_NULL(list, NS_ERROR);
353 pthread_mutex_t * mutex = NSGetCacheMutex();
354 pthread_mutex_lock(mutex);
356 NSCacheElement * iter = list->head;
357 NSCacheElement * next = NULL;
359 NSCacheType type = list->cacheType;
361 if (type == NS_CONSUMER_CACHE_MESSAGE)
365 next = (NSCacheElement *) iter->next;
367 NSRemoveMessage(((NSStoreMessage *) iter->data)->msg);
368 NSOICFree(iter->data);
376 else if (type == NS_CONSUMER_CACHE_PROVIDER)
380 next = (NSCacheElement *) iter->next;
382 NSRemoveProvider((NSProvider_internal *) iter->data);
391 pthread_mutex_unlock(mutex);
396 bool NSConsumerCompareIdCacheData(NSCacheType type, void * data, const char * id)
398 NS_VERIFY_NOT_NULL(data, false);
399 NS_VERIFY_NOT_NULL(id, false);
401 if (type == NS_CONSUMER_CACHE_MESSAGE)
403 NSMessage * msg = ((NSStoreMessage *) data)->msg;
405 char msgId[NS_DEVICE_ID_LENGTH] = {0, };
406 snprintf(msgId, NS_DEVICE_ID_LENGTH, "%lld", (long long int)msg->messageId);
407 if (!strcmp(msgId, id))
414 else if (type == NS_CONSUMER_CACHE_PROVIDER)
416 NSProvider_internal * prov = (NSProvider_internal *) data;
417 if (!strcmp(prov->providerId, id))