added cacheadapter interface and memorycache of notification.
[platform/upstream/iotivity.git] / service / notification / src / consumer / NSConsumerCache.c
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "NSConsumerCache.h"
22 #include "NSConsumerCommon.h"
23 #include "NSConstants.h"
24
25 #include "stdlib.h"
26 #include "oic_malloc.h"
27 #include "oic_string.h"
28
29 NSCacheList * NSConsumerCacheInit()
30 {
31     NSCacheList * retNSCacheList = (NSCacheList *)OICMalloc(sizeof(NSCacheList));
32     if (!retNSCacheList)
33     {
34         return NULL;
35     }
36
37     retNSCacheList->head = NULL;
38     retNSCacheList->tail = NULL;
39
40     return retNSCacheList;
41 }
42
43 NSResult NSConsumerCacheDestroy(NSCacheList * list)
44 {
45     NSCacheObject * iter = list->head;
46
47     NSCacheObject * next = NULL;
48     while (iter)
49     {
50         next = (NSCacheObject *)iter->next;
51         if (NS_OK != NSConsumerCacheDelete(list, iter))
52         {
53             return NS_ERROR;
54         }
55         iter = next;
56     }
57
58     OICFree((NSCacheList *) list);
59
60     return NS_OK;
61 }
62
63 NSResult NSConsumerCacheInsert(NSCacheList * list, NSCacheObject * newObj)
64 {
65     NSCacheObject * obj = (NSCacheObject *)OICMalloc(sizeof(NSCacheObject));
66     if (!obj)
67     {
68         NS_LOG(ERROR, "Fail to Create New Object");
69         return NS_ERROR;
70     }
71
72     NSMessage_consumer * msgObj = (NSMessage_consumer *) OICMalloc(sizeof(NSMessage_consumer));
73     NSMessage_consumer * newMsgObj = (NSMessage_consumer *) newObj->data;
74
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);
78
79     msgObj->mTitle = OICStrdup(newMsgObj->mTitle);
80     msgObj->mId = OICStrdup(newMsgObj->mId);
81     msgObj->mContentText = OICStrdup(newMsgObj->mContentText);
82
83     if (!msgObj->mId)
84     {
85         NS_LOG(ERROR, "Notification Attributes copy fail");
86     }
87
88     msgObj->addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
89     if (!msgObj->addr)
90     {
91         NS_LOG(ERROR, "OCDevAddr allocation is failed.");
92     }
93
94     memcpy(msgObj->addr, newMsgObj->addr, sizeof(OCDevAddr));
95     msgObj->type = newMsgObj->type;
96
97     //obj->data = (NSCacheData *)OICMalloc(sizeof(NSCacheData));
98     obj->data = (NSCacheData *) msgObj;
99     //memcpy(obj->data, (NSCacheData *)msgObj, sizeof(NSCacheData));
100     obj->next = NULL;
101
102     msgObj = NULL;
103     msgObj = (NSMessage_consumer *) obj->data;
104
105     if (!list->head)
106     {
107         list->head = obj;
108         return NS_OK;
109     }
110
111     NSCacheObject * iter = list->head;
112     while (iter)
113     {
114         if (iter == obj)
115         {
116             return NS_ERROR;
117         }
118         iter = (NSCacheObject *) iter->next;
119     }
120
121     if (list->tail)
122     {
123         (list->tail)->next = obj;
124     }
125
126     list->tail = obj;
127
128     return NS_OK;
129 }
130
131 NSResult NSConsumerCacheDelete(NSCacheList * list, NSCacheObject * delObj) // Free?
132 {
133     NSCacheObject * beDelete;
134     if (list->head == delObj)
135     {
136         beDelete = list->head;
137         list->head = (NSCacheObject *) list->head->next;
138         return NS_OK;
139     }
140
141     NSCacheObject * pre = list->head;
142     beDelete = (NSCacheObject *) pre->next;
143     while (beDelete)
144     {
145         if (beDelete == delObj)
146         {
147             if (beDelete == list->tail)
148             {
149                 pre->next = NULL;
150                 list->tail = pre;
151             }
152             else
153             {
154                 pre->next = beDelete->next;
155             }
156             OICFree((NSCacheObject *) beDelete);
157             break;
158         }
159         pre = beDelete;
160         beDelete = (NSCacheObject *) beDelete->next;
161     }
162
163     return NS_OK;
164 }
165
166 NSCacheObject * NSConsumerCacheFind(NSCacheList * list, char * findId)
167 {
168
169     NS_LOG_V(DEBUG, "findID: %s", findId);
170     NSCacheObject * retObj = list->head;
171     if (!retObj)
172     {
173         NS_LOG(DEBUG, "findcache is null");
174         return retObj;
175     }
176
177     NSMessage_consumer * obj = (NSMessage_consumer *) retObj->data;
178     NS_LOG_V(DEBUG, "objmID: %s", obj->mId);
179
180     while (retObj)
181     {
182         NSMessage_consumer * obj = (NSMessage_consumer *) retObj->data;
183         NS_LOG_V(DEBUG, "objmID: %s", obj->mId);
184         if (!strcmp(obj->mId, findId))
185         {
186             return retObj;
187         }
188         retObj = (NSCacheObject *) retObj->next;
189     }
190
191     return retObj;
192 }
193
194 NSResult NSConsumerCacheWrite(NSCacheList * list, NSCacheObject * writeObj)
195 {
196     NSCacheObject * beModify = NSConsumerCacheFind(list, ((NSMessage_consumer *) writeObj->data)->mId);
197     if (!beModify)
198     {
199         return NS_ERROR;
200     }
201
202     NSMessage_consumer * msgObj = (NSMessage_consumer *) beModify->data;
203     NSMessage_consumer * msgWriteObj = (NSMessage_consumer *) writeObj->data;
204
205     if (msgWriteObj->mId)
206     {
207         strcpy(msgObj->mId, msgWriteObj->mId);
208     }
209     if (msgWriteObj->mTitle)
210     {
211         strcpy(msgObj->mTitle, msgWriteObj->mTitle);
212     }
213     if (msgWriteObj->mContentText)
214     {
215         strcpy(msgObj->mContentText, msgWriteObj->mContentText);
216     }
217
218     memcpy(msgObj->addr, msgWriteObj->addr, sizeof(OCDevAddr));
219     msgObj->type = msgWriteObj->type;
220
221     return NS_OK;
222 }
223
224 NSCacheObject * NSConsumerCacheRead(NSCacheList * list, NSCacheObject * readObj)
225 {
226     NSCacheObject * retObj = NSConsumerCacheFind(list, ((NSMessage_consumer *) readObj->data)->mId);
227     if (!retObj)
228     {
229         return NULL;
230     }
231
232     return retObj;
233 }
234
235
236