From e3cdccca0b49c86d0bbc68a525379e5ae57f0425 Mon Sep 17 00:00:00 2001 From: KIM JungYong Date: Fri, 2 Sep 2016 18:58:29 +0900 Subject: [PATCH] Update Consumer C APIs. 1) Parameter as NSProvider structure is changed C string providerId. 2) NSTopicLL is seperated from NSProvider structure. 3) Topic releated API is renamed and changed return type. 4) NSUnsubscribe API is commented out. Conflicts: service/notification/examples/linux/notificationconsumer.c Change-Id: Ib46ed952cdab58cb9f95eaeb4e405b7da4adeb2f Signed-off-by: KIM JungYong Reviewed-on: https://gerrit.iotivity.org/gerrit/11335 Reviewed-by: Uze Choi Tested-by: Uze Choi --- .../examples/linux/notificationconsumer.c | 22 +++--- service/notification/include/NSCommon.h | 1 - service/notification/include/NSConsumerInterface.h | 41 ++++------ .../notification/src/consumer/NSConsumerCommon.c | 16 ---- .../src/consumer/NSConsumerInterface.c | 92 +++++++++------------- .../src/consumer/NSConsumerScheduler.c | 31 ++++---- service/notification/unittest/NSConsumerTest.cpp | 27 ++++--- 7 files changed, 94 insertions(+), 136 deletions(-) diff --git a/service/notification/examples/linux/notificationconsumer.c b/service/notification/examples/linux/notificationconsumer.c index 098020a..3d96209 100644 --- a/service/notification/examples/linux/notificationconsumer.c +++ b/service/notification/examples/linux/notificationconsumer.c @@ -43,6 +43,7 @@ char CLOUD_ACCESS_TOKEN[50]; NSProvider * g_provider = NULL; +NSTopicLL * g_topicLL = NULL; FILE* server_fopen(const char *path, const char *mode) { @@ -50,12 +51,12 @@ FILE* server_fopen(const char *path, const char *mode) return fopen("oic_ns_provider_db.dat", mode); } -void printProviderTopicList(NSProvider *provider) +void printProviderTopicList(NSTopicLL * topics) { printf("printProviderTopicList\n"); - if (provider->topicLL) + if (topics) { - NSTopicLL * iter = provider->topicLL; + NSTopicLL * iter = topics; while (iter) { printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state); @@ -74,12 +75,13 @@ void onProviderChanged(NSProvider * provider, NSProviderState response) printf("notification resource discovered\n"); printf("subscribe result %d\n", NSSubscribe(provider)); printf("startSubscribing\n"); + } - } else if (response == NS_TOPIC) + else if (response == NS_TOPIC) { printf ("Provider Topic Updated\n"); - - printProviderTopicList(provider); + g_topicLL = NSConsumerGetTopicList(provider->providerId); + printProviderTopicList(g_topicLL); g_provider = provider; } } @@ -183,15 +185,15 @@ int main(void) printf("3. Get Topics\n"); if(g_provider) { - NSConsumerGetInterestTopics(g_provider); + g_topicLL = NSConsumerGetTopicList(g_provider->providerId); } break; case 4: printf("4. Select Topics\n"); - if (g_provider && g_provider->topicLL) + if (g_provider && g_topicLL) { - NSTopicLL * iter = g_provider->topicLL; + NSTopicLL * iter = g_topicLL; int i = 0; while (iter) { @@ -199,7 +201,7 @@ int main(void) printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state); iter = iter->next; } - NSConsumerSelectInterestTopics(g_provider); + NSConsumerUpdateTopicList(g_provider->providerId, g_topicLL); } break; case 5: diff --git a/service/notification/include/NSCommon.h b/service/notification/include/NSCommon.h index 27906cc..c0ec948 100644 --- a/service/notification/include/NSCommon.h +++ b/service/notification/include/NSCommon.h @@ -118,7 +118,6 @@ typedef struct typedef struct { char providerId[NS_UUID_STRING_SIZE]; - NSTopicLL * topicLL; } NSProvider; diff --git a/service/notification/include/NSConsumerInterface.h b/service/notification/include/NSConsumerInterface.h index c01f7bf..42c1dcb 100644 --- a/service/notification/include/NSConsumerInterface.h +++ b/service/notification/include/NSConsumerInterface.h @@ -66,7 +66,7 @@ typedef struct /** * Initialize notification service for consumer - * @param[in] config NSConsumerconfig structure of callback functions + * @param[in] config NSConsumerconfig structure of callback functions * @return ::NS_OK or result code of NSResult */ NSResult NSStartConsumer(NSConsumerConfig config); @@ -79,10 +79,10 @@ NSResult NSStopConsumer(); /** * Request to discover to remote address as parameter. - * @param[in] server address combined with IP address and port number using delimiter : + * @param[in] server address combined with IP address and port number using delimiter : * @return ::NS_OK or result code of NSResult */ -NSResult NSConsumerEnableRemoteService(char *serverAddress); +NSResult NSConsumerEnableRemoteService(const char *serverAddress); /** * Request discovery manually @@ -92,24 +92,17 @@ NSResult NSRescanProvider(); /** * Request to subscribe notification message resource of provider - * @param[in] provider Provider who send the notification message + * @param[in] providerId the Id of Provider who send the notification message * @return ::NS_OK or result code of NSResult */ -NSResult NSSubscribe(NSProvider *provider); - -/** - * 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 NSSubscribe(const char * providerId); /** * Send sync type to provider in order to synchronize notification status with other consumers * when consumer consumes the notification such as READ, DELETE - * @param[in] providerId Provider id of the Notification message - * @param[in] messageId Notification message id to synchronize the status - * @param[in] type changed notification status from NSSyncType + * @param[in] providerId Provider id of the Notification message + * @param[in] messageId Notification message id to synchronize the status + * @param[in] type changed notification status from NSSyncType * @return ::NS_OK or result code of NSResult */ NSResult NSConsumerSendSyncInfo( @@ -117,31 +110,25 @@ NSResult NSConsumerSendSyncInfo( /** * Request NSProvider that is matched by provider id - * @param[in] providerId the id of provider that user wants to get + * @param[in] providerId the id of provider that user wants to get * @return NSProvider */ NSProvider * NSConsumerGetProvider(const char * providerId); /** - * Request NSMessage that is matched by message id - * @param[in] messageId the id of message that user wants to get - * @return NSMessage - */ -NSMessage * NSConsumerGetMessage(uint64_t messageId); - -/** * Request NSTopic list that is subscribed from provider - * @param[in] provider the provider that user wants to get + * @param[in] providerId the Id of provider that user wants to get * @return NSResult */ -NSResult NSConsumerGetInterestTopics(NSProvider * provider); +NSTopicLL * NSConsumerGetTopicList(const char * providerId); /** * Select Topic list that is wanted to subscribe from provider - * @param[in] provider the provider that user wants to set + * @param[in] providerId the Id of provider that user wants to set + * @param[in] topics the topic list that user wants to set * @return NSResult */ -NSResult NSConsumerSelectInterestTopics(NSProvider * provider); +NSResult NSConsumerUpdateTopicList(const char * providerId, NSTopicLL * topics); #ifdef __cplusplus } diff --git a/service/notification/src/consumer/NSConsumerCommon.c b/service/notification/src/consumer/NSConsumerCommon.c index ef15ae2..3f00281 100644 --- a/service/notification/src/consumer/NSConsumerCommon.c +++ b/service/notification/src/consumer/NSConsumerCommon.c @@ -448,16 +448,6 @@ NSProvider * NSCopyProvider(NSProvider_internal * prov) NSProvider * newProv = (NSProvider *) OICMalloc(sizeof(NSProvider)); NS_VERIFY_NOT_NULL(newProv, NULL); - newProv->topicLL = NULL; - - if (prov->topicLL) - { - NSTopicLL * topicList = NSCopyTopicLL(prov->topicLL); - NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(topicList, NULL, NSRemoveProvider(newProv)); - - newProv->topicLL = topicList; - } - OICStrcpy(newProv->providerId, NS_DEVICE_ID_LENGTH, prov->providerId); return newProv; @@ -482,12 +472,6 @@ void NSRemoveProvider_internal(NSProvider_internal * prov) void NSRemoveProvider(NSProvider * prov) { NS_VERIFY_NOT_NULL_V(prov); - - if (prov->topicLL) - { - NSRemoveTopicLL(prov->topicLL); - } - NSOICFree(prov); } diff --git a/service/notification/src/consumer/NSConsumerInterface.c b/service/notification/src/consumer/NSConsumerInterface.c index 3858873..db95e81 100644 --- a/service/notification/src/consumer/NSConsumerInterface.c +++ b/service/notification/src/consumer/NSConsumerInterface.c @@ -67,8 +67,9 @@ NSResult NSStopConsumer() return NS_OK; } -NSResult NSConsumerEnableRemoteService(char *serverAddress) +NSResult NSConsumerEnableRemoteService(const char *serverAddress) { + NS_VERIFY_NOT_NULL(serverAddress, NS_ERROR); bool isStartedConsumer = NSIsStartedConsumer(); NS_VERIFY_NOT_NULL(isStartedConsumer == true ? (void *) 1 : NULL, NS_ERROR); @@ -93,12 +94,17 @@ NSResult NSConsumerEnableRemoteService(char *serverAddress) return NSConsumerPushEvent(discoverTask); } -NSResult NSSubscribe(NSProvider * provider) +NSResult NSSubscribe(const char * providerId) { + NS_VERIFY_NOT_NULL(providerId, NS_ERROR); bool isStartedConsumer = NSIsStartedConsumer(); NS_VERIFY_NOT_NULL(isStartedConsumer == true ? (void *) 1 : NULL, NS_ERROR); - NSProvider * prov = (NSProvider *)NSCopyProvider((NSProvider_internal *) provider); + NSProvider_internal * provInternal = NSConsumerFindNSProvider(providerId); + NS_VERIFY_NOT_NULL(provInternal, NS_ERROR); + + NSProvider * prov = (NSProvider *)NSCopyProvider(provInternal); + NSRemoveProvider_internal(provInternal); NS_VERIFY_NOT_NULL(prov, NS_ERROR); NSTask * subscribeTask = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE, (void *) prov); @@ -107,12 +113,17 @@ NSResult NSSubscribe(NSProvider * provider) return NSConsumerPushEvent(subscribeTask); } -NSResult NSUnsubscribe(NSProvider * provider) +NSResult NSUnsubscribe(const char * providerId) { + NS_VERIFY_NOT_NULL(providerId, NS_ERROR); bool isStartedConsumer = NSIsStartedConsumer(); NS_VERIFY_NOT_NULL(isStartedConsumer == true ? (void *) 1 : NULL, NS_ERROR); - NSProvider * prov = (NSProvider *)NSCopyProvider((NSProvider_internal *) provider); + NSProvider_internal * provInternal = NSConsumerFindNSProvider(providerId); + NS_VERIFY_NOT_NULL(provInternal, NS_ERROR); + + NSProvider * prov = (NSProvider *)NSCopyProvider((NSProvider_internal *) provInternal); + NSRemoveProvider_internal(provInternal); NS_VERIFY_NOT_NULL(prov, NS_ERROR); NSTask * unsubscribeTask = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL, (void *) prov); @@ -123,6 +134,7 @@ NSResult NSUnsubscribe(NSProvider * provider) NSResult NSConsumerSendSyncInfo(const char * providerId, uint64_t messageId, NSSyncType type) { + NS_VERIFY_NOT_NULL(providerId, NS_ERROR); bool isStartedConsumer = NSIsStartedConsumer(); NS_VERIFY_NOT_NULL(isStartedConsumer == true ? (void *) 1 : NULL, NS_ERROR); @@ -152,6 +164,7 @@ NSResult NSRescanProvider() NSProvider * NSConsumerGetProvider(const char * providerId) { + NS_VERIFY_NOT_NULL(providerId, NULL); bool isStartedConsumer = NSIsStartedConsumer(); NS_VERIFY_NOT_NULL(isStartedConsumer == true ? (void *) 1 : NULL, NULL); @@ -167,74 +180,45 @@ NSProvider * NSConsumerGetProvider(const char * providerId) return retProv; } -NSMessage * NSConsumerGetMessage(uint64_t messageId) +NSTopicLL * NSConsumerGetTopicList(const char * providerId) { + NS_VERIFY_NOT_NULL(providerId, NULL); bool isStartedConsumer = NSIsStartedConsumer(); NS_VERIFY_NOT_NULL(isStartedConsumer == true ? (void *) 1 : NULL, NULL); - char msgId[NS_DEVICE_ID_LENGTH] = { 0, }; - snprintf(msgId, NS_DEVICE_ID_LENGTH, "%lld", (long long int)messageId); - - return (NSMessage *) NSConsumerFindNSMessage(msgId); -} - -NSResult NSConsumerGetInterestTopics(NSProvider * provider) -{ - bool isStartedConsumer = NSIsStartedConsumer(); - NS_VERIFY_NOT_NULL(isStartedConsumer == true ? (void *) 1 : NULL, NS_ERROR); - - NS_VERIFY_NOT_NULL(provider, NS_ERROR); + NS_LOG_V(DEBUG, "NSProvider ID: %s", providerId); + NSProvider_internal * prov_internal = NSConsumerFindNSProvider(providerId); + NS_VERIFY_NOT_NULL(prov_internal, NULL); - NS_LOG_V(DEBUG, "NSProvider ID: %s", provider->providerId); - NSProvider_internal * prov_internal = NSConsumerFindNSProvider(provider->providerId); - NS_VERIFY_NOT_NULL(prov_internal, NS_ERROR); - - NSSelector selector = prov_internal->accessPolicy; + NSTopicLL * retTopics = prov_internal->topicLL; + prov_internal->topicLL = NULL; NSRemoveProvider_internal(prov_internal); - NS_VERIFY_NOT_NULL(selector == NS_SELECTION_CONSUMER ? (void *) 1 : NULL, NS_ERROR); - NSProvider * prov = (NSProvider *)NSCopyProvider((NSProvider_internal *) provider); - NS_VERIFY_NOT_NULL(prov, NS_ERROR); - - NSTask * topicTask = NSMakeTask(TASK_CONSUMER_GET_TOPIC_LIST, (void *) prov); - NS_VERIFY_NOT_NULL(topicTask, NS_ERROR); - - return NSConsumerPushEvent(topicTask); + return retTopics; } -NSResult NSConsumerSelectInterestTopics(NSProvider * provider) +NSResult NSConsumerUpdateTopicList(const char * providerId, NSTopicLL * topics) { + NS_VERIFY_NOT_NULL(providerId, NS_ERROR); + NS_VERIFY_NOT_NULL(topics, NS_ERROR); bool isStartedConsumer = NSIsStartedConsumer(); NS_VERIFY_NOT_NULL(isStartedConsumer == true ? (void *) 1 : NULL, NS_ERROR); - NS_VERIFY_NOT_NULL(provider, NS_ERROR); - NS_VERIFY_NOT_NULL(provider->topicLL, NS_ERROR); - - NSProvider_internal * prov_internal = NSConsumerFindNSProvider(provider->providerId); + NSProvider_internal * prov_internal = NSConsumerFindNSProvider(providerId); NS_VERIFY_NOT_NULL(prov_internal, NS_ERROR); + NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(prov_internal->topicLL, NS_ERROR, + NSRemoveProvider_internal(prov_internal)); NSSelector selector = prov_internal->accessPolicy; - NSRemoveProvider_internal(prov_internal); - NS_VERIFY_NOT_NULL(selector == NS_SELECTION_CONSUMER ? (void *) 1 : NULL, NS_ERROR); + NS_VERIFY_NOT_NULL_WITH_POST_CLEANING( + selector == NS_SELECTION_CONSUMER ? (void *) 1 : NULL, NS_ERROR, + NSRemoveProvider_internal(prov_internal)); - NSProvider * prov = (NSProvider *)NSCopyProvider((NSProvider_internal *) provider); - NS_VERIFY_NOT_NULL(prov, NS_ERROR); + NSRemoveTopicLL(prov_internal->topicLL); + prov_internal->topicLL = NSCopyTopicLL(topics); - NSTask * topicTask = NSMakeTask(TASK_CONSUMER_SELECT_TOPIC_LIST, (void *) prov); + NSTask * topicTask = NSMakeTask(TASK_CONSUMER_SELECT_TOPIC_LIST, (void *) prov_internal); NS_VERIFY_NOT_NULL(topicTask, NS_ERROR); return NSConsumerPushEvent(topicTask); } - -NSResult NSDropNSMessage(NSMessage * obj) -{ - NS_VERIFY_NOT_NULL(obj, NS_ERROR); - - obj->messageId = 0; - NSOICFree(obj->title); - NSOICFree(obj->contentText); - NSOICFree(obj->sourceName); - NSOICFree(obj); - - return NS_OK; -} diff --git a/service/notification/src/consumer/NSConsumerScheduler.c b/service/notification/src/consumer/NSConsumerScheduler.c index 8c8f5c9..47224cd 100644 --- a/service/notification/src/consumer/NSConsumerScheduler.c +++ b/service/notification/src/consumer/NSConsumerScheduler.c @@ -271,22 +271,7 @@ void NSConsumerTaskProcessing(NSTask * task) case TASK_CONSUMER_GET_TOPIC_LIST: case TASK_CONSUMER_SELECT_TOPIC_LIST: { - NSProvider * provider = (NSProvider *)task->taskData; - NSProvider_internal * prov = - NSConsumerFindNSProvider(provider->providerId); - NS_VERIFY_NOT_NULL_V(prov); - if (task->taskType == TASK_CONSUMER_SELECT_TOPIC_LIST) - { - NSRemoveTopicLL(prov->topicLL); - prov->topicLL = NSCopyTopicLL((NSTopicLL *)provider->topicLL); - } - - NSTask * topTask = NSMakeTask(task->taskType, prov); - NS_VERIFY_NOT_NULL_V(topTask); - NSConsumerCommunicationTaskProcessing(topTask); - - NSRemoveProvider((NSProvider *)task->taskData); - NSOICFree(task); + NSConsumerCommunicationTaskProcessing(task); break; } case TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL: @@ -326,7 +311,6 @@ void NSConsumerTaskProcessing(NSTask * task) } case TASK_RECV_SYNCINFO: case TASK_CONSUMER_RECV_MESSAGE: - case TASK_CONSUMER_PROVIDER_DISCOVERED: case TASK_CONSUMER_SENT_REQ_OBSERVE: case TASK_CONSUMER_RECV_PROVIDER_CHANGED: case TASK_MAKE_SYNCINFO: @@ -336,6 +320,19 @@ void NSConsumerTaskProcessing(NSTask * task) NSConsumerInternalTaskProcessing(task); break; } + case TASK_CONSUMER_PROVIDER_DISCOVERED: + { + NSTask * getTopicTask = (NSTask *)OICMalloc(sizeof(NSTask)); + NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(getTopicTask, + NSRemoveProvider_internal((NSProvider_internal *) task->taskData)); + getTopicTask->nextTask = NULL; + getTopicTask->taskData = + (void *) NSCopyProvider_internal((NSProvider_internal *) task->taskData); + getTopicTask->taskType = TASK_CONSUMER_REQ_TOPIC_LIST; + NSConsumerCommunicationTaskProcessing(getTopicTask); + NSConsumerInternalTaskProcessing(task); + break; + } default: { NS_LOG(ERROR, "Unknown type of task"); diff --git a/service/notification/unittest/NSConsumerTest.cpp b/service/notification/unittest/NSConsumerTest.cpp index e539f7f..388957d 100644 --- a/service/notification/unittest/NSConsumerTest.cpp +++ b/service/notification/unittest/NSConsumerTest.cpp @@ -151,6 +151,8 @@ protected: TEST_F(NotificationConsumerTest, StartConsumerPositive) { EXPECT_EQ(NS_OK, NSStartConsumer(cfg)); + std::unique_lock< std::mutex > lock{ mutexForCondition }; + responseCon.wait_for(lock, g_waitForResponse); } TEST_F(NotificationConsumerTest, StopConsumerPositive) @@ -231,7 +233,7 @@ TEST_F(NotificationConsumerTest, ExpectSubscribeSuccess) // std::cout << "Income Accepted subscription : " << std::endl; // }); - NSResult ret = NSSubscribe(g_provider); + NSResult ret = NSSubscribe(g_provider->providerId); std::unique_lock< std::mutex > lock{ mutexForCondition }; responseCon.wait_for(lock, g_waitForResponse); @@ -439,17 +441,20 @@ TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenConsumerPostSync) responseCon.wait_for(lock, g_waitForResponse); } - EXPECT_EQ(NS_SYNC_DELETED, type); -} - -TEST_F(NotificationConsumerTest, ExpectUnsubscribeSuccess) -{ - NSResult ret = NSUnsubscribe(g_provider); - std::unique_lock< std::mutex > lock{ mutexForCondition }; - responseCon.wait_for(lock, g_waitForResponse); - g_providerSimul.deleteNotificationResource(); NSStopConsumer(); - EXPECT_EQ(NS_OK, ret); + EXPECT_EQ(NS_SYNC_DELETED, type); } + +//TEST_F(NotificationConsumerTest, ExpectUnsubscribeSuccess) +//{ +// NSResult ret = NSUnsubscribe(g_provider->providerId); +// std::unique_lock< std::mutex > lock{ mutexForCondition }; +// responseCon.wait_for(lock, g_waitForResponse); +// +// g_providerSimul.deleteNotificationResource(); +// NSStopConsumer(); +// +// EXPECT_EQ(NS_OK, ret); +//} -- 2.7.4