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"
25 #include "oic_malloc.h"
26 #include "oic_string.h"
28 NSCacheList * NSConsumerCacheInit()
30 NSCacheList * retNSCacheList = (NSCacheList *)OICMalloc(sizeof(NSCacheList));
36 retNSCacheList->head = NULL;
37 retNSCacheList->last = NULL;
39 return retNSCacheList;
42 NSResult NSConsumerCacheDestroy(NSCacheList * list)
44 NSCacheObject * iter = list->head;
46 NSCacheObject * next = NULL;
49 next = (NSCacheObject *)iter->next;
50 if (NS_OK != NSConsumerCacheDelete(list, iter))
57 OICFree((NSCacheList *) list);
62 NSResult NSConsumerCacheInsert(NSCacheList * list, NSCacheObject * newObj)
64 NSCacheObject * obj = (NSCacheObject *)OICMalloc(sizeof(NSCacheObject));
67 NS_CONSUMER_LOG(ERROR, "Fail to Create New Object");
71 NSMessage_consumer * msgObj = (NSMessage_consumer *) OICMalloc(sizeof(NSMessage_consumer));
72 NSMessage_consumer * newMsgObj = (NSMessage_consumer *) newObj->data;
74 NS_CONSUMER_LOG_V(DEBUG, "Title: %s", newMsgObj->mTitle);
75 NS_CONSUMER_LOG_V(DEBUG, "ID: %s", newMsgObj->mId);
76 NS_CONSUMER_LOG_V(DEBUG, "TEXT: %s", newMsgObj->mContentText);
78 msgObj->mTitle = OICStrdup(newMsgObj->mTitle);
79 msgObj->mId = OICStrdup(newMsgObj->mId);
80 msgObj->mContentText = OICStrdup(newMsgObj->mContentText);
84 NS_CONSUMER_LOG(ERROR, "Notification Attributes copy fail");
87 msgObj->addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
90 NS_CONSUMER_LOG(ERROR, "OCDevAddr allocation is failed.");
93 memcpy(msgObj->addr, newMsgObj->addr, sizeof(OCDevAddr));
94 msgObj->type = newMsgObj->type;
96 //obj->data = (NSCacheData *)OICMalloc(sizeof(NSCacheData));
97 obj->data = (NSCacheData *) msgObj;
98 //memcpy(obj->data, (NSCacheData *)msgObj, sizeof(NSCacheData));
102 msgObj = (NSMessage_consumer *) obj->data;
110 NSCacheObject * iter = list->head;
117 iter = (NSCacheObject *) iter->next;
122 (list->last)->next = obj;
130 NSResult NSConsumerCacheDelete(NSCacheList * list, NSCacheObject * delObj) // Free?
132 NSCacheObject * beDelete;
133 if (list->head == delObj)
135 beDelete = list->head;
136 list->head = (NSCacheObject *) list->head->next;
140 NSCacheObject * pre = list->head;
141 beDelete = (NSCacheObject *) pre->next;
144 if (beDelete == delObj)
146 if (beDelete == list->last)
153 pre->next = beDelete->next;
155 OICFree((NSCacheObject *) beDelete);
159 beDelete = (NSCacheObject *) beDelete->next;
165 NSCacheObject * NSConsumerCacheFind(NSCacheList * list, char * findId)
168 NS_CONSUMER_LOG_V(DEBUG, "findID: %s", findId);
169 NSCacheObject * retObj = list->head;
172 NS_CONSUMER_LOG(DEBUG, "findcache is null");
176 NSMessage_consumer * obj = (NSMessage_consumer *) retObj->data;
177 NS_CONSUMER_LOG_V(DEBUG, "objmID: %s", obj->mId);
181 NSMessage_consumer * obj = (NSMessage_consumer *) retObj->data;
182 NS_CONSUMER_LOG_V(DEBUG, "objmID: %s", obj->mId);
183 if (!strcmp(obj->mId, findId))
187 retObj = (NSCacheObject *) retObj->next;
193 NSResult NSConsumerCacheWrite(NSCacheList * list, NSCacheObject * writeObj)
195 NSCacheObject * beModify = NSConsumerCacheFind(list, ((NSMessage_consumer *) writeObj->data)->mId);
201 NSMessage_consumer * msgObj = (NSMessage_consumer *) beModify->data;
202 NSMessage_consumer * msgWriteObj = (NSMessage_consumer *) writeObj->data;
204 if (msgWriteObj->mId)
206 strcpy(msgObj->mId, msgWriteObj->mId);
208 if (msgWriteObj->mTitle)
210 strcpy(msgObj->mTitle, msgWriteObj->mTitle);
212 if (msgWriteObj->mContentText)
214 strcpy(msgObj->mContentText, msgWriteObj->mContentText);
217 memcpy(msgObj->addr, msgWriteObj->addr, sizeof(OCDevAddr));
218 msgObj->type = msgWriteObj->type;
223 NSCacheObject * NSConsumerCacheRead(NSCacheList * list, NSCacheObject * readObj)
225 NSCacheObject * retObj = NSConsumerCacheFind(list, ((NSMessage_consumer *) readObj->data)->mId);