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 "NSConstants.h"
22 #include "NSConsumerCommon.h"
23 #include "NSConsumerInternalTaskController.h"
24 #include "NSStructs.h"
26 #include "oic_malloc.h"
28 NSCacheList ** NSGetCacheList()
30 static NSCacheList * cache = NULL;
34 void NSSetCacheList(NSCacheList * cache)
36 *(NSGetCacheList()) = cache;
39 void NSDestroyCacheList()
42 cache = *(NSGetCacheList());
45 NSStorageDestroy(cache);
49 NSResult NSCacheUpdate(NSCacheList * cache, NSTask * task, NSConsumerMessageTypes type)
51 NSMessage_consumer * noti = (NSMessage_consumer *) task->taskData;
54 NSCacheElement * obj = (NSCacheElement *)OICMalloc(sizeof(NSCacheElement));
55 NS_VERTIFY_NOT_NULL(obj, NS_ERROR);
57 obj->data = (NSCacheData *) noti;
60 NS_LOG(DEBUG, "try to write to storage");
61 NSResult ret = NSStorageWrite(cache, obj);
62 NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(ret == NS_OK ? (void *) 1 : NULL,
63 NS_ERROR, NSRemoveMessage(noti));
65 NSRemoveMessage(noti);
71 void NSConsumerSubscriptionTaskProcessing(NSTask * task)
73 NS_VERTIFY_NOT_NULL_V(task);
76 if (!*(NSGetCacheList()))
78 NS_LOG(DEBUG, "Cache Init");
79 cache = NSStorageCreate();
80 NS_VERTIFY_NOT_NULL_V(cache);
82 cache->cacheType = NS_CONSUMER_CACHE_MESSAGE;
83 NSSetCacheList(cache);
85 cache = *(NSGetCacheList());
87 NSResult ret = NS_ERROR;
88 NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
89 switch (task->taskType)
91 case TASK_CONSUMER_RECV_MESSAGE:
93 NS_LOG(DEBUG, "Receive New Notification");
95 ret = NSCacheUpdate(cache, task, Notification);
96 NS_VERTIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
101 NS_LOG(DEBUG, "Receive Read Notification");
103 ret = NSCacheUpdate(cache, task, Read);
104 NS_VERTIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
107 case TASK_RECV_DISMISS:
109 NS_LOG(DEBUG, "Receive Dismiss Notification");
111 ret = NSCacheUpdate(cache, task, Dismiss);
112 NS_VERTIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
117 NS_LOG(ERROR, "Unknown TASK Type");