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 "NSConsumerCache.h"
22 #include "NSConsumerCommon.h"
23 #include "NSConstants.h"
26 #include "oic_malloc.h"
27 #include "oic_string.h"
29 NSCacheList * NSConsumerCacheInit()
31 NSCacheList * retNSCacheList = (NSCacheList *)OICMalloc(sizeof(NSCacheList));
37 retNSCacheList->head = NULL;
38 retNSCacheList->tail = NULL;
40 return retNSCacheList;
43 NSResult NSConsumerCacheDestroy(NSCacheList * list)
45 NSCacheObject * iter = list->head;
47 NSCacheObject * next = NULL;
50 next = (NSCacheObject *)iter->next;
51 if (NS_OK != NSConsumerCacheDelete(list, iter))
58 OICFree((NSCacheList *) list);
63 NSResult NSConsumerCacheInsert(NSCacheList * list, NSCacheObject * newObj)
65 NSCacheObject * obj = (NSCacheObject *)OICMalloc(sizeof(NSCacheObject));
68 NS_LOG(ERROR, "Fail to Create New Object");
72 NSMessage_consumer * msgObj = (NSMessage_consumer *) OICMalloc(sizeof(NSMessage_consumer));
73 NSMessage_consumer * newMsgObj = (NSMessage_consumer *) newObj->data;
75 NS_LOG_V(DEBUG, "Title: %s", newMsgObj->mTitle);
76 NS_LOG_V(DEBUG, "ID: %s", newMsgObj->mId);
77 NS_LOG_V(DEBUG, "TEXT: %s", newMsgObj->mContentText);
79 msgObj->mTitle = OICStrdup(newMsgObj->mTitle);
80 msgObj->mId = OICStrdup(newMsgObj->mId);
81 msgObj->mContentText = OICStrdup(newMsgObj->mContentText);
85 NS_LOG(ERROR, "Notification Attributes copy fail");
88 msgObj->addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
91 NS_LOG(ERROR, "OCDevAddr allocation is failed.");
94 memcpy(msgObj->addr, newMsgObj->addr, sizeof(OCDevAddr));
95 msgObj->type = newMsgObj->type;
97 //obj->data = (NSCacheData *)OICMalloc(sizeof(NSCacheData));
98 obj->data = (NSCacheData *) msgObj;
99 //memcpy(obj->data, (NSCacheData *)msgObj, sizeof(NSCacheData));
103 msgObj = (NSMessage_consumer *) obj->data;
111 NSCacheObject * iter = list->head;
118 iter = (NSCacheObject *) iter->next;
123 (list->tail)->next = obj;
131 NSResult NSConsumerCacheDelete(NSCacheList * list, NSCacheObject * delObj) // Free?
133 NSCacheObject * beDelete;
134 if (list->head == delObj)
136 beDelete = list->head;
137 list->head = (NSCacheObject *) list->head->next;
141 NSCacheObject * pre = list->head;
142 beDelete = (NSCacheObject *) pre->next;
145 if (beDelete == delObj)
147 if (beDelete == list->tail)
154 pre->next = beDelete->next;
156 OICFree((NSCacheObject *) beDelete);
160 beDelete = (NSCacheObject *) beDelete->next;
166 NSCacheObject * NSConsumerCacheFind(NSCacheList * list, char * findId)
169 NS_LOG_V(DEBUG, "findID: %s", findId);
170 NSCacheObject * retObj = list->head;
173 NS_LOG(DEBUG, "findcache is null");
177 NSMessage_consumer * obj = (NSMessage_consumer *) retObj->data;
178 NS_LOG_V(DEBUG, "objmID: %s", obj->mId);
182 NSMessage_consumer * obj = (NSMessage_consumer *) retObj->data;
183 NS_LOG_V(DEBUG, "objmID: %s", obj->mId);
184 if (!strcmp(obj->mId, findId))
188 retObj = (NSCacheObject *) retObj->next;
194 NSResult NSConsumerCacheWrite(NSCacheList * list, NSCacheObject * writeObj)
196 NSCacheObject * beModify = NSConsumerCacheFind(list, ((NSMessage_consumer *) writeObj->data)->mId);
202 NSMessage_consumer * msgObj = (NSMessage_consumer *) beModify->data;
203 NSMessage_consumer * msgWriteObj = (NSMessage_consumer *) writeObj->data;
205 if (msgWriteObj->mId)
207 strcpy(msgObj->mId, msgWriteObj->mId);
209 if (msgWriteObj->mTitle)
211 strcpy(msgObj->mTitle, msgWriteObj->mTitle);
213 if (msgWriteObj->mContentText)
215 strcpy(msgObj->mContentText, msgWriteObj->mContentText);
218 memcpy(msgObj->addr, msgWriteObj->addr, sizeof(OCDevAddr));
219 msgObj->type = msgWriteObj->type;
224 NSCacheObject * NSConsumerCacheRead(NSCacheList * list, NSCacheObject * readObj)
226 NSCacheObject * retObj = NSConsumerCacheFind(list, ((NSMessage_consumer *) readObj->data)->mId);