From 8acdc24115836ae2beea49c8519d4ae567e6f99e Mon Sep 17 00:00:00 2001 From: "abitha.s" Date: Fri, 12 Aug 2016 14:17:21 +0530 Subject: [PATCH] Updated Topic resource changes for C++ wrapper 1. Modified the Topic resource and Topic LL changes in Provider and Consumer 2. Updated the APIs for Topic resource in C++ wrapper 3. Updated the sample application for calling Topic APIs patch 1 : initial commit patch 2 : updated commit message patch 3 : updated review comments patch 4 : updated commit message patch 5 : removed NSAccessPolicy struct Change-Id: I737bef2eebb129c5f13034d990398d75dd13b3b7 Signed-off-by: abitha.s Reviewed-on: https://gerrit.iotivity.org/gerrit/10623 Tested-by: jenkins-iotivity Reviewed-by: Chihyun Cho Reviewed-by: Uze Choi --- service/notification/SConscript | 2 +- .../notification/cpp-wrapper/common/NSMessage.cpp | 12 ++- .../notification/cpp-wrapper/common/NSMessage.h | 9 +- .../notification/cpp-wrapper/common/NSTopic.cpp | 47 +++++++++ service/notification/cpp-wrapper/common/NSTopic.h | 109 ++++++++++++++++++++ .../cpp-wrapper/common/NSTopicsList.cpp | 78 ++++++++++++++ .../notification/cpp-wrapper/common/NSTopicsList.h | 91 +++++++++++++++++ service/notification/cpp-wrapper/common/NSUtils.h | 2 +- .../cpp-wrapper/consumer/inc/NSConsumerService.h | 8 ++ .../cpp-wrapper/consumer/inc/NSProvider.h | 33 +++++- .../cpp-wrapper/consumer/src/NSConsumerService.cpp | 35 +++++-- .../cpp-wrapper/consumer/src/NSProvider.cpp | 48 ++++++++- .../examples/linux/notificationserviceconsumer.cpp | 108 ++++++++++++++++++-- .../examples/linux/notificationserviceprovider.cpp | 113 +++++++++++++++++++-- .../cpp-wrapper/provider/inc/NSConsumer.h | 24 ++++- .../cpp-wrapper/provider/inc/NSProviderService.h | 47 +++++++-- .../cpp-wrapper/provider/src/NSConsumer.cpp | 30 +++++- .../cpp-wrapper/provider/src/NSProviderService.cpp | 57 ++++++++++- 18 files changed, 805 insertions(+), 48 deletions(-) create mode 100755 service/notification/cpp-wrapper/common/NSTopic.cpp create mode 100755 service/notification/cpp-wrapper/common/NSTopic.h create mode 100755 service/notification/cpp-wrapper/common/NSTopicsList.cpp create mode 100755 service/notification/cpp-wrapper/common/NSTopicsList.h diff --git a/service/notification/SConscript b/service/notification/SConscript index 774e258..ba2db6c 100755 --- a/service/notification/SConscript +++ b/service/notification/SConscript @@ -124,4 +124,4 @@ SConscript('examples/SConscript') # SConscript('android/SConscript') # Go to build c++ wrapper -#SConscript('cpp-wrapper/SConscript') +SConscript('cpp-wrapper/SConscript') diff --git a/service/notification/cpp-wrapper/common/NSMessage.cpp b/service/notification/cpp-wrapper/common/NSMessage.cpp index 68f9e76..8556295 100755 --- a/service/notification/cpp-wrapper/common/NSMessage.cpp +++ b/service/notification/cpp-wrapper/common/NSMessage.cpp @@ -33,7 +33,7 @@ namespace OIC m_type = NSMessage::NSMessageType::NS_MESSAGE_ALERT; m_ttl = 0; m_mediaContents = new NSMediaContents(); - + if (msg != nullptr) { m_messageId = msg->messageId; @@ -59,12 +59,15 @@ namespace OIC if ((msg->mediaContents->iconImage != nullptr) && strlen(msg->mediaContents->iconImage)) m_mediaContents->setIconImage(msg->mediaContents->iconImage); + if ((msg->topic != nullptr) && strlen(msg->topic)) + m_topic.assign(msg->topic, strlen(msg->topic)); + } } NSMessage::~NSMessage() { - if(m_mediaContents != nullptr) + if (m_mediaContents != nullptr) delete m_mediaContents; } @@ -113,6 +116,11 @@ namespace OIC return m_mediaContents; } + std::string NSMessage::getTopic() const + { + return m_topic; + } + void NSMessage::setType(const NSMessageType &type) { m_type = type; diff --git a/service/notification/cpp-wrapper/common/NSMessage.h b/service/notification/cpp-wrapper/common/NSMessage.h index 2e55e1c..f9dab49 100755 --- a/service/notification/cpp-wrapper/common/NSMessage.h +++ b/service/notification/cpp-wrapper/common/NSMessage.h @@ -54,7 +54,7 @@ namespace OIC * Constructor of NSMessage. */ NSMessage(): m_messageId(0), m_type(NSMessageType::NS_MESSAGE_ALERT), m_ttl(0), - m_mediaContents(new NSMediaContents) { } + m_mediaContents(new NSMediaContents) { } /** * Constructor of NSMessage. @@ -180,6 +180,12 @@ namespace OIC */ void setMediaContents(NSMediaContents *mediaContents); + /** + * This method is for getting Topic from the Notification service Message. + * + * @return Topic as string. + */ + std::string getTopic() const; private: uint64_t m_messageId; @@ -192,6 +198,7 @@ namespace OIC std::string m_contentText; std::string m_sourceName; NSMediaContents *m_mediaContents; + std::string m_topic; }; } diff --git a/service/notification/cpp-wrapper/common/NSTopic.cpp b/service/notification/cpp-wrapper/common/NSTopic.cpp new file mode 100755 index 0000000..ce099d9 --- /dev/null +++ b/service/notification/cpp-wrapper/common/NSTopic.cpp @@ -0,0 +1,47 @@ +//****************************************************************** +// +// 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 "NSTopic.h" + +namespace OIC +{ + namespace Service + { + std::string NSTopic::getTopicName() const + { + return m_topicName; + } + + void NSTopic::setTopicName(const std::string &topicName) + { + m_topicName = topicName; + } + + NSTopic::NSTopicState NSTopic::getState() const + { + return m_state; + } + + void NSTopic::setState(const NSTopic::NSTopicState state) + { + m_state = state; + } + } +} diff --git a/service/notification/cpp-wrapper/common/NSTopic.h b/service/notification/cpp-wrapper/common/NSTopic.h new file mode 100755 index 0000000..36a3747 --- /dev/null +++ b/service/notification/cpp-wrapper/common/NSTopic.h @@ -0,0 +1,109 @@ +//****************************************************************** +// +// 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +/** + * @file + * + * This file contains Notification service topic representation. + */ + +#ifndef _NS_TOPIC_H_ +#define _NS_TOPIC_H_ + + +#include +#include "NSCommon.h" + +namespace OIC +{ + namespace Service + { + /** + * @class NSTopic + * @brief This class provides a set of APIs for Notification service Topic. + */ + class NSTopic + { + public: + /** + * Notification topic State + */ + enum class NSTopicState + { + UNSUBSCRIBED = 0, + SUBSCRIBED = 1, + }; + + /** + * Constructor of NSTopic. + */ + NSTopic(): m_state(NSTopicState::UNSUBSCRIBED) { } + + /** + * Constructor of NSTopic. + * + * @param topicName - topicName of the Notification service Topic. + * @param state - as NSTopicState. + */ + NSTopic(const std::string &topicName, const NSTopicState state) + : m_topicName(topicName) , m_state(state) {} + + + /** + * Destructor of NSTopic. + */ + ~NSTopic() = default; + + /** + * This method is for getting topicName from the Notification service Topic. + * + * @return topicName as string. + */ + std::string getTopicName() const; + + /** + * This method is for setting topicName for the Notification service Topic. + * + * @param topicName - as string. + */ + void setTopicName(const std::string &topicName); + + /** + * This method is for getting state from the Notification service Topic. + * + * @return state as NSTopicState. + */ + NSTopicState getState() const; + + /** + * This method is for setting state for the Notification service Topic. + * + * @param state - as NSTopicState. + */ + void setState(const NSTopicState state); + + private: + std::string m_topicName; + NSTopicState m_state; + + }; + } +} +#endif /* _NS_TOPIC_H_ */ diff --git a/service/notification/cpp-wrapper/common/NSTopicsList.cpp b/service/notification/cpp-wrapper/common/NSTopicsList.cpp new file mode 100755 index 0000000..35d2365 --- /dev/null +++ b/service/notification/cpp-wrapper/common/NSTopicsList.cpp @@ -0,0 +1,78 @@ +//****************************************************************** +// +// 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 "NSTopicsList.h" +#include "oic_malloc.h" + +namespace OIC +{ + namespace Service + { + NSTopicsList::NSTopicsList(::NSTopicLL *topics) + { + ::NSTopicLL *topicsNode = nullptr; + topicsNode = topics; + + while (topicsNode != nullptr) + { + addTopic(topicsNode->topicName, (NSTopic::NSTopicState)topicsNode->state); + topicsNode = topicsNode->next; + } + + topicsNode = topics; + while (topicsNode != nullptr) + { + topics = topics->next; + if (topicsNode->topicName) + OICFree(topicsNode->topicName); + OICFree(topicsNode); + topicsNode = topics; + } + } + + NSTopicsList::~NSTopicsList() + { + for (auto it : getTopicsList()) + { + delete it; + } + getTopicsList().clear(); + } + + void NSTopicsList::addTopic(const std::string &topicName, NSTopic::NSTopicState state) + { + m_topicsList.push_back(new NSTopic(topicName, state)); + } + + void NSTopicsList::removeTopic(const std::string &topicName) + { + for (auto it : getTopicsList()) + { + if (it->getTopicName().compare(topicName) == 0) + m_topicsList.remove(it); + } + } + + std::list NSTopicsList::getTopicsList() + { + return m_topicsList; + } + } +} diff --git a/service/notification/cpp-wrapper/common/NSTopicsList.h b/service/notification/cpp-wrapper/common/NSTopicsList.h new file mode 100755 index 0000000..9274826 --- /dev/null +++ b/service/notification/cpp-wrapper/common/NSTopicsList.h @@ -0,0 +1,91 @@ +//****************************************************************** +// +// 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +/** + * @file + * + * This file contains Notification service topics linked list representation. + */ + +#ifndef _NS_TOPICS_LIST_H_ +#define _NS_TOPICS_LIST_H_ + + +#include +#include +#include "NSTopic.h" + +namespace OIC +{ + namespace Service + { + /** + * @class NSTopicsList + * @brief This class provides a set of APIs for Notification service Topics Linked list. + */ + class NSTopicsList + { + public: + /** + * Constructor of NSTopicsList. + */ + NSTopicsList() = default; + + /** + * Constructor of NSTopicsList. + * + * @param topics - pointer to NSTopicLL struct to initialize. + */ + NSTopicsList(::NSTopicLL *topics); + + /** + * Destructor of NSTopicsList. + */ + ~NSTopicsList(); + + /** + * This method is for adding topic for the Notification service Topics Linked list. + * + * @param topicName as string. + * @param state - as NSTopicState. + */ + void addTopic(const std::string &topicName, NSTopic::NSTopicState state); + + /** + * This method is for removing topic from the Notification service Topics Linked list. + * + * @param topicName as string. + */ + void removeTopic(const std::string &topicName); + + /** + * This method is for getting topic LL from the Notification service Topics Linked list. + * + * @return topic as NSTopics Linked list. + */ + std::list getTopicsList(); + + private: + std::list m_topicsList; + + }; + } +} +#endif /* _NS_TOPICS_LIST_H_ */ diff --git a/service/notification/cpp-wrapper/common/NSUtils.h b/service/notification/cpp-wrapper/common/NSUtils.h index 00c00a5..99c0c3d 100755 --- a/service/notification/cpp-wrapper/common/NSUtils.h +++ b/service/notification/cpp-wrapper/common/NSUtils.h @@ -41,7 +41,7 @@ namespace OIC SUCCESS = 300, FAIL = 400, }; - + /** * NSResponse code of notification service */ diff --git a/service/notification/cpp-wrapper/consumer/inc/NSConsumerService.h b/service/notification/cpp-wrapper/consumer/inc/NSConsumerService.h index 2f6aa89..36e2781 100755 --- a/service/notification/cpp-wrapper/consumer/inc/NSConsumerService.h +++ b/service/notification/cpp-wrapper/consumer/inc/NSConsumerService.h @@ -111,6 +111,14 @@ namespace OIC */ NSProvider *getProvider(const std::string &id); + + /** + * Request NSMessage that is matched by message id + * @param[in] messageId the id of message that user wants to get + * @return NSMessage + */ + NSMessage *getMessage(uint64_t messageId); + /** * get consumer config values * @return ConsumerConfig callbaks set diff --git a/service/notification/cpp-wrapper/consumer/inc/NSProvider.h b/service/notification/cpp-wrapper/consumer/inc/NSProvider.h index 5ace980..a556ae2 100755 --- a/service/notification/cpp-wrapper/consumer/inc/NSProvider.h +++ b/service/notification/cpp-wrapper/consumer/inc/NSProvider.h @@ -31,6 +31,8 @@ #include #include "NSSyncInfo.h" #include "NSMessage.h" +#include "NSUtils.h" +#include "NSTopicsList.h" namespace OIC { @@ -60,7 +62,7 @@ namespace OIC /** * Constructor of NSProvider. */ - NSProvider(): m_messageCb(NULL), m_syncInfoCb(NULL) {} + NSProvider(): m_topicList(new NSTopicsList()), m_messageCb(NULL), m_syncInfoCb(NULL) {} /** * Constructor of NSProvider. @@ -68,7 +70,16 @@ namespace OIC * @param providerId - providerId of the Notification. */ NSProvider(const std::string &providerId) : m_providerId(providerId), - m_messageCb(NULL), m_syncInfoCb(NULL) {} + m_topicList(new NSTopicsList()), m_messageCb(NULL), m_syncInfoCb(NULL) {} + + /** + * Constructor of NSProvider. + * + * @param providerId - providerId of the Notification. + * @param topicList - NSTopicsList of interested Topics. + */ + NSProvider(const std::string &providerId, NSTopicsList *topicList) : m_providerId( + providerId), m_topicList(topicList), m_messageCb(NULL), m_syncInfoCb(NULL) {} /** * Constructor of NSProvider. @@ -81,7 +92,7 @@ namespace OIC /** * Destructor of NSProvider. */ - ~NSProvider() = default; + ~NSProvider(); /** * This method is for getting ProviderId from the Notification service provider. @@ -91,6 +102,13 @@ namespace OIC std::string getProviderId() const; /** + * This method is for getting NSTopic List from the Notification service provider. + * + * @return NSTopicsList as pointer. + */ + NSTopicsList *getTopicList() const; + + /** * This method is for requesting subscription of Notification service. * */ @@ -120,6 +138,14 @@ namespace OIC SyncInfoReceivedCallback syncHandle); /** + * Select Topic list that is wanted to subscribe from provider + * + * @param topicList - NSTopicsList of interested Topics. + * @return NSResult + */ + NSResult selectInterestTopics(NSTopicsList *topicList); + + /** * This method is for getting the registered cb of Notification message received. * * @return messageCb - MessageReceivedCallback . @@ -138,6 +164,7 @@ namespace OIC private: std::string m_providerId; + NSTopicsList *m_topicList; MessageReceivedCallback m_messageCb; SyncInfoReceivedCallback m_syncInfoCb; diff --git a/service/notification/cpp-wrapper/consumer/src/NSConsumerService.cpp b/service/notification/cpp-wrapper/consumer/src/NSConsumerService.cpp index d4eb618..18ebf51 100755 --- a/service/notification/cpp-wrapper/consumer/src/NSConsumerService.cpp +++ b/service/notification/cpp-wrapper/consumer/src/NSConsumerService.cpp @@ -43,24 +43,35 @@ namespace OIC void onNSProviderChanged(::NSProvider *provider, ::NSResponse response) { NS_LOG(DEBUG, "onNSProviderChanged - IN"); - NS_LOG_V(DEBUG, "provider Id : %s",provider->providerId); - NS_LOG_V(DEBUG, "response : %d",(int)response); - + NS_LOG_V(DEBUG, "provider Id : %s", provider->providerId); + NS_LOG_V(DEBUG, "response : %d", (int)response); + NSProvider *nsProvider = new NSProvider(provider); auto changeCallback = NSConsumerService::getInstance()->getConsumerConfig().m_changedCb; - if(response == NS_ALLOW) + if (response == NS_ALLOW) { NSConsumerService::getInstance()->getAcceptedProviders().push_back(nsProvider); if (changeCallback != NULL) changeCallback(nsProvider, (NSResponse) response); } - else if(response == NS_DENY) + else if (response == NS_DENY) { NSConsumerService::getInstance()->getAcceptedProviders().remove(nsProvider); if (changeCallback != NULL) changeCallback(nsProvider, (NSResponse) response); delete nsProvider; } + else if (response == NS_TOPIC) + { + NSProvider *oldProvider = NSConsumerService::getInstance()->getProvider( + nsProvider->getProviderId()); + if (oldProvider != nullptr) + { + NSConsumerService::getInstance()->getAcceptedProviders().remove(oldProvider); + NSConsumerService::getInstance()->getAcceptedProviders().push_back(nsProvider); + delete oldProvider; + } + } NS_LOG(DEBUG, "onNSProviderChanged - OUT"); } @@ -130,7 +141,7 @@ namespace OIC m_config = config; NSConsumerConfig nsConfig; nsConfig.discoverCb = onNSProviderDiscovered; - nsConfig.changedCb= onNSProviderChanged; + nsConfig.changedCb = onNSProviderChanged; nsConfig.messageCb = onNSMessageReceived; nsConfig.syncInfoCb = onNSSyncInfoReceived; @@ -178,6 +189,18 @@ namespace OIC return NULL; } + NSMessage *getMessage(uint64_t messageId) + { + NS_LOG(DEBUG, "getMessage - IN"); + ::NSMessage *message = NSConsumerGetMessage(messageId); + NSMessage *nsMessage = new NSMessage(message); + + delete message->mediaContents; + delete message; + NS_LOG(DEBUG, "getMessage - OUT"); + return nsMessage; + } + NSConsumerService::ConsumerConfig NSConsumerService::getConsumerConfig() { return m_config; diff --git a/service/notification/cpp-wrapper/consumer/src/NSProvider.cpp b/service/notification/cpp-wrapper/consumer/src/NSProvider.cpp index 7aa94be..c65e552 100755 --- a/service/notification/cpp-wrapper/consumer/src/NSProvider.cpp +++ b/service/notification/cpp-wrapper/consumer/src/NSProvider.cpp @@ -23,7 +23,6 @@ #include "NSConsumerInterface.h" #include "NSConstants.h" #include "NSCommon.h" -#include "NSUtils.h" #include "oic_string.h" namespace OIC @@ -34,6 +33,27 @@ namespace OIC { ::NSProvider *provider = new ::NSProvider; OICStrcpy(provider->providerId, NS_UTILS_UUID_STRING_SIZE, m_providerId.c_str()); + + provider->topicLL = NULL; + + if (m_topicList != nullptr) + { + for (auto it : m_topicList->getTopicsList()) + { + ::NSTopicLL *topic = new ::NSTopicLL; + OICStrcpy(topic->topicName, it->getTopicName().length(), + it->getTopicName().c_str()); + topic->state = (::NSTopicState)it->getState(); + topic->next = NULL; + if (provider->topicLL == NULL) + provider->topicLL = topic; + else + { + topic->next = provider->topicLL; + provider->topicLL = topic; + } + } + } return provider; } @@ -44,14 +64,29 @@ namespace OIC if (provider != nullptr) { m_providerId.assign(provider->providerId, NS_UTILS_UUID_STRING_SIZE); + if (provider->topicLL != nullptr) + m_topicList = new NSTopicsList(provider->topicLL); + else + m_topicList = new NSTopicsList(); } } + NSProvider::~NSProvider() + { + if (m_topicList != nullptr) + delete m_topicList; + } + std::string NSProvider::getProviderId() const { return m_providerId; } + NSTopicsList *NSProvider::getTopicList() const + { + return m_topicList; + } + void NSProvider::subscribe() { NS_LOG(DEBUG, "subscribe - IN"); @@ -81,6 +116,17 @@ namespace OIC m_syncInfoCb = syncHandle; } + NSResult NSProvider::selectInterestTopics(NSTopicsList *topicList) + { + NS_LOG(DEBUG, "selectInterestTopics - IN"); + NSProvider *provider = new NSProvider(getProviderId(), topicList); + NSResult result = (NSResult) NSConsumerSelectInterestTopics( + provider->getNSProvider()); + delete provider; + NS_LOG(DEBUG, "selectInterestTopics - OUT"); + return result; + } + NSProvider::MessageReceivedCallback NSProvider::getMessageReceivedCb() { return m_messageCb; diff --git a/service/notification/cpp-wrapper/examples/linux/notificationserviceconsumer.cpp b/service/notification/cpp-wrapper/examples/linux/notificationserviceconsumer.cpp index fe2af59..effe625 100755 --- a/service/notification/cpp-wrapper/examples/linux/notificationserviceconsumer.cpp +++ b/service/notification/cpp-wrapper/examples/linux/notificationserviceconsumer.cpp @@ -24,6 +24,7 @@ #include "NSConsumerService.h" #include "NSMessage.h" #include "NSProvider.h" +#include "NSTopicsList.h" #include "ocstack.h" #define TAG "NotiConsumerWrapperExample" @@ -32,6 +33,7 @@ using namespace OIC::Service; bool isExit = false; std::string REMOTE_SERVER_ADDRESS; +std::string mainProvider; void onNotificationPostedCb(OIC::Service::NSMessage *notification) { @@ -39,6 +41,7 @@ void onNotificationPostedCb(OIC::Service::NSMessage *notification) std::cout << "title : " << notification->getTitle() << std::endl; std::cout << "content : " << notification->getContentText() << std::endl; std::cout << "source : " << notification->getSourceName() << std::endl; + std::cout << "topic : " << notification->getTopic() << std::endl; } void onNotificationSyncCb(OIC::Service::NSSyncInfo *sync) @@ -54,12 +57,68 @@ void onDiscoverNotificationCb(OIC::Service::NSProvider *provider) std::cout << "startSubscribing" << std::endl; } -void onProviderChangedCb(OIC::Service::NSProvider *provider,OIC::Service::NSResponse response) +void onProviderChangedCb(OIC::Service::NSProvider *provider, OIC::Service::NSResponse response) { std::cout << "Subscription accepted" << std::endl; std::cout << "subscribed provider Id : " << provider->getProviderId() << std::endl; - if(response == OIC::Service::NSResponse::ALLOW) + if (response == OIC::Service::NSResponse::ALLOW) + { provider->setListener(onNotificationPostedCb, onNotificationSyncCb); + if (mainProvider.empty()) + mainProvider = provider->getProviderId(); + } + else if (response == OIC::Service::NSResponse::TOPIC) + { + std::cout << "Provider Topic Updated" << std::endl; + for (auto it : provider->getTopicList()->getTopicsList()) + { + std::cout << "Topic Name: " << it->getTopicName() << std::endl; + std::cout << "Topic state: " << (int) it->getState() << std::endl; + } + + std::cout << "7. Get Topics" << std::endl; + std::cout << "8. Select Topics" << std::endl; + std::cout << "0. Exit" << std::endl; + std::cout << "input: " << std::endl; + + int num = 0; + std::cin >> num; + switch (num) + { + case 7: + { + std::cout << "getInterestTopics" << std::endl; + if (provider != nullptr) + { + auto topicList = provider->getTopicList(); + if (topicList != nullptr) + for (auto it : topicList->getTopicsList()) + { + std::cout << "Topic Name: " << it->getTopicName() << std::endl; + std::cout << "Topic state: " << (int) it->getState() << std::endl; + } + } + } + break; + case 8: + { + std::cout << "selectInterestTopics" << std::endl; + if (provider != nullptr) + { + NSTopicsList *topicList = new NSTopicsList(); + topicList->addTopic("OCF_TOPIC1", NSTopic::NSTopicState::UNSUBSCRIBED); + topicList->addTopic("OCF_TOPIC2", NSTopic::NSTopicState::UNSUBSCRIBED); + topicList->addTopic("OCF_TOPIC3", NSTopic::NSTopicState::UNSUBSCRIBED); + + provider->selectInterestTopics(topicList); + } + } + break; + case 0: + default: + break; + } + } } void *OCProcessThread(void *ptr) @@ -103,10 +162,12 @@ int main(void) std::cout << "1. Start Consumer" << std::endl; std::cout << "2. Stop Consumer" << std::endl; + std::cout << "3. getInterestTopics" << std::endl; + std::cout << "4. selectInterestTopics" << std::endl; #ifdef WITH_CLOUD - std::cout << "3. Enable NS Consumer RemoteService" << std::endl; + std::cout << "5. Enable NS Consumer RemoteService" << std::endl; #endif - std::cout << "5. Exit" << std::endl; + std::cout << "6. Exit" << std::endl; std::cout << "Input: " << std::endl; std::cin >> num; @@ -120,18 +181,49 @@ int main(void) std::cout << "2. Stop the Notification Consumer" << std::endl; NSConsumerService::getInstance()->Stop(); break; -#ifdef WITH_CLOUD case 3: { - std::cout << "3. Enable NS Consumer RemoteService" << std::endl; + std::cout << "getInterestTopics" << std::endl; + OIC::Service::NSProvider *provider = NSConsumerService::getInstance()->getProvider(mainProvider); + if (provider != nullptr) + { + auto topicList = provider->getTopicList(); + if (topicList != nullptr) + for (auto it : topicList->getTopicsList()) + { + std::cout << "Topic Name: " << it->getTopicName() << std::endl; + std::cout << "Topic state: " << (int) it->getState() << std::endl; + } + } + } + break; + case 4: + { + std::cout << "selectInterestTopics" << std::endl; + OIC::Service::NSProvider *provider = NSConsumerService::getInstance()->getProvider(mainProvider); + if (provider != nullptr) + { + NSTopicsList *topicList = new NSTopicsList(); + topicList->addTopic("OCF_TOPIC1", NSTopic::NSTopicState::UNSUBSCRIBED); + topicList->addTopic("OCF_TOPIC2", NSTopic::NSTopicState::UNSUBSCRIBED); + topicList->addTopic("OCF_TOPIC3", NSTopic::NSTopicState::UNSUBSCRIBED); + + provider->selectInterestTopics(topicList); + } + } + break; +#ifdef WITH_CLOUD + case 5: + { + std::cout << "5. Enable NS Consumer RemoteService" << std::endl; std::cout << "Input the Server Address :"; std::cin >> REMOTE_SERVER_ADDRESS; NSConsumerService::getInstance()->EnableRemoteService(REMOTE_SERVER_ADDRESS); break; } #endif - case 5: - std::cout << "5. Exit" << std::endl; + case 6: + std::cout << "6. Exit" << std::endl; isExit = true; break; default: diff --git a/service/notification/cpp-wrapper/examples/linux/notificationserviceprovider.cpp b/service/notification/cpp-wrapper/examples/linux/notificationserviceprovider.cpp index 339d1f0..ca2a37d 100755 --- a/service/notification/cpp-wrapper/examples/linux/notificationserviceprovider.cpp +++ b/service/notification/cpp-wrapper/examples/linux/notificationserviceprovider.cpp @@ -22,6 +22,8 @@ #include #include "NSCommon.h" #include "NSProviderService.h" +#include "NSUtils.h" +#include "NSTopicsList.h" #include "logger.h" #include "octypes.h" #include "pthread.h" @@ -32,6 +34,7 @@ #define TAG "NotiProviderWrapperExample" using namespace std; using namespace OIC::Service; +std::string mainConsumer; extern char *strdup(const char *s); @@ -60,6 +63,8 @@ void subscribeRequestCallback(OIC::Service::NSConsumer *consumer) std::cout << "consumer requested to subscribe" << std::endl; std::cout << "Consumer Device ID: " << consumer->getConsumerId() << std::endl; + if (mainConsumer.empty()) + mainConsumer = consumer->getConsumerId(); consumer->acceptSubscription(consumer, true); } @@ -92,11 +97,18 @@ int main() std::cout << "2. Start the Notification Provider(Accepter: Consumer)" << std::endl; std::cout << "3. SendMessage " << std::endl; std::cout << "4. SendSyncInfo" << std::endl; + + std::cout << "5. AddTopic" << std::endl; + std::cout << "6. DeleteTopic" << std::endl; + std::cout << "7. SelectTopic" << std::endl; + std::cout << "8. UnselectTopic" << std::endl; + std::cout << "9. GetConsumerTopics" << std::endl; + std::cout << "10. GetTopics" << std::endl; #ifdef WITH_CLOUD - std::cout << "5. Enable NS Provider RemoteService" << std::endl; - std::cout << "6. Disable NS Provider RemoteService" << std::endl; + std::cout << "11. Enable NS Provider RemoteService" << std::endl; + std::cout << "12. Disable NS Provider RemoteService" << std::endl; #endif - std::cout << "9. Stop the Notification Provider" << std::endl; + std::cout << "13. Stop the Notification Provider" << std::endl; std::cout << "0. Exit()" << std::endl; std::cout << "input : "; @@ -111,7 +123,7 @@ int main() NSProviderService::ProviderConfig cfg; cfg.m_subscribeRequestCb = subscribeRequestCallback; cfg.m_syncInfoCb = syncCallback; - cfg.policy = (bool) NSProviderService::NSAccessPolicy::NS_ACCESS_ALLOW; + cfg.policy = true; NSProviderService::getInstance()->Start(cfg); break; @@ -122,7 +134,7 @@ int main() NSProviderService::ProviderConfig cfg; cfg.m_subscribeRequestCb = subscribeRequestCallback; cfg.m_syncInfoCb = syncCallback; - cfg.policy = (bool) NSProviderService::NSAccessPolicy::NS_ACCESS_DENY; + cfg.policy = false; NSProviderService::getInstance()->Start(cfg); break; @@ -133,6 +145,7 @@ int main() std::string title; std::string body; + std::string topic; std::cout << "id : " << ++id << std::endl; std::cout << "title : "; @@ -142,8 +155,12 @@ int main() std::cout << "body : "; std::cin >> body; + std::cout << "topic : "; + std::cin >> topic; + std::cout << "app - mTitle : " << title << std::endl; std::cout << "app - mContentText : " << body << std::endl; + std::cout << "app - mTopic : " << topic << std::endl; OIC::Service::NSMessage *msg = NSProviderService::getInstance()->CreateMessage(); @@ -163,24 +180,100 @@ int main() OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ); break; } -#ifdef WITH_CLOUD + case 5: + std::cout << "AddTopic" << std::endl; + NSProviderService::getInstance()->AddTopic("OCF_TOPIC1"); + NSProviderService::getInstance()->AddTopic("OCF_TOPIC2"); + NSProviderService::getInstance()->AddTopic("OCF_TOPIC3"); + NSProviderService::getInstance()->AddTopic("OCF_TOPIC4"); + break; + + case 6: + std::cout << "DeleteTopic" << std::endl; + NSProviderService::getInstance()->DeleteTopic("OCF_TOPIC2"); + break; + + case 7: + { + std::cout << "SelectTopic" << std::endl; + OIC::Service::NSConsumer *consumer = NSProviderService::getInstance()->getConsumer(mainConsumer); + if (consumer != nullptr) + { + consumer->selectTopic("OCF_TOPIC1"); + consumer->selectTopic("OCF_TOPIC2"); + consumer->selectTopic("OCF_TOPIC3"); + consumer->selectTopic("OCF_TOPIC4"); + std::cout << "SelectTopic completed" << std::endl; + } + break; + } + case 8: + { + std::cout << "UnSelectTopic" << std::endl; + OIC::Service::NSConsumer *consumer = NSProviderService::getInstance()->getConsumer(mainConsumer); + if (consumer != nullptr) + { + consumer->unselectTopic("OCF_TOPIC1"); + std::cout << "UnSelectTopic completed" << std::endl; + } + break; + } + break; + + case 9: + { + std::cout << "GetConsumerTopics" << std::endl; + OIC::Service::NSConsumer *consumer = NSProviderService::getInstance()->getConsumer(mainConsumer); + if (consumer != nullptr) + { + auto nsTopics = consumer->getConsumerTopics(); + if (nsTopics != nullptr) + { + for (auto it : nsTopics->getTopicsList()) + { + + std::cout << it->getTopicName() << std::endl; + std::cout << (int) it->getState() << std::endl; + } + delete nsTopics; + } + std::cout << "GetConsumerTopics completed" << std::endl; + } + } + break; + + case 10: { - std::cout << "3. Enable NS Provider RemoteService" << std::endl; + std::cout << "GetTopics" << std::endl; + auto nsTopics = NSProviderService::getInstance()->GetTopics(); + for (auto it : nsTopics->getTopicsList()) + { + + std::cout << it->getTopicName() << std::endl; + std::cout << (int) it->getState() << std::endl; + } + delete nsTopics; + } + break; +#ifdef WITH_CLOUD + case 11: + { + std::cout << "11. Enable NS Provider RemoteService" << std::endl; std::cout << "Input the Server Address :"; std::cin >> REMOTE_SERVER_ADDRESS; NSProviderService::getInstance()->EnableRemoteService(REMOTE_SERVER_ADDRESS); break; } - case 6: + case 12: { - std::cout << "3. Disable NS Provider RemoteService" << std::endl; + std::cout << "12. Disable NS Provider RemoteService" << std::endl; std::cout << "Input the Server Address :"; NSProviderService::getInstance()->DisableRemoteService(REMOTE_SERVER_ADDRESS); break; } #endif - case 9: + case 13: NSProviderService::getInstance()->Stop(); break; case 0: diff --git a/service/notification/cpp-wrapper/provider/inc/NSConsumer.h b/service/notification/cpp-wrapper/provider/inc/NSConsumer.h index ed2bc56..c71c666 100755 --- a/service/notification/cpp-wrapper/provider/inc/NSConsumer.h +++ b/service/notification/cpp-wrapper/provider/inc/NSConsumer.h @@ -30,6 +30,8 @@ #include #include "NSCommon.h" +#include "NSUtils.h" +#include "NSTopicsList.h" namespace OIC { @@ -50,7 +52,7 @@ namespace OIC /** * Constructor of NSConsumer. * - * @param consumerId - consumerId of the Notification service Consumer. + * @param consumerId -consumerId of the Notification service Consumer. */ NSConsumer(const std::string &consumerId) : m_consumerId(consumerId) {} @@ -82,6 +84,26 @@ namespace OIC */ int acceptSubscription(NSConsumer *consumer, bool accepted); + /** + * Select a topic name for a consumer + * @param[in] topicName Topic name to select + * @return :: OK or result code of NSResult + */ + NSResult selectTopic(const std::string &topicName); + + /** + * Unselect a topic from the topic list for consumer + * @param[in] topicName Topic name to unselect + * @return :: OK or result code of NSResult + */ + NSResult unselectTopic(const std::string &topicName); + + /** + * Request topic list with selection state for the consumer + * @return :: Topic list + */ + NSTopicsList *getConsumerTopics(); + private: ::NSConsumer *getNSConsumer(); diff --git a/service/notification/cpp-wrapper/provider/inc/NSProviderService.h b/service/notification/cpp-wrapper/provider/inc/NSProviderService.h index deed41f..75ca9a2 100755 --- a/service/notification/cpp-wrapper/provider/inc/NSProviderService.h +++ b/service/notification/cpp-wrapper/provider/inc/NSProviderService.h @@ -32,6 +32,7 @@ #include "NSSyncInfo.h" #include "NSMessage.h" #include "NSUtils.h" +#include "NSTopicsList.h" namespace OIC { @@ -81,15 +82,6 @@ namespace OIC } ProviderConfig; /** - * Access policy exchanged between provider and consumer during subscription process - */ - enum class NSAccessPolicy - { - NS_ACCESS_ALLOW = 0, - NS_ACCESS_DENY = 1, - }; - - /** * API for starting the NS Provider * * @return NSProviderService Pointer to singleton instance created @@ -147,13 +139,48 @@ namespace OIC NSMessage *CreateMessage(); /** + * request to get NSConsumer pointer + * @param id -id as string + * + * @return pointer to NSConsumer + */ + NSConsumer *getConsumer(const std::string &id); + + /** + * Add topic to topic list which is located in provider service storage + * @param[in] topicName Topic name to add + * @return :: OK or result code of NSResult + */ + NSResult AddTopic(const std::string &topicName); + + /** + * Delete topic from topic list + * @param[in] topicName Topic name to delete + * @return :: OK or result code of NSResult + */ + NSResult DeleteTopic(const std::string &topicName); + + /** + * Request topics list already registered by provider user + * @return :: Topic list + */ + NSTopicsList *GetTopics(); + + /** * get Provider config values * @return ProviderConfig callbaks set */ ProviderConfig getProviderConfig(); + /** + * get list of Consumers accepted. + * @return m_acceptedConsumers -list of accepted Consumers + */ + std::list getAcceptedConsumers(); + private : ProviderConfig m_config; + std::list m_acceptedConsumers; private: NSProviderService() @@ -161,7 +188,7 @@ namespace OIC m_config.m_subscribeRequestCb = NULL; m_config.m_syncInfoCb = NULL; } - ~NSProviderService() = default; + ~NSProviderService(); NSProviderService(const NSProviderService &) = delete; NSProviderService &operator=(const NSProviderService &) = delete; NSProviderService(const NSProviderService &&) = delete; diff --git a/service/notification/cpp-wrapper/provider/src/NSConsumer.cpp b/service/notification/cpp-wrapper/provider/src/NSConsumer.cpp index e4d35bf..7c99f3d 100755 --- a/service/notification/cpp-wrapper/provider/src/NSConsumer.cpp +++ b/service/notification/cpp-wrapper/provider/src/NSConsumer.cpp @@ -22,8 +22,8 @@ #include #include "NSProviderInterface.h" #include "NSConstants.h" -#include "NSUtils.h" #include "oic_string.h" +#include "oic_malloc.h" namespace OIC { @@ -57,5 +57,33 @@ namespace OIC NS_LOG(DEBUG, "acceptSubscription - OUT"); return NS_ERROR; } + + NSResult NSConsumer::selectTopic(const std::string &topicName) + { + NS_LOG(DEBUG, "selectTopic - IN"); + NSResult result = (NSResult) NSProviderSelectTopic(OICStrdup(getConsumerId().c_str()), + OICStrdup(topicName.c_str())); + NS_LOG(DEBUG, "selectTopic - OUT"); + return result; + } + + NSResult NSConsumer::unselectTopic(const std::string &topicName) + { + NS_LOG(DEBUG, "unselectTopic - IN"); + NSResult result = (NSResult) NSProviderUnselectTopic(OICStrdup(getConsumerId().c_str()), + OICStrdup(topicName.c_str())); + NS_LOG(DEBUG, "unselectTopic - OUT"); + return result; + } + + NSTopicsList *NSConsumer::getConsumerTopics() + { + NS_LOG(DEBUG, "getConsumerTopics - IN"); + ::NSTopicLL *topics = NSProviderGetConsumerTopics(OICStrdup(getConsumerId().c_str())); + + NSTopicsList *nsTopics = new NSTopicsList(topics); + NS_LOG(DEBUG, "getConsumerTopics - OUT"); + return nsTopics; + } } } diff --git a/service/notification/cpp-wrapper/provider/src/NSProviderService.cpp b/service/notification/cpp-wrapper/provider/src/NSProviderService.cpp index af34fa6..0df46f7 100755 --- a/service/notification/cpp-wrapper/provider/src/NSProviderService.cpp +++ b/service/notification/cpp-wrapper/provider/src/NSProviderService.cpp @@ -23,10 +23,11 @@ #include #include "NSCommon.h" #include "NSProviderInterface.h" -#include "oic_string.h" #include "NSConsumer.h" #include "NSSyncInfo.h" #include "NSConstants.h" +#include "oic_string.h" +#include "oic_malloc.h" namespace OIC { @@ -38,7 +39,7 @@ namespace OIC NSConsumer *nsConsumer = new NSConsumer(consumer); if (NSProviderService::getInstance()->getProviderConfig().m_subscribeRequestCb != NULL) NSProviderService::getInstance()->getProviderConfig().m_subscribeRequestCb(nsConsumer); - delete nsConsumer; + NSProviderService::getInstance()->getAcceptedConsumers().push_back(nsConsumer); NS_LOG(DEBUG, "onConsumerSubscribedCallback - OUT"); } @@ -72,6 +73,15 @@ namespace OIC return nsMsg; } + NSProviderService::~NSProviderService() + { + for (auto it : getAcceptedConsumers()) + { + delete it; + } + getAcceptedConsumers().clear(); + } + NSProviderService *NSProviderService::getInstance() { static NSProviderService s_instance; @@ -134,7 +144,7 @@ namespace OIC NSResult result = NSResult::ERROR; if (msg != nullptr) { - ::NSMessage * nsMsg = getNSMessage(msg); + ::NSMessage *nsMsg = getNSMessage(msg); result = (NSResult) NSSendMessage(nsMsg); delete nsMsg->mediaContents; delete nsMsg; @@ -168,9 +178,50 @@ namespace OIC return nsMessage; } + NSConsumer *NSProviderService::getConsumer(const std::string &id) + { + for (auto it : getAcceptedConsumers()) + { + if (it->getConsumerId() == id) + return it; + } + return NULL; + } + + NSResult NSProviderService::AddTopic(const std::string &topicName) + { + NS_LOG(DEBUG, "AddTopic - IN"); + NSResult result = (NSResult) NSProviderAddTopic(OICStrdup(topicName.c_str())); + NS_LOG(DEBUG, "AddTopic - OUT"); + return result; + } + + NSResult NSProviderService::DeleteTopic(const std::string &topicName) + { + NS_LOG(DEBUG, "DeleteTopic - IN"); + NSResult result = (NSResult) NSProviderDeleteTopic(OICStrdup(topicName.c_str())); + NS_LOG(DEBUG, "DeleteTopic - OUT"); + return result; + } + + NSTopicsList *NSProviderService::GetTopics() + { + NS_LOG(DEBUG, "GetTopics - IN"); + ::NSTopicLL *topics = NSProviderGetTopics(); + + NSTopicsList *nsTopics = new NSTopicsList(topics); + NS_LOG(DEBUG, "GetTopics - OUT"); + return nsTopics; + } + NSProviderService::ProviderConfig NSProviderService::getProviderConfig() { return m_config; } + + std::list NSProviderService::getAcceptedConsumers() + { + return m_acceptedConsumers; + } } } -- 2.7.4