- by adapting cache adpater, add consumer cache module which uses memory list on linux
Change-Id: I6cc344f0be36aff7ec9045b4ae200b57c8def777
Signed-off-by: YounghyunJoo <yh_.joo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/8317
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
notification_env.AppendUnique(CPPPATH = ['src/common'])
notification_env.AppendUnique(CPPPATH = ['src/provider'])
notification_env.AppendUnique(CPPPATH = ['src/provider/cache/linux'])
+notification_env.AppendUnique(CPPPATH = ['src/consumer'])
notification_env.AppendUnique(CPPPATH = ['../../resource/csdk/stack/include'])
notification_env.AppendUnique(CPPPATH = ['../../resource/csdk/connectivity/api'])
if target_os == 'linux':
notification_env.AppendUnique(CPPPATH = ['src/provider/cache/linux'])
+ notification_env.AppendUnique(CPPPATH = ['src/consumer/cache/linux'])
if target_os == 'android':
notification_env.AppendUnique(CPPPATH = ['src/provider/cache/android'])
+ notification_env.AppendUnique(CPPPATH = ['src/consumer/cache/android'])
notification_env.PrependUnique(LIBS = [
'octbstack',
if target_os == 'linux':
PROVIDER_CACHE = File('src/provider/cache/linux/NSProviderMemoryCache.c')
+ CONSUMER_CACHE = File('src/consumer/cache/linux/NSConsumerMemoryCache.c')
if target_os == 'android':
PROVIDER_CACHE = File('src/provider/cache/linux/NSProviderMemoryCache.c')
+ CONSUMER_CACHE = File('src/consumer/cache/linux/NSConsumerMemoryCache.c')
notification_provider_src = [
env.Glob('src/provider/*.c'),
env.Glob('src/common/*.c'), PROVIDER_CACHE]
notification_consumer_src = [
- env.Glob('src/common/*.c'),
- env.Glob('src/consumer/*.c')]
+ env.Glob('src/consumer/*.c'),
+ env.Glob('src/common/*.c'), CONSUMER_CACHE]
providersdk = notification_env.SharedLibrary('notification_provider', notification_provider_src)
notification_env.InstallTarget(providersdk, 'libnotification_provider')
notification_env.InstallTarget(consumersdk, 'libnotification_consumer')
notification_env.UserInstallTargetLib(consumersdk, 'libnotification_consumer')
-notification_env.UserInstallTargetHeader('include/NSProviderIngerface.h',\
+notification_env.UserInstallTargetHeader('include/NSProviderInterface.h',\
'service/notification', 'NSProviderInterface.h')
-notification_env.UserInstallTargetHeader('include/NSConsumerIngerface.h',\
+notification_env.UserInstallTargetHeader('include/NSConsumerInterface.h',\
'service/notification', 'NSConsumerInterface.h')
# Go to build Unit test
{
NS_PROVIDER_CACHE_SUBSCRIBER = 1000,
NS_PROVIDER_CACHE_MESSAGE = 1001,
+
+ NS_CONSUMER_CACHE_MESSAGE = 2001,
} NSCacheType;
typedef enum eResourceType
NS_RESOURCE_SYNC = 1001,
} NSResourceType;
-#endif /* _NS_CONSTANTS_H_ */
+#endif /* _NS_CONSTANTS_H_ */
\ No newline at end of file
+++ /dev/null
-//******************************************************************
-//
-// Copyright 2016 Samsung Electronics All Rights Reserved.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-
-#include "NSConsumerCache.h"
-#include "NSConsumerCommon.h"
-#include "NSConstants.h"
-
-#include "stdlib.h"
-#include "oic_malloc.h"
-#include "oic_string.h"
-
-NSCacheList * NSConsumerCacheInit()
-{
- NSCacheList * retNSCacheList = (NSCacheList *)OICMalloc(sizeof(NSCacheList));
- if (!retNSCacheList)
- {
- return NULL;
- }
-
- retNSCacheList->head = NULL;
- retNSCacheList->tail = NULL;
-
- return retNSCacheList;
-}
-
-NSResult NSConsumerCacheDestroy(NSCacheList * list)
-{
- NSCacheObject * iter = list->head;
-
- NSCacheObject * next = NULL;
- while (iter)
- {
- next = (NSCacheObject *)iter->next;
- if (NS_OK != NSConsumerCacheDelete(list, iter))
- {
- return NS_ERROR;
- }
- iter = next;
- }
-
- OICFree((NSCacheList *) list);
-
- return NS_OK;
-}
-
-NSResult NSConsumerCacheInsert(NSCacheList * list, NSCacheObject * newObj)
-{
- NSCacheObject * obj = (NSCacheObject *)OICMalloc(sizeof(NSCacheObject));
- if (!obj)
- {
- NS_LOG(ERROR, "Fail to Create New Object");
- return NS_ERROR;
- }
-
- NSMessage_consumer * msgObj = (NSMessage_consumer *) OICMalloc(sizeof(NSMessage_consumer));
- NSMessage_consumer * newMsgObj = (NSMessage_consumer *) newObj->data;
-
- NS_LOG_V(DEBUG, "Title: %s", newMsgObj->mTitle);
- NS_LOG_V(DEBUG, "ID: %s", newMsgObj->mId);
- NS_LOG_V(DEBUG, "TEXT: %s", newMsgObj->mContentText);
-
- msgObj->mTitle = OICStrdup(newMsgObj->mTitle);
- msgObj->mId = OICStrdup(newMsgObj->mId);
- msgObj->mContentText = OICStrdup(newMsgObj->mContentText);
-
- if (!msgObj->mId)
- {
- NS_LOG(ERROR, "Notification Attributes copy fail");
- }
-
- msgObj->addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
- if (!msgObj->addr)
- {
- NS_LOG(ERROR, "OCDevAddr allocation is failed.");
- }
-
- memcpy(msgObj->addr, newMsgObj->addr, sizeof(OCDevAddr));
- msgObj->type = newMsgObj->type;
-
- //obj->data = (NSCacheData *)OICMalloc(sizeof(NSCacheData));
- obj->data = (NSCacheData *) msgObj;
- //memcpy(obj->data, (NSCacheData *)msgObj, sizeof(NSCacheData));
- obj->next = NULL;
-
- msgObj = NULL;
- msgObj = (NSMessage_consumer *) obj->data;
-
- if (!list->head)
- {
- list->head = obj;
- return NS_OK;
- }
-
- NSCacheObject * iter = list->head;
- while (iter)
- {
- if (iter == obj)
- {
- return NS_ERROR;
- }
- iter = (NSCacheObject *) iter->next;
- }
-
- if (list->tail)
- {
- (list->tail)->next = obj;
- }
-
- list->tail = obj;
-
- return NS_OK;
-}
-
-NSResult NSConsumerCacheDelete(NSCacheList * list, NSCacheObject * delObj) // Free?
-{
- NSCacheObject * beDelete;
- if (list->head == delObj)
- {
- beDelete = list->head;
- list->head = (NSCacheObject *) list->head->next;
- return NS_OK;
- }
-
- NSCacheObject * pre = list->head;
- beDelete = (NSCacheObject *) pre->next;
- while (beDelete)
- {
- if (beDelete == delObj)
- {
- if (beDelete == list->tail)
- {
- pre->next = NULL;
- list->tail = pre;
- }
- else
- {
- pre->next = beDelete->next;
- }
- OICFree((NSCacheObject *) beDelete);
- break;
- }
- pre = beDelete;
- beDelete = (NSCacheObject *) beDelete->next;
- }
-
- return NS_OK;
-}
-
-NSCacheObject * NSConsumerCacheFind(NSCacheList * list, char * findId)
-{
-
- NS_LOG_V(DEBUG, "findID: %s", findId);
- NSCacheObject * retObj = list->head;
- if (!retObj)
- {
- NS_LOG(DEBUG, "findcache is null");
- return retObj;
- }
-
- NSMessage_consumer * obj = (NSMessage_consumer *) retObj->data;
- NS_LOG_V(DEBUG, "objmID: %s", obj->mId);
-
- while (retObj)
- {
- NSMessage_consumer * obj = (NSMessage_consumer *) retObj->data;
- NS_LOG_V(DEBUG, "objmID: %s", obj->mId);
- if (!strcmp(obj->mId, findId))
- {
- return retObj;
- }
- retObj = (NSCacheObject *) retObj->next;
- }
-
- return retObj;
-}
-
-NSResult NSConsumerCacheWrite(NSCacheList * list, NSCacheObject * writeObj)
-{
- NSCacheObject * beModify = NSConsumerCacheFind(list, ((NSMessage_consumer *) writeObj->data)->mId);
- if (!beModify)
- {
- return NS_ERROR;
- }
-
- NSMessage_consumer * msgObj = (NSMessage_consumer *) beModify->data;
- NSMessage_consumer * msgWriteObj = (NSMessage_consumer *) writeObj->data;
-
- if (msgWriteObj->mId)
- {
- strcpy(msgObj->mId, msgWriteObj->mId);
- }
- if (msgWriteObj->mTitle)
- {
- strcpy(msgObj->mTitle, msgWriteObj->mTitle);
- }
- if (msgWriteObj->mContentText)
- {
- strcpy(msgObj->mContentText, msgWriteObj->mContentText);
- }
-
- memcpy(msgObj->addr, msgWriteObj->addr, sizeof(OCDevAddr));
- msgObj->type = msgWriteObj->type;
-
- return NS_OK;
-}
-
-NSCacheObject * NSConsumerCacheRead(NSCacheList * list, NSCacheObject * readObj)
-{
- NSCacheObject * retObj = NSConsumerCacheFind(list, ((NSMessage_consumer *) readObj->data)->mId);
- if (!retObj)
- {
- return NULL;
- }
-
- return retObj;
-}
-
-
-
#include <stdio.h>
#include <stdbool.h>
-#include "NSCommon.h"
+#include "NSConsumerInterface.h"
#include "NSStructs.h"
#include "ocstack.h"
-#include "NSConsumerInterface.h"
#define NS_QOS OC_LOW_QOS
#define NS_RESOURCE_TYPE "oic.r.notification"
void NSConsumerMessageHandlerExit()
{
+ NSDestroyCacheList();
NSThreadStop(*(NSGetMsgHandleThreadHandle()));
}
#include "NSConstants.h"
#include "NSConsumerCommon.h"
-#include "NSConsumerCache.h"
#include "NSStructs.h"
#include "oic_malloc.h"
*(NSGetCacheList()) = cache;
}
+void NSDestroyCacheList()
+{
+ NSCacheList * cache;
+ cache = *(NSGetCacheList());
+ if (!cache)
+ {
+ NSCacheDestroy(cache);
+ }
+}
+
NSResult NSCacheUpdate(NSCacheList * cache, NSTask * task, NSConsumerMessageTypes type)
{
NSMessage_consumer * noti = (NSMessage_consumer *) task->taskData;
noti->type = type;
- NSCacheObject * obj = (NSCacheObject *)OICMalloc(sizeof(NSCacheObject));
+ NSCacheElement * obj = (NSCacheElement *)OICMalloc(sizeof(NSCacheElement));
if (!obj)
{
NS_LOG(ERROR, "Cache allocation is failed");
obj->data = (NSCacheData *) noti;
obj->next = NULL;
- if (NS_OK != NSConsumerCacheWrite(cache, obj))
+ if (NS_OK != NSCacheWrite(cache, obj))
{
- NS_LOG(DEBUG, "CacheUpdate - NSConsumerCacheInsert");
- if (NS_OK != NSConsumerCacheInsert(cache, (NSCacheObject *) obj))
- {
- NS_LOG(ERROR, "Cache insert fail");
- return NS_ERROR;
- }
+ NS_LOG(ERROR, "Cache write fail");
+ return NS_ERROR;
}
NSRemoveMessage(noti);
if (!*(NSGetCacheList()))
{
NS_LOG(DEBUG, "Cache Init");
- cache = NSConsumerCacheInit();
+ cache = NSCacheCreate();
+ cache->cacheType = NS_CONSUMER_CACHE_MESSAGE;
if (!cache)
{
NS_LOG(ERROR, "Cache create fail");
NSSetCacheList(cache);
}
cache = *(NSGetCacheList());
- if (!cache->head)
- {
- NS_LOG(DEBUG, "Cache Head is null 2");
- }
- else{
- NS_LOG(DEBUG, "Cache Head is not null 2");
- }
-
NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
case TASK_RECV_READ:
{
NS_LOG(DEBUG, "Receive Read Notification");
- // TODO update Cache.
+
if (NS_OK != NSCacheUpdate(cache, task, Read))
{
NS_LOG(ERROR, "Cache Update fail");
#endif // __cplusplus
#include "NSStructs.h"
-#include "NSConsumerCache.h"
+#include "NSCacheAdapter.h"
#include "NSConsumerNotification.h"
+void NSDestroyCacheList();
+
void NSConsumerSubscriptionHandleMsg(NSTask *);
#ifdef __cplusplus
--- /dev/null
+//******************************************************************
+//
+// Copyright 2016 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#include "NSConsumerMemoryCache.h"
+#include "oic_malloc.h"
+#include "oic_string.h"
+
+pthread_mutex_t * NSGetCacheMutex()
+{
+ static pthread_mutex_t NSCacheMutex;
+ return & NSCacheMutex;
+}
+
+void NSSetCacheMutex(pthread_mutex_t mutex)
+{
+ *(NSGetCacheMutex()) = mutex;
+}
+
+NSCacheList * NSCacheCreate()
+{
+ pthread_mutex_t * mutex = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
+ pthread_mutex_init(mutex, NULL);
+ NSSetCacheMutex(*mutex);
+ mutex = NSGetCacheMutex();
+
+
+ pthread_mutex_lock(mutex);
+
+ NSCacheList * newList = (NSCacheList *) OICMalloc(sizeof(NSCacheList));
+ if (!newList)
+ {
+ pthread_mutex_unlock(mutex);
+ NS_LOG (ERROR, "Failed to Create Cache");
+ return NULL;
+ }
+
+ newList->head = newList->tail = NULL;
+
+ pthread_mutex_unlock(mutex);
+
+ return newList;
+}
+
+NSCacheElement * NSCacheRead(NSCacheList * list, const char * findId)
+{
+ pthread_mutex_t * mutex = NSGetCacheMutex();
+
+ pthread_mutex_lock(mutex);
+
+ NSCacheElement * iter = list->head;
+ NSCacheElement * next = NULL;
+ NSCacheType type = list->cacheType;
+
+ while (iter)
+ {
+ next = iter->next;
+
+ pthread_mutex_unlock(mutex);
+
+ if (NSConsumerCompareIdCacheData(type, iter->data, findId))
+ {
+ pthread_mutex_unlock(mutex);
+
+ return iter;
+ }
+
+ iter = next;
+ }
+
+ pthread_mutex_unlock(mutex);
+ NS_LOG (DEBUG, "No Cache Element");
+ return NULL;
+}
+
+NSResult NSCacheWrite(NSCacheList * list, NSCacheElement * newObj)
+{
+ pthread_mutex_t * mutex = NSGetCacheMutex();
+
+ pthread_mutex_lock(mutex);
+
+ NSCacheType type = list->cacheType;
+
+ if (!newObj)
+ {
+ pthread_mutex_unlock(mutex);
+ NS_LOG (ERROR, "Failed to Write Cache");
+ return NS_ERROR;
+ }
+
+ if (type == NS_CONSUMER_CACHE_MESSAGE)
+ {
+ pthread_mutex_unlock(mutex);
+
+ return NSConsumerCacheWriteMessage(list, newObj);
+ }
+
+ NS_LOG (ERROR, "Not Supported Type");
+ pthread_mutex_unlock(mutex);
+
+ return NS_ERROR;
+}
+
+NSResult NSCacheDelete(NSCacheList * list, const char * delId)
+{
+ pthread_mutex_t * mutex = NSGetCacheMutex();
+
+ pthread_mutex_lock(mutex);
+
+ if (!delId)
+ {
+ pthread_mutex_unlock(mutex);
+ NS_LOG (ERROR, "Failed to Delete Cache");
+ return NS_ERROR;
+ }
+
+ NSCacheElement * prev = list->head;
+ NSCacheElement * del = list->head;
+
+ NSCacheType type = list->cacheType;
+
+ if (NSConsumerCompareIdCacheData(type, del->data, delId))
+ {
+ if (del == list->head)
+ {
+ if (del == list->tail)
+ list->tail = del->next;
+ list->head = del->next;
+
+ NSRemoveMessage((NSMessage_consumer *) del->data);
+ OICFree(del);
+ pthread_mutex_unlock(mutex);
+
+ return NS_OK;
+ }
+ }
+
+ del = del->next;
+ while (del)
+ {
+ if (NSConsumerCompareIdCacheData(type, del->data, delId))
+ {
+ if (del == list->tail)
+ list->tail = prev;
+
+ prev->next = del->next;
+ NSRemoveMessage((NSMessage_consumer *) del->data);
+ OICFree(del);
+ pthread_mutex_unlock(mutex);
+
+ return NS_OK;
+ }
+
+ prev = del;
+ del = del->next;
+ }
+ pthread_mutex_unlock(mutex);
+ return NS_OK;
+}
+
+NSResult NSConsumerCacheWriteMessage(NSCacheList * list, NSCacheElement * newObj)
+{
+ pthread_mutex_t * mutex = NSGetCacheMutex();
+
+ pthread_mutex_lock(mutex);
+
+ if (!newObj)
+ {
+ pthread_mutex_unlock(mutex);
+ NS_LOG (ERROR, "Failed to Write Message Cache");
+ return NS_ERROR;
+ }
+
+ NSMessage_consumer * newMsgObj = (NSMessage_consumer *) newObj->data;
+
+ pthread_mutex_unlock(mutex);
+ NSCacheElement * it = NSCacheRead(list, newMsgObj->mId);
+ pthread_mutex_lock(mutex);
+
+ if (it)
+ {
+ NSMessage_consumer * msgObj = (NSMessage_consumer *) it->data;
+ it->data = (void *) NSCopyMessage(newMsgObj);
+ if (!it->data)
+ {
+ NS_LOG (ERROR, "Failed to CopyMessage");
+ it->data = (void *) msgObj;
+ pthread_mutex_unlock(mutex);
+
+ return NS_ERROR;
+ }
+ NSRemoveMessage(msgObj);
+ pthread_mutex_unlock(mutex);
+
+ return NS_OK;
+ }
+
+ NSCacheElement * obj = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
+ if (!obj)
+ {
+ NS_LOG(ERROR, "Fail to Create New Object");
+ pthread_mutex_unlock(mutex);
+
+ return NS_ERROR;
+ }
+ obj->data = (void *) NSCopyMessage(newMsgObj);
+ if (!obj->data)
+ {
+ NS_LOG (ERROR, "Failed to CopyMessage");
+ pthread_mutex_unlock(mutex);
+
+ return NS_ERROR;
+ }
+ obj->next = NULL;
+
+ if (!list->head)
+ {
+ list->head = obj;
+ list->tail = obj;
+ pthread_mutex_unlock(mutex);
+
+ return NS_OK;
+ }
+
+ (list->tail)->next = obj;
+ list->tail = obj;
+ pthread_mutex_unlock(mutex);
+
+ return NS_OK;
+}
+
+NSResult NSCacheDestroy(NSCacheList * list)
+{
+ pthread_mutex_t * mutex = NSGetCacheMutex();
+
+ pthread_mutex_lock(mutex);
+
+ NSCacheElement * iter = list->head;
+ NSCacheElement * next = NULL;
+
+ NSCacheType type = list->cacheType;
+
+ if (type == NS_CONSUMER_CACHE_MESSAGE)
+ {
+
+ while (iter)
+ {
+ next = (NSCacheElement *) iter->next;
+
+ NSRemoveMessage((NSMessage_consumer *) iter->data);
+ OICFree(iter);
+
+ iter = next;
+ }
+
+ OICFree(list);
+ }
+
+ pthread_mutex_unlock(mutex);
+
+ return NS_OK;
+}
+
+bool NSConsumerCompareIdCacheData(NSCacheType type, void * data, const char * id)
+{
+ if (data == NULL)
+ {
+ return false;
+ }
+
+ if (type == NS_CONSUMER_CACHE_MESSAGE)
+ {
+ NSMessage_consumer * msg = (NSMessage_consumer *) data;
+
+ if (!strcmp(msg->mId, id))
+ {
+ return true;
+ }
+
+ return false;
+ }
+
+ return false;
+}
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-#ifndef _NS_CONSUMER_CACHE_H_
-#define _NS_CONSUMER_CACHE_H_
+#ifndef _NS_CONSUMER_MEMORY_CACHE_H_
+#define _NS_CONSUMER_MEMORY_CACHE_H_
#ifdef __cplusplus
extern "C" {
#include <stdlib.h>
#include <stdbool.h>
-
-#include "NSCommon.h"
+#include <pthread.h>
+#include "NSCacheAdapter.h"
#include "NSConsumerCommon.h"
-typedef void * NSCacheData;
-
-typedef struct _NSCacheObject
-{
- NSCacheData * data;
- struct _NSCacheObject * next;
-} NSCacheObject;
-
-NSCacheList * NSConsumerCacheInit();
-NSResult NSConsumerCacheDestroy(NSCacheList * list);
-
-NSResult NSConsumerCacheInsert(NSCacheList * list, NSCacheObject * newObj);
-NSResult NSConsumerCacheDelete(NSCacheList * list, NSCacheObject * delObj);
-
-NSCacheObject * NSConsumerCacheFind(NSCacheList * list, char * findId);
-
-NSResult NSConsumerCacheWrite(NSCacheList * list, NSCacheObject * writeObj);
-NSCacheObject * NSConsumerCacheRead(NSCacheList * list, NSCacheObject * readObj);
-
+bool NSConsumerCompareIdCacheData(NSCacheType type, void * data, const char * id);
+NSResult NSConsumerCacheWriteMessage(NSCacheList * list, NSCacheElement * newObj);
#ifdef __cplusplus
}
#endif // __cplusplus
-#endif // _NS_CONSUMER_CACHE_H_
+#endif // _NS_CONSUMER_MEMORY_CACHE_H_