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 NSCacheElement * NSGetProviderFromAddr(NSCacheList * list, const char * addr, uint16_t port)
83 NS_VERIFY_NOT_NULL(list, NULL);
84 NS_VERIFY_NOT_NULL(addr, NULL);
86 (list->cacheType != NS_CONSUMER_CACHE_PROVIDER) ? NULL : (void *) 1, NULL);
88 pthread_mutex_t * mutex = NSGetCacheMutex();
89 pthread_mutex_lock(mutex);
91 NSCacheElement * iter = list->head;
95 NSProviderConnectionInfo * connection =
96 ((NSProviderConnectionInfo *) iter->data)->next;
99 char * conAddr = connection->addr->addr;
100 uint16_t conPort = connection->addr->port;
102 if (!strcmp(conAddr, addr) && conPort == port)
104 pthread_mutex_unlock(mutex);
107 connection = connection->next;
113 NS_LOG (DEBUG, "No Cache Element");
114 pthread_mutex_unlock(mutex);
118 NSResult NSStorageWrite(NSCacheList * list, NSCacheElement * newObj)
120 NS_VERIFY_NOT_NULL(list, NS_ERROR);
121 NS_VERIFY_NOT_NULL(newObj, NS_ERROR);
123 NSCacheType type = list->cacheType;
124 NS_LOG_V(DEBUG, "cache type : %d", type);
126 if (type == NS_CONSUMER_CACHE_MESSAGE)
128 return NSConsumerCacheWriteMessage(list, newObj);
130 else if (type == NS_CONSUMER_CACHE_PROVIDER)
132 return NSConsumerCacheWriteProvider(list, newObj);
135 NS_LOG (ERROR, "Not Supported Type");
140 NSResult NSStorageDelete(NSCacheList * list, const char * delId)
142 NS_VERIFY_NOT_NULL(list, NS_ERROR);
143 NS_VERIFY_NOT_NULL(delId, NS_ERROR);
145 NSCacheType type = list->cacheType;
147 pthread_mutex_t * mutex = NSGetCacheMutex();
148 pthread_mutex_lock(mutex);
150 NSCacheElement * prev = list->head;
151 NSCacheElement * del = list->head;
152 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(del, NS_ERROR, pthread_mutex_unlock(mutex));
154 if (NSConsumerCompareIdCacheData(type, del->data, delId))
156 if (del == list->head)
158 if (del == list->tail)
160 list->tail = del->next;
163 list->head = del->next;
165 if (type == NS_CONSUMER_CACHE_MESSAGE)
167 NSRemoveMessage((NSMessage *) del->data);
169 else if (type == NS_CONSUMER_CACHE_PROVIDER)
171 NSRemoveProvider_internal((NSProvider_internal *) del->data);
174 pthread_mutex_unlock(mutex);
183 if (NSConsumerCompareIdCacheData(type, del->data, delId))
185 if (del == list->tail)
190 prev->next = del->next;
191 if (type == NS_CONSUMER_CACHE_MESSAGE)
193 NSRemoveMessage((NSMessage *) del->data);
195 else if (type == NS_CONSUMER_CACHE_PROVIDER)
197 NSRemoveProvider_internal((NSProvider_internal *) del->data);
200 pthread_mutex_unlock(mutex);
208 pthread_mutex_unlock(mutex);
212 void NSConsumerRemoveMessageStore(NSCacheElement * ele, NSStoreMessage * msg)
218 NSResult NSConsumerCacheWriteMessage(NSCacheList * list, NSCacheElement * newObj)
220 NS_VERIFY_NOT_NULL(list, NS_ERROR);
221 NS_VERIFY_NOT_NULL(newObj, NS_ERROR);
223 pthread_mutex_t * mutex = NSGetCacheMutex();
225 NSMessage * newMsgObj = ((NSStoreMessage *)newObj->data)->msg;
227 char msgId[NS_DEVICE_ID_LENGTH] = {0, };
228 snprintf(msgId, NS_DEVICE_ID_LENGTH, "%lld", (long long int)newMsgObj->messageId);
229 NSCacheElement * it = NSStorageRead(list, msgId);
233 NS_LOG(DEBUG, "Update message status.");
234 pthread_mutex_lock(mutex);
235 NSStoreMessage * sMsgObj = (NSStoreMessage *) it->data;
236 if(sMsgObj->status == ((NSStoreMessage *)newObj->data)->status)
238 NS_LOG (DEBUG, "Already receive message");
239 pthread_mutex_unlock(mutex);
243 sMsgObj->status = ((NSStoreMessage *)newObj->data)->status;
244 pthread_mutex_unlock(mutex);
248 NS_LOG(DEBUG, "Add message at storage.");
249 NSStoreMessage * sMsgObj = (NSStoreMessage *) OICMalloc(sizeof(NSStoreMessage));
250 NS_VERIFY_NOT_NULL(sMsgObj, NS_ERROR);
252 NSCacheElement * obj = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
253 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(obj, NS_ERROR, NSOICFree(sMsgObj));
255 sMsgObj->status = NS_SYNC_UNREAD;
256 sMsgObj->msg = (void *) NSCopyMessage(newMsgObj);
257 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(sMsgObj->msg, NS_ERROR,
258 NSConsumerRemoveMessageStore(obj, sMsgObj));
261 obj->data = (NSCacheData *) sMsgObj;
263 pthread_mutex_lock(mutex);
268 pthread_mutex_unlock(mutex);
272 (list->tail)->next = obj;
274 pthread_mutex_unlock(mutex);
279 NSResult NSConsumerCacheWriteProvider(NSCacheList * list, NSCacheElement * newObj)
281 NS_VERIFY_NOT_NULL(list, NS_ERROR);
282 NS_VERIFY_NOT_NULL(newObj, NS_ERROR);
284 pthread_mutex_t * mutex = NSGetCacheMutex();
286 NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1");
287 NSProvider_internal * prov = (NSProvider_internal *)newObj->data;
288 NS_LOG_V (DEBUG, "%s", prov->providerId);
289 NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1");
291 NSProvider_internal * newProvObj = (NSProvider_internal *) newObj->data;
293 NSCacheElement * it = NSStorageRead(list, newProvObj->providerId);
294 pthread_mutex_lock(mutex);
298 NSProvider_internal * provObj = (NSProvider_internal *) it->data;
300 NSProviderConnectionInfo * infos = provObj->connection;
301 NSProviderConnectionInfo * lastConn = infos->next;
305 lastConn = lastConn->next;
307 infos->next = NSCopyProviderConnections(newProvObj->connection);
309 pthread_mutex_unlock(mutex);
314 NSCacheElement * obj = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
315 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(obj, NS_ERROR, pthread_mutex_unlock(mutex));
317 NS_LOG_V(DEBUG, "New Object address : %s:%d", newProvObj->connection->addr->addr, newProvObj->connection->addr->port);
318 obj->data = (void *) NSCopyProvider_internal(newProvObj);
320 NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2");
321 prov = (NSProvider_internal *)obj->data;
322 NS_LOG_V (DEBUG, "%s", prov->providerId);
323 NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2");
327 NS_LOG (ERROR, "Failed to CopyProvider");
328 pthread_mutex_unlock(mutex);
339 NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!3");
340 prov = (NSProvider_internal *)list->tail->data;
341 NS_LOG_V (DEBUG, "%s", prov->providerId);
342 NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!3");
344 pthread_mutex_unlock(mutex);
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 NSCacheElement * NSPopProviderCacheList(NSCacheList * list)
364 NS_VERIFY_NOT_NULL(list, NULL);
366 pthread_mutex_t * mutex = NSGetCacheMutex();
367 pthread_mutex_lock(mutex);
369 NSCacheElement * head = list->head;
372 if (list->tail == head)
377 list->head = head->next;
381 pthread_mutex_unlock(mutex);
386 NSResult NSStorageDestroy(NSCacheList * list)
388 NS_VERIFY_NOT_NULL(list, NS_ERROR);
390 pthread_mutex_t * mutex = NSGetCacheMutex();
391 pthread_mutex_lock(mutex);
393 NSCacheElement * iter = list->head;
394 NSCacheElement * next = NULL;
396 NSCacheType type = list->cacheType;
398 if (type == NS_CONSUMER_CACHE_MESSAGE)
402 next = (NSCacheElement *) iter->next;
404 NSRemoveMessage(((NSStoreMessage *) iter->data)->msg);
405 NSOICFree(iter->data);
413 else if (type == NS_CONSUMER_CACHE_PROVIDER)
417 next = (NSCacheElement *) iter->next;
419 NSRemoveProvider_internal((NSProvider_internal *) iter->data);
428 pthread_mutex_unlock(mutex);
433 bool NSConsumerCompareIdCacheData(NSCacheType type, void * data, const char * id)
435 NS_VERIFY_NOT_NULL(data, false);
436 NS_VERIFY_NOT_NULL(id, false);
438 if (type == NS_CONSUMER_CACHE_MESSAGE)
440 NSMessage * msg = ((NSStoreMessage *) data)->msg;
442 char msgId[NS_DEVICE_ID_LENGTH] = {0, };
443 snprintf(msgId, NS_DEVICE_ID_LENGTH, "%lld", (long long int)msg->messageId);
444 if (!strcmp(msgId, id))
451 else if (type == NS_CONSUMER_CACHE_PROVIDER)
453 NSProvider_internal * prov = (NSProvider_internal *) data;
454 if (!strcmp(prov->providerId, id))