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 * NSConsumerStorageCreate()
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 * NSConsumerStorageRead(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 ((NSProvider_internal *) iter->data)->connection;
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 NSConsumerStorageWrite(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_PROVIDER)
128 return NSConsumerCacheWriteProvider(list, newObj);
131 NS_LOG (ERROR, "Not Supported Type");
136 NSResult NSConsumerStorageDelete(NSCacheList * list, const char * delId)
138 NS_VERIFY_NOT_NULL(list, NS_ERROR);
139 NS_VERIFY_NOT_NULL(delId, NS_ERROR);
141 NSCacheType type = list->cacheType;
143 pthread_mutex_t * mutex = NSGetCacheMutex();
144 pthread_mutex_lock(mutex);
146 NSCacheElement * prev = list->head;
147 NSCacheElement * del = list->head;
148 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(del, NS_ERROR, pthread_mutex_unlock(mutex));
150 if (NSConsumerCompareIdCacheData(type, del->data, delId))
152 if (del == list->head)
154 if (del == list->tail)
156 list->tail = del->next;
159 list->head = del->next;
161 if (type == NS_CONSUMER_CACHE_PROVIDER)
163 NSRemoveProvider_internal((void *) del->data);
166 pthread_mutex_unlock(mutex);
175 if (NSConsumerCompareIdCacheData(type, del->data, delId))
177 if (del == list->tail)
182 prev->next = del->next;
184 if (type == NS_CONSUMER_CACHE_PROVIDER)
186 NSRemoveProvider_internal((NSProvider_internal *) del->data);
189 pthread_mutex_unlock(mutex);
197 pthread_mutex_unlock(mutex);
201 NSResult NSConsumerCacheWriteProvider(NSCacheList * list, NSCacheElement * newObj)
203 NS_VERIFY_NOT_NULL(list, NS_ERROR);
204 NS_VERIFY_NOT_NULL(newObj, NS_ERROR);
206 pthread_mutex_t * mutex = NSGetCacheMutex();
208 NSProvider_internal * newProvObj = (NSProvider_internal *) newObj->data;
210 NSCacheElement * it = NSConsumerStorageRead(list, newProvObj->providerId);
212 pthread_mutex_lock(mutex);
216 if (newProvObj->connection)
218 NSProvider_internal * provObj = (NSProvider_internal *) it->data;
220 NSProviderConnectionInfo * infos = provObj->connection;
221 NSProviderConnectionInfo * lastConn = infos->next;
225 lastConn = lastConn->next;
227 infos->next = NSCopyProviderConnections(newProvObj->connection);
230 if (newProvObj->topicLL)
232 NSProvider_internal * provObj = (NSProvider_internal *) it->data;
233 NSRemoveTopicLL(provObj->topicLL);
234 provObj->topicLL = NSCopyTopicLL(newProvObj->topicLL);
237 pthread_mutex_unlock(mutex);
242 NSCacheElement * obj = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
243 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(obj, NS_ERROR, pthread_mutex_unlock(mutex));
245 NS_LOG_V(INFO_PRIVATE, "New Object address : %s:%d", newProvObj->connection->addr->addr, newProvObj->connection->addr->port);
246 obj->data = (void *) NSCopyProvider_internal(newProvObj);
250 NS_LOG (ERROR, "Failed to CopyProvider");
252 pthread_mutex_unlock(mutex);
263 pthread_mutex_unlock(mutex);
268 (list->tail)->next = obj;
271 pthread_mutex_unlock(mutex);
276 NSCacheElement * NSPopProviderCacheList(NSCacheList * list)
278 NS_VERIFY_NOT_NULL(list, NULL);
280 pthread_mutex_t * mutex = NSGetCacheMutex();
281 pthread_mutex_lock(mutex);
283 NSCacheElement * head = list->head;
286 if (list->tail == head)
291 list->head = head->next;
295 pthread_mutex_unlock(mutex);
300 NSResult NSConsumerStorageDestroy(NSCacheList * list)
302 NS_VERIFY_NOT_NULL(list, NS_ERROR);
304 pthread_mutex_t * mutex = NSGetCacheMutex();
305 pthread_mutex_lock(mutex);
307 NSCacheElement * iter = list->head;
308 NSCacheElement * next = NULL;
310 NSCacheType type = list->cacheType;
312 if (type == NS_CONSUMER_CACHE_PROVIDER)
316 next = (NSCacheElement *) iter->next;
318 NSRemoveProvider_internal((void *) iter->data);
327 pthread_mutex_unlock(mutex);
332 bool NSConsumerCompareIdCacheData(NSCacheType type, void * data, const char * id)
334 NS_VERIFY_NOT_NULL(data, false);
335 NS_VERIFY_NOT_NULL(id, false);
337 if (type == NS_CONSUMER_CACHE_PROVIDER)
339 NSProvider_internal * prov = (NSProvider_internal *) data;
340 if (!strcmp(prov->providerId, id))