From b295037eef0e8e0e5be9c604bc51ff4b53ca42f7 Mon Sep 17 00:00:00 2001 From: KIM JungYong Date: Fri, 26 Aug 2016 18:47:25 +0900 Subject: [PATCH] Remove consumer callback for discovered provider. Consumer service has two type of callback within parameter as provider object. One of callback is called when the provider discovered, and others is called the provider changed. But, discovered callback is imprecise called owing to provider policy. So, discovered event is added in provider changed callback. Two of callback is united as changed name of ProviderStateCallback and removed discovered callback. Change-Id: I60eebaf78aaae78a61b3772138b582df3748910c Signed-off-by: KIM JungYong Reviewed-on: https://gerrit.iotivity.org/gerrit/10973 Tested-by: jenkins-iotivity Reviewed-by: Uze Choi Tested-by: Uze Choi --- .../examples/linux/notificationconsumer.c | 2 +- service/notification/include/NSCommon.h | 1 + service/notification/include/NSConsumerInterface.h | 11 +----- .../notification/src/consumer/NSConsumerCommon.c | 40 ++----------------- .../notification/src/consumer/NSConsumerCommon.h | 5 +-- .../src/consumer/NSConsumerCommunication.c | 4 +- .../src/consumer/NSConsumerInterface.c | 3 -- .../consumer/NSConsumerInternalTaskController.c | 8 +++- service/notification/unittest/NSConsumerTest.cpp | 46 ++++++---------------- 9 files changed, 29 insertions(+), 91 deletions(-) diff --git a/service/notification/examples/linux/notificationconsumer.c b/service/notification/examples/linux/notificationconsumer.c index 9d44402..a92ad70 100644 --- a/service/notification/examples/linux/notificationconsumer.c +++ b/service/notification/examples/linux/notificationconsumer.c @@ -20,6 +20,7 @@ #include #include +#include #include "pthread.h" #include "ocstack.h" @@ -138,7 +139,6 @@ int main(void) } NSConsumerConfig cfg; - cfg.discoverCb = onDiscoverNotification; cfg.changedCb = onProviderChanged; cfg.messageCb = onNotificationPosted; cfg.syncInfoCb = onNotificationSync; diff --git a/service/notification/include/NSCommon.h b/service/notification/include/NSCommon.h index 42a4a4a..1c550ed 100644 --- a/service/notification/include/NSCommon.h +++ b/service/notification/include/NSCommon.h @@ -67,6 +67,7 @@ typedef enum eResult */ typedef enum eResponse { + NS_DISCOVERED = 0, NS_ALLOW = 1, NS_DENY = 2, NS_TOPIC = 3, diff --git a/service/notification/include/NSConsumerInterface.h b/service/notification/include/NSConsumerInterface.h index 798b733..35f516c 100644 --- a/service/notification/include/NSConsumerInterface.h +++ b/service/notification/include/NSConsumerInterface.h @@ -35,17 +35,11 @@ extern "C" #include "NSCommon.h" /** - * Invoked when the discovered provider is received - * @param[in] provider Provider who has the notification resource - */ -typedef void (* NSProviderDiscoveredCallback)(NSProvider *); - -/** * Invoked when the provider state is changed * @param[in] provider Provider which has the notification resource * @param[in] response Response which has the provider state */ -typedef void (* NSProviderChangedCallback)(NSProvider *, NSResponse); +typedef void (* NSProviderStateCallback)(NSProvider *, NSResponse); /** * Invoked when the notification message from provider is received @@ -64,8 +58,7 @@ typedef void (* NSSyncInfoReceivedCallback)(NSSyncInfo *); typedef struct { - NSProviderDiscoveredCallback discoverCb; - NSProviderChangedCallback changedCb; + NSProviderStateCallback changedCb; NSMessageReceivedCallback messageCb; NSSyncInfoReceivedCallback syncInfoCb; diff --git a/service/notification/src/consumer/NSConsumerCommon.c b/service/notification/src/consumer/NSConsumerCommon.c index f5af451..51fbaf3 100644 --- a/service/notification/src/consumer/NSConsumerCommon.c +++ b/service/notification/src/consumer/NSConsumerCommon.c @@ -105,46 +105,14 @@ bool NSIsStartedConsumer() return * NSGetBoneIsStartedConsumer(); } -NSProviderDiscoveredCallback * NSGetBoneDiscoverCb() +NSProviderStateCallback * NSGetProviderChangedCb() { - static NSProviderDiscoveredCallback g_discoverCb = NULL; - - return & g_discoverCb; -} - -void NSSetDiscoverProviderCb(NSProviderDiscoveredCallback cb) -{ - * NSGetBoneDiscoverCb() = cb; -} - -NSProviderDiscoveredCallback NSGetDiscoverCb() -{ - return * NSGetBoneDiscoverCb(); -} - -void * NSDiscoveredProviderFunc(void * provider) -{ - NSGetDiscoverCb()((NSProvider *) provider); - - return NULL; -} - -void NSDiscoveredProvider(NSProvider * provider) -{ - NS_VERIFY_NOT_NULL_V(provider); - - NSConsumerThread * thread = NSThreadInit(NSDiscoveredProviderFunc, (void *) provider); - NS_VERIFY_NOT_NULL_V(thread); -} - -NSProviderChangedCallback * NSGetProviderChangedCb() -{ - static NSProviderChangedCallback g_changedCb = NULL; + static NSProviderStateCallback g_changedCb = NULL; return & g_changedCb; } -void NSSetProviderChangedCb(NSProviderChangedCallback cb) +void NSSetProviderChangedCb(NSProviderStateCallback cb) { *(NSGetProviderChangedCb()) = cb; } @@ -398,7 +366,7 @@ void NSRemoveTopicLL(NSTopicLL * topicHead) while (iter) { - following = (NSTopicLL *) iter->next; + following = iter->next; NSRemoveTopicNode(iter); diff --git a/service/notification/src/consumer/NSConsumerCommon.h b/service/notification/src/consumer/NSConsumerCommon.h index 9c02e8d..db60ea0 100644 --- a/service/notification/src/consumer/NSConsumerCommon.h +++ b/service/notification/src/consumer/NSConsumerCommon.h @@ -106,10 +106,7 @@ typedef struct bool NSIsStartedConsumer(); void NSSetIsStartedConsumer(bool setValue); -void NSSetDiscoverProviderCb(NSProviderDiscoveredCallback cb); -void NSDiscoveredProvider(NSProvider * provider); - -void NSSetProviderChangedCb(NSProviderChangedCallback cb); +void NSSetProviderChangedCb(NSProviderStateCallback cb); void NSProviderChanged(NSProvider * provider, NSResponse response); void NSSetMessagePostedCb(NSMessageReceivedCallback cb); diff --git a/service/notification/src/consumer/NSConsumerCommunication.c b/service/notification/src/consumer/NSConsumerCommunication.c index f9ca253..1e15e21 100644 --- a/service/notification/src/consumer/NSConsumerCommunication.c +++ b/service/notification/src/consumer/NSConsumerCommunication.c @@ -29,8 +29,6 @@ #define NS_SYNC_URI "/notification/sync" -static unsigned long NS_MESSAGE_ACCEPTANCE = 1; - NSMessage * NSCreateMessage_internal(uint64_t msgId, const char * providerId); NSSyncInfo * NSCreateSyncInfo_consumer(uint64_t msgId, const char * providerId, NSSyncType state); @@ -191,7 +189,7 @@ OCStackApplicationResult NSConsumerMessageListener( NSTaskType type = TASK_CONSUMER_RECV_MESSAGE; - if (newNoti->messageId == NS_MESSAGE_ACCEPTANCE || newNoti->messageId == NS_DENY) + if (newNoti->messageId == NS_ALLOW || newNoti->messageId == NS_DENY) { NS_LOG(DEBUG, "Receive subscribe result"); type = TASK_CONSUMER_RECV_PROVIDER_CHANGED; diff --git a/service/notification/src/consumer/NSConsumerInterface.c b/service/notification/src/consumer/NSConsumerInterface.c index a6d4828..e4a5328 100644 --- a/service/notification/src/consumer/NSConsumerInterface.c +++ b/service/notification/src/consumer/NSConsumerInterface.c @@ -36,12 +36,10 @@ NSResult NSStartConsumer(NSConsumerConfig config) bool isStartedConsumer = NSIsStartedConsumer(); NS_VERIFY_NOT_NULL(isStartedConsumer == false ? (void *) 1 : NULL, NS_OK); - NS_VERIFY_NOT_NULL(config.discoverCb, NS_ERROR); NS_VERIFY_NOT_NULL(config.messageCb, NS_ERROR); NS_VERIFY_NOT_NULL(config.syncInfoCb, NS_ERROR); NS_VERIFY_NOT_NULL(config.changedCb, NS_ERROR); - NSSetDiscoverProviderCb(config.discoverCb); NSSetMessagePostedCb(config.messageCb); NSSetNotificationSyncCb(config.syncInfoCb); NSSetProviderChangedCb(config.changedCb); @@ -59,7 +57,6 @@ NSResult NSStopConsumer() bool isStartedConsumer = NSIsStartedConsumer(); NS_VERIFY_NOT_NULL(isStartedConsumer == true ? (void *) 1 : NULL, NS_ERROR); - NSSetDiscoverProviderCb(NULL); NSSetMessagePostedCb(NULL); NSSetNotificationSyncCb(NULL); NSSetProviderChangedCb(NULL); diff --git a/service/notification/src/consumer/NSConsumerInternalTaskController.c b/service/notification/src/consumer/NSConsumerInternalTaskController.c index 505d414..9174aec 100644 --- a/service/notification/src/consumer/NSConsumerInternalTaskController.c +++ b/service/notification/src/consumer/NSConsumerInternalTaskController.c @@ -222,7 +222,11 @@ void NSCancelAllSubscription() while ((obj = NSPopProviderCacheList(ProviderCache))) { NS_LOG(DEBUG, "build NSTask"); - NSTask * task = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL, (void *) obj->data); + NSProvider * prov = NSCopyProvider((NSProvider_internal *) obj->data); + NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(prov, + NSRemoveProvider_internal((NSProvider_internal *) obj->data)); + + NSTask * task = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL, prov); NS_VERIFY_NOT_NULL_V(task); NSConsumerPushEvent(task); @@ -284,7 +288,7 @@ void NSConsumerHandleProviderDiscovered(NSProvider_internal * provider) { NS_LOG(DEBUG, "accepter is NS_ACCEPTER_CONSUMER, Callback to user"); NSProvider * providerForCb = NSCopyProvider(provider); - NSDiscoveredProvider(providerForCb); + NSProviderChanged(providerForCb, NS_DISCOVERED); } else { diff --git a/service/notification/unittest/NSConsumerTest.cpp b/service/notification/unittest/NSConsumerTest.cpp index d60d7d8..aaaa683 100644 --- a/service/notification/unittest/NSConsumerTest.cpp +++ b/service/notification/unittest/NSConsumerTest.cpp @@ -51,6 +51,7 @@ namespace NS_SELECTION_PROVIDER = 1 }; + NSConsumerConfig cfg; } class TestWithMock: public testing::Test @@ -84,11 +85,6 @@ public: NotificationConsumerTest() = default; ~NotificationConsumerTest() = default; - static void NSProviderDiscoveredCallbackEmpty(NSProvider *) - { - std::cout << __func__ << std::endl; - } - static void NSNotificationReceivedCallbackEmpty(NSMessage *) { std::cout << __func__ << std::endl; @@ -117,7 +113,7 @@ protected: if (g_isStartedStack == false) { - OC::PlatformConfig cfg + OC::PlatformConfig occfg { OC::ServiceType::InProc, OC::ModeType::Both, @@ -125,7 +121,7 @@ protected: 0, OC::QualityOfService::LowQos }; - OC::OCPlatform::Configure(cfg); + OC::OCPlatform::Configure(occfg); try { @@ -137,6 +133,10 @@ protected: } g_isStartedStack = true; + + cfg.changedCb = NSProviderChangedCallback; + cfg.messageCb = NSNotificationReceivedCallbackEmpty; + cfg.syncInfoCb = NSSyncCallbackEmpty; } } @@ -150,11 +150,6 @@ protected: TEST_F(NotificationConsumerTest, StartConsumerPositive) { - NSConsumerConfig cfg; - cfg.discoverCb = NSProviderDiscoveredCallbackEmpty; - cfg.changedCb = NSProviderChangedCallback; - cfg.messageCb = NSNotificationReceivedCallbackEmpty; - cfg.syncInfoCb = NSSyncCallbackEmpty; EXPECT_EQ(NS_OK, NSStartConsumer(cfg)); } @@ -165,18 +160,13 @@ TEST_F(NotificationConsumerTest, StopConsumerPositive) TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerFirst) { - mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty).Do( - [this](NSProvider *) + mocks.ExpectCallFunc(NSProviderChangedCallback).Do( + [this](NSProvider *, NSResponse) { std::cout << "Call Discovered" << std::endl; responseCon.notify_all(); }); - NSConsumerConfig cfg; - cfg.discoverCb = NSProviderDiscoveredCallbackEmpty; - cfg.changedCb = NSProviderChangedCallback; - cfg.messageCb = NSNotificationReceivedCallbackEmpty; - cfg.syncInfoCb = NSSyncCallbackEmpty; NSStartConsumer(cfg); g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER); @@ -198,19 +188,14 @@ TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsu responseCon.wait_for(lock, g_waitForResponse); } - mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty).Do( - [this](NSProvider * provider) + mocks.ExpectCallFunc(NSProviderChangedCallback).Do( + [this](NSProvider * provider, NSResponse) { std::cout << "Call Discovered" << std::endl; g_provider = provider; responseCon.notify_all(); }); - NSConsumerConfig cfg; - cfg.discoverCb = NSProviderDiscoveredCallbackEmpty; - cfg.changedCb = NSProviderChangedCallback; - cfg.messageCb = NSNotificationReceivedCallbackEmpty; - cfg.syncInfoCb = NSSyncCallbackEmpty; NSStartConsumer(cfg); std::unique_lock< std::mutex > lock{ mutexForCondition }; @@ -221,8 +206,8 @@ TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsu TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenRescan) { g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER); - mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty).Do( - [this](NSProvider * provider) + mocks.ExpectCallFunc(NSProviderChangedCallback).Do( + [this](NSProvider * provider, NSResponse) { std::cout << "Call Discovered" << std::endl; g_provider = provider; @@ -281,11 +266,6 @@ TEST_F(NotificationConsumerTest, ExpectReceiveNotificationWithAccepterisProvider g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_PROVIDER); - NSConsumerConfig cfg; - cfg.discoverCb = NSProviderDiscoveredCallbackEmpty; - cfg.changedCb = NSProviderChangedCallback; - cfg.messageCb = NSNotificationReceivedCallbackEmpty; - cfg.syncInfoCb = NSSyncCallbackEmpty; NSStartConsumer(cfg); { std::unique_lock< std::mutex > lock{ mutexForCondition }; -- 2.7.4