From 2b2940031dcb584c6c1f54ed05d00dcce3c2eb24 Mon Sep 17 00:00:00 2001 From: "abitha.s" Date: Wed, 8 Feb 2017 14:46:32 +0530 Subject: [PATCH] [IOT-1808]Changes in C++ APIs to remove TopicList raw pointers. 1. Changes in NSProvider APIs for get and set TopicsList to shared pointers. 2. Changes in NSConsumer API for get ConsumerTopicList to shared pointer 3. Changes in NSProviderService API to get RegisteredTopicsList to shared pointer. 4. changes to return as object of list of Topics in TopicsList Patch 2 : rebased patch 3 : added shared_ptr changes to setTopicList as per review patch 4 : added modifiability feature for preventing TopicsList modification patch 5 : added unsetModifiability to only preven modification patch 6 : rebased Change-Id: I3aacef4f3a5809623add69a16fd2ad9f93d11dd9 Signed-off-by: abitha.s Reviewed-on: https://gerrit.iotivity.org/gerrit/17113 Tested-by: jenkins-iotivity Reviewed-by: Uze Choi --- .../main/jni/consumer/JniNotificationConsumer.cpp | 17 +++---- .../main/jni/provider/JniNotificationProvider.cpp | 12 ++--- .../cpp-wrapper/common/NSTopicsList.cpp | 53 +++++++++++++++++----- .../notification/cpp-wrapper/common/NSTopicsList.h | 16 +++++-- .../cpp-wrapper/consumer/inc/NSProvider.h | 16 +++---- .../cpp-wrapper/consumer/src/NSConsumerService.cpp | 5 +- .../cpp-wrapper/consumer/src/NSProvider.cpp | 37 ++++++--------- .../examples/linux/notificationserviceconsumer.cpp | 11 ++--- .../examples/linux/notificationserviceprovider.cpp | 10 ++-- .../cpp-wrapper/provider/inc/NSConsumer.h | 3 +- .../cpp-wrapper/provider/inc/NSProviderService.h | 2 +- .../cpp-wrapper/provider/src/NSConsumer.cpp | 4 +- .../cpp-wrapper/provider/src/NSProviderService.cpp | 4 +- .../cpp-wrapper/unittest/NSConsumerServiceTest.cpp | 12 ++--- .../cpp-wrapper/unittest/NSProviderServiceTest.cpp | 41 +++++------------ 15 files changed, 124 insertions(+), 119 deletions(-) diff --git a/service/notification/android/notification-service/src/main/jni/consumer/JniNotificationConsumer.cpp b/service/notification/android/notification-service/src/main/jni/consumer/JniNotificationConsumer.cpp index fe22c8b..cfe8c20 100755 --- a/service/notification/android/notification-service/src/main/jni/consumer/JniNotificationConsumer.cpp +++ b/service/notification/android/notification-service/src/main/jni/consumer/JniNotificationConsumer.cpp @@ -200,7 +200,7 @@ jobject getJavaTopicState(JNIEnv *env, OIC::Service::NSTopic::NSTopicState nsSta return topicState; } -jobject getJavaTopicsList(JNIEnv *env, OIC::Service::NSTopicsList *topicList) +jobject getJavaTopicsList(JNIEnv *env, std::shared_ptr topicList) { NS_LOGD ("ConsumerService_getJavaTopicsList - IN"); jclass cls_topicList = (jclass) (env->NewLocalRef(g_cls_TopicsList)); @@ -230,8 +230,8 @@ jobject getJavaTopicsList(JNIEnv *env, OIC::Service::NSTopicsList *topicList) } for (auto it : topicList->getTopicsList()) { - jobject jState = getJavaTopicState(env, it->getState()); - std::string topicName = it->getTopicName(); + jobject jState = getJavaTopicState(env, it.getState()); + std::string topicName = it.getTopicName(); jstring jTopicName = env->NewStringUTF(topicName.c_str()); env->CallVoidMethod(obj_topicList, mid_addTopic, jTopicName, jState); } @@ -303,7 +303,7 @@ const char *getNativeTopicName(JNIEnv *env, jobject jTopic) } -OIC::Service::NSTopicsList *getNativeTopicsList(JNIEnv *env, jobject jTopicList) +std::shared_ptr getNativeTopicsList(JNIEnv *env, jobject jTopicList) { NS_LOGD ("ConsumerService_getNativeTopicsList - IN"); @@ -344,28 +344,25 @@ OIC::Service::NSTopicsList *getNativeTopicsList(JNIEnv *env, jobject jTopicList) NS_LOGE ("Error: MethodId for Vector get not found"); return nullptr; } - OIC::Service::NSTopicsList *nsTopicList = new OIC::Service::NSTopicsList(); + std::shared_ptr nsTopicList = std::make_shared(); for (int index = 0; index < size; index++) { jobject topicObj = env->CallObjectMethod(jobj, getMethod, index); if (topicObj == NULL) { NS_LOGE ("Error: object of field Topic is null"); - delete nsTopicList; return nullptr; } const char *name = getNativeTopicName(env, topicObj); if (name == nullptr) { NS_LOGE ("Error: Couldn't find topic Name"); - delete nsTopicList; return nullptr; } std::string topicName(name); OIC::Service::NSTopic::NSTopicState state = OIC::Service::NSTopic::NSTopicState::UNSUBSCRIBED; if (!getNativeTopicState(env, topicObj, state)) { - delete nsTopicList; return nullptr; } nsTopicList->addTopic(topicName, state); @@ -1347,7 +1344,7 @@ JNIEXPORT jobject JNICALL Java_org_iotivity_service_ns_consumer_Provider_nativeG return NULL; } jlong jProvider = env->GetLongField(jObj, nativeHandle); - OIC::Service::NSTopicsList *topicList = nullptr; + std::shared_ptr topicList = nullptr; if (jProvider) { NS_LOGD ("calling subscribe on mNativeHandle"); @@ -1387,7 +1384,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_service_ns_consumer_Provider_nativeUpda ThrowNSException(JNI_INVALID_VALUE, "TopicList cannot be null"); return; } - OIC::Service::NSTopicsList *nsTopicsList = getNativeTopicsList(env, jTopicsList); + std::shared_ptr nsTopicsList = getNativeTopicsList(env, jTopicsList); if (nsTopicsList == nullptr) { ThrowNSException(JNI_INVALID_VALUE, "NSTopicList cannot be created "); diff --git a/service/notification/android/notification-service/src/main/jni/provider/JniNotificationProvider.cpp b/service/notification/android/notification-service/src/main/jni/provider/JniNotificationProvider.cpp index 64771ab..d16c2a6 100755 --- a/service/notification/android/notification-service/src/main/jni/provider/JniNotificationProvider.cpp +++ b/service/notification/android/notification-service/src/main/jni/provider/JniNotificationProvider.cpp @@ -608,7 +608,7 @@ jobject getJavaTopicState(JNIEnv *env, OIC::Service::NSTopic::NSTopicState nsSta return topicState; } -jobject getJavaTopicsList(JNIEnv *env, OIC::Service::NSTopicsList *topicList) +jobject getJavaTopicsList(JNIEnv *env, std::shared_ptr topicList) { NS_LOGD ("JNIProviderService: getJavaTopicsList - IN"); jclass cls_topicList = (jclass) (env->NewLocalRef(g_cls_TopicsList)); @@ -638,8 +638,8 @@ jobject getJavaTopicsList(JNIEnv *env, OIC::Service::NSTopicsList *topicList) } for (auto it : topicList->getTopicsList()) { - jobject jState = getJavaTopicState(env, it->getState()); - std::string topicName = it->getTopicName(); + jobject jState = getJavaTopicState(env, it.getState()); + std::string topicName = it.getTopicName(); jstring jTopicName = env->NewStringUTF(topicName.c_str()); env->CallVoidMethod(obj_topicList, mid_addTopic, jTopicName, jState); } @@ -1151,7 +1151,7 @@ Java_org_iotivity_service_ns_provider_ProviderService_nativeGetRegisteredTopicLi { NS_LOGD ("JNIProviderService: nativeGetRegisteredTopicList - IN"); - OIC::Service::NSTopicsList *topicList = + std::shared_ptr topicList = OIC::Service::NSProviderService::getInstance()->getRegisteredTopicList(); if (topicList == nullptr) { @@ -1160,7 +1160,6 @@ Java_org_iotivity_service_ns_provider_ProviderService_nativeGetRegisteredTopicLi } jobject obj_topicList = getJavaTopicsList(env, topicList); - delete topicList; NS_LOGD ("JNIProviderService: nativeGetRegisteredTopicList - OUT"); return obj_topicList; @@ -1281,14 +1280,13 @@ JNIEXPORT jobject JNICALL Java_org_iotivity_service_ns_provider_Consumer_nativeG return NULL; } env->ReleaseStringUTFChars(jConsumerId, id); - OIC::Service::NSTopicsList *topicList = nsConsumer->getConsumerTopicList(); + std::shared_ptr topicList = nsConsumer->getConsumerTopicList(); if (topicList == nullptr) { ThrowNSException(JNI_NO_NATIVE_POINTER, "Topic List doesn't exist"); return NULL; } jobject obj_topicList = getJavaTopicsList(env, topicList); - delete topicList; NS_LOGD ("JNIProviderService: nativeGetConsumerTopicList - OUT"); return obj_topicList; diff --git a/service/notification/cpp-wrapper/common/NSTopicsList.cpp b/service/notification/cpp-wrapper/common/NSTopicsList.cpp index 915547b..ff2b0e3 100755 --- a/service/notification/cpp-wrapper/common/NSTopicsList.cpp +++ b/service/notification/cpp-wrapper/common/NSTopicsList.cpp @@ -25,14 +25,16 @@ namespace OIC { namespace Service { - NSTopicsList::NSTopicsList(::NSTopicLL *topics) + NSTopicsList::NSTopicsList(::NSTopicLL *topics, bool modify) { ::NSTopicLL *topicsNode = nullptr; topicsNode = topics; + m_modifiable = modify; while (topicsNode != nullptr) { - addTopic(topicsNode->topicName, (NSTopic::NSTopicState)topicsNode->state); + m_topicsList.push_back(new NSTopic( + topicsNode->topicName, (NSTopic::NSTopicState)topicsNode->state)); topicsNode = topicsNode->next; } @@ -41,47 +43,74 @@ namespace OIC { for (auto it : topicsList.getTopicsList()) { - addTopic(it->getTopicName(), it->getState()); + m_topicsList.push_back(new NSTopic(it.getTopicName(), it.getState())); } + m_modifiable = false; } NSTopicsList &NSTopicsList::operator=(const NSTopicsList &topicsList) { for (auto it : topicsList.getTopicsList()) { - this->addTopic(it->getTopicName(), it->getState()); + this->m_topicsList.push_back(new NSTopic(it.getTopicName(), it.getState())); } return *this; + m_modifiable = false; } NSTopicsList::~NSTopicsList() { - for (auto it : getTopicsList()) + for (auto it : m_topicsList) { delete it; } - getTopicsList().clear(); + m_topicsList.clear(); } void NSTopicsList::addTopic(const std::string &topicName, NSTopic::NSTopicState state) { - m_topicsList.push_back(new NSTopic(topicName, state)); + if(m_modifiable) + { + m_topicsList.push_back(new NSTopic(topicName, state)); + } + else + { + //TODO: add exception code for Invalid operation + } } void NSTopicsList::removeTopic(const std::string &topicName) { - for (auto it : getTopicsList()) + if(m_modifiable) { - if (it->getTopicName().compare(topicName) == 0) + for (auto it : m_topicsList) { - m_topicsList.remove(it); + if (it->getTopicName().compare(topicName) == 0) + { + m_topicsList.remove(it); + } } } + else + { + //TODO: add exception code for Invalid operation + } + } + + std::list NSTopicsList::getTopicsList() const + { + std::list topicList; + for (auto it : m_topicsList) + { + NSTopic topic(it->getTopicName(), it->getState()); + topicList.push_back(topic); + } + return topicList; } - std::list NSTopicsList::getTopicsList() const + void NSTopicsList::unsetModifiability() { - return m_topicsList; + m_modifiable = false; } } } diff --git a/service/notification/cpp-wrapper/common/NSTopicsList.h b/service/notification/cpp-wrapper/common/NSTopicsList.h index f868bd2..288882c 100755 --- a/service/notification/cpp-wrapper/common/NSTopicsList.h +++ b/service/notification/cpp-wrapper/common/NSTopicsList.h @@ -46,14 +46,14 @@ namespace OIC /** * Constructor of NSTopicsList. */ - NSTopicsList() = default; + NSTopicsList() : m_modifiable(true) { } /** * Constructor of NSTopicsList. * * @param topics - pointer to NSTopicLL struct to initialize. */ - NSTopicsList(::NSTopicLL *topics); + NSTopicsList(::NSTopicLL *topics, bool modify); /** * Copy Constructor of NSTopicsList. @@ -95,10 +95,20 @@ namespace OIC * * @return topic as NSTopics Linked list. */ - std::list getTopicsList() const; + std::list getTopicsList() const; + + /** + * This method is for preventing for add/remove Topic from Topicslist. + * Below method restricts the application from illegally modifying Topics when + * Provider is in Invalid state. By calling the API, the service prevents and protects + * the integrity of TopicsList updation when the associated object is Invalid. + * + */ + void unsetModifiability(); private: std::list m_topicsList; + bool m_modifiable; }; } diff --git a/service/notification/cpp-wrapper/consumer/inc/NSProvider.h b/service/notification/cpp-wrapper/consumer/inc/NSProvider.h index 170c4f2..10c26e1 100755 --- a/service/notification/cpp-wrapper/consumer/inc/NSProvider.h +++ b/service/notification/cpp-wrapper/consumer/inc/NSProvider.h @@ -69,7 +69,7 @@ namespace OIC /** * Constructor of NSProvider. */ - NSProvider(): m_topicList(new NSTopicsList()), m_state(NSProviderState::DENY), + NSProvider(): m_topicList(std::make_shared()), m_state(NSProviderState::DENY), m_subscribedState(NSProviderSubscribedState::DENY), m_stateCb(NULL), m_messageCb(NULL), m_syncInfoCb(NULL) { @@ -81,7 +81,7 @@ namespace OIC * @param providerId - providerId of the Notification. */ NSProvider(const std::string &providerId) : m_providerId(providerId), - m_topicList(new NSTopicsList()), m_state(NSProviderState::DENY), + m_topicList(std::make_shared()), m_state(NSProviderState::DENY), m_subscribedState(NSProviderSubscribedState::DENY), m_stateCb(NULL), m_messageCb(NULL), m_syncInfoCb(NULL) { @@ -93,7 +93,7 @@ namespace OIC * @param providerId - providerId of the Notification. * @param topicList - NSTopicsList of interested Topics. */ - NSProvider(const std::string &providerId, NSTopicsList *topicList) : m_providerId( + NSProvider(const std::string &providerId, std::shared_ptr topicList) : m_providerId( providerId), m_topicList(topicList), m_state(NSProviderState::DENY), m_subscribedState(NSProviderSubscribedState::DENY), m_stateCb(NULL), m_messageCb(NULL), m_syncInfoCb(NULL) @@ -125,7 +125,7 @@ namespace OIC /** * Destructor of NSProvider. */ - ~NSProvider(); + ~NSProvider() = default; /** * This method is for getting ProviderId from the Notification service provider. @@ -139,7 +139,7 @@ namespace OIC * * @return NSTopicsList as pointer. */ - NSTopicsList *getTopicList() const; + std::shared_ptr getTopicList() const; /** * Update Topic list that is wanted to be subscribed from provider @@ -147,7 +147,7 @@ namespace OIC * @param topicList - NSTopicsList of interested Topics. * @return NSResult */ - NSResult updateTopicList(NSTopicsList *topicList); + NSResult updateTopicList(std::shared_ptr topicList); /** * This method is for getting ProviderState from the Notification service provider. @@ -238,7 +238,7 @@ namespace OIC * * @param topicsList as NSTopicsList pointer. */ - void setTopicList(NSTopicsList *topicsList); + void setTopicList(std::shared_ptr topicsList); /** * This method is for setting ProviderState for the Notification service provider. @@ -260,7 +260,7 @@ namespace OIC private: std::string m_providerId; - NSTopicsList *m_topicList; + std::shared_ptr m_topicList; NSProviderState m_state; NSProviderSubscribedState m_subscribedState; diff --git a/service/notification/cpp-wrapper/consumer/src/NSConsumerService.cpp b/service/notification/cpp-wrapper/consumer/src/NSConsumerService.cpp index 1aca21e..48debdf 100755 --- a/service/notification/cpp-wrapper/consumer/src/NSConsumerService.cpp +++ b/service/notification/cpp-wrapper/consumer/src/NSConsumerService.cpp @@ -53,7 +53,7 @@ namespace OIC auto discoveredCallback = NSConsumerService::getInstance()->getProviderDiscoveredCb(); nsProvider->setProviderState((NSProviderState)state); auto topicLL = NSConsumerGetTopicList(provider->providerId); - nsProvider->setTopicList(new NSTopicsList(topicLL)); + nsProvider->setTopicList(std::make_shared(topicLL, false)); NSConsumerService::getInstance()->getAcceptedProviders()->addProvider(nsProvider); if (state == NS_DISCOVERED) { @@ -120,7 +120,7 @@ namespace OIC else if (state == NS_TOPIC) { auto topicLL = NSConsumerGetTopicList(provider->providerId); - oldProvider->setTopicList(new NSTopicsList(topicLL)); + oldProvider->setTopicList(std::make_shared(topicLL, false)); if (changeCallback != NULL) { NS_LOG(DEBUG, "initiating the callback for Response : NS_TOPIC"); @@ -147,6 +147,7 @@ namespace OIC else if (state == NS_STOPPED) { oldProvider->setProviderSubscribedState(NSProviderSubscribedState::DENY); + oldProvider->getTopicList()->unsetModifiability(); NSConsumerService::getInstance()->getAcceptedProviders()->removeProvider( oldProvider->getProviderId()); NS_LOG(DEBUG, "initiating the State callback : NS_STOPPED"); diff --git a/service/notification/cpp-wrapper/consumer/src/NSProvider.cpp b/service/notification/cpp-wrapper/consumer/src/NSProvider.cpp index 5dd53c1..00bb053 100755 --- a/service/notification/cpp-wrapper/consumer/src/NSProvider.cpp +++ b/service/notification/cpp-wrapper/consumer/src/NSProvider.cpp @@ -47,7 +47,7 @@ namespace OIC m_state = NSProviderState::DENY; m_subscribedState = NSProviderSubscribedState::DENY; - m_topicList = new NSTopicsList(); + m_topicList = std::make_shared(); if (provider != nullptr) { @@ -58,13 +58,13 @@ namespace OIC NSProvider::NSProvider(const NSProvider &provider) { m_providerId = provider.getProviderId(); - m_topicList = new NSTopicsList(); + m_topicList = std::make_shared(); auto topicsList = provider.getTopicList(); if (topicsList != nullptr) { for (auto it : topicsList->getTopicsList()) { - getTopicList()->addTopic(it->getTopicName(), it->getState()); + getTopicList()->addTopic(it.getTopicName(), it.getState()); } } setListener(provider.getProviderStateReceivedCb(), provider.getMessageReceivedCb(), @@ -76,13 +76,13 @@ namespace OIC NSProvider &NSProvider::operator=(const NSProvider &provider) { this->m_providerId = provider.getProviderId(); - this->m_topicList = new NSTopicsList(); + this->m_topicList = std::make_shared(); auto topicsList = provider.getTopicList(); if (topicsList != nullptr) { for (auto it : topicsList->getTopicsList()) { - this->getTopicList()->addTopic(it->getTopicName(), it->getState()); + this->getTopicList()->addTopic(it.getTopicName(), it.getState()); } } this->setListener(provider.getProviderStateReceivedCb(), provider.getMessageReceivedCb(), @@ -92,35 +92,28 @@ namespace OIC return *this; } - NSProvider::~NSProvider() - { - if (m_topicList != nullptr) - { - delete m_topicList; - } - } - std::string NSProvider::getProviderId() const { return m_providerId; } - NSTopicsList *NSProvider::getTopicList() const + std::shared_ptr NSProvider::getTopicList() const { NS_LOG(DEBUG, "getTopicList - IN"); if (!isValid()) { return nullptr; } - NSTopicsList *topicList = new NSTopicsList(); + std::shared_ptr topicList = std::make_shared(); for (auto it : m_topicList->getTopicsList()) { - topicList->addTopic(it->getTopicName(), it->getState()); + topicList->addTopic(it.getTopicName(), it.getState()); } + topicList->unsetModifiability(); return topicList; } - NSResult NSProvider::updateTopicList(NSTopicsList *topicList) + NSResult NSProvider::updateTopicList(std::shared_ptr topicList) { NS_LOG(DEBUG, "updateTopicList - IN"); if (!isValid()) @@ -142,8 +135,8 @@ namespace OIC return NSResult::ERROR; } topic->topicName = NULL; - topic->topicName = OICStrdup(it->getTopicName().c_str()); - topic->state = (::NSTopicState)it->getState(); + topic->topicName = OICStrdup(it.getTopicName().c_str()); + topic->state = (::NSTopicState)it.getState(); topic->next = NULL; if (topicLL == NULL) { @@ -295,12 +288,8 @@ namespace OIC return m_syncInfoCb; } - void NSProvider::setTopicList(NSTopicsList *topicsList) + void NSProvider::setTopicList(std::shared_ptr topicsList) { - if (m_topicList != nullptr) - { - delete m_topicList; - } m_topicList = topicsList; } diff --git a/service/notification/cpp-wrapper/examples/linux/notificationserviceconsumer.cpp b/service/notification/cpp-wrapper/examples/linux/notificationserviceconsumer.cpp index 2276a7c..cb7675c 100755 --- a/service/notification/cpp-wrapper/examples/linux/notificationserviceconsumer.cpp +++ b/service/notification/cpp-wrapper/examples/linux/notificationserviceconsumer.cpp @@ -117,8 +117,8 @@ void onProviderStateChangedCb(OIC::Service::NSProviderState state) { for (auto it : topicList->getTopicsList()) { - std::cout << "Topic Name: " << it->getTopicName() << std::endl; - std::cout << "Topic state: " << (int) it->getState() << std::endl; + std::cout << "Topic Name: " << it.getTopicName() << std::endl; + std::cout << "Topic state: " << (int) it.getState() << std::endl; } } } @@ -288,8 +288,8 @@ int main(void) { for (auto it : topicList->getTopicsList()) { - std::cout << "Topic Name: " << it->getTopicName() << std::endl; - std::cout << "Topic state: " << (int) it->getState() << std::endl; + std::cout << "Topic Name: " << it.getTopicName() << std::endl; + std::cout << "Topic state: " << (int) it.getState() << std::endl; } } } @@ -302,13 +302,12 @@ int main(void) NSConsumerService::getInstance()->getProvider(mainProvider); if (provider != nullptr) { - NSTopicsList *topicList = new NSTopicsList(); + std::shared_ptr topicList = std::make_shared(); topicList->addTopic("OCF_TOPIC1", NSTopic::NSTopicState::SUBSCRIBED); topicList->addTopic("OCF_TOPIC2", NSTopic::NSTopicState::SUBSCRIBED); topicList->addTopic("OCF_TOPIC3", NSTopic::NSTopicState::UNSUBSCRIBED); provider->updateTopicList(topicList); - delete topicList; } break; } diff --git a/service/notification/cpp-wrapper/examples/linux/notificationserviceprovider.cpp b/service/notification/cpp-wrapper/examples/linux/notificationserviceprovider.cpp index 03dca85..87d33d6 100755 --- a/service/notification/cpp-wrapper/examples/linux/notificationserviceprovider.cpp +++ b/service/notification/cpp-wrapper/examples/linux/notificationserviceprovider.cpp @@ -353,10 +353,9 @@ int main() for (auto it : nsTopics->getTopicsList()) { - std::cout << it->getTopicName() << std::endl; - std::cout << (int) it->getState() << std::endl; + std::cout << it.getTopicName() << std::endl; + std::cout << (int) it.getState() << std::endl; } - delete nsTopics; } std::cout << "GetConsumerTopicList completed" << std::endl; } @@ -370,10 +369,9 @@ int main() for (auto it : nsTopics->getTopicsList()) { - std::cout << it->getTopicName() << std::endl; - std::cout << (int) it->getState() << std::endl; + std::cout << it.getTopicName() << std::endl; + std::cout << (int) it.getState() << std::endl; } - delete nsTopics; break; } #ifdef WITH_CLOUD diff --git a/service/notification/cpp-wrapper/provider/inc/NSConsumer.h b/service/notification/cpp-wrapper/provider/inc/NSConsumer.h index 36506e8..6a3f89d 100755 --- a/service/notification/cpp-wrapper/provider/inc/NSConsumer.h +++ b/service/notification/cpp-wrapper/provider/inc/NSConsumer.h @@ -29,6 +29,7 @@ #include +#include #include "NSCommon.h" #include "NSUtils.h" #include "NSTopicsList.h" @@ -104,7 +105,7 @@ namespace OIC * Request topic list with selection state for the consumer * @return :: Topic list */ - NSTopicsList *getConsumerTopicList(); + std::shared_ptr getConsumerTopicList(); private: ::NSConsumer *getNSConsumer(); diff --git a/service/notification/cpp-wrapper/provider/inc/NSProviderService.h b/service/notification/cpp-wrapper/provider/inc/NSProviderService.h index 86fa446..575ea93 100755 --- a/service/notification/cpp-wrapper/provider/inc/NSProviderService.h +++ b/service/notification/cpp-wrapper/provider/inc/NSProviderService.h @@ -165,7 +165,7 @@ namespace OIC * Request topics list already registered by provider user * @return :: Topic list */ - NSTopicsList *getRegisteredTopicList(); + std::shared_ptr getRegisteredTopicList(); /** * get Provider config values diff --git a/service/notification/cpp-wrapper/provider/src/NSConsumer.cpp b/service/notification/cpp-wrapper/provider/src/NSConsumer.cpp index 86649e3..0d675ab 100755 --- a/service/notification/cpp-wrapper/provider/src/NSConsumer.cpp +++ b/service/notification/cpp-wrapper/provider/src/NSConsumer.cpp @@ -89,7 +89,7 @@ namespace OIC return result; } - NSTopicsList *NSConsumer::getConsumerTopicList() + std::shared_ptr NSConsumer::getConsumerTopicList() { NS_LOG(DEBUG, "getConsumerTopicList - IN"); if (!isValid()) @@ -98,7 +98,7 @@ namespace OIC } ::NSTopicLL *topics = NSProviderGetConsumerTopics(getConsumerId().c_str()); - NSTopicsList *nsTopics = new NSTopicsList(topics); + std::shared_ptr nsTopics = std::make_shared(topics, false); NS_LOG(DEBUG, "getConsumerTopicList - OUT"); return nsTopics; } diff --git a/service/notification/cpp-wrapper/provider/src/NSProviderService.cpp b/service/notification/cpp-wrapper/provider/src/NSProviderService.cpp index e349795..094c4e4 100755 --- a/service/notification/cpp-wrapper/provider/src/NSProviderService.cpp +++ b/service/notification/cpp-wrapper/provider/src/NSProviderService.cpp @@ -257,12 +257,12 @@ namespace OIC return result; } - NSTopicsList *NSProviderService::getRegisteredTopicList() + std::shared_ptr NSProviderService::getRegisteredTopicList() { NS_LOG(DEBUG, "getRegisteredTopicList - IN"); ::NSTopicLL *topics = NSProviderGetTopics(); - NSTopicsList *nsTopics = new NSTopicsList(topics); + std::shared_ptr nsTopics = std::make_shared(topics, false); NS_LOG(DEBUG, "getRegisteredTopicList - OUT"); return nsTopics; } diff --git a/service/notification/cpp-wrapper/unittest/NSConsumerServiceTest.cpp b/service/notification/cpp-wrapper/unittest/NSConsumerServiceTest.cpp index 0168227..caa1502 100755 --- a/service/notification/cpp-wrapper/unittest/NSConsumerServiceTest.cpp +++ b/service/notification/cpp-wrapper/unittest/NSConsumerServiceTest.cpp @@ -528,12 +528,12 @@ TEST_F(NotificationServiceConsumerTest, ExpectEQTopicList) ASSERT_NE(nullptr, g_provider) << "error: discovery failure"; - std::list retTopic = g_provider->getTopicList()->getTopicsList(); + auto retTopic = g_provider->getTopicList()->getTopicsList(); auto it1 = retTopic.begin(); auto it2 = topics.begin(); while ( it1 != retTopic.end() || it2 != topics.end() ) { - if ((*it1)->getTopicName() != *it2) + if ((*it1).getTopicName() != *it2) { isSame = false; break; } @@ -548,12 +548,12 @@ TEST_F(NotificationServiceConsumerTest, ExpectFailUpdateTopicOnConsumer) { ASSERT_NE(nullptr, g_provider) << "error: discovery failure"; - OIC::Service::NSTopicsList *retTopic = g_provider->getTopicList(); + auto retTopic = g_provider->getTopicList(); for (auto it : retTopic->getTopicsList()) { - std::cout << "Topic Name: " << it->getTopicName() << std::endl; - std::cout << "state : " << (int) it->getState() << std::endl; - it->setState(OIC::Service::NSTopic::NSTopicState::SUBSCRIBED); + std::cout << "Topic Name: " << it.getTopicName() << std::endl; + std::cout << "state : " << (int) it.getState() << std::endl; + it.setState(OIC::Service::NSTopic::NSTopicState::SUBSCRIBED); } OIC::Service::NSResult ret = g_provider->updateTopicList(retTopic); std::cout << "ret : " << (int) ret << std::endl; diff --git a/service/notification/cpp-wrapper/unittest/NSProviderServiceTest.cpp b/service/notification/cpp-wrapper/unittest/NSProviderServiceTest.cpp index d2a45b6..7b6cd1c 100755 --- a/service/notification/cpp-wrapper/unittest/NSProviderServiceTest.cpp +++ b/service/notification/cpp-wrapper/unittest/NSProviderServiceTest.cpp @@ -222,7 +222,6 @@ TEST_F(NotificationProviderServiceTest, NeverCallNotifyOnConsumerByAcceptIsFalse ASSERT_NE(nullptr, g_consumer) << "error: discovery failure"; g_consumer->acceptSubscription(false); - OIC::Service::NSMessage msg = OIC::Service::NSProviderService::getInstance()->createMessage(); msgID = (int)msg.getMessageId(); msg.setTitle(std::string("Title")); @@ -334,7 +333,7 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualAddedTopicsAndRegisteredTopic std::unique_lock< std::mutex > lock { mutexForCondition }; responseCon.wait_for(lock, g_waitForResponse); bool isSame = false; - OIC::Service::NSTopicsList *topicList = + auto topicList = OIC::Service::NSProviderService::getInstance()->getRegisteredTopicList(); if (!topicList) { @@ -347,7 +346,7 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualAddedTopicsAndRegisteredTopic int i = 0; for (auto itr : topicList->getTopicsList()) { - compString[i] = itr->getTopicName(); i++; + compString[i] = itr.getTopicName(); i++; } std::cout << compString[0] << std::endl; std::cout << compString[1] << std::endl; @@ -360,10 +359,6 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualAddedTopicsAndRegisteredTopic OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1); OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2); - if (topicList != nullptr) - { - delete topicList; - } responseCon.wait_for(lock, g_waitForResponse); } @@ -377,7 +372,7 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualUnregisteredTopicsAndRegister std::unique_lock< std::mutex > lock { mutexForCondition }; responseCon.wait_for(lock, g_waitForResponse); bool isSame = false; - OIC::Service::NSTopicsList *topicList = + auto topicList = OIC::Service::NSProviderService::getInstance()->getRegisteredTopicList(); if (!topicList) { @@ -386,8 +381,9 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualUnregisteredTopicsAndRegister } else { - std::list::iterator it = topicList->getTopicsList().begin(); - std::string compStr = (*it)->getTopicName() ; + auto topic = topicList->getTopicsList(); + auto it = topic.begin(); + std::string compStr = (*it).getTopicName() ; std::cout << compStr << std::endl; if (str1.compare(compStr) == 0 ) { @@ -397,10 +393,6 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualUnregisteredTopicsAndRegister EXPECT_EQ(isSame, true); OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1); - if (topicList != nullptr) - { - delete topicList; - } responseCon.wait_for(lock, g_waitForResponse); } @@ -419,7 +411,7 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualSetConsumerTopicsAndGetConsum responseCon.wait_for(lock, g_waitForResponse); bool isSame = false; - OIC::Service::NSTopicsList *topicList = g_consumer->getConsumerTopicList(); + auto topicList = g_consumer->getConsumerTopicList(); if (!topicList) { @@ -432,8 +424,8 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualSetConsumerTopicsAndGetConsum int i = 0, state[10] = {0}; for (auto itr : topicList->getTopicsList()) { - compString[i] = itr->getTopicName(); - state[i++] = (int) itr->getState(); + compString[i] = itr.getTopicName(); + state[i++] = (int) itr.getState(); } std::cout << compString[0] << std::endl; std::cout << compString[1] << std::endl; @@ -448,10 +440,6 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualSetConsumerTopicsAndGetConsum OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1); OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2); - if (topicList != nullptr) - { - delete topicList; - } responseCon.wait_for(lock, g_waitForResponse); } @@ -472,7 +460,7 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualUnSetConsumerTopicsAndGetCons responseCon.wait_for(lock, g_waitForResponse); bool isSame = false; - OIC::Service::NSTopicsList *topicList = g_consumer->getConsumerTopicList(); + auto topicList = g_consumer->getConsumerTopicList(); if (!topicList) { @@ -485,8 +473,8 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualUnSetConsumerTopicsAndGetCons int i = 0, state[10] = {0}; for (auto itr : topicList->getTopicsList()) { - compString[i] = itr->getTopicName(); - state[i++] = (int) itr->getState(); + compString[i] = itr.getTopicName(); + state[i++] = (int) itr.getState(); } std::cout << compString[0] << std::endl; std::cout << compString[1] << std::endl; @@ -501,11 +489,6 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualUnSetConsumerTopicsAndGetCons OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1); OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2); - - if (topicList != nullptr) - { - delete topicList; - } responseCon.wait_for(lock, g_waitForResponse); } -- 2.7.4