From 0de3ef9fc3c82a2576ef76e3106fb50359012011 Mon Sep 17 00:00:00 2001 From: "jaesick.shin" Date: Tue, 17 May 2016 18:03:56 +0900 Subject: [PATCH] added cacheadapter interface and memorycache of notification. this patch included cacheadapter and using the memorycache. previosuly, we used the only observeid(unique id). currently, we use the ip in OCDevaddr Struct. Change-Id: Ie1b3ff6a88b00f83772f2cddd08a186a2122d69f Signed-off-by: jaesick.shin Reviewed-on: https://gerrit.iotivity.org/gerrit/8191 Tested-by: jenkins-iotivity Reviewed-by: Uze Choi --- service/SConscript | 4 +- service/notification/SConscript | 19 +- .../examples/linux/notificationconsumer.c | 4 +- .../examples/linux/notificationprovider.c | 22 +- service/notification/include/NSCommon.h | 58 +++- service/notification/include/NSConsumerInterface.h | 74 ++++- service/notification/include/NSProviderInterface.h | 64 +++- service/notification/src/common/NSCacheAdapter.h | 38 +++ service/notification/src/common/NSConstants.h | 35 ++ service/notification/src/common/NSStructs.h | 32 ++ service/notification/src/common/NSUtil.c | 53 +++ service/notification/src/common/NSUtil.h | 34 ++ .../notification/src/consumer/NSConsumerCache.c | 33 +- .../notification/src/consumer/NSConsumerCache.h | 6 - .../notification/src/consumer/NSConsumerCommon.c | 18 +- .../notification/src/consumer/NSConsumerCommon.h | 25 +- .../src/consumer/NSConsumerDiscovery.c | 49 +-- .../src/consumer/NSConsumerInterface.c | 42 ++- .../notification/src/consumer/NSConsumerListener.c | 29 +- .../src/consumer/NSConsumerMessageHandler.c | 34 +- .../src/consumer/NSConsumerNotification.c | 67 ++-- .../notification/src/consumer/NSConsumerQueue.c | 3 +- .../src/consumer/NSConsumerSubsription.c | 33 +- .../notification/src/consumer/NSConsumerSystem.c | 2 + service/notification/src/consumer/NSThread.c | 13 +- .../src/provider/NSProviderCacheAdapter.c | 105 ------ .../src/provider/NSProviderDiscovery.c | 3 +- .../src/provider/NSProviderDiscovery.h | 2 +- .../src/provider/NSProviderInterface.c | 65 ++-- .../notification/src/provider/NSProviderListener.c | 8 +- .../notification/src/provider/NSProviderListener.h | 1 + .../src/provider/NSProviderNotification.c | 90 +++-- .../src/provider/NSProviderNotification.h | 14 +- .../NSProviderScheduler.c} | 2 +- .../NSProviderScheduler.h} | 7 +- .../src/provider/NSProviderSubscription.c | 140 ++++++-- .../src/provider/NSProviderSubscription.h | 9 +- .../provider/cache/linux/NSProviderMemoryCache.c | 361 +++++++++++++++++++++ .../linux/NSProviderMemoryCache.h} | 27 +- 39 files changed, 1173 insertions(+), 452 deletions(-) create mode 100755 service/notification/src/common/NSCacheAdapter.h create mode 100755 service/notification/src/common/NSUtil.c create mode 100755 service/notification/src/common/NSUtil.h delete mode 100644 service/notification/src/provider/NSProviderCacheAdapter.c rename service/notification/src/{common/NSQueueScheduler.c => provider/NSProviderScheduler.c} (95%) mode change 100644 => 100755 rename service/notification/src/{common/NSQueueScheduler.h => provider/NSProviderScheduler.h} (90%) mode change 100644 => 100755 create mode 100755 service/notification/src/provider/cache/linux/NSProviderMemoryCache.c rename service/notification/src/provider/{NSProviderCacheAdapter.h => cache/linux/NSProviderMemoryCache.h} (68%) mode change 100644 => 100755 diff --git a/service/SConscript b/service/SConscript index 2e8d1f5..bbd66d4 100755 --- a/service/SConscript +++ b/service/SConscript @@ -43,8 +43,8 @@ if target_os not in ['arduino','darwin', 'ios']: if target_os in ['linux']: SConscript('scene-manager/SConscript') - #if target_os in ['linux']: - #SConscript('notification/SConscript') + if target_os in ['linux']: + SConscript('notification/SConscript') # Build simulator module if target_os in ['linux'] and env.get('SIMULATOR', False): diff --git a/service/notification/SConscript b/service/notification/SConscript index 067bbe2..b1e51df 100644 --- a/service/notification/SConscript +++ b/service/notification/SConscript @@ -48,6 +48,12 @@ notification_env.AppendUnique(CPPPATH = ['src/provider']) 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']) + +if target_os == 'android': + notification_env.AppendUnique(CPPPATH = ['src/provider/cache/android']) + notification_env.PrependUnique(LIBS = [ 'octbstack', 'oc_logger', @@ -56,7 +62,7 @@ notification_env.PrependUnique(LIBS = [ ]) if target_os not in ['windows', 'winrt']: - notification_env.AppendUnique(CXXFLAGS = ['-O2', '-g', '-Wall', '-fmessage-length=0', '-std=c++0x']) + notification_env.AppendUnique(CXXFLAGS = ['-O2', '-g', '-Wall', '-fmessage-length=0']) if target_os not in ['darwin', 'ios', 'windows', 'winrt']: notification_env.AppendUnique(LINKFLAGS = ['-Wl,--no-undefined']) @@ -76,11 +82,18 @@ if target_os == 'android': ###################################################################### # Source files and Targets ###################################################################### + +if target_os == 'linux': + PROVIDER_CACHE = File('src/provider/cache/linux/NSProviderMemoryCache.c') + +if target_os == 'android': + PROVIDER_CACHE = File('src/provider/cache/linux/NSProviderMemoryCache.c') + notification_provider_src = [ env.Glob('src/provider/*.c'), - env.Glob('src/common/*.c')] + env.Glob('src/common/*.c'), PROVIDER_CACHE] notification_consumer_src = [ - #env.Glob('src/common/*.c'), + env.Glob('src/common/*.c'), env.Glob('src/consumer/*.c')] providersdk = notification_env.SharedLibrary('notification_provider', notification_provider_src) diff --git a/service/notification/examples/linux/notificationconsumer.c b/service/notification/examples/linux/notificationconsumer.c index 701786a..85810bd 100644 --- a/service/notification/examples/linux/notificationconsumer.c +++ b/service/notification/examples/linux/notificationconsumer.c @@ -13,13 +13,13 @@ void onNotificationPosted(NSProvider * provider, NSMessage * notification) printf("title : %s\n", notification->mTitle); printf("content : %s\n", notification->mContentText); // NSDropNSObject(notification); - NSRead(notification); + NSConsumerReadCheck(notification); } void onDiscoverNotification(NSProvider * provider) { printf("notification resource discovered\n"); - printf("subscribe result %d\n", NSSubscribeProvider(provider)); + printf("subscribe result %d\n", NSSubscribe(provider)); printf("startSubscribing\n"); } diff --git a/service/notification/examples/linux/notificationprovider.c b/service/notification/examples/linux/notificationprovider.c index e4422d3..c614294 100644 --- a/service/notification/examples/linux/notificationprovider.c +++ b/service/notification/examples/linux/notificationprovider.c @@ -20,7 +20,7 @@ int id; void OCProcessThread(void * ptr) { - (void*) ptr; + (void) ptr; while (!isExit) { if (OCProcess() != OC_STACK_OK) @@ -36,17 +36,16 @@ void subscribeRequestCallback(NSConsumer *consumer) OIC_LOG(INFO, TAG, "consumer requested to subscribe"); char *cid = consumer->mId; - printf("Consumer ID: %s\n", cid); + printf("NS_ Consumer ID: %s\n", cid); NSAccept(consumer, true); } -void syncCallback(NSProvider *provider, NSSync *sync) +void syncCallback(NSSync *sync) { OIC_LOG(INFO, TAG, "sync requested"); - printf("Sync State: %d\n", sync->mState); - + printf("NS_ Sync State: %d\n", sync->mState); } int main() @@ -80,8 +79,8 @@ int main() printf("4. NSRead \n"); //printf("5. NSAccept \n"); printf("6. NSGetConsumerList \n"); - printf("7. startPresence \n"); - printf("8. stopPresence \n"); + //printf("7. startPresence \n"); + //printf("8. stopPresence \n"); printf("0. Exit() \n"); printf("input : "); @@ -120,6 +119,7 @@ int main() printf("app - mTitle : %s \n", title); printf("app - mContentText : %s \n", body); + NSMessage * msg = (NSMessage *)malloc(sizeof(NSMessage)); sprintf(charID, "%d", id); @@ -175,12 +175,12 @@ int main() */ break; case 7: - OIC_LOG(INFO, TAG, "NSStartPresence"); - NSTestStartPresence(); + OIC_LOG(INFO, TAG, "NSStartPresence - not working"); + //NSTestStartPresence(); break; case 8: - OIC_LOG(INFO, TAG, "NSStopPresence"); - NSTestStopPresence(); + OIC_LOG(INFO, TAG, "NSStopPresence- not working"); + //NSTestStopPresence(); break; default: OIC_LOG(INFO, TAG, "Under Construction"); diff --git a/service/notification/include/NSCommon.h b/service/notification/include/NSCommon.h index 3348303..3d299be 100644 --- a/service/notification/include/NSCommon.h +++ b/service/notification/include/NSCommon.h @@ -18,9 +18,17 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +/** + * @file + * + * This file provides APIs of Notification Service for common functions. + */ + #ifndef _NS_COMMON_H_ #define _NS_COMMON_H_ +#include + #define NS_ATTRIBUTE_POLICY "ACCEPTER" #define NS_ATTRIBUTE_MESSAGE "MESSAGE_URI" #define NS_ATTRIBUTE_SYNC "SYNC_URI" @@ -31,6 +39,9 @@ #define NS_ATTRIBUTE_STATE "STATE" #define NS_ATTRIBUTE_DEVICE "DEVICE" +/** + * Result code of notification service + */ typedef enum eResult { NS_OK = 100, @@ -42,6 +53,9 @@ typedef enum eResult } NSResult; +/** + * Access policy exchanged between provider and consumer during subscription process + */ typedef enum eAccessPolicy { NS_ACCEPTER_PROVIDER = 0, @@ -49,39 +63,55 @@ typedef enum eAccessPolicy } NSAccessPolicy; +/** + * Notification message status to synchronize + */ +typedef enum +{ + Notification_Read, + Notification_Dismiss, +} NSSyncTypes; + +/** + * Device information + */ typedef struct { char * mId; void * mUserData; } NSDevice; +/** + * Consumer information is same to device information + */ typedef NSDevice NSConsumer; +/** + * Provider information + */ typedef struct { char * mId; char * mUserData; char * messageUri; char * syncUri; + OCDoHandle messageHandle; + OCDoHandle syncHandle; } NSProvider; +/** + * Notification Message + */ typedef struct { - // Mandatory char * mId; char * mTitle; - - //Optional char * mContentText; - } NSMessage; -typedef enum -{ - Notification_Read, - Notification_Dismiss, -} NSSyncTypes; - +/** + * Synchronization information of the notification message + */ typedef struct { // Mandatory @@ -90,15 +120,7 @@ typedef struct //Optional NSDevice * mDevice; - } NSSync; -typedef void (* NSSubscribeRequestCallback)(NSConsumer *); -typedef void (* NSSyncCallback)(NSProvider *, NSSync *); - -typedef void (* NSProviderDiscoveredCallback)(NSProvider *); -// NSMessage should deleted as user. -typedef void (* NSNotificationReceivedCallback)(NSProvider *, NSMessage *); - #endif /* _NS_COMMON_H_ */ diff --git a/service/notification/include/NSConsumerInterface.h b/service/notification/include/NSConsumerInterface.h index 108f797..9461cc5 100644 --- a/service/notification/include/NSConsumerInterface.h +++ b/service/notification/include/NSConsumerInterface.h @@ -18,33 +18,87 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +/** + * @file + * + * This file provides APIs of Notification Service for Consumer. + */ + #ifndef _NS_CONSUMER_INTERFACE_H_ #define _NS_CONSUMER_INTERFACE_H_ #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif // __cplusplus #include "NSCommon.h" -NSResult NSStartConsumer( - NSProviderDiscoveredCallback discoverCb, - NSNotificationReceivedCallback postCb, - NSSyncCallback syncCb); +/** + * Provider and consumer use this callback function to receive the status of the message + * synchronization + * @param[in] provider Provider who requests synchronization with the status + * @param[in] sync Synchronization information of the notification message + */ +typedef void (*NSSyncCallback)(NSProvider *, NSSync *); + +/** + * Consumer uses this callback function to receive the discovered providers + * @param[in] provider Provider who has the notification resource + */ +typedef void (*NSProviderDiscoveredCallback)(NSProvider *); + +/** + * Consumer use this callback function to receive notification message from provider + * synchronization + * @param[in] provider Provider who sends notification message + * @param[in] message Notification message + */ +typedef void (*NSNotificationReceivedCallback)(NSProvider *, NSMessage *); +/** + * Initialize notification service for consumer + * @param[in] providerDiscoveredCallback Callback function to discover notification providers + * @param[in] notificationReceivedCallback Callback function to receive notification messages + * @param[in] syncCallback Callback function to receive synchronization status of notification + * @return ::NS_OK or result code of NSResult + */ +NSResult NSStartConsumer(NSProviderDiscoveredCallback discoverCb, + NSNotificationReceivedCallback postCb, NSSyncCallback syncCb); + +/** + * Terminate notification service for consumer + * @return ::NS_OK or result code of NSResult + */ NSResult NSStopConsumer(); -NSResult NSRead(NSMessage *); +/** + * Request discovery manually + * @return ::NS_OK or result code of NSResult + */ +NSResult NSRescanProvider(); -NSResult NSDismiss(NSMessage *); +/** + * Request to subscribe notification message resource of provider + * @param[in] provider Provider who send the notification message + * @return ::NS_OK or result code of NSResult + */ +NSResult NSSubscribe(NSProvider *provider); -NSResult NSSubscribeProvider(NSProvider *); +/** + * Request to unsubscribe in order not to receive notification message from provider + * @param[in] provider Provider who send the notification message + * @return ::NS_OK or result code of NSResult + */ +NSResult NSUnsubscribe(NSProvider *provider); -NSResult NSUnsubscribeProvider(NSProvider *); +NSResult NSConsumerReadCheck(NSMessage *); -NSResult NSRescanProvider(); +NSResult NSConsumerDismissCheck(NSMessage *); +//** Remove below functions **// NSResult NSDropNSObject(NSMessage *); +//** end of functions to be removed **// #ifdef __cplusplus } diff --git a/service/notification/include/NSProviderInterface.h b/service/notification/include/NSProviderInterface.h index 606ff5c..f5c5578 100644 --- a/service/notification/include/NSProviderInterface.h +++ b/service/notification/include/NSProviderInterface.h @@ -18,33 +18,83 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +/** + * @file + * + * This file provides APIs of Notification Service for Provider. + */ + #ifndef _NS_PROVIDER_INTERFACE_H_ #define _NS_PROVIDER_INTERFACE_H_ #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif // __cplusplus #include "NSCommon.h" #include #include +/** + * Provider uses this callback function to receive subscription request of consumer + * @param[in] consumer Consumer who subscribes the resource + */ +typedef void (*NSSubscribeRequestCallback)(NSConsumer *); + +/** + * Provider use this callback function to receive the status of the message + * synchronization + * @param[in] sync Synchronization information of the notification message + */ +typedef void (*NSSyncCallback)(NSSync *); + +/** + * Initialize notification service for provider + * @param[in] policy Accepter + * @param[in] subscribeRequestCallback Callback function to register for receiving + * subscription request from consumer + * @param[in] syncCallback Callback function to register for receiving sync data + * @return ::NS_OK or result code of NSResult + */ NSResult NSStartProvider(NSAccessPolicy policy, NSSubscribeRequestCallback subscribeRequestCb, NSSyncCallback syncCb); +/** + * Terminate notification service for provider + * @return ::NS_OK or result code of NSResult + */ NSResult NSStopProvider(); +/** + * Send notification message to all subscribers + * @param[in] message Notification message including id, title, contentText + * @return ::NS_OK or result code of NSResult + */ NSResult NSSendNotification(NSMessage *msg); -NSResult NSReadCheck(NSMessage *msg); - +/** + * Send acceptance to consumer who subscribes the resource of notification message + * @param[in] consumer Consumer who subscribes the resource + * @param[in] accepted the result of acceptance; Allow or Deny + * @return ::NS_OK or result code of NSResult + */ NSResult NSAccept(NSConsumer *consumer, bool accepted); -NSResult NSGetConsumerList(uint8_t *list, uint32_t size); - -NSResult NSTestStartPresence(); +/** + * Get consumer list that is stored in the cache of notification service + * @param[in] list Consumer list + * @param[in] size the number of consumers stored in the cache + * @return ::NS_OK or result code of NSResult + */ +// NSResult NSGetConsumerList(uint8_t *list, uint32_t size); -NSResult NSTestStopPresence(); +/** + * Send read-check to provider in order to synchronize notification status with other consumers + * @param[in] message Notification message to synchronize the status + * @return ::NS_OK or result code of NSResult + */ +NSResult NSProviderReadCheck(NSMessage *); #ifdef __cplusplus } diff --git a/service/notification/src/common/NSCacheAdapter.h b/service/notification/src/common/NSCacheAdapter.h new file mode 100755 index 0000000..9f732f5 --- /dev/null +++ b/service/notification/src/common/NSCacheAdapter.h @@ -0,0 +1,38 @@ +//****************************************************************** +// +// 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef _NS_CACHEADAPTER__H_ +#define _NS_CACHEADAPTER__H_ + +#include "logger.h" +#include +#include +#include "ocstack.h" +#include "ocpayload.h" +#include "NSStructs.h" +#include "NSConstants.h" + +NSCacheList * NSCacheCreate(); +NSCacheElement * NSCacheRead(NSCacheList * list, char * findId); +NSResult NSCacheWrite(NSCacheList * list, NSCacheElement * newObj); +NSResult NSCacheDelete(NSCacheList * list, char * delId); +NSResult NSCacheDestroy(NSCacheList * list); + +#endif /* _NS_CACHEADAPTER__H_ */ diff --git a/service/notification/src/common/NSConstants.h b/service/notification/src/common/NSConstants.h index bbf4aef..83a4f6c 100644 --- a/service/notification/src/common/NSConstants.h +++ b/service/notification/src/common/NSConstants.h @@ -21,6 +21,29 @@ #ifndef _NS_CONSTANTS_H_ #define _NS_CONSTANTS_H_ +#define __NS_FILE__ ( strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__ ) + +#ifdef TB_LOG +#include "logger.h" +#define NS_LOG_V(level, format, ...) (OIC_LOG_V((level), __NS_FILE__, (format), __VA_ARGS__)) +#define NS_LOG(level, msg) (OIC_LOG((level), __NS_FILE__, (msg))) +#else +#include "logger.h" +#define NS_CONVERT_LEVEL(level) ( \ + ((level) == 0) ? "DEBUG" : \ + ((level) == 1) ? "INFO" : \ + ((level) == 2) ? "WARNING" : \ + ((level) == 3) ? "ERROR" : "FATAL") +#define NS_LOG_V(level, format, ...) { \ + printf("%s: %s ", NS_CONVERT_LEVEL(level), __NS_FILE__); \ + printf((format), __VA_ARGS__); \ + printf("\n"); } +#define NS_LOG(level, msg) { \ + printf("%s: %s ", NS_CONVERT_LEVEL(level), __NS_FILE__); \ + printf((msg)); \ + printf("\n"); } +#endif + #define NS_TAG "IOT_NOTI" // SCHEDULE // @@ -97,4 +120,16 @@ typedef enum eCache } NSCache; +typedef enum eCacheType +{ + NS_PROVIDER_CACHE_SUBSCRIBER = 1000, + NS_PROVIDER_CACHE_MESSAGE = 1001, +} NSCacheType; + +typedef enum eResourceType +{ + NS_RESOURCE_MESSAGE = 1000, + NS_RESOURCE_SYNC = 1001, +} NSResourceType; + #endif /* _NS_CONSTANTS_H_ */ diff --git a/service/notification/src/common/NSStructs.h b/service/notification/src/common/NSStructs.h index 8305aaf..f8c6ca1 100644 --- a/service/notification/src/common/NSStructs.h +++ b/service/notification/src/common/NSStructs.h @@ -23,6 +23,8 @@ #include #include +#include +#include #include "NSCommon.h" #include "NSConstants.h" @@ -33,4 +35,34 @@ typedef struct _nsTask struct _nsTask* nextTask; } NSTask; +typedef void * NSCacheData; + +typedef struct _NSCacheElement +{ + NSCacheData * data; + struct _NSCacheElement * next; +} NSCacheElement; + +typedef struct +{ + NSCacheType cacheType; + NSCacheElement * head; + NSCacheElement * tail; +} NSCacheList; + +typedef struct +{ + char * id; // ip + int syncObId; + int messageObId; + bool isWhite; +}NSCacheSubData; + +typedef struct +{ + char * id; // ip ? ? ? + int messageType; // noti = 1, read = 2, dismiss = 3 + NSMessage * nsMessage; +}NSCacheMsgData; + #endif /* _NS_STRUCTS_H_ */ diff --git a/service/notification/src/common/NSUtil.c b/service/notification/src/common/NSUtil.c new file mode 100755 index 0000000..0ddeaa9 --- /dev/null +++ b/service/notification/src/common/NSUtil.c @@ -0,0 +1,53 @@ +//****************************************************************** +// +// 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 "NSUtil.h" + +NSResult NSFreeMessage(NSMessage * obj) +{ + if (!obj) + { + return NS_ERROR; + } + + if (obj->mId) + { + OICFree(obj->mId); + obj->mId = NULL; + } + + if (obj->mTitle) + { + OICFree(obj->mTitle); + obj->mTitle = NULL; + } + + if (obj->mContentText) + { + OICFree(obj->mContentText); + obj->mContentText = NULL; + } + + OICFree(obj); + + return NS_OK; +} + + diff --git a/service/notification/src/common/NSUtil.h b/service/notification/src/common/NSUtil.h new file mode 100755 index 0000000..c52d9cd --- /dev/null +++ b/service/notification/src/common/NSUtil.h @@ -0,0 +1,34 @@ +//****************************************************************** +// +// 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef _NS_UTIL__H_ +#define _NS_UTIL__H_ + +#include "logger.h" +#include +#include +#include "ocstack.h" +#include "ocpayload.h" +#include "NSStructs.h" +#include "NSConstants.h" + +NSResult NSFreeMessage(NSMessage *); + +#endif /* _NS_UTIL__H_ */ diff --git a/service/notification/src/consumer/NSConsumerCache.c b/service/notification/src/consumer/NSConsumerCache.c index 1fad925..b9874c8 100644 --- a/service/notification/src/consumer/NSConsumerCache.c +++ b/service/notification/src/consumer/NSConsumerCache.c @@ -20,6 +20,7 @@ #include "NSConsumerCache.h" #include "NSConsumerCommon.h" +#include "NSConstants.h" #include "stdlib.h" #include "oic_malloc.h" @@ -34,7 +35,7 @@ NSCacheList * NSConsumerCacheInit() } retNSCacheList->head = NULL; - retNSCacheList->last = NULL; + retNSCacheList->tail = NULL; return retNSCacheList; } @@ -64,16 +65,16 @@ NSResult NSConsumerCacheInsert(NSCacheList * list, NSCacheObject * newObj) NSCacheObject * obj = (NSCacheObject *)OICMalloc(sizeof(NSCacheObject)); if (!obj) { - NS_CONSUMER_LOG(ERROR, "Fail to Create New Object"); + 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_CONSUMER_LOG_V(DEBUG, "Title: %s", newMsgObj->mTitle); - NS_CONSUMER_LOG_V(DEBUG, "ID: %s", newMsgObj->mId); - NS_CONSUMER_LOG_V(DEBUG, "TEXT: %s", newMsgObj->mContentText); + 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); @@ -81,13 +82,13 @@ NSResult NSConsumerCacheInsert(NSCacheList * list, NSCacheObject * newObj) if (!msgObj->mId) { - NS_CONSUMER_LOG(ERROR, "Notification Attributes copy fail"); + NS_LOG(ERROR, "Notification Attributes copy fail"); } msgObj->addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr)); if (!msgObj->addr) { - NS_CONSUMER_LOG(ERROR, "OCDevAddr allocation is failed."); + NS_LOG(ERROR, "OCDevAddr allocation is failed."); } memcpy(msgObj->addr, newMsgObj->addr, sizeof(OCDevAddr)); @@ -117,12 +118,12 @@ NSResult NSConsumerCacheInsert(NSCacheList * list, NSCacheObject * newObj) iter = (NSCacheObject *) iter->next; } - if (list->last) + if (list->tail) { - (list->last)->next = obj; + (list->tail)->next = obj; } - list->last = obj; + list->tail = obj; return NS_OK; } @@ -143,10 +144,10 @@ NSResult NSConsumerCacheDelete(NSCacheList * list, NSCacheObject * delObj) // Fr { if (beDelete == delObj) { - if (beDelete == list->last) + if (beDelete == list->tail) { pre->next = NULL; - list->last = pre; + list->tail = pre; } else { @@ -165,21 +166,21 @@ NSResult NSConsumerCacheDelete(NSCacheList * list, NSCacheObject * delObj) // Fr NSCacheObject * NSConsumerCacheFind(NSCacheList * list, char * findId) { - NS_CONSUMER_LOG_V(DEBUG, "findID: %s", findId); + NS_LOG_V(DEBUG, "findID: %s", findId); NSCacheObject * retObj = list->head; if (!retObj) { - NS_CONSUMER_LOG(DEBUG, "findcache is null"); + NS_LOG(DEBUG, "findcache is null"); return retObj; } NSMessage_consumer * obj = (NSMessage_consumer *) retObj->data; - NS_CONSUMER_LOG_V(DEBUG, "objmID: %s", obj->mId); + NS_LOG_V(DEBUG, "objmID: %s", obj->mId); while (retObj) { NSMessage_consumer * obj = (NSMessage_consumer *) retObj->data; - NS_CONSUMER_LOG_V(DEBUG, "objmID: %s", obj->mId); + NS_LOG_V(DEBUG, "objmID: %s", obj->mId); if (!strcmp(obj->mId, findId)) { return retObj; diff --git a/service/notification/src/consumer/NSConsumerCache.h b/service/notification/src/consumer/NSConsumerCache.h index 802b2f9..22b1576 100644 --- a/service/notification/src/consumer/NSConsumerCache.h +++ b/service/notification/src/consumer/NSConsumerCache.h @@ -40,12 +40,6 @@ typedef struct _NSCacheObject struct _NSCacheObject * next; } NSCacheObject; -typedef struct -{ - NSCacheObject * head; - NSCacheObject * last; -} NSCacheList; - NSCacheList * NSConsumerCacheInit(); NSResult NSConsumerCacheDestroy(NSCacheList * list); diff --git a/service/notification/src/consumer/NSConsumerCommon.c b/service/notification/src/consumer/NSConsumerCommon.c index 94c48bf..1cc11aa 100644 --- a/service/notification/src/consumer/NSConsumerCommon.c +++ b/service/notification/src/consumer/NSConsumerCommon.c @@ -19,6 +19,7 @@ //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #include "NSConsumerCommon.h" +#include "NSConstants.h" #include "NSThread.h" #include "oic_malloc.h" #include "oic_string.h" @@ -68,7 +69,7 @@ void NSDiscoveredProvider(NSProvider * provider) { if (!NSThreadInit(NSDiscoverdProviderFunc, (void *) provider)) { - NS_CONSUMER_LOG(ERROR, "execute discovered provider callback fail"); + NS_LOG(ERROR, "execute discovered provider callback fail"); return; } } @@ -107,7 +108,7 @@ void NSNotificationSync(NSProvider * provider, NSSync * sync) if (!NSThreadInit(NSNotificationSyncFunc, (void *) obj)) { - NS_CONSUMER_LOG(ERROR, "execute noti post callback fail"); + NS_LOG(ERROR, "execute noti post callback fail"); return; } } @@ -149,7 +150,7 @@ void NSNotificationPost(NSProvider * provider, NSMessage * msg) NSMessageData * obj = (NSMessageData *)OICMalloc(sizeof(NSMessageData)); if (!obj) { - NS_CONSUMER_LOG(ERROR, "NSMessageData allocation is failed"); + NS_LOG(ERROR, "NSMessageData allocation is failed"); return; } obj->provider = provider; @@ -157,7 +158,7 @@ void NSNotificationPost(NSProvider * provider, NSMessage * msg) if (!NSThreadInit(NSNotificationPostFunc, (void *) obj)) { - NS_CONSUMER_LOG(ERROR, "execute noti post callback fail"); + NS_LOG(ERROR, "execute noti post callback fail"); return; } } @@ -213,7 +214,7 @@ NSMessage_consumer * NSCopyMessage(NSMessage_consumer * msg) newMsg->addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr)); if (!newMsg->addr) { - NS_CONSUMER_LOG(ERROR, "OCDevAddr allocation is failed"); + NS_LOG(ERROR, "OCDevAddr allocation is failed"); } memcpy(newMsg->addr, msg->addr, sizeof(OCDevAddr)); @@ -260,10 +261,3 @@ OCStackResult NSRequestToResourceIntrospection(OCDoHandle * handle, payload, CT_DEFAULT, NS_QOS, &cbdata, NULL, 0); } -const char * NS_CONVERT_LEVEL(int level) -{ - return ((level) == 0) ? "DEBUG" : - ((level) == 1) ? "INFO" : - ((level) == 2) ? "WARNING" : - ((level) == 3) ? "ERROR" : "FATAL"; -} diff --git a/service/notification/src/consumer/NSConsumerCommon.h b/service/notification/src/consumer/NSConsumerCommon.h index 4a09769..00d7612 100644 --- a/service/notification/src/consumer/NSConsumerCommon.h +++ b/service/notification/src/consumer/NSConsumerCommon.h @@ -31,30 +31,7 @@ extern "C" { #include "NSCommon.h" #include "NSStructs.h" #include "ocstack.h" - -#define NS_LOG_PRINTF 0 - -#define __NS_FILE__ ( strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__ ) - -const char * NS_CONVERT_LEVEL(int level); - -// TODO fix ns log using oic log. -#ifdef TB_LOG -#include "logger.h" -#define NS_CONSUMER_LOG_V(level, format, ...) (OIC_LOG_V((level), __NS_FILE__, (format), __VA_ARGS__)) -#define NS_CONSUMER_LOG(level, msg) (OIC_LOG((level), __NS_FILE__, (msg))) -#else -#include "logger.h" -#define NS_CONSUMER_LOG_V(level, format, ...) { \ - printf("%s: %s ", NS_CONVERT_LEVEL(level), __NS_FILE__); \ - printf((format), __VA_ARGS__); \ - printf("\n"); } -#define NS_CONSUMER_LOG(level, msg) { \ - printf("%s: %s ", NS_CONVERT_LEVEL(level), __NS_FILE__); \ - printf((msg)); \ - printf("\n"); } - -#endif +#include "NSConsumerInterface.h" #define NS_QOS OC_LOW_QOS #define NS_RESOURCE_TYPE "oic.r.notification" diff --git a/service/notification/src/consumer/NSConsumerDiscovery.c b/service/notification/src/consumer/NSConsumerDiscovery.c index dc6ab9a..0eed05c 100644 --- a/service/notification/src/consumer/NSConsumerDiscovery.c +++ b/service/notification/src/consumer/NSConsumerDiscovery.c @@ -23,6 +23,7 @@ #include #include "NSCommon.h" #include "NSConsumerCommon.h" +#include "NSConstants.h" #include "ocpayload.h" #include "oic_malloc.h" @@ -42,11 +43,11 @@ OCStackApplicationResult NSConsumerPresenceListener( OCDoHandle handle, OCClientResponse * clientResponse) { (void) handle; - NS_CONSUMER_LOG_V(DEBUG, "Presence income : %s:%d", + NS_LOG_V(DEBUG, "Presence income : %s:%d", clientResponse->devAddr.addr, clientResponse->devAddr.port); - NS_CONSUMER_LOG_V(DEBUG, "Presence result : %d", + NS_LOG_V(DEBUG, "Presence result : %d", clientResponse->result); - NS_CONSUMER_LOG_V(DEBUG, "Presence sequenceNum : %d", + NS_LOG_V(DEBUG, "Presence sequenceNum : %d", clientResponse->sequenceNumber); if (!NSIsStartedConsumer()) @@ -59,7 +60,7 @@ OCStackApplicationResult NSConsumerPresenceListener( clientResponse->result == OC_STACK_PRESENCE_STOPPED) { // TODO find request and cancel - NS_CONSUMER_LOG(DEBUG, "stopped presence or resource is deleted."); + NS_LOG(DEBUG, "stopped presence or resource is deleted."); //OCCancel(handle, NS_QOS, NULL, 0); } @@ -76,11 +77,11 @@ OCStackApplicationResult NSProviderDiscoverListener( OCDoHandle handle, OCClientResponse * clientResponse) { (void) handle; - NS_CONSUMER_LOG_V(DEBUG, "Discover income : %s:%d", + NS_LOG_V(DEBUG, "Discover income : %s:%d", clientResponse->devAddr.addr, clientResponse->devAddr.port); - NS_CONSUMER_LOG_V(DEBUG, "Discover result : %d", + NS_LOG_V(DEBUG, "Discover result : %d", clientResponse->result); - NS_CONSUMER_LOG_V(DEBUG, "Discover sequenceNum : %d", + NS_LOG_V(DEBUG, "Discover sequenceNum : %d", clientResponse->sequenceNumber); if (!NSIsStartedConsumer()) @@ -114,13 +115,13 @@ OCStackApplicationResult NSGetProviderInformation( (void) handle; int64_t accepter = 0; - NS_CONSUMER_LOG_V(DEBUG, "GET response income : %s:%d", + NS_LOG_V(DEBUG, "GET response income : %s:%d", clientResponse->devAddr.addr, clientResponse->devAddr.port); - NS_CONSUMER_LOG_V(DEBUG, "GET response result : %d", + NS_LOG_V(DEBUG, "GET response result : %d", clientResponse->result); - NS_CONSUMER_LOG_V(DEBUG, "GET response sequenceNum : %d", + NS_LOG_V(DEBUG, "GET response sequenceNum : %d", clientResponse->sequenceNumber); - NS_CONSUMER_LOG_V(DEBUG, "GET response resource uri : %s", + NS_LOG_V(DEBUG, "GET response resource uri : %s", clientResponse->resourceUri); if (!NSIsStartedConsumer()) @@ -130,21 +131,21 @@ OCStackApplicationResult NSGetProviderInformation( if (!clientResponse->payload) { - NS_CONSUMER_LOG(ERROR, "payload is null"); + NS_LOG(ERROR, "payload is null"); return OC_STACK_KEEP_TRANSACTION; } if (!OCRepPayloadGetPropInt((OCRepPayload *)clientResponse->payload, NS_PAYLOAD_KEY_ACCEPTER, & accepter)) { - NS_CONSUMER_LOG(ERROR, "can not seach for accepter"); + NS_LOG(ERROR, "can not seach for accepter"); return OC_STACK_KEEP_TRANSACTION; } NSProvider * newProvider = (NSProvider *)OICMalloc(sizeof(NSProvider)); if (!newProvider) { - NS_CONSUMER_LOG(DEBUG, "NSProvider allocation fail"); + NS_LOG(DEBUG, "NSProvider allocation fail"); return OC_STACK_KEEP_TRANSACTION; } @@ -153,7 +154,7 @@ OCStackApplicationResult NSGetProviderInformation( newProvider->mUserData = (void *)OICMalloc(sizeof(OCDevAddr)); if (!newProvider) { - NS_CONSUMER_LOG(DEBUG, "OCDevAddr allocation fail"); + NS_LOG(DEBUG, "OCDevAddr allocation fail"); OICFree(newProvider); return OC_STACK_KEEP_TRANSACTION; } @@ -163,7 +164,7 @@ OCStackApplicationResult NSGetProviderInformation( OCRepPayload * payload = (OCRepPayload *)clientResponse->payload; while (payload) { - NS_CONSUMER_LOG_V(DEBUG, "Payload Key : %s", payload->values->name); + NS_LOG_V(DEBUG, "Payload Key : %s", payload->values->name); payload = payload->next; } } @@ -173,7 +174,7 @@ OCStackApplicationResult NSGetProviderInformation( { OICFree(newProvider->mUserData); OICFree(newProvider); - NS_CONSUMER_LOG(ERROR, "can not seach for message uri"); + NS_LOG(ERROR, "can not seach for message uri"); return OC_STACK_KEEP_TRANSACTION; } @@ -183,24 +184,24 @@ OCStackApplicationResult NSGetProviderInformation( OICFree(newProvider->messageUri); OICFree(newProvider->mUserData); OICFree(newProvider); - NS_CONSUMER_LOG(ERROR, "can not seach for sync uri"); + NS_LOG(ERROR, "can not seach for sync uri"); return OC_STACK_KEEP_TRANSACTION; } if (accepter == NS_ACCEPTER_CONSUMER) { - NS_CONSUMER_LOG(DEBUG, "accepter is NS_ACCEPTER_CONSUMER, Callback to user"); + NS_LOG(DEBUG, "accepter is NS_ACCEPTER_CONSUMER, Callback to user"); NSDiscoveredProvider(newProvider); } else { - NS_CONSUMER_LOG(DEBUG, "accepter is NS_ACCEPTER_PROVIDER, request subscribe"); + NS_LOG(DEBUG, "accepter is NS_ACCEPTER_PROVIDER, request subscribe"); NSTask * task = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE, (void *) newProvider); if (!task) { - NS_CONSUMER_LOG(DEBUG, "NSTask allocation fail"); + NS_LOG(DEBUG, "NSTask allocation fail"); return OC_STACK_KEEP_TRANSACTION; } @@ -214,11 +215,11 @@ void NSConsumerDiscoveryHandleMsg(NSTask * task) { if (!task) { - NS_CONSUMER_LOG(ERROR, "task is null"); + NS_LOG(ERROR, "task is null"); return; } - NS_CONSUMER_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType); + NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType); if (task->taskType == TASK_EVENT_CONNECTED || task->taskType == TASK_CONSUMER_REQ_DISCOVER) { NSRequestToResourceIntrospection(NULL, OC_REST_DISCOVER, NULL, NS_DISCOVER_QUERY, @@ -226,7 +227,7 @@ void NSConsumerDiscoveryHandleMsg(NSTask * task) } else { - NS_CONSUMER_LOG(ERROR, "Unknown type message"); + NS_LOG(ERROR, "Unknown type message"); } } diff --git a/service/notification/src/consumer/NSConsumerInterface.c b/service/notification/src/consumer/NSConsumerInterface.c index a81aff2..558a7d3 100644 --- a/service/notification/src/consumer/NSConsumerInterface.c +++ b/service/notification/src/consumer/NSConsumerInterface.c @@ -25,6 +25,7 @@ #include "NSCommon.h" #include "NSConsumerCommon.h" +#include "NSConstants.h" #include "NSConsumerMessageHandler.h" #include "oic_malloc.h" @@ -36,10 +37,28 @@ NSResult NSStartConsumer( { if (NSIsStartedConsumer()) { - printf("is already started consumer service?\n"); + NS_LOG(DEBUG, "is already started consumer service?"); return NS_OK; } + if (!discoverCb) + { + NS_LOG(ERROR, "discoverCb is NULL"); + return NS_ERROR; + } + + if (!postCb) + { + NS_LOG(ERROR, "postCb is NULL"); + return NS_ERROR; + } + + if (!syncCb) + { + NS_LOG(ERROR, "syncCb is NULL"); + return NS_ERROR; + } + NSSetDiscoverProviderCb(discoverCb); NSSetNotificationPostedCb(postCb); NSSetNotificationSyncCb(syncCb); @@ -47,6 +66,7 @@ NSResult NSStartConsumer( if (NS_OK != NSConsumerMessageHandlerInit()) { + NSStopConsumer(); return NS_ERROR; } @@ -60,27 +80,29 @@ NSResult NSStopConsumer() NSSetNotificationSyncCb(NULL); NSSetIsStartedConsumer(false); + NSConsumerMessageHandlerExit(); + return NS_OK; } -NSResult NSSubscribeProvider(NSProvider * provider) +NSResult NSSubscribe(NSProvider * provider) { NSTask * subscribeTask = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE, (void *) provider); if (!subscribeTask) { - NS_CONSUMER_LOG(ERROR, "NSTask allocation fail"); + NS_LOG(ERROR, "NSTask allocation fail"); return NS_ERROR; } return NSConsumerPushEvent(subscribeTask); } -NSResult NSUnsubscribeProvider(NSProvider * provider) +NSResult NSUnsubscribe(NSProvider * provider) { NSTask * unsubscribeTask = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL, (void *) provider); if (!unsubscribeTask) { - NS_CONSUMER_LOG(ERROR, "NSTask allocation fail"); + NS_LOG(ERROR, "NSTask allocation fail"); return NS_ERROR; } @@ -92,31 +114,31 @@ NSResult NSRescanProvider() NSTask * discoverTask = NSMakeTask(TASK_CONSUMER_REQ_DISCOVER, NULL); if (!discoverTask) { - NS_CONSUMER_LOG(ERROR, "NSTask allocation fail"); + NS_LOG(ERROR, "NSTask allocation fail"); return NS_ERROR; } return NSConsumerPushEvent(discoverTask); } -NSResult NSRead(NSMessage * obj) +NSResult NSConsumerReadCheck(NSMessage * obj) { NSTask * readTask = NSMakeTask(TASK_SEND_READ, (void *) obj); if (!readTask) { - NS_CONSUMER_LOG(ERROR, "NSTask allocation fail"); + NS_LOG(ERROR, "NSTask allocation fail"); return NS_ERROR; } return NSConsumerPushEvent(readTask); } -NSResult NSDismiss(NSMessage * obj) +NSResult NSConsumerDismissCheck(NSMessage * obj) { NSTask * dismissTask = NSMakeTask(TASK_SEND_DISMISS, (void *) obj); if (!dismissTask) { - NS_CONSUMER_LOG(ERROR, "NSTask allocation fail"); + NS_LOG(ERROR, "NSTask allocation fail"); return NS_ERROR; } diff --git a/service/notification/src/consumer/NSConsumerListener.c b/service/notification/src/consumer/NSConsumerListener.c index 8cba403..18adc7b 100644 --- a/service/notification/src/consumer/NSConsumerListener.c +++ b/service/notification/src/consumer/NSConsumerListener.c @@ -23,6 +23,7 @@ #include #include +#include "NSConstants.h" #include "NSConsumerCommon.h" #include "payload_logging.h" #include "cautilinterface.h" @@ -56,14 +57,14 @@ NSResult NSConsumerListenerInit() if (OC_STACK_OK != NSRequestToResourceIntrospection(NULL, OC_REST_PRESENCE, NULL, NS_PRESENCE_SUBSCRIBE_QUERY, NULL, NSConsumerPresenceListener)) { - NS_CONSUMER_LOG(ERROR, "Presence request fail"); + NS_LOG(ERROR, "Presence request fail"); return NS_ERROR; } if (OC_STACK_OK != NSRequestToResourceIntrospection(NULL, OC_REST_DISCOVER, NULL, NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener)) { - NS_CONSUMER_LOG(ERROR, "Discover request fail"); + NS_LOG(ERROR, "Discover request fail"); return NS_ERROR; } @@ -75,7 +76,7 @@ OCStackApplicationResult NSRIResponseListener( { if (ctx == NULL) { - NS_CONSUMER_LOG(ERROR, "Callback is null"); + NS_LOG(ERROR, "Callback is null"); return OC_STACK_DELETE_TRANSACTION; } @@ -84,6 +85,10 @@ OCStackApplicationResult NSRIResponseListener( OIC_LOG_PAYLOAD(INFO, clientResponse->payload); ((onRIResponse)ctx)(handle, clientResponse); } + else + { + NS_LOG_V(ERROR, "result is not ok : %d", clientResponse->result); + } return OC_STACK_KEEP_TRANSACTION; } @@ -91,21 +96,21 @@ OCStackApplicationResult NSRIResponseListener( void NSConnectionStateListener(CATransportAdapter_t adapter, const char *remote_address, bool connected) { - NS_CONSUMER_LOG_V(DEBUG, "adapter : %d", adapter); - NS_CONSUMER_LOG_V(DEBUG, "remote_address : %s", remote_address); - NS_CONSUMER_LOG_V(DEBUG, "isConnect : %d", connected); + NS_LOG_V(DEBUG, "adapter : %d", adapter); + NS_LOG_V(DEBUG, "remote_address : %s", remote_address); + NS_LOG_V(DEBUG, "isConnect : %d", connected); (void) adapter; (void) remote_address; if (connected) { - NS_CONSUMER_LOG(DEBUG, "try to discover notification provider."); + NS_LOG(DEBUG, "try to discover notification provider."); NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED, NULL); if (!task) { - NS_CONSUMER_LOG(ERROR, "NSTask allocation fail."); + NS_LOG(ERROR, "NSTask allocation fail."); return; } NSConsumerPushEvent(task); @@ -114,19 +119,19 @@ void NSConnectionStateListener(CATransportAdapter_t adapter, void NSAdapterStateListener(CATransportAdapter_t adapter, bool enabled) { - NS_CONSUMER_LOG_V(DEBUG, "adapter : %d", adapter); - NS_CONSUMER_LOG_V(DEBUG, "isEnabled : %d", enabled); + NS_LOG_V(DEBUG, "adapter : %d", adapter); + NS_LOG_V(DEBUG, "isEnabled : %d", enabled); (void) adapter; if (enabled) { - NS_CONSUMER_LOG(DEBUG, "try to discover notification provider."); + NS_LOG(DEBUG, "try to discover notification provider."); NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED, NULL); if (!task) { - NS_CONSUMER_LOG(ERROR, "NSTask allocation fail."); + NS_LOG(ERROR, "NSTask allocation fail."); return; } NSConsumerPushEvent(task); diff --git a/service/notification/src/consumer/NSConsumerMessageHandler.c b/service/notification/src/consumer/NSConsumerMessageHandler.c index 831ff04..6e0fcbc 100644 --- a/service/notification/src/consumer/NSConsumerMessageHandler.c +++ b/service/notification/src/consumer/NSConsumerMessageHandler.c @@ -26,6 +26,7 @@ #include "oic_malloc.h" #include "NSStructs.h" +#include "NSConstants.h" #include "NSConsumerCommon.h" #include "NSThread.h" @@ -72,20 +73,20 @@ NSResult NSConsumerMessageHandlerInit() if (NS_OK != NSConsumerListenerInit()) { - NS_CONSUMER_LOG(ERROR, "listener init fail"); + NS_LOG(ERROR, "listener init fail"); return NS_ERROR; } if (NS_OK != NSConsumerSystemInit()) { - NS_CONSUMER_LOG(ERROR, "system init fail"); + NS_LOG(ERROR, "system init fail"); return NS_ERROR; } handle = NSThreadInit(NSConsumerMsgHandleThreadFunc, NULL); if (!handle) { - NS_CONSUMER_LOG(ERROR, "msg handle thread init fail"); + NS_LOG(ERROR, "msg handle thread init fail"); return NS_ERROR; } NSSetMsgHandleThreadHandle(handle); @@ -93,7 +94,7 @@ NSResult NSConsumerMessageHandlerInit() queue = NSCreateQueue(); if (!queue) { - NS_CONSUMER_LOG(ERROR, "msg handle queue create fail"); + NS_LOG(ERROR, "msg handle queue create fail"); return NS_ERROR; } NSSetMsgHandleQueue(queue); @@ -105,7 +106,7 @@ NSResult NSConsumerPushEvent(NSTask * task) { if (!NSThreadInit(NSConsumerMsgPushThreadFunc, (void *) task)) { - NS_CONSUMER_LOG(ERROR, "insert to queue thread init fail"); + NS_LOG(ERROR, "insert to queue thread init fail"); return NS_ERROR; } @@ -122,10 +123,10 @@ void * NSConsumerMsgHandleThreadFunc(void * threadHandle) NSConsumerQueue * queue = NULL; NSConsumerQueueObject * obj = NULL; NSThreadHandle * handle = (NSThreadHandle *) threadHandle; - NS_CONSUMER_LOG(DEBUG, "created thread for consumer message handle"); + NS_LOG(DEBUG, "created thread for consumer message handle"); if (!handle) { - NS_CONSUMER_LOG(ERROR, "thread handle is null"); + NS_LOG(ERROR, "thread handle is null"); return NULL; } @@ -133,7 +134,7 @@ void * NSConsumerMsgHandleThreadFunc(void * threadHandle) { if (!handle->isStarted) { - NS_CONSUMER_LOG(ERROR, "msg handler thread will be terminated"); + NS_LOG(ERROR, "msg handler thread will be terminated"); break; } @@ -149,11 +150,13 @@ void * NSConsumerMsgHandleThreadFunc(void * threadHandle) } NSThreadLock(handle); - NS_CONSUMER_LOG(DEBUG, "msg handler working"); + NS_LOG(DEBUG, "msg handler working"); obj = NSPopQueue(queue); - // TODO processing obj - NSConsumerHandleMsg((NSTask *)(obj->data)); + if (obj) + { + NSConsumerHandleMsg((NSTask *)(obj->data)); + } NSThreadUnlock(handle); @@ -169,14 +172,14 @@ void * NSConsumerMsgPushThreadFunc(void * data) NSThreadHandle * handle = *(NSGetMsgHandleThreadHandle()); if (!handle) { - NS_CONSUMER_LOG(ERROR, "NSThreadHandle is null. can not insert to queue"); + NS_LOG(ERROR, "NSThreadHandle is null. can not insert to queue"); return NULL; } obj = (NSConsumerQueueObject *)OICMalloc(sizeof(NSConsumerQueueObject)); if (!obj) { - NS_CONSUMER_LOG(ERROR, "NSConsumerQueueObject allocation fail"); + NS_LOG(ERROR, "NSConsumerQueueObject allocation fail"); return NULL; } @@ -188,7 +191,7 @@ void * NSConsumerMsgPushThreadFunc(void * data) queue = *(NSGetMsgHandleQueue()); if (!queue) { - NS_CONSUMER_LOG(ERROR, "NSQueue is null. can not insert to queue"); + NS_LOG(ERROR, "NSQueue is null. can not insert to queue"); OICFree(data); OICFree(obj); } @@ -213,6 +216,7 @@ void NSConsumerHandleMsg(NSTask * task) break; } case TASK_CONSUMER_REQ_SUBSCRIBE: + case TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL: case TASK_SEND_READ: case TASK_SEND_DISMISS: { @@ -227,7 +231,7 @@ void NSConsumerHandleMsg(NSTask * task) break; } default: - NS_CONSUMER_LOG(ERROR, "Unknown type of task"); + NS_LOG(ERROR, "Unknown type of task"); break; } } diff --git a/service/notification/src/consumer/NSConsumerNotification.c b/service/notification/src/consumer/NSConsumerNotification.c index 84e81c0..62fc71e 100644 --- a/service/notification/src/consumer/NSConsumerNotification.c +++ b/service/notification/src/consumer/NSConsumerNotification.c @@ -20,6 +20,7 @@ #include "NSConsumerNotification.h" +#include "NSConstants.h" #include "NSConsumerCommon.h" #include "oic_malloc.h" #include "oic_string.h" @@ -33,15 +34,15 @@ NSResult NSPushToCache(OCClientResponse * clientResponse, NSTaskType type); NSResult NSConsumerSubscribeProvider(NSProvider * provider) { - if (OC_STACK_OK != NSRequestToResourceIntrospection(NULL, OC_REST_OBSERVE, - (OCDevAddr *) provider->mUserData, + if (OC_STACK_OK != NSRequestToResourceIntrospection(&(provider->messageHandle), + OC_REST_OBSERVE, (OCDevAddr *) provider->mUserData, provider->messageUri, NULL, NSConsumerNotificationListener)) { return NS_ERROR; } - if (OC_STACK_OK != NSRequestToResourceIntrospection(NULL, OC_REST_OBSERVE, - (OCDevAddr *) provider->mUserData, + if (OC_STACK_OK != NSRequestToResourceIntrospection(&(provider->syncHandle), + OC_REST_OBSERVE, (OCDevAddr *) provider->mUserData, provider->syncUri, NULL, NSConsumerSyncListener)) { return NS_ERROR; @@ -71,7 +72,7 @@ OCStackApplicationResult NSConsumerSyncListener( NSProvider * provider = NSGetProvider(clientResponse); if (!provider) { - NS_CONSUMER_LOG(ERROR, "getting provider is failed"); + NS_LOG(ERROR, "getting provider is failed"); return OC_STACK_KEEP_TRANSACTION; } @@ -85,12 +86,12 @@ OCStackApplicationResult NSConsumerSyncListener( if (newNoti->mState != Notification_Read) { - NS_CONSUMER_LOG(DEBUG, "newNoti->type : Dismiss"); + NS_LOG(DEBUG, "newNoti->type : Dismiss"); taskType = TASK_RECV_DISMISS; } else { - NS_CONSUMER_LOG(DEBUG, "newNoti->type : Read"); + NS_LOG(DEBUG, "newNoti->type : Read"); } NSNotificationSync(provider, newNoti); @@ -111,7 +112,7 @@ OCStackApplicationResult NSConsumerNotificationListener( NSProvider * provider = NSGetProvider(clientResponse); if (!provider) { - NS_CONSUMER_LOG(ERROR, "getting provider is failed"); + NS_LOG(ERROR, "getting provider is failed"); return OC_STACK_KEEP_TRANSACTION; } @@ -123,7 +124,7 @@ OCStackApplicationResult NSConsumerNotificationListener( NSTaskType taskType = TASK_CONSUMER_RECV_NOTIFICATION; - NS_CONSUMER_LOG(DEBUG, "newNoti->type == Notification"); + NS_LOG(DEBUG, "newNoti->type == Notification"); NSNotificationPost(provider, (NSMessage *) newNoti); if (NS_OK != NSPushToCache(clientResponse, taskType)) @@ -145,7 +146,7 @@ NSResult NSPushToCache(OCClientResponse * clientResponse, NSTaskType type) NSTask * task = NSMakeTask(type, (void *) cachedNoti); if (!task) { - NS_CONSUMER_LOG(ERROR, "NSTask allocation fail"); + NS_LOG(ERROR, "NSTask allocation fail"); NSRemoveMessage(cachedNoti); return NS_ERROR; } @@ -174,7 +175,7 @@ NSMessage_consumer * NSBuildOICNotification(OCClientResponse * clientResponse) OCRepPayload * payload = (OCRepPayload *)clientResponse->payload; if (!OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_ID, &retNoti->mId)) { - NS_CONSUMER_LOG(ERROR, "id of received notification is null"); + NS_LOG(ERROR, "id of received notification is null"); OICFree(retNoti); return NULL; } @@ -182,9 +183,10 @@ NSMessage_consumer * NSBuildOICNotification(OCClientResponse * clientResponse) OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_TITLE, &retNoti->mTitle); OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_TEXT, &retNoti->mContentText); - NS_CONSUMER_LOG_V(DEBUG, "Msg ID : %s", retNoti->mId); - NS_CONSUMER_LOG_V(DEBUG, "Msg Title : %s", retNoti->mTitle); - NS_CONSUMER_LOG_V(DEBUG, "Msg Content : %s", retNoti->mContentText); + NS_LOG_V(DEBUG, "Msg Address : %s", clientResponse->addr->addr); + NS_LOG_V(DEBUG, "Msg ID : %s", retNoti->mId); + NS_LOG_V(DEBUG, "Msg Title : %s", retNoti->mTitle); + NS_LOG_V(DEBUG, "Msg Content : %s", retNoti->mContentText); retNoti->addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr)); memcpy(retNoti->addr, clientResponse->addr, sizeof(OCDevAddr)); @@ -212,14 +214,14 @@ NSSync * NSBuildOICNotificationSync(OCClientResponse * clientResponse) OCRepPayload * payload = (OCRepPayload *)clientResponse->payload; if (!OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_ID, &retSync->mMessageId)) { - NS_CONSUMER_LOG(ERROR, "id of received sync is null"); + NS_LOG(ERROR, "id of received sync is null"); OICFree(retSync); return NULL; } int64_t state; if (!OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_STATE, & state)) { - NS_CONSUMER_LOG(ERROR, "state of received sync is null"); + NS_LOG(ERROR, "state of received sync is null"); OICFree(retSync->mMessageId); OICFree(retSync); return NULL; @@ -227,8 +229,8 @@ NSSync * NSBuildOICNotificationSync(OCClientResponse * clientResponse) retSync->mState = (NSSyncTypes) state; - NS_CONSUMER_LOG_V(DEBUG, "Sync ID : %s", retSync->mMessageId); - NS_CONSUMER_LOG_V(DEBUG, "Sync State : %d", (int) retSync->mState); + NS_LOG_V(DEBUG, "Sync ID : %s", retSync->mMessageId); + NS_LOG_V(DEBUG, "Sync State : %d", (int) retSync->mState); return retSync; } @@ -238,7 +240,7 @@ NSProvider * NSGetProvider(OCClientResponse * clientResponse) NSProvider * newProvider = (NSProvider *)OICMalloc(sizeof(NSProvider)); if (!newProvider) { - NS_CONSUMER_LOG(DEBUG, "NSProvider allocation fail"); + NS_LOG(DEBUG, "NSProvider allocation fail"); return NULL; } @@ -247,7 +249,7 @@ NSProvider * NSGetProvider(OCClientResponse * clientResponse) newProvider->mUserData = (void *)OICMalloc(sizeof(OCDevAddr)); if (!newProvider->mUserData) { - NS_CONSUMER_LOG(DEBUG, "OCDevAddr allocation fail"); + NS_LOG(DEBUG, "OCDevAddr allocation fail"); OICFree(newProvider); return NULL; } @@ -260,16 +262,16 @@ void NSConsumerNotificationHandleMsg(NSTask * task) { if (!task) { - NS_CONSUMER_LOG(ERROR, "task is null"); + NS_LOG(ERROR, "task is null"); return; } - NS_CONSUMER_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType); + NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType); if (task->taskType == TASK_CONSUMER_REQ_SUBSCRIBE) { if (NS_OK != NSConsumerSubscribeProvider((NSProvider *)task->taskData)) { - NS_CONSUMER_LOG(ERROR, "Subscribe fail"); + NS_LOG(ERROR, "Subscribe fail"); return; } } @@ -278,29 +280,38 @@ void NSConsumerNotificationHandleMsg(NSTask * task) NSMessage_consumer * nsConsumer = (NSMessage_consumer *) task->taskData; if (!nsConsumer) { - NS_CONSUMER_LOG(ERROR, "taskData is NULL"); + NS_LOG(ERROR, "taskData is NULL"); return; } OCRepPayload * payload = OCRepPayloadCreate (); if (!payload) { - NS_CONSUMER_LOG(ERROR, "Failed to create POST payload object"); + NS_LOG(ERROR, "Failed to create POST payload object"); return; } + + int type = (task->taskType == TASK_SEND_READ) ? 0 : 1; OCRepPayloadSetPropString(payload, "ID", (char *) nsConsumer->mId); - OCRepPayloadSetPropInt(payload, "STATE", (int) nsConsumer->type); + OCRepPayloadSetPropInt(payload, "STATE", type); // TODO fix param for uri if (NS_OK != NSConsumerPostProvider( (OCDevAddr *) nsConsumer->addr, (OCPayload *) payload, "/notification/sync")) { - NS_CONSUMER_LOG(ERROR, "Subscribe fail"); + NS_LOG(ERROR, "Subscribe fail"); return; } } + else if (task->taskType == TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL) + { + NSProvider * provider = (NSProvider *)task->taskData; + + OCCancel(provider->messageHandle, NS_QOS, NULL, 0); + OCCancel(provider->syncHandle, NS_QOS, NULL, 0); + } else { - NS_CONSUMER_LOG(ERROR, "Unknown type message"); + NS_LOG(ERROR, "Unknown type message"); } } diff --git a/service/notification/src/consumer/NSConsumerQueue.c b/service/notification/src/consumer/NSConsumerQueue.c index 8e0ded8..f37bec6 100644 --- a/service/notification/src/consumer/NSConsumerQueue.c +++ b/service/notification/src/consumer/NSConsumerQueue.c @@ -20,6 +20,7 @@ #include "NSConsumerQueue.h" +#include "NSConstants.h" #include "oic_malloc.h" #include "NSConsumerCommon.h" @@ -60,7 +61,7 @@ bool NSPushQueue(NSConsumerQueue * queue, NSConsumerQueueObject * object) if (!object) { - NS_CONSUMER_LOG(ERROR, "object is null. can not insert to queue"); + NS_LOG(ERROR, "object is null. can not insert to queue"); } if (!(queue->head)) diff --git a/service/notification/src/consumer/NSConsumerSubsription.c b/service/notification/src/consumer/NSConsumerSubsription.c index 8df1f91..5b5eea3 100644 --- a/service/notification/src/consumer/NSConsumerSubsription.c +++ b/service/notification/src/consumer/NSConsumerSubsription.c @@ -20,6 +20,7 @@ #include "NSConsumerSubsription.h" +#include "NSConstants.h" #include "NSConsumerCommon.h" #include "NSConsumerCache.h" #include "NSStructs.h" @@ -45,17 +46,17 @@ NSResult NSCacheUpdate(NSCacheList * cache, NSTask * task, NSConsumerMessageType NSCacheObject * obj = (NSCacheObject *)OICMalloc(sizeof(NSCacheObject)); if (!obj) { - NS_CONSUMER_LOG(ERROR, "Cache allocation is failed"); + NS_LOG(ERROR, "Cache allocation is failed"); } obj->data = (NSCacheData *) noti; obj->next = NULL; if (NS_OK != NSConsumerCacheWrite(cache, obj)) { - NS_CONSUMER_LOG(DEBUG, "CacheUpdate - NSConsumerCacheInsert"); + NS_LOG(DEBUG, "CacheUpdate - NSConsumerCacheInsert"); if (NS_OK != NSConsumerCacheInsert(cache, (NSCacheObject *) obj)) { - NS_CONSUMER_LOG(ERROR, "Cache insert fail"); + NS_LOG(ERROR, "Cache insert fail"); return NS_ERROR; } } @@ -70,18 +71,18 @@ void NSConsumerSubscriptionHandleMsg(NSTask * task) { if (!task) { - NS_CONSUMER_LOG(ERROR, "task is null"); + NS_LOG(ERROR, "task is null"); return; } NSCacheList * cache; if (!*(NSGetCacheList())) { - NS_CONSUMER_LOG(DEBUG, "Cache Init"); + NS_LOG(DEBUG, "Cache Init"); cache = NSConsumerCacheInit(); if (!cache) { - NS_CONSUMER_LOG(ERROR, "Cache create fail"); + NS_LOG(ERROR, "Cache create fail"); return; } NSSetCacheList(cache); @@ -89,52 +90,52 @@ void NSConsumerSubscriptionHandleMsg(NSTask * task) cache = *(NSGetCacheList()); if (!cache->head) { - NS_CONSUMER_LOG(DEBUG, "Cache Head is null 2"); + NS_LOG(DEBUG, "Cache Head is null 2"); } else{ - NS_CONSUMER_LOG(DEBUG, "Cache Head is not null 2"); + NS_LOG(DEBUG, "Cache Head is not null 2"); } - NS_CONSUMER_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType); + NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType); switch (task->taskType) { case TASK_CONSUMER_RECV_NOTIFICATION: { - NS_CONSUMER_LOG(DEBUG, "Receive New Notification"); + NS_LOG(DEBUG, "Receive New Notification"); if (NS_OK != NSCacheUpdate(cache, task, Notification)) { - NS_CONSUMER_LOG(ERROR, "Cache Update fail"); + NS_LOG(ERROR, "Cache Update fail"); return; } break; } case TASK_RECV_READ: { - NS_CONSUMER_LOG(DEBUG, "Receive Read Notification"); + NS_LOG(DEBUG, "Receive Read Notification"); // TODO update Cache. if (NS_OK != NSCacheUpdate(cache, task, Read)) { - NS_CONSUMER_LOG(ERROR, "Cache Update fail"); + NS_LOG(ERROR, "Cache Update fail"); return; } break; } case TASK_RECV_DISMISS: { - NS_CONSUMER_LOG(DEBUG, "Receive Dismiss Notification"); + NS_LOG(DEBUG, "Receive Dismiss Notification"); if (NS_OK != NSCacheUpdate(cache, task, Dismiss)) { - NS_CONSUMER_LOG(ERROR, "Cache Update fail"); + NS_LOG(ERROR, "Cache Update fail"); return; } break; } default : { - NS_CONSUMER_LOG(ERROR, "Unknown TASK Type"); + NS_LOG(ERROR, "Unknown TASK Type"); return ; } } diff --git a/service/notification/src/consumer/NSConsumerSystem.c b/service/notification/src/consumer/NSConsumerSystem.c index 20b342f..d132b1c 100644 --- a/service/notification/src/consumer/NSConsumerSystem.c +++ b/service/notification/src/consumer/NSConsumerSystem.c @@ -19,6 +19,8 @@ //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #include "NSConsumerSystem.h" + +#include "NSConstants.h" #include "NSConsumerCommon.h" NSResult NSConsumerSystemInit() diff --git a/service/notification/src/consumer/NSThread.c b/service/notification/src/consumer/NSThread.c index d75284c..810e176 100644 --- a/service/notification/src/consumer/NSThread.c +++ b/service/notification/src/consumer/NSThread.c @@ -20,6 +20,7 @@ #include "NSThread.h" +#include "NSConstants.h" #include "NSConsumerCommon.h" #include @@ -31,14 +32,14 @@ NSThreadHandle * NSThreadInit(NSThreadFunc func, void * data) { if (!func) { - NS_CONSUMER_LOG(ERROR, "thread function is null"); + NS_LOG(ERROR, "thread function is null"); return NULL; } NSThreadHandle * handle = (NSThreadHandle *)OICMalloc(sizeof(NSThreadHandle)); if (!handle) { - NS_CONSUMER_LOG(ERROR, "thread allocation fail"); + NS_LOG(ERROR, "thread allocation fail"); return NULL; } @@ -47,21 +48,21 @@ NSThreadHandle * NSThreadInit(NSThreadFunc func, void * data) pthread_mutexattr_init(&(handle->mutex_attr)); if (pthread_mutexattr_settype(&(handle->mutex_attr), PTHREAD_MUTEX_RECURSIVE)) { - NS_CONSUMER_LOG(ERROR, "thread mutex_attr init fail"); + NS_LOG(ERROR, "thread mutex_attr init fail"); NSDestroyThreadHandle(handle); return NULL; } if (pthread_mutex_init(&(handle->mutex), &(handle->mutex_attr))) { - NS_CONSUMER_LOG(ERROR, "thread mutex init fail"); + NS_LOG(ERROR, "thread mutex init fail"); NSDestroyThreadHandle(handle); return NULL; } if (pthread_mutex_lock(&(handle->mutex))) { - NS_CONSUMER_LOG(ERROR, "thread mutex lock fail"); + NS_LOG(ERROR, "thread mutex lock fail"); NSDestroyThreadHandle(handle); return NULL; } @@ -71,7 +72,7 @@ NSThreadHandle * NSThreadInit(NSThreadFunc func, void * data) if (pthread_create(&(handle->thread_id), NULL, func, (data == NULL) ? (void *) handle : (void *)data)) { - NS_CONSUMER_LOG(ERROR, "thread create fail"); + NS_LOG(ERROR, "thread create fail"); NSDestroyThreadHandle(handle); return NULL; } diff --git a/service/notification/src/provider/NSProviderCacheAdapter.c b/service/notification/src/provider/NSProviderCacheAdapter.c deleted file mode 100644 index 0b5a270..0000000 --- a/service/notification/src/provider/NSProviderCacheAdapter.c +++ /dev/null @@ -1,105 +0,0 @@ -//****************************************************************** -// -// 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 "NSProviderCacheAdapter.h" - - -NSWhiteList whiteMessageList; -NSBlackList blackMessageList; - -NSWhiteList whiteSyncList; -NSBlackList blackSyncList; - -NSResult NSProviderInitCache() -{ - // create cache - - return NS_OK; -} - -NSResult NSProviderWriteToCache(NSCache target, OCObservationId observeId, int state) -{ - printf("NSProviderWriteToCache - 0\n"); - if(state == 0) // message - { - printf("NSProviderWriteToCache - 0 - 1\n"); - if (NS_CONSUMER_BLACKLIST == target) - { - printf("NSProviderWriteToCache - 0 - 1 - 1\n"); - blackMessageList.idList[blackMessageList.size] = observeId; - blackMessageList.size++; - } - else if (NS_CONSUMER_WHITELIST == target) - { - printf("NSProviderWriteToCache - 0 - 1 - 2\n"); - whiteMessageList.idList[whiteMessageList.size] = observeId; - whiteMessageList.size++; - } - } - else if(state == 1) - { - printf("NSProviderWriteToCache - 1 - 1\n"); - if (NS_CONSUMER_BLACKLIST == target) - { - printf("NSProviderWriteToCache - 1 - 1 - 1\n"); - blackSyncList.idList[blackSyncList.size] = observeId; - blackSyncList.size++; - } - else if (NS_CONSUMER_WHITELIST == target) - { - printf("NSProviderWriteToCache - 1 - 1 - 2\n"); - whiteSyncList.idList[whiteSyncList.size] = observeId; - whiteSyncList.size++; - } - } - - return NS_OK; -} - -NSCommonList * NSProviderReadCache(NSCache target, int state) -{ - if(state == 0) // message - { - if(NS_CONSUMER_BLACKLIST == target) - { - return (NSCommonList *)&blackMessageList; - } - else if(NS_CONSUMER_WHITELIST == target) - { - return (NSCommonList *)&whiteMessageList; - } - } - else if(state == 1) - { - if(NS_CONSUMER_BLACKLIST == target) - { - return (NSCommonList *)&blackSyncList; - } - else if(NS_CONSUMER_WHITELIST == target) - { - return (NSCommonList *)&whiteSyncList; - } - } - return NULL; -} - -NSResult NSProviderDeleteCache() -{ - return NS_OK; -} diff --git a/service/notification/src/provider/NSProviderDiscovery.c b/service/notification/src/provider/NSProviderDiscovery.c index 975f0ca..5f245bc 100644 --- a/service/notification/src/provider/NSProviderDiscovery.c +++ b/service/notification/src/provider/NSProviderDiscovery.c @@ -65,7 +65,7 @@ void * NSDiscoverySchedule(void * ptr) NSTask *node = NSHeadMsg[DISCOVERY_SCHEDULER]; NSHeadMsg[DISCOVERY_SCHEDULER] = node->nextTask; - switch (node->taskType) + switch ((int)node->taskType) { case TASK_START_PRESENCE: NSStartPresence(); @@ -77,6 +77,7 @@ void * NSDiscoverySchedule(void * ptr) NSRegisterResource(); break; } + OICFree(node); } diff --git a/service/notification/src/provider/NSProviderDiscovery.h b/service/notification/src/provider/NSProviderDiscovery.h index 31e5329..c020ecb 100644 --- a/service/notification/src/provider/NSProviderDiscovery.h +++ b/service/notification/src/provider/NSProviderDiscovery.h @@ -23,7 +23,7 @@ #include "NSCommon.h" #include "NSStructs.h" -#include "NSQueueScheduler.h" +#include "NSProviderScheduler.h" #include "NSProviderResource.h" NSResult NSStartPresence(); diff --git a/service/notification/src/provider/NSProviderInterface.c b/service/notification/src/provider/NSProviderInterface.c index f7ab929..16d98b3 100644 --- a/service/notification/src/provider/NSProviderInterface.c +++ b/service/notification/src/provider/NSProviderInterface.c @@ -19,13 +19,17 @@ //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #include "NSProviderInterface.h" -#include "NSQueueScheduler.h" +#include "NSProviderScheduler.h" #include "NSProviderListener.h" +#include "NSCacheAdapter.h" #include "cautilinterface.h" +#include "NSProviderSubscription.h" +#include "NSProviderNotification.h" -bool initSchedule = false; +bool initProvider = false; static NSSubscribeRequestCallback g_subscribeRequestCb = NULL; static NSSyncCallback g_syncCb = NULL; +pthread_mutex_t NSCacheMutex; void NSRegisterSubscribeRequestCb(NSSubscribeRequestCallback subscribeRequestCb) { @@ -42,9 +46,9 @@ void NSSubscribeRequestCb(NSConsumer *consumer) g_subscribeRequestCb(consumer); } -void NSSyncCb(NSProvider *provider, NSSync *sync) +void NSSyncCb(NSSync *sync) { - g_syncCb(provider, sync); + g_syncCb(sync); } NSResult NSStartProvider(NSAccessPolicy policy, NSSubscribeRequestCallback subscribeRequestCb, @@ -54,9 +58,11 @@ NSResult NSStartProvider(NSAccessPolicy policy, NSSubscribeRequestCallback subsc NSSetSubscriptionAcceptPolicy(policy); - if (!initSchedule) + if (!initProvider) { - initSchedule = true; + initProvider = true; + + NSSetList(); NSInitScheduler(); NSStartScheduler(); } @@ -73,11 +79,18 @@ NSResult NSStartProvider(NSAccessPolicy policy, NSSubscribeRequestCallback subsc return NS_OK; } +void NSSetList() +{ + pthread_mutex_init(&NSCacheMutex, NULL); + NSInitSubscriptionList(); + NSInitMessageList(); +} + NSResult NSStopProvider() { - NSRegisterSubscribeRequestCb(NULL); - NSRegisterSyncCb(NULL); - initSchedule = false; + NSRegisterSubscribeRequestCb((NSSubscribeRequestCallback)NULL); + NSRegisterSyncCb((NSSyncCallback)NULL); + initProvider = false; return NS_OK; } @@ -90,22 +103,11 @@ NSResult NSSendNotification(NSMessage *msg) return NS_OK; } -NSResult NSReadCheck(NSMessage *msg) +NSResult NSProviderReadCheck(NSMessage *msg) { OIC_LOG(INFO, INTERFACE_TAG, "Read Sync"); - NSTask * task = (NSTask*) OICMalloc(sizeof(NSTask)); - if (!task) - { - OIC_LOG(ERROR, LISTENER_TAG, PCF("Fail to allocate memory")); - return NS_ERROR; - } - - task->nextTask = NULL; - task->taskData = msg; - task->taskType = TASK_SEND_READ; - - NSPushQueue(NOTIFICATION_SCHEDULER, task->taskType, task); + NSPushQueue(NOTIFICATION_SCHEDULER, TASK_SEND_READ, msg); return NS_OK; } @@ -114,11 +116,19 @@ NSResult NSAccept(NSConsumer *consumer, bool accepted) { OIC_LOG(INFO, INTERFACE_TAG, "Response Acceptance"); - NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_ALLOW, consumer); + if(accepted) + { + NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_ALLOW, consumer); + } + else + { + NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_DENY, consumer); + } return NS_OK; } +/* NSResult NSGetConsumerList(uint8_t *list, uint32_t size) { OIC_LOG(INFO, INTERFACE_TAG, "Get consumer list"); @@ -127,13 +137,13 @@ NSResult NSGetConsumerList(uint8_t *list, uint32_t size) size = NSGetNumberOfObsList(); return NS_OK; -} +}*/ void * NSResponseSchedule(void * ptr) { if (ptr == NULL) { - OIC_LOG(ERROR, INTERFACE_TAG, "Fail to response to User"); + OIC_LOG(INFO, INTERFACE_TAG, "Init NSResponseSchedule"); } while (NSIsRunning[RESPONSE_SCHEDULER]) @@ -153,8 +163,9 @@ void * NSResponseSchedule(void * ptr) printf("before - TASK_CB_SUBSCRIPTION :\n"); OCEntityHandlerRequest * request = (OCEntityHandlerRequest*)node->taskData; NSConsumer consumer; + consumer.mId = strdup(request->devAddr.addr); - int * obId = (int *) malloc (sizeof(int)); + int * obId = (int *) OICMalloc(sizeof(int)); *obId = request->obsInfo.obsId; consumer.mUserData = obId; @@ -165,7 +176,7 @@ void * NSResponseSchedule(void * ptr) case TASK_CB_SYNC: { NSSync * sync = (NSSync*)node->taskData; - NSSyncCb(NULL, sync); + NSSyncCb(sync); break; } default: diff --git a/service/notification/src/provider/NSProviderListener.c b/service/notification/src/provider/NSProviderListener.c index 35edddf..7c59380 100644 --- a/service/notification/src/provider/NSProviderListener.c +++ b/service/notification/src/provider/NSProviderListener.c @@ -31,6 +31,8 @@ OCEntityHandlerResult NSEntityHandlerNotificationCb(OCEntityHandlerFlag flag, { }, { 0 }, false }; + (void)callback; + // Validate pointer if (!entityHandlerRequest) { @@ -53,7 +55,7 @@ OCEntityHandlerResult NSEntityHandlerNotificationCb(OCEntityHandlerFlag flag, { OIC_LOG (INFO, LISTENER_TAG, "Received OC_REST_GET from client"); - NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_POLICY, entityHandlerRequest); + NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_POLICY, (void *)entityHandlerRequest); ehResult = OC_EH_OK; } @@ -150,6 +152,8 @@ OCEntityHandlerResult NSEntityHandlerMessageCb(OCEntityHandlerFlag flag, { }, { 0 }, false }; + (void)callback; + // Validate pointer if (!entityHandlerRequest) { @@ -272,6 +276,8 @@ OCEntityHandlerResult NSEntityHandlerSyncCb(OCEntityHandlerFlag flag, { }, { 0 }, false }; + (void)callback; + // Validate pointer if (!entityHandlerRequest) { diff --git a/service/notification/src/provider/NSProviderListener.h b/service/notification/src/provider/NSProviderListener.h index aabb82a..152dcca 100644 --- a/service/notification/src/provider/NSProviderListener.h +++ b/service/notification/src/provider/NSProviderListener.h @@ -28,6 +28,7 @@ #include "NSStructs.h" #include "NSConstants.h" #include "NSProviderSystem.h" +#include "NSProviderScheduler.h" #include "cautilinterface.h" OCEntityHandlerResult NSEntityHandlerNotificationCb(OCEntityHandlerFlag flag, diff --git a/service/notification/src/provider/NSProviderNotification.c b/service/notification/src/provider/NSProviderNotification.c index 98c23cb..3c31b98 100644 --- a/service/notification/src/provider/NSProviderNotification.c +++ b/service/notification/src/provider/NSProviderNotification.c @@ -20,22 +20,13 @@ #include "NSProviderNotification.h" -NSResult NSGetObsIdList(OCObservationId *list) +NSResult NSInitMessageList() { - // // TODO: return white list - - list = NULL; - + messageList = NSCacheCreate(); + messageList->cacheType = NS_PROVIDER_CACHE_MESSAGE; return NS_OK; } -uint32_t NSGetNumberOfObsList() -{ - // TODO: return # of list - - return 0; -} - NSResult NSGetMessagePayload(NSMessage *msg, OCRepPayload** msgPayload) { @@ -79,6 +70,9 @@ NSResult NSSendMessage(NSMessage *msg) int i; // Set Resource and get resource handle OCResourceHandle rHandle; + OCObservationId obArray[255] = + { 0, }; + int obCount = 0; if (NSPutMessageResource(msg, &rHandle) != NS_OK) { OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Fail to put notification resource")); @@ -87,11 +81,9 @@ NSResult NSSendMessage(NSMessage *msg) // Send Notification to subscribers - NSWhiteList * whiteList = NSProviderReadCache(NS_CONSUMER_WHITELIST, 0); - - if(whiteList->size == 0) + if (consumerSubList->head == NULL) { - printf("printf - no observers\n"); + printf("printf - no observers (consumerSubList->head == NULL)\n"); OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("no observers")); return NS_ERROR; } @@ -105,28 +97,51 @@ NSResult NSSendMessage(NSMessage *msg) return NS_ERROR; } - for(i = 0; i < whiteList->size; ++i) + printf("printf - no observers - 3\n"); + NSCacheElement * it = consumerSubList->head; + printf("printf - no observers - 4\n"); + while (it) + { + printf("printf - no observers - 5\n"); + NSCacheSubData * subData = (NSCacheSubData *) it->data; + printf("NS_ subData->id = %s\n", subData->id); + printf("NS_ subData->messageId = %d\n", subData->messageObId); + printf("NS_ subData->obID = %d\n", subData->syncObId); + printf("NS_ subData->isWhite = %d\n", subData->isWhite); + + printf("printf - no observers - 6\n"); + if (subData->isWhite) + { + printf("printf - no observers - 7\n"); + obArray[obCount++] = subData->messageObId; + printf("printf - no observers - 8\n"); + } + + it = it->next; + } + printf("printf - no observers - 9\n"); + for (i = 0; i < obCount; ++i) { printf("NS_ -------------------------------------------------------message\n"); - printf("NS_ whiteList->idList[%d] = %d\n", i, whiteList->idList[i]); + printf("NS_ whiteList->idList[%d] = %d\n", i, obArray[i]); printf("NS_ -------------------------------------------------------message\n"); } - + printf("printf - no observers - 10\n"); // Notify message to subscribers - OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, whiteList->idList, whiteList->size, payload, OC_LOW_QOS); + OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray, obCount, payload, + OC_LOW_QOS); printf("NS_ message ocstackResult = %d\n", ocstackResult); - if (ocstackResult != OC_STACK_OK) { - printf("printf - no observers - 3\n"); + printf("printf - no observers - 11\n"); OIC_LOG(ERROR, NOTIFICATION_TAG, "fail to send message"); OCRepPayloadDestroy(payload); return NS_ERROR; } - printf("printf - no observers - 4\n"); + printf("printf - no observers - 12\n"); OCRepPayloadDestroy(payload); return NS_OK; @@ -135,8 +150,10 @@ NSResult NSSendMessage(NSMessage *msg) NSResult NSSendSync(NSSync *sync) { OIC_LOG(DEBUG, NOTIFICATION_TAG, "Send Notification Sync to consumer"); + + OCObservationId obArray[255] = { 0, }; + int obCount = 0; int i; - // Set Resource and get resource handle OCResourceHandle rHandle; if (NSPutSyncResource(sync, &rHandle) != NS_OK) @@ -145,12 +162,18 @@ NSResult NSSendSync(NSSync *sync) return NS_ERROR; } - NSWhiteList * whiteList = NSProviderReadCache(NS_CONSUMER_WHITELIST, 1); + NSCacheElement * it = consumerSubList->head; - if(whiteList->size == 0) + while (it) { - OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("no observers")); - return NS_ERROR; + NSCacheSubData * subData = (NSCacheSubData *) it->data; + if (subData->isWhite) + { + obArray[obCount++] = subData->syncObId; + } + + it = it->next; + } // Send sync to subscribers @@ -164,14 +187,15 @@ NSResult NSSendSync(NSSync *sync) // Notify sync to subscribers - for(i = 0; i < whiteList->size; ++i) + for (i = 0; i < obCount; ++i) { - printf("NS_ -------------------------------------------------------sync\n"); - printf("NS_ whiteList->idList[%d] = %d\n", i, whiteList->idList[i]); - printf("NS_ -------------------------------------------------------sync\n"); + printf("NS_ -------------------------------------------------------message\n"); + printf("NS_ whiteList->idList[%d] = %d\n", i, obArray[i]); + printf("NS_ -------------------------------------------------------message\n"); } - OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, whiteList->idList, whiteList->size, payload, OC_LOW_QOS); + OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray, + obCount, payload, OC_LOW_QOS); printf("NS_ sync ocstackResult = %d\n", ocstackResult); if (ocstackResult != OC_STACK_OK) diff --git a/service/notification/src/provider/NSProviderNotification.h b/service/notification/src/provider/NSProviderNotification.h index e8afe37..e51b493 100644 --- a/service/notification/src/provider/NSProviderNotification.h +++ b/service/notification/src/provider/NSProviderNotification.h @@ -22,20 +22,22 @@ #define _NS_PROVIDER_NOTIFICATION_H_ #include +#include "../common/NSCacheAdapter.h" #include "logger.h" -#include "NSQueueScheduler.h" +#include "NSProviderScheduler.h" #include "NSProviderListener.h" #include "NSProviderResource.h" -#include "NSProviderCacheAdapter.h" +#include "NSProviderSubscription.h" + +NSCacheList * messageList; NSResult NSRegisterResource(); -NSResult NSGetObsIdList(OCObservationId *list); +NSResult NSInitMessageList(); -uint32_t NSGetNumberOfObsList(); +NSResult NSPutNotificationMessage(NSMessage *, OCResourceHandle *); -NSResult NSPutNotificationMessage(NSMessage *msg, OCResourceHandle *rHandle); +NSResult NSSendNotificationMessage(NSMessage *); -NSResult NSSendNotificationMessage(NSMessage *msg); #endif /* _NS_PROVIDER_NOTIFICATION_H_ */ diff --git a/service/notification/src/common/NSQueueScheduler.c b/service/notification/src/provider/NSProviderScheduler.c old mode 100644 new mode 100755 similarity index 95% rename from service/notification/src/common/NSQueueScheduler.c rename to service/notification/src/provider/NSProviderScheduler.c index 89b478a..af3a90c --- a/service/notification/src/common/NSQueueScheduler.c +++ b/service/notification/src/provider/NSProviderScheduler.c @@ -18,7 +18,7 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -#include "NSQueueScheduler.h" +#include "NSProviderScheduler.h" pthread_t NSThread[THREAD_COUNT]; pthread_mutex_t NSMutex[THREAD_COUNT]; diff --git a/service/notification/src/common/NSQueueScheduler.h b/service/notification/src/provider/NSProviderScheduler.h old mode 100644 new mode 100755 similarity index 90% rename from service/notification/src/common/NSQueueScheduler.h rename to service/notification/src/provider/NSProviderScheduler.h index 906e5e2..6c9bf1d --- a/service/notification/src/common/NSQueueScheduler.h +++ b/service/notification/src/provider/NSProviderScheduler.h @@ -18,8 +18,8 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -#ifndef _QUEUE_SCHEDULER_H_ -#define _QUEUE_SCHEDULER_H_ +#ifndef _PROVIDER_SCHEDULER_H_ +#define _PROVIDER_SCHEDULER_H_ #include #include @@ -45,9 +45,10 @@ extern void * NSDiscoverySchedule(void *ptr); extern void * NSSubScriptionSchedule(void *ptr); extern void * NSNotificationSchedule(void *ptr); +void NSSetList(); bool NSInitScheduler(); bool NSStartScheduler(); bool NSStopScheduler(); void NSPushQueue(NSSchedulerType, NSTaskType, void*); -#endif /* _QUEUE_SCHEDULER_H_ */ +#endif /* _PROVIDER_SCHEDULER_H_ */ diff --git a/service/notification/src/provider/NSProviderSubscription.c b/service/notification/src/provider/NSProviderSubscription.c index ac58034..a0aae6d 100644 --- a/service/notification/src/provider/NSProviderSubscription.c +++ b/service/notification/src/provider/NSProviderSubscription.c @@ -20,6 +20,13 @@ #include "NSProviderSubscription.h" +NSResult NSInitSubscriptionList() +{ + consumerSubList = NSCacheCreate(); + consumerSubList->cacheType = NS_PROVIDER_CACHE_SUBSCRIBER; + return NS_OK; +} + NSResult NSSetSubscriptionAcceptPolicy(NSAccessPolicy policy) { if (policy == NS_ACCEPTER_PROVIDER) @@ -47,7 +54,8 @@ NSResult NSSendAccessPolicyResponse(OCEntityHandlerRequest *entityHandlerRequest // put notification resource OCResourceHandle notificationResourceHandle; - if(NSPutNotificationResource(NSGetSubscriptionAccepter(), ¬ificationResourceHandle) != NS_OK) + if (NSPutNotificationResource(NSGetSubscriptionAccepter(), ¬ificationResourceHandle) + != NS_OK) { OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("Failed to put notification resource")); return NS_ERROR; @@ -77,8 +85,7 @@ NSResult NSSendAccessPolicyResponse(OCEntityHandlerRequest *entityHandlerRequest response.resourceHandle = entityHandlerRequest->resource; response.persistentBufferFlag = 0; response.ehResult = OC_EH_OK; - response.payload = (OCPayload *)payload; - + response.payload = (OCPayload *) payload; // Send Response if (OCDoResponse(&response) != OC_STACK_OK) @@ -91,7 +98,7 @@ NSResult NSSendAccessPolicyResponse(OCEntityHandlerRequest *entityHandlerRequest return NS_OK; } -void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, int state) +void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, NSResourceType resourceType) { OIC_LOG(INFO, SUBSCRIPTION_TAG, "Start to subscription process"); @@ -99,17 +106,29 @@ void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, int stat printf("state = 0 \n"); // write consumer info to cache - if(state == 0) + + if (resourceType == NS_RESOURCE_MESSAGE) { + int obId = entityHandlerRequest->obsInfo.obsId; + + NSCacheElement * element = (NSCacheElement *) malloc(sizeof(NSCacheElement)); + + NSCacheSubData * subData = (NSCacheSubData *) malloc(sizeof(NSCacheSubData)); + subData->id = OICStrdup(entityHandlerRequest->devAddr.addr); + subData->isWhite = false; + subData->messageObId = entityHandlerRequest->obsInfo.obsId; + subData->syncObId = 0; - printf("state = 0 - 1\n"); + element->data = (void*) subData; + element->next = NULL; - if (NSProviderWriteToCache(NS_CONSUMER_BLACKLIST, entityHandlerRequest->obsInfo.obsId, state) != NS_OK) + printf("NS_ message ob Id = %d\n", subData->messageObId); + + if (NSCacheWrite(consumerSubList, element) != NS_OK) { - OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("fail to write consumer black list")); + printf("Cache Write Error\n"); } - printf("state = 0 - 2\n"); - // check access policy + if (NSGetSubscriptionAccepter() == NS_ACCEPTER_PROVIDER) { // OCDevAddr --> NSConsumer @@ -123,14 +142,28 @@ void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, int stat NSSendSubscriptionResponse(entityHandlerRequest, NS_ACCEPTER_CONSUMER, true); } } - else if(state == 1) + else if (resourceType == NS_RESOURCE_SYNC) { - printf("state = 1 - 0\n"); - if (NSProviderWriteToCache(NS_CONSUMER_WHITELIST, entityHandlerRequest->obsInfo.obsId, state) != NS_OK) + int obId = entityHandlerRequest->obsInfo.obsId; + + NSCacheElement * element = (NSCacheElement *) malloc(sizeof(NSCacheElement)); + + NSCacheSubData * subData = (NSCacheSubData *) malloc(sizeof(NSCacheSubData)); + printf("NS_ entityHandlerRequest->devAddr.addr = %s\n", entityHandlerRequest->devAddr.addr); + subData->id = OICStrdup(entityHandlerRequest->devAddr.addr); + subData->isWhite = false; + subData->syncObId = entityHandlerRequest->obsInfo.obsId; + subData->messageObId = 0; + + element->data = (void*) subData; + element->next = NULL; + + printf("NS_ sync ob Id = %d\n", subData->syncObId); + + if (NSCacheWrite(consumerSubList, element) != NS_OK) { - OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("fail to write consumer white list")); + printf("Cache Write Error\n"); } - printf("state = 1 - 1\n"); } } @@ -141,9 +174,20 @@ void NSHandleUnsubscription(OCEntityHandlerRequest *entityHandlerRequest) // ProcessObserveUnregister (entityHandlerRequest); // write consumer info to cache - if (NSProviderWriteToCache(NS_CONSUMER_BLACKLIST, entityHandlerRequest->devAddr) != NS_OK) + int obId = entityHandlerRequest->obsInfo.obsId; + + NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement)); + NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData)); + subData->id = OICStrdup(entityHandlerRequest->devAddr.addr); + subData->isWhite = false; + subData->messageObId = entityHandlerRequest->obsInfo.obsId; + + element->data = (void*) subData; + element->next = NULL; + + if (NSCacheWrite(consumerSubList, element) != NS_OK) { - OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to write consumer black list"); + OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to write consumer white list"); } } @@ -155,7 +199,7 @@ void NSAskAcceptanceToUser(OCEntityHandlerRequest *entityHandlerRequest) // one queue is required for response callback to ask subscription allowance } -NSResult NSSendResponse(int observeId, bool accepted) +NSResult NSSendResponse(const char * id, bool accepted) { OCRepPayload* payload = OCRepPayloadCreate(); @@ -176,7 +220,11 @@ NSResult NSSendResponse(int observeId, bool accepted) OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_ID, "0000-0000-0000-0000"); OCRepPayloadSetPropBool(payload, NS_ATTRIBUTE_ACCPETANCE, accepted); - if (OCNotifyListOfObservers(rHandle, &observeId, 1, payload, OC_HIGH_QOS) != OC_STACK_OK) + NSCacheElement * element = NSCacheRead(consumerSubList, id); + NSCacheSubData * subData = element->data; + + if (OCNotifyListOfObservers(rHandle, &subData->messageObId, 1, payload, OC_HIGH_QOS) + != OC_STACK_OK) { OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to send Acceptance"); OCRepPayloadDestroy(payload); @@ -204,7 +252,19 @@ NSResult NSSendSubscriptionResponse(OCEntityHandlerRequest *entityHandlerRequest printf("accepted 1 \n"); if (accepted) { - if (NSProviderWriteToCache(NS_CONSUMER_WHITELIST, entityHandlerRequest->obsInfo.obsId) != NS_OK) + int obId = entityHandlerRequest->obsInfo.obsId; + + NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement)); + + NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData)); + subData->id = OICStrdup(entityHandlerRequest->devAddr.addr); + subData->isWhite = true; + subData->messageObId = entityHandlerRequest->obsInfo.obsId; + + element->data = (void*) subData; + element->next = NULL; + + if (NSCacheWrite(consumerSubList, element) != NS_OK) { OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to write consumer white list"); } @@ -244,7 +304,8 @@ void * NSSubScriptionSchedule(void *ptr) case TASK_RECV_SUBSCRIPTION: printf("before TASK_RECV_SUBSCRIPTION:\n"); - NSHandleSubscription((OCEntityHandlerRequest*) node->taskData, 0); + NSHandleSubscription((OCEntityHandlerRequest*) node->taskData, + NS_RESOURCE_MESSAGE); printf("after TASK_RECV_SUBSCRIPTION:\n"); break; @@ -255,27 +316,42 @@ void * NSSubScriptionSchedule(void *ptr) case TASK_SEND_ALLOW: { - NSConsumer * consumer = (NSConsumer *)node->taskData; - int * pObId = (int *)consumer->mUserData; + NSConsumer * consumer = (NSConsumer *) node->taskData; + int * pObId = (int *) consumer->mUserData; + + NSCacheSubData * subData = (NSCacheMsgData *) OICMalloc(sizeof(NSCacheSubData)); + subData->id = OICStrdup(consumer->mId); + subData->isWhite = true; + subData->messageObId = 0; + subData->syncObId = 0; + + NSCacheUpdateSubScriptionState(consumerSubList, subData); printf("observer ID = %d\n", *pObId); - if (NSProviderWriteToCache(NS_CONSUMER_WHITELIST, *pObId, 0) != NS_OK) - { - OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to write consumer white list"); - } - NSSendResponse(*pObId, true); + NSSendResponse(consumer->mId, true); break; } case TASK_SEND_DENY: + { + NSConsumer * consumer = (NSConsumer *) node->taskData; + int * pObId = (int *) consumer->mUserData; -// NSSendSubscriptionResponse((OCEntityHandlerRequest*) node->taskData, -// NS_ACCEPTER_PROVIDER, false); - break; + NSCacheSubData * subData = (NSCacheMsgData *) OICMalloc(sizeof(NSCacheSubData)); + subData->id = OICStrdup(consumer->mId); + subData->isWhite = false; + subData->messageObId = 0; + subData->syncObId = 0; + NSCacheUpdateSubScriptionState(consumerSubList, subData); + printf("observer ID = %d\n", *pObId); + NSSendResponse(consumer->mId, false); + break; + } case TASK_SYNC_SUBSCRIPTION: - NSHandleSubscription((OCEntityHandlerRequest*) node->taskData, 1); + NSHandleSubscription((OCEntityHandlerRequest*) node->taskData, + NS_RESOURCE_SYNC); break; } diff --git a/service/notification/src/provider/NSProviderSubscription.h b/service/notification/src/provider/NSProviderSubscription.h index 847e4af..60813fb 100644 --- a/service/notification/src/provider/NSProviderSubscription.h +++ b/service/notification/src/provider/NSProviderSubscription.h @@ -26,15 +26,18 @@ #include "ocpayload.h" #include "NSCommon.h" #include "NSConstants.h" -#include "NSQueueScheduler.h" +#include "NSProviderScheduler.h" #include "NSProviderResource.h" +#include "NSCacheAdapter.h" -static NSAccessPolicy NSSubscriptionAccepter; +NSAccessPolicy NSSubscriptionAccepter; +NSCacheList * consumerSubList; +NSResult NSInitSubscriptionList(); NSResult NSSetSubscriptionAcceptPolicy(NSAccessPolicy policy); int NSGetSubscriptionAccepter(); NSResult NSSendAccessPolicyResponse(OCEntityHandlerRequest *entityHandlerRequest); -void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, int state); +void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, NSResourceType resourceType); void NSHandleUnsubscription(OCEntityHandlerRequest *entityHandlerRequest); void NSAskAcceptanceToUser(OCEntityHandlerRequest *entityHandlerRequest); NSResult NSSendSubscriptionResponse(OCEntityHandlerRequest *entityHandlerRequest, diff --git a/service/notification/src/provider/cache/linux/NSProviderMemoryCache.c b/service/notification/src/provider/cache/linux/NSProviderMemoryCache.c new file mode 100755 index 0000000..7e41915 --- /dev/null +++ b/service/notification/src/provider/cache/linux/NSProviderMemoryCache.c @@ -0,0 +1,361 @@ +//****************************************************************** +// +// 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 "NSProviderMemoryCache.h" + +NSCacheList * NSCacheCreate() +{ + pthread_mutex_lock(&NSCacheMutex); + NSCacheList * newList = (NSCacheList *) OICMalloc(sizeof(NSCacheList)); + if (!newList) + { + pthread_mutex_unlock(&NSCacheMutex); + return NULL; + } + + newList->head = newList->tail = NULL; + + pthread_mutex_unlock(&NSCacheMutex); + + return newList; +} + +NSCacheElement * NSCacheRead(NSCacheList * list, char * findId) +{ + pthread_mutex_lock(&NSCacheMutex); + NSCacheElement * iter = list->head; + NSCacheElement * next = NULL; + NSCacheType type = list->cacheType; + + printf("NS_ findId = %s\n", findId); + + while (iter) + { + next = iter->next; + + printf("NS_ findId2 = %s\n", findId); + pthread_mutex_unlock(&NSCacheMutex); + + if (NSProviderCompareIdCacheData(type, iter->data, findId)) + { + pthread_mutex_unlock(&NSCacheMutex); + return iter; + } + + iter = next; + } + + pthread_mutex_unlock(&NSCacheMutex); + + return NULL; +} + +NSResult NSCacheUpdateSubScriptionState(NSCacheList * list, NSCacheSubData * subData) +{ + pthread_mutex_lock(&NSCacheMutex); + + NSCacheType type = list->cacheType; + + printf("NS_ NSCacheWrite\n"); + + if (subData == NULL) + { + printf("NS_ NSCacheWrite - newObj is NULL\n"); + pthread_mutex_unlock(&NSCacheMutex); + return NS_ERROR; + } + + printf("NS_ findId0 - 0 = %s\n", subData->id); + pthread_mutex_unlock(&NSCacheMutex); + NSCacheElement * it = NSCacheRead(list, subData->id); + pthread_mutex_lock(&NSCacheMutex); + printf("NS_ findId0 -1 = %s\n", subData->id); + if (it) + { + printf("NS_ findId1 = %s\n", subData->id); + NSCacheSubData * itData = (NSCacheSubData *) it->data; + printf("NS_ findId2 = %s\n", subData->id); + if (strcmp(itData->id, subData->id) == 0) + { + printf("NS_ findId3_subdata_id = %s\n", subData->id); + printf("NS_ findId3_subdata_messageobId = %d\n", subData->messageObId); + printf("NS_ findId3_subdata_syncobId = %d\n", subData->syncObId); + printf("NS_ findId3_subdata_isWhite = %d\n", subData->isWhite); + + printf("NS_ findId3_itdata_id = %s\n", itData->id); + printf("NS_ findId3_itdata_messageobId = %d\n", itData->messageObId); + printf("NS_ findId3_itdata_isWhite = %d\n", itData->isWhite); + + itData->isWhite = subData->isWhite; + + printf("_NS : PROVIDER_CACHE_SUBSCRIBER UPDATE\n"); + + pthread_mutex_unlock(&NSCacheMutex); + return NS_OK; + + } + } + pthread_mutex_unlock(&NSCacheMutex); + return NS_OK; +} + +NSResult NSCacheWrite(NSCacheList * list, NSCacheElement * newObj) +{ + pthread_mutex_lock(&NSCacheMutex); + + NSCacheType type = list->cacheType; + + printf("NS_ NSCacheWrite\n"); + + if (newObj == NULL) + { + printf("NS_ NSCacheWrite - newObj is NULL\n"); + pthread_mutex_unlock(&NSCacheMutex); + return NS_ERROR; + } + + if (type == NS_PROVIDER_CACHE_SUBSCRIBER) + { + NSCacheSubData * subData = (NSCacheSubData *) newObj->data; + + printf("NS_ findId0 - 0 = %s\n", subData->id); + pthread_mutex_unlock(&NSCacheMutex); + NSCacheElement * it = NSCacheRead(list, subData->id); + pthread_mutex_lock(&NSCacheMutex); + printf("NS_ findId0 -1 = %s\n", subData->id); + if (it) + { + printf("NS_ findId1 = %s\n", subData->id); + NSCacheSubData * itData = (NSCacheSubData *) it->data; + printf("NS_ findId2 = %s\n", subData->id); + if (strcmp(itData->id, subData->id) == 0) + { + printf("NS_ findId3_subdata_id = %s\n", subData->id); + printf("NS_ findId3_subdata_messageobId = %d\n", subData->messageObId); + printf("NS_ findId3_subdata_syncobId = %d\n", subData->syncObId); + printf("NS_ findId3_subdata_isWhite = %d\n", subData->isWhite); + + printf("NS_ findId3_itdata_id = %s\n", itData->id); + printf("NS_ findId3_itdata_messageobId = %d\n", itData->messageObId); + printf("NS_ findId3_itdata_isWhite = %d\n", itData->isWhite); + if (itData->messageObId == 0) + { + itData->messageObId = subData->messageObId; + } + + if (itData->syncObId == 0) + { + itData->syncObId = subData->syncObId; + } + + //itData->isWhite = subData->isWhite; + + printf("_NS : PROVIDER_CACHE_SUBSCRIBER UPDATE\n"); + + pthread_mutex_unlock(&NSCacheMutex); + return NS_OK; + + } + } + + } + else if (type == NS_PROVIDER_CACHE_MESSAGE) + { + NSCacheMsgData * msgData = (NSCacheMsgData *) newObj->data; + + NSCacheElement * it = NSCacheRead(list, msgData->id); + if (it) + { + NSCacheMsgData * itData = (NSCacheMsgData *) it->data; + + if (strcmp(itData->id, msgData->id) == 0) + { + + itData->messageType = msgData->messageType; + printf("_NS : PROVIDER_CACHE_MESSAGE UPDATE\n"); + pthread_mutex_unlock(&NSCacheMutex); + return NS_OK; + + } + } + } + + printf("NS_ berfore list->head == NULL\n"); + if (list->head == NULL) + { + list->head = list->tail = newObj; + printf("NS_ list->head == NULL\n"); + pthread_mutex_unlock(&NSCacheMutex); + return NS_OK; + } + + list->tail = list->tail->next = newObj; + printf("NS_ list->head == not NULL\n"); + pthread_mutex_unlock(&NSCacheMutex); + return NS_OK; +} + +NSResult NSCacheDelete(NSCacheList * list, char * delId) +{ + + pthread_mutex_lock(&NSCacheMutex); + NSCacheElement * prev = list->head; + NSCacheElement * del = list->head; + + NSCacheType type = list->cacheType; + + if (NSProviderCompareIdCacheData(type, del->data, delId)) + { + if (del == list->head) // first object + { + if (del == list->tail) // first object (one object) + list->tail = del->next; + + list->head = del->next; + + NSProviderDeleteCacheData(type, del->data); + OICFree(del); + pthread_mutex_unlock(&NSCacheMutex); + return NS_OK; + } + } + + del = del->next; + while (del) + { + if (NSProviderCompareIdCacheData(type, del->data, delId)) + { + if (del == list->tail) // delete object same to last object + list->tail = prev; + + prev->next = del->next; + NSProviderDeleteCacheData(type, del->data); + OICFree(del); + pthread_mutex_unlock(&NSCacheMutex); + return NS_OK; + } + + prev = del; + del = del->next; + } + pthread_mutex_unlock(&NSCacheMutex); + return NS_OK; +} + +NSResult NSCacheDestroy(NSCacheList * list) +{ + NSCacheElement * iter = list->head; + NSCacheElement * next = NULL; + + NSCacheType type = list->cacheType; + + while (iter) + { + next = (NSCacheElement *) iter->next; + + NSProviderDeleteCacheData(type, iter->data); + OICFree(iter); + + iter = next; + } + + OICFree(list); + + return NS_OK; +} + +bool NSProviderCompareIdCacheData(NSCacheType type, void * data, char * id) +{ + if (data == NULL) + { + return false; + } + + if (type == NS_PROVIDER_CACHE_SUBSCRIBER) + { + NSCacheSubData * subData = (NSCacheSubData *) data; + + printf("NS_ subdata->id = %s\n", subData->id); + printf("NS_ id = %s\n", id); + + if (strcmp(subData->id, id) == 0) + { + return true; + } + return false; + + } + else if (type == NS_PROVIDER_CACHE_MESSAGE) + { + NSCacheMsgData * msgData = (NSCacheMsgData *) data; + + if (strcmp(msgData->id, id) == 0) + { + return true; + } + + return false; + } + + return false; +} + +NSResult NSProviderDeleteCacheData(NSCacheType type, void * data) +{ + if (data == NULL) + { + return NS_OK; + } + + if (type == NS_PROVIDER_CACHE_SUBSCRIBER) + { + NSCacheSubData * subData = (NSCacheSubData *) data; + if (subData->id) + { + OICFree(subData->id); + subData->id = NULL; + } + + OICFree(subData); + + return NS_OK; + } + else if (type == NS_PROVIDER_CACHE_MESSAGE) + { + NSCacheMsgData * msgData = (NSCacheMsgData *) data; + + if (msgData->id) + { + OICFree(msgData->id); + msgData->id = NULL; + } + + if (msgData->nsMessage) + { + NSFreeMessage(msgData->nsMessage); + } + + OICFree(msgData); + + return NS_OK; + } + + return NS_OK; +} + diff --git a/service/notification/src/provider/NSProviderCacheAdapter.h b/service/notification/src/provider/cache/linux/NSProviderMemoryCache.h old mode 100644 new mode 100755 similarity index 68% rename from service/notification/src/provider/NSProviderCacheAdapter.h rename to service/notification/src/provider/cache/linux/NSProviderMemoryCache.h index 32b2c21..4e96aff --- a/service/notification/src/provider/NSProviderCacheAdapter.h +++ b/service/notification/src/provider/cache/linux/NSProviderMemoryCache.h @@ -21,29 +21,24 @@ #ifndef _NS_PROVIDER_CACHEADAPTER__H_ #define _NS_PROVIDER_CACHEADAPTER__H_ -#include "logger.h" +#include #include +#include +#include "logger.h" #include "ocstack.h" #include "ocpayload.h" #include "NSStructs.h" #include "NSConstants.h" +#include "NSCacheAdapter.h" + +NSResult NSProviderDeleteCacheData(NSCacheType, void *); -typedef struct _messageData -{ - NSMessage * msg; - uint8_t state; - uint8_t size; -}NSMessageData; +bool NSProviderCompareIdCacheData(NSCacheType, void *, char *); -typedef struct _observerList -{ - OCObservationId idList[100]; - uint8_t size; -}NSWhiteList, NSBlackList; +bool NSProviderIsFoundCacheData(NSCacheType, void *, void*); -typedef NSWhiteList NSCommonList; +NSResult NSCacheUpdateSubScriptionState(NSCacheList *, NSCacheSubData *); -NSResult NSProviderWriteToCache(NSCache target, OCObservationId observeId, int state); -NSCommonList * NSProviderReadCache(NSCache target, int state); +pthread_mutex_t NSCacheMutex; -#endif /* _NS_PROVIDER_CACHE__H_ */ +#endif /* _NS_PROVIDER_CACHEADAPTER__H_ */ -- 2.7.4