1 //******************************************************************
\r
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
\r
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
7 // Licensed under the Apache License, Version 2.0 (the "License");
\r
8 // you may not use this file except in compliance with the License.
\r
9 // You may obtain a copy of the License at
\r
11 // http://www.apache.org/licenses/LICENSE-2.0
\r
13 // Unless required by applicable law or agreed to in writing, software
\r
14 // distributed under the License is distributed on an "AS IS" BASIS,
\r
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 // See the License for the specific language governing permissions and
\r
17 // limitations under the License.
\r
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
20 #include "NSProviderMemoryCache.h"
\r
22 NSCacheList * NSCacheCreate()
\r
24 pthread_mutex_lock(&NSCacheMutex);
\r
25 NSCacheList * newList = (NSCacheList *) OICMalloc(sizeof(NSCacheList));
\r
28 pthread_mutex_unlock(&NSCacheMutex);
\r
32 newList->head = newList->tail = NULL;
\r
34 pthread_mutex_unlock(&NSCacheMutex);
\r
39 NSCacheElement * NSCacheRead(NSCacheList * list, const char * findId)
\r
41 pthread_mutex_lock(&NSCacheMutex);
\r
42 NSCacheElement * iter = list->head;
\r
43 NSCacheElement * next = NULL;
\r
44 NSCacheType type = list->cacheType;
\r
46 printf("NS_ findId = %s\n", findId);
\r
52 printf("NS_ findId2 = %s\n", findId);
\r
54 if (NSProviderCompareIdCacheData(type, iter->data, findId))
\r
56 pthread_mutex_unlock(&NSCacheMutex);
\r
63 pthread_mutex_unlock(&NSCacheMutex);
\r
68 NSResult NSCacheUpdateSubScriptionState(NSCacheList * list, NSCacheSubData * subData)
\r
70 pthread_mutex_lock(&NSCacheMutex);
\r
72 printf("NS_ NSCacheUpdateSubScriptionState\n");
\r
74 if (subData == NULL)
\r
76 printf("NS_ NSCacheWrite - newObj is NULL\n");
\r
77 pthread_mutex_unlock(&NSCacheMutex);
\r
81 printf("NS_ findId0 - 0 = %s\n", subData->id);
\r
82 pthread_mutex_unlock(&NSCacheMutex);
\r
83 NSCacheElement * it = NSCacheRead(list, subData->id);
\r
84 pthread_mutex_lock(&NSCacheMutex);
\r
85 printf("NS_ findId0 -1 = %s\n", subData->id);
\r
88 printf("NS_ findId1 = %s\n", subData->id);
\r
89 NSCacheSubData * itData = (NSCacheSubData *) it->data;
\r
90 printf("NS_ findId2 = %s\n", subData->id);
\r
91 if (strcmp(itData->id, subData->id) == 0)
\r
93 printf("NS_ findId3_subdata_id = %s\n", subData->id);
\r
94 printf("NS_ findId3_subdata_messageobId = %d\n", subData->messageObId);
\r
95 printf("NS_ findId3_subdata_syncobId = %d\n", subData->syncObId);
\r
96 printf("NS_ findId3_subdata_isWhite = %d\n", subData->isWhite);
\r
98 printf("NS_ findId3_itdata_id = %s\n", itData->id);
\r
99 printf("NS_ findId3_itdata_messageobId = %d\n", itData->messageObId);
\r
100 printf("NS_ findId3_itdata_isWhite = %d\n", itData->isWhite);
\r
102 itData->isWhite = subData->isWhite;
\r
104 printf("_NS : PROVIDER_CACHE_SUBSCRIBER UPDATE\n");
\r
106 pthread_mutex_unlock(&NSCacheMutex);
\r
111 pthread_mutex_unlock(&NSCacheMutex);
\r
115 NSResult NSCacheWrite(NSCacheList * list, NSCacheElement * newObj)
\r
117 pthread_mutex_lock(&NSCacheMutex);
\r
119 NSCacheType type = list->cacheType;
\r
121 printf("NS_ NSCacheWrite\n");
\r
123 if (newObj == NULL)
\r
125 printf("NS_ NSCacheWrite - newObj is NULL\n");
\r
126 pthread_mutex_unlock(&NSCacheMutex);
\r
130 if (type == NS_PROVIDER_CACHE_SUBSCRIBER)
\r
132 NSCacheSubData * subData = (NSCacheSubData *) newObj->data;
\r
134 printf("NS_ findId0 - 0 = %s\n", subData->id);
\r
135 pthread_mutex_unlock(&NSCacheMutex);
\r
136 NSCacheElement * it = NSCacheRead(list, subData->id);
\r
137 pthread_mutex_lock(&NSCacheMutex);
\r
138 printf("NS_ findId0 -1 = %s\n", subData->id);
\r
141 printf("NS_ findId1 = %s\n", subData->id);
\r
142 NSCacheSubData * itData = (NSCacheSubData *) it->data;
\r
143 printf("NS_ findId2 = %s\n", subData->id);
\r
144 if (strcmp(itData->id, subData->id) == 0)
\r
146 printf("NS_ findId3_subdata_id = %s\n", subData->id);
\r
147 printf("NS_ findId3_subdata_messageobId = %d\n", subData->messageObId);
\r
148 printf("NS_ findId3_subdata_syncobId = %d\n", subData->syncObId);
\r
149 printf("NS_ findId3_subdata_isWhite = %d\n", subData->isWhite);
\r
151 printf("NS_ findId3_itdata_id = %s\n", itData->id);
\r
152 printf("NS_ findId3_itdata_messageobId = %d\n", itData->messageObId);
\r
153 printf("NS_ findId3_itdata_isWhite = %d\n", itData->isWhite);
\r
154 if (itData->messageObId == 0)
\r
156 itData->messageObId = subData->messageObId;
\r
159 if (itData->syncObId == 0)
\r
161 itData->syncObId = subData->syncObId;
\r
164 //itData->isWhite = subData->isWhite;
\r
166 printf("_NS : PROVIDER_CACHE_SUBSCRIBER UPDATE\n");
\r
168 pthread_mutex_unlock(&NSCacheMutex);
\r
175 else if (type == NS_PROVIDER_CACHE_MESSAGE)
\r
177 NSCacheMsgData * msgData = (NSCacheMsgData *) newObj->data;
\r
179 NSCacheElement * it = NSCacheRead(list, msgData->id);
\r
182 NSCacheMsgData * itData = (NSCacheMsgData *) it->data;
\r
184 if (strcmp(itData->id, msgData->id) == 0)
\r
187 itData->messageType = msgData->messageType;
\r
188 printf("_NS : PROVIDER_CACHE_MESSAGE UPDATE\n");
\r
189 pthread_mutex_unlock(&NSCacheMutex);
\r
196 printf("NS_ berfore list->head == NULL\n");
\r
197 if (list->head == NULL)
\r
199 list->head = list->tail = newObj;
\r
200 printf("NS_ list->head == NULL\n");
\r
201 pthread_mutex_unlock(&NSCacheMutex);
\r
205 list->tail = list->tail->next = newObj;
\r
206 printf("NS_ list->head == not NULL\n");
\r
207 pthread_mutex_unlock(&NSCacheMutex);
\r
211 NSResult NSCacheDelete(NSCacheList * list, const char * delId)
\r
213 pthread_mutex_lock(&NSCacheMutex);
\r
214 NSCacheElement * prev = list->head;
\r
215 NSCacheElement * del = list->head;
\r
217 NSCacheType type = list->cacheType;
\r
219 if (NSProviderCompareIdCacheData(type, del->data, delId))
\r
221 if (del == list->head) // first object
\r
223 if (del == list->tail) // first object (one object)
\r
224 list->tail = del->next;
\r
226 list->head = del->next;
\r
228 NSProviderDeleteCacheData(type, del->data);
\r
230 pthread_mutex_unlock(&NSCacheMutex);
\r
238 if (NSProviderCompareIdCacheData(type, del->data, delId))
\r
240 if (del == list->tail) // delete object same to last object
\r
243 prev->next = del->next;
\r
244 NSProviderDeleteCacheData(type, del->data);
\r
246 pthread_mutex_unlock(&NSCacheMutex);
\r
253 pthread_mutex_unlock(&NSCacheMutex);
\r
257 NSResult NSCacheDestroy(NSCacheList * list)
\r
259 NSCacheElement * iter = list->head;
\r
260 NSCacheElement * next = NULL;
\r
262 NSCacheType type = list->cacheType;
\r
266 next = (NSCacheElement *) iter->next;
\r
268 NSProviderDeleteCacheData(type, iter->data);
\r
279 bool NSProviderCompareIdCacheData(NSCacheType type, void * data, const char * id)
\r
286 if (type == NS_PROVIDER_CACHE_SUBSCRIBER)
\r
288 NSCacheSubData * subData = (NSCacheSubData *) data;
\r
290 printf("NS_ subdata->id = %s\n", subData->id);
\r
291 printf("NS_ id = %s\n", id);
\r
293 if (strcmp(subData->id, id) == 0)
\r
300 else if (type == NS_PROVIDER_CACHE_MESSAGE)
\r
302 NSCacheMsgData * msgData = (NSCacheMsgData *) data;
\r
304 if (strcmp(msgData->id, id) == 0)
\r
315 NSResult NSProviderDeleteCacheData(NSCacheType type, void * data)
\r
322 if (type == NS_PROVIDER_CACHE_SUBSCRIBER)
\r
324 NSCacheSubData * subData = (NSCacheSubData *) data;
\r
327 OICFree(subData->id);
\r
328 subData->id = NULL;
\r
335 else if (type == NS_PROVIDER_CACHE_MESSAGE)
\r
337 NSCacheMsgData * msgData = (NSCacheMsgData *) data;
\r
341 OICFree(msgData->id);
\r
342 msgData->id = NULL;
\r
345 if (msgData->nsMessage)
\r
347 NSFreeMessage(msgData->nsMessage);
\r