From: abitha.s Date: Tue, 15 Nov 2016 12:47:45 +0000 (+0530) Subject: Updated with Valgrind memory leak fixes for Notification Provider and consumer. X-Git-Tag: 1.2.1~80 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=143cc22acf28d20200c1f06cb60b30a249e9f354;p=platform%2Fupstream%2Fiotivity.git Updated with Valgrind memory leak fixes for Notification Provider and consumer. Updated NS sample App to allow/deny consumer subscription. Change-Id: I6f3609895cbfe2d4d8b18247b7e7dd4c7a438161 Signed-off-by: abitha.s Reviewed-on: https://gerrit.iotivity.org/gerrit/14369 Tested-by: jenkins-iotivity Reviewed-by: Uze Choi Reviewed-by: Phil Coval --- diff --git a/service/notification/cpp-wrapper/consumer/src/NSConsumerService.cpp b/service/notification/cpp-wrapper/consumer/src/NSConsumerService.cpp index 204ab55d0..67b7fb629 100755 --- a/service/notification/cpp-wrapper/consumer/src/NSConsumerService.cpp +++ b/service/notification/cpp-wrapper/consumer/src/NSConsumerService.cpp @@ -25,6 +25,7 @@ #include "NSCommon.h" #include "NSConstants.h" #include "oic_string.h" +#include "oic_malloc.h" namespace OIC { @@ -111,6 +112,23 @@ namespace OIC NS_LOG(DEBUG, "initiating the callback for Response : NS_TOPIC"); changeCallback((NSProviderState)state); } + if (topicLL) + { + NSTopicLL *iter = topicLL; + NSTopicLL *following = NULL; + + while (iter) + { + following = iter->next; + if (iter) + { + NSOICFree(iter->topicName); + iter->next = NULL; + NSOICFree(iter); + } + iter = following; + } + } } else if (state == NS_STOPPED) { diff --git a/service/notification/cpp-wrapper/consumer/src/NSProvider.cpp b/service/notification/cpp-wrapper/consumer/src/NSProvider.cpp index e9e4e48a5..0c5588a41 100755 --- a/service/notification/cpp-wrapper/consumer/src/NSProvider.cpp +++ b/service/notification/cpp-wrapper/consumer/src/NSProvider.cpp @@ -161,15 +161,15 @@ namespace OIC getProviderId().c_str()); NSResult result = (NSResult) NSConsumerUpdateTopicList(getProviderId().c_str(), topicLL); - if(topicLL) + if (topicLL) { - NSTopicLL * iter = topicLL; - NSTopicLL * following = NULL; + NSTopicLL *iter = topicLL; + NSTopicLL *following = NULL; while (iter) { following = iter->next; - if(iter) + if (iter) { NSOICFree(iter->topicName); iter->next = NULL; diff --git a/service/notification/cpp-wrapper/examples/linux/notificationserviceconsumer.cpp b/service/notification/cpp-wrapper/examples/linux/notificationserviceconsumer.cpp index 82bd7892a..f7c027c12 100755 --- a/service/notification/cpp-wrapper/examples/linux/notificationserviceconsumer.cpp +++ b/service/notification/cpp-wrapper/examples/linux/notificationserviceconsumer.cpp @@ -37,7 +37,7 @@ std::string REMOTE_SERVER_ADDRESS; std::string mainProvider; uint64_t mainMessageId = 0; -FILE* server_fopen(const char *path, const char *mode) +FILE *server_fopen(const char *path, const char *mode) { (void)path; return fopen("oic_ns_provider_db.dat", mode); @@ -56,22 +56,22 @@ void onNotificationPostedCb(OIC::Service::NSMessage *notification) std::cout << "type : " << (int) notification->getType() << std::endl; std::cout << "TTL : " << notification->getTTL() << std::endl; std::cout << "time : " << notification->getTime() << std::endl; - if(notification->getMediaContents() != nullptr) + if (notification->getMediaContents() != nullptr) { std::cout << "MediaContents IconImage : " << notification->getMediaContents()->getIconImage() - << std::endl; + << std::endl; } std::cout << "ExtraInfo " << std::endl; OC::OCRepresentation rep = notification->getExtraInfo(); - for(auto it : rep.getResourceTypes()) + for (auto it : rep.getResourceTypes()) { std::cout << "resourceType : " << it << std::endl; } - for(auto it : rep.getResourceInterfaces()) + for (auto it : rep.getResourceInterfaces()) { std::cout << "Interface : " << it << std::endl; } - for(auto it : rep.getValues()) + for (auto it : rep.getValues()) { std::cout << "Key : " << it.first << std::endl; } @@ -195,17 +195,21 @@ int main(void) switch (num) { case 1: - std::cout << "Start the Notification Consumer" << std::endl; - NSConsumerService::getInstance()->start(onDiscoverNotificationCb); - break; + { + std::cout << "Start the Notification Consumer" << std::endl; + NSConsumerService::getInstance()->start(onDiscoverNotificationCb); + break; + } case 2: - std::cout << "Stop the Notification Consumer" << std::endl; - NSConsumerService::getInstance()->stop(); - break; + { + std::cout << "Stop the Notification Consumer" << std::endl; + NSConsumerService::getInstance()->stop(); + break; + } case 3: { std::cout << "SendSyncInfo" << std::endl; - if(!mainMessageId) + if (!mainMessageId) { std::cout << "Message ID is empty" << std::endl; break; @@ -213,51 +217,52 @@ int main(void) std::cout << "1. Send Read Sync" << std::endl; std::cout << "2. Send Delete Sync" << std::endl; int syn = 0; - while(!(std::cin >> syn)){ - std::cout << "Bad value!" <> syn)) + { + std::cout << "Bad value!" << std::endl;; std::cin.clear(); std::cin.ignore(numeric_limits::max(), '\n'); } switch (syn) { case 1: - { - std::cout << "Sending Read Sync" << std::endl; - auto provider = NSConsumerService::getInstance()->getProvider( - mainProvider); - if (provider != nullptr) { - provider->sendSyncInfo(mainMessageId, - OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ); + std::cout << "Sending Read Sync" << std::endl; + auto provider = NSConsumerService::getInstance()->getProvider( + mainProvider); + if (provider != nullptr) + { + provider->sendSyncInfo(mainMessageId, + OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ); + } } - } - break; + break; case 2: - { - std::cout << "Sending Delete Sync" << std::endl; - auto provider = NSConsumerService::getInstance()->getProvider( - mainProvider); - if (provider != nullptr) { - provider->sendSyncInfo(mainMessageId, - OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED); + std::cout << "Sending Delete Sync" << std::endl; + auto provider = NSConsumerService::getInstance()->getProvider( + mainProvider); + if (provider != nullptr) + { + provider->sendSyncInfo(mainMessageId, + OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED); + } } - } - break; + break; default: - { - cout << "Invalid Input!. sending default Read Sync"; - auto provider = NSConsumerService::getInstance()->getProvider( - mainProvider); - if (provider != nullptr) { - provider->sendSyncInfo(mainMessageId, - OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ); + cout << "Invalid Input!. sending default Read Sync"; + auto provider = NSConsumerService::getInstance()->getProvider( + mainProvider); + if (provider != nullptr) + { + provider->sendSyncInfo(mainMessageId, + OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ); + } + std::cin.clear(); + std::cin.ignore(numeric_limits::max(), '\n'); + break; } - std::cin.clear(); - std::cin.ignore(numeric_limits::max(), '\n'); - break; - } } break; } @@ -307,15 +312,19 @@ int main(void) } #endif case 7: - std::cout << "Exit" << std::endl; - NSConsumerService::getInstance()->stop(); - isExit = true; - break; + { + std::cout << "Exit" << std::endl; + NSConsumerService::getInstance()->stop(); + isExit = true; + break; + } default: - std::cout << "Under Construction" << std::endl; - std::cin.clear(); - std::cin.ignore(numeric_limits::max(), '\n'); - break; + { + std::cout << "Under Construction" << std::endl; + std::cin.clear(); + std::cin.ignore(numeric_limits::max(), '\n'); + break; + } } } diff --git a/service/notification/cpp-wrapper/examples/linux/notificationserviceprovider.cpp b/service/notification/cpp-wrapper/examples/linux/notificationserviceprovider.cpp index e00494fb5..57f635eb0 100755 --- a/service/notification/cpp-wrapper/examples/linux/notificationserviceprovider.cpp +++ b/service/notification/cpp-wrapper/examples/linux/notificationserviceprovider.cpp @@ -58,6 +58,7 @@ void *OCProcessThread(void *ptr) (void) ptr; while (!isExit) { + usleep(2000); if (OCProcess() != OC_STACK_OK) { std::cout << "OCStack process error" << std::endl; @@ -90,22 +91,25 @@ OIC::Service::NSConsumer *printAvailableConsumers() std::cout << "Choose the Consumer ID for operation" << std::endl; int pos = 1; unsigned int option = 0; - for(auto it: discoveredConsumers) + for (auto it : discoveredConsumers) { - std::cout << pos << ". " << it <> option)){ - std::cout << "Bad value!" <> option)) + { + std::cout << "Bad value!" << std::endl;; std::cin.clear(); std::cin.ignore(numeric_limits::max(), '\n'); } option--; if (option > discoveredConsumers.size()) + { return NULL; + } std::string consumerId = discoveredConsumers[option]; OIC::Service::NSConsumer *consumer = NSProviderService::getInstance()->getConsumer( - consumerId); + consumerId); return consumer; } @@ -233,13 +237,13 @@ int main() msg->setTopic(topic); msg->setTTL(40); msg->setTime("12:30"); - OIC::Service::NSMediaContents *mediaContents = - new OIC::Service::NSMediaContents("iconImage"); + OIC::Service::NSMediaContents *mediaContents = + new OIC::Service::NSMediaContents("iconImage"); msg->setMediaContents(mediaContents); OC::OCRepresentation rep; - rep.setValue("Key1","Value1"); - rep.setValue("Key2","Value2"); + rep.setValue("Key1", "Value1"); + rep.setValue("Key2", "Value2"); msg->setExtraInfo(rep); mainMessageId = msg->getMessageId(); @@ -247,7 +251,7 @@ int main() std::cout << "MessageID for Message : " << msg->getMessageId() << std::endl; NSProviderService::getInstance()->sendMessage(msg); - delete mediaContents; + delete msg; break; } case 6: @@ -255,7 +259,7 @@ int main() std::cout << "------------------------------------" << std::endl; std::cout << "SendSyncInfo" << std::endl; std::cout << "------------------------------------" << std::endl; - if(!mainMessageId) + if (!mainMessageId) { std::cout << "Message ID is empty" << std::endl; break; @@ -263,53 +267,56 @@ int main() std::cout << "1. Send Read Sync" << std::endl; std::cout << "2. Send Delete Sync" << std::endl; int syn = 0; - while(!(std::cin >> syn)){ - std::cout << "Bad value!" <> syn)) + { + std::cout << "Bad value!" << std::endl;; std::cin.clear(); std::cin.ignore(numeric_limits::max(), '\n'); } switch (syn) { case 1: - { - std::cout << "Sending Read Sync" << std::endl; - NSProviderService::getInstance()->sendSyncInfo(mainMessageId, - OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ); - } - break; + { + std::cout << "Sending Read Sync" << std::endl; + NSProviderService::getInstance()->sendSyncInfo(mainMessageId, + OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ); + } + break; case 2: - { - std::cout << "Sending Delete Sync" << std::endl; - NSProviderService::getInstance()->sendSyncInfo(mainMessageId, - OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED); - } - break; - default: - { - cout << "Invalid Input!. sending default Read Sync"; - NSProviderService::getInstance()->sendSyncInfo(mainMessageId, - OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ); - std::cin.clear(); - std::cin.ignore(numeric_limits::max(), '\n'); + { + std::cout << "Sending Delete Sync" << std::endl; + NSProviderService::getInstance()->sendSyncInfo(mainMessageId, + OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED); + } break; - } + default: + { + cout << "Invalid Input!. sending default Read Sync"; + NSProviderService::getInstance()->sendSyncInfo(mainMessageId, + OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ); + std::cin.clear(); + std::cin.ignore(numeric_limits::max(), '\n'); + break; + } } break; } case 7: - std::cout << "RegisterTopic" << std::endl; - NSProviderService::getInstance()->registerTopic("OCF_TOPIC1"); - NSProviderService::getInstance()->registerTopic("OCF_TOPIC2"); - NSProviderService::getInstance()->registerTopic("OCF_TOPIC3"); - NSProviderService::getInstance()->registerTopic("OCF_TOPIC4"); - break; - + { + std::cout << "RegisterTopic" << std::endl; + NSProviderService::getInstance()->registerTopic("OCF_TOPIC1"); + NSProviderService::getInstance()->registerTopic("OCF_TOPIC2"); + NSProviderService::getInstance()->registerTopic("OCF_TOPIC3"); + NSProviderService::getInstance()->registerTopic("OCF_TOPIC4"); + break; + } case 8: - std::cout << "UnregisterTopic" << std::endl; - NSProviderService::getInstance()->unregisterTopic("OCF_TOPIC2"); - break; - + { + std::cout << "UnregisterTopic" << std::endl; + NSProviderService::getInstance()->unregisterTopic("OCF_TOPIC2"); + break; + } case 9: { std::cout << "SetTopic" << std::endl; @@ -389,19 +396,25 @@ int main() } #endif case 15: - std::cout << "Stop the Notification Provider" << std::endl; - NSProviderService::getInstance()->stop(); - break; + { + std::cout << "Stop the Notification Provider" << std::endl; + NSProviderService::getInstance()->stop(); + break; + } case 16: - std::cout << "Exit()" << std::endl; - NSProviderService::getInstance()->stop(); - isExit = true; - break; + { + std::cout << "Exit()" << std::endl; + NSProviderService::getInstance()->stop(); + isExit = true; + break; + } default: - std::cout << "Under Construction" << std::endl; - std::cin.clear(); - std::cin.ignore(numeric_limits::max(), '\n'); - break; + { + std::cout << "Under Construction" << std::endl; + std::cin.clear(); + std::cin.ignore(numeric_limits::max(), '\n'); + break; + } } std::cout << std::endl; diff --git a/service/notification/cpp-wrapper/provider/SConscript b/service/notification/cpp-wrapper/provider/SConscript index 956830e78..2d2cce676 100755 --- a/service/notification/cpp-wrapper/provider/SConscript +++ b/service/notification/cpp-wrapper/provider/SConscript @@ -52,6 +52,7 @@ notification_env.AppendUnique(CPPPATH = ['../../src/common']) notification_env.PrependUnique(LIBS = [ 'oc_logger', 'oc', + 'octbstack', 'notification_provider' ]) notification_env.AppendUnique(CXXFLAGS = ['-std=c++0x','-frtti']) diff --git a/service/notification/cpp-wrapper/provider/src/NSProviderService.cpp b/service/notification/cpp-wrapper/provider/src/NSProviderService.cpp index 79d2dc5ae..7fd88d6df 100755 --- a/service/notification/cpp-wrapper/provider/src/NSProviderService.cpp +++ b/service/notification/cpp-wrapper/provider/src/NSProviderService.cpp @@ -27,6 +27,7 @@ #include "NSSyncInfo.h" #include "NSConstants.h" #include "OCRepresentation.h" +#include "ocpayload.h" #include "oic_string.h" #include "oic_malloc.h" @@ -172,8 +173,15 @@ namespace OIC OICFree(nsMsg->contentText); OICFree(nsMsg->sourceName); OICFree(nsMsg->topic); - OICFree(nsMsg->extraInfo); - delete nsMsg->mediaContents; + if (nsMsg->mediaContents != NULL) + { + if (nsMsg->mediaContents->iconImage != NULL) + { + OICFree(nsMsg->mediaContents->iconImage); + } + delete nsMsg->mediaContents; + } + OCPayloadDestroy((OCPayload *) nsMsg->extraInfo); delete nsMsg; } else diff --git a/service/notification/cpp-wrapper/unittest/NSConsumerServiceSimulator.h b/service/notification/cpp-wrapper/unittest/NSConsumerServiceSimulator.h index 4d3f51d33..6d05f003f 100755 --- a/service/notification/cpp-wrapper/unittest/NSConsumerServiceSimulator.h +++ b/service/notification/cpp-wrapper/unittest/NSConsumerServiceSimulator.h @@ -28,173 +28,173 @@ class NSConsumerSimulator { -private: - std::function m_messageFunc; - std::function m_syncFunc; - - std::shared_ptr m_syncResource; - std::shared_ptr m_msgResource; - std::shared_ptr m_topicResource; - -public: - NSConsumerSimulator() - : m_messageFunc(), m_syncFunc(), - m_syncResource() - { - } - ~NSConsumerSimulator() = default; - - NSConsumerSimulator(const NSConsumerSimulator &) = delete; - NSConsumerSimulator & operator = (const NSConsumerSimulator &) = delete; - - NSConsumerSimulator(NSConsumerSimulator &&) = delete; - NSConsumerSimulator & operator = (NSConsumerSimulator &&) = delete; - - void findProvider() - { - OC::OCPlatform::findResource("", std::string("/oic/res?rt=oic.wk.notification"), - OCConnectivityType::CT_DEFAULT, - std::bind(&NSConsumerSimulator::findResultCallback, this, std::placeholders::_1), - OC::QualityOfService::LowQos); - } - - void syncToProvider(int & type, const int & id, const std::string & providerID) - { - if (m_syncResource == nullptr) + private: + std::function m_messageFunc; + std::function m_syncFunc; + + std::shared_ptr m_syncResource; + std::shared_ptr m_msgResource; + std::shared_ptr m_topicResource; + + public: + NSConsumerSimulator() + : m_messageFunc(), m_syncFunc(), + m_syncResource() { - return; } + ~NSConsumerSimulator() = default; - OC::OCRepresentation rep; - rep.setValue("providerId", providerID); - rep.setValue("messageId", id); - rep.setValue("state", type); + NSConsumerSimulator(const NSConsumerSimulator &) = delete; + NSConsumerSimulator &operator = (const NSConsumerSimulator &) = delete; - m_syncResource->post(rep, OC::QueryParamsMap(), &onPost, OC::QualityOfService::LowQos); - } + NSConsumerSimulator(NSConsumerSimulator &&) = delete; + NSConsumerSimulator &operator = (NSConsumerSimulator &&) = delete; - bool cancelObserves() - { - if((msgResourceCancelObserve(OC::QualityOfService::HighQos) == OC_STACK_OK) && - (syncResourceCancelObserve(OC::QualityOfService::HighQos) == OC_STACK_OK)) + void findProvider() { - return true; + OC::OCPlatform::findResource("", std::string("/oic/res?rt=oic.wk.notification"), + OCConnectivityType::CT_DEFAULT, + std::bind(&NSConsumerSimulator::findResultCallback, this, std::placeholders::_1), + OC::QualityOfService::LowQos); } - return false; - } - - void setCallback(std::function messageFunc, - const std::function & syncFunc) - { - m_messageFunc = messageFunc; - m_syncFunc = syncFunc; - } - -private: - static void onPost(const OC::HeaderOptions &/*headerOption*/, - const OC::OCRepresentation & /*rep*/ , const int /*eCode*/) - { - } - void findResultCallback(std::shared_ptr resource) - { - - - if(resource->uri() == "/notification") + + void syncToProvider(int &type, const int &id, const std::string &providerID) { - resource->get(OC::QueryParamsMap(), - std::bind(&NSConsumerSimulator::onGet, this, - std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, - resource), OC::QualityOfService::LowQos); + if (m_syncResource == nullptr) + { + return; + } + + OC::OCRepresentation rep; + rep.setValue("providerId", providerID); + rep.setValue("messageId", id); + rep.setValue("state", type); + + m_syncResource->post(rep, OC::QueryParamsMap(), &onPost, OC::QualityOfService::LowQos); } - } - void onGet(const OC::HeaderOptions &/*headerOption*/, - const OC::OCRepresentation & /*rep*/ , const int /*eCode*/, - std::shared_ptr resource) - { - OC::QueryParamsMap map; - map.insert(std::pair(std::string("consumerId"), - std::string("123456789012345678901234567890123456"))); + bool cancelObserves() + { + if ((msgResourceCancelObserve(OC::QualityOfService::HighQos) == OC_STACK_OK) && + (syncResourceCancelObserve(OC::QualityOfService::HighQos) == OC_STACK_OK)) + { + return true; + } + return false; + } - try + void setCallback(std::function messageFunc, + const std::function &syncFunc) { + m_messageFunc = messageFunc; + m_syncFunc = syncFunc; + } - std::vector rts{"oic.wk.notification"}; + private: + static void onPost(const OC::HeaderOptions &/*headerOption*/, + const OC::OCRepresentation & /*rep*/ , const int /*eCode*/) + { + } + void findResultCallback(std::shared_ptr resource) + { - m_msgResource - = OC::OCPlatform::constructResourceObject( - std::string(resource->host()), std::string(resource->uri() + "/message"), - OCConnectivityType(resource->connectivityType()), true, rts, - std::vector(resource->getResourceInterfaces())); - m_msgResource->observe(OC::ObserveType::Observe, map, - std::bind(&NSConsumerSimulator::onObserve, this, - std::placeholders::_1, std::placeholders::_2, - std::placeholders::_3, std::placeholders::_4, resource), - OC::QualityOfService::LowQos); + if (resource->uri() == "/notification") + { + resource->get(OC::QueryParamsMap(), + std::bind(&NSConsumerSimulator::onGet, this, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, + resource), OC::QualityOfService::LowQos); + } } - catch(std::exception & e) + void onGet(const OC::HeaderOptions &/*headerOption*/, + const OC::OCRepresentation & /*rep*/ , const int /*eCode*/, + std::shared_ptr resource) { - std::cout << "OC::ResoureInitException : " << e.what() << std::endl; + + OC::QueryParamsMap map; + map.insert(std::pair(std::string("consumerId"), + std::string("123456789012345678901234567890123456"))); + + try + { + + std::vector rts{"oic.wk.notification"}; + + m_msgResource + = OC::OCPlatform::constructResourceObject( + std::string(resource->host()), std::string(resource->uri() + "/message"), + OCConnectivityType(resource->connectivityType()), true, rts, + std::vector(resource->getResourceInterfaces())); + + m_msgResource->observe(OC::ObserveType::Observe, map, + std::bind(&NSConsumerSimulator::onObserve, this, + std::placeholders::_1, std::placeholders::_2, + std::placeholders::_3, std::placeholders::_4, resource), + OC::QualityOfService::LowQos); + } + catch (std::exception &e) + { + std::cout << "OC::ResoureInitException : " << e.what() << std::endl; + } + m_syncResource + = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/sync", + resource->connectivityType(), true, resource->getResourceTypes(), + resource->getResourceInterfaces()); + + m_syncResource->observe(OC::ObserveType::Observe, map, + std::bind(&NSConsumerSimulator::onObserve, this, + std::placeholders::_1, std::placeholders::_2, + std::placeholders::_3, std::placeholders::_4, resource), + OC::QualityOfService::LowQos); + + m_topicResource + = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/topic", + resource->connectivityType(), true, resource->getResourceTypes(), + resource->getResourceInterfaces()); } - m_syncResource - = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/sync", - resource->connectivityType(), true, resource->getResourceTypes(), - resource->getResourceInterfaces()); - - m_syncResource->observe(OC::ObserveType::Observe, map, - std::bind(&NSConsumerSimulator::onObserve, this, - std::placeholders::_1, std::placeholders::_2, - std::placeholders::_3, std::placeholders::_4, resource), - OC::QualityOfService::LowQos); - - m_topicResource - = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/topic", - resource->connectivityType(), true, resource->getResourceTypes(), - resource->getResourceInterfaces()); - } - void onObserve(const OC::HeaderOptions &/*headerOption*/, - const OC::OCRepresentation &rep , const int & /*eCode*/, const int &, - std::shared_ptr ) - { - - if (rep.getUri() == "/notification/message" && rep.hasAttribute("messageId") - && rep.getValue("messageId") != 1) + void onObserve(const OC::HeaderOptions &/*headerOption*/, + const OC::OCRepresentation &rep , const int & /*eCode*/, const int &, + std::shared_ptr ) { - m_messageFunc(int(rep.getValue("messageId")), - std::string(rep.getValueToString("title")), - std::string(rep.getValueToString("contentText")), - std::string(rep.getValueToString("source"))); - if(rep.getValue("messageId") == 3) + + if (rep.getUri() == "/notification/message" && rep.hasAttribute("messageId") + && rep.getValue("messageId") != 1) + { + m_messageFunc(int(rep.getValue("messageId")), + std::string(rep.getValueToString("title")), + std::string(rep.getValueToString("contentText")), + std::string(rep.getValueToString("source"))); + if (rep.getValue("messageId") == 3) + { + m_topicResource->get(OC::QueryParamsMap(), + std::bind(&NSConsumerSimulator::onTopicGet, this, std::placeholders::_1, + std::placeholders::_2, std::placeholders::_3, m_topicResource), + OC::QualityOfService::LowQos); + } + } + else if (rep.getUri() == "/notification/sync") { - m_topicResource->get(OC::QueryParamsMap(), - std::bind(&NSConsumerSimulator::onTopicGet, this, std::placeholders::_1, - std::placeholders::_2, std::placeholders::_3, m_topicResource), - OC::QualityOfService::LowQos); + m_syncFunc(int(rep.getValue("state")), int(rep.getValue("messageId"))); } } - else if (rep.getUri() == "/notification/sync") + void onTopicGet(const OC::HeaderOptions &/*headerOption*/, + const OC::OCRepresentation & /*rep*/ , const int /*eCode*/, + std::shared_ptr /*resource*/) + { + } + + OCStackResult msgResourceCancelObserve(OC::QualityOfService qos) + { + return m_msgResource->cancelObserve(qos); + } + + OCStackResult syncResourceCancelObserve(OC::QualityOfService qos) { - m_syncFunc(int(rep.getValue("state")), int(rep.getValue("messageId"))); + return m_syncResource->cancelObserve(qos); } - } - void onTopicGet(const OC::HeaderOptions &/*headerOption*/, - const OC::OCRepresentation & /*rep*/ , const int /*eCode*/, - std::shared_ptr /*resource*/) - { - } - - OCStackResult msgResourceCancelObserve(OC::QualityOfService qos) - { - return m_msgResource->cancelObserve(qos); - } - - OCStackResult syncResourceCancelObserve(OC::QualityOfService qos) - { - return m_syncResource->cancelObserve(qos); - } }; diff --git a/service/notification/cpp-wrapper/unittest/NSConsumerServiceTest.cpp b/service/notification/cpp-wrapper/unittest/NSConsumerServiceTest.cpp index 56b500a69..e64b3e710 100755 --- a/service/notification/cpp-wrapper/unittest/NSConsumerServiceTest.cpp +++ b/service/notification/cpp-wrapper/unittest/NSConsumerServiceTest.cpp @@ -467,17 +467,17 @@ TEST_F(NotificationServiceConsumerTest, ExpectCallbackDismissCheckWhenConsumerPo TEST_F(NotificationServiceConsumerTest, ExpectGetProviderSuccessWithValidProviderId) { - OIC::Service::NSProvider * provider = - OIC::Service::NSConsumerService::getInstance()->getProvider(g_provider->getProviderId()); + OIC::Service::NSProvider *provider = + OIC::Service::NSConsumerService::getInstance()->getProvider(g_provider->getProviderId()); int ret = strcmp(provider->getProviderId().c_str(), g_provider->getProviderId().c_str()); EXPECT_EQ(0, ret); } TEST_F(NotificationServiceConsumerTest, ExpectGetProviderSuccessWithInvalidProviderId) { - OIC::Service::NSProvider * provider = - OIC::Service::NSConsumerService::getInstance()->getProvider("123456789012345678901234567890123457"); - EXPECT_EQ(provider, (void*)NULL); + OIC::Service::NSProvider *provider = + OIC::Service::NSConsumerService::getInstance()->getProvider("123456789012345678901234567890123457"); + EXPECT_EQ(provider, (void *)NULL); } TEST_F(NotificationServiceConsumerTest, ExpectCallbackTopicUpdated) @@ -485,11 +485,11 @@ TEST_F(NotificationServiceConsumerTest, ExpectCallbackTopicUpdated) OIC::Service::NSProviderState revState = OIC::Service::NSProviderState::STOPPED; mocks.OnCallFunc(ProviderChangedCallbackEmpty).Do( [this, & revState](OIC::Service::NSProviderState state) - { - std::cout << "Income Changed Callback : " << (int)state << std::endl; - revState = state; - responseCon.notify_all(); - }); + { + std::cout << "Income Changed Callback : " << (int)state << std::endl; + revState = state; + responseCon.notify_all(); + }); NSProviderSimulator::NS_TopicList topics; topics.push_back("1"); @@ -514,15 +514,15 @@ TEST_F(NotificationServiceConsumerTest, ExpectEQTopicList) topics.push_back("3"); std::list retTopic = g_provider->getTopicList()->getTopicsList(); - auto it1=retTopic.begin(); - auto it2=topics.begin(); - while( it1 != retTopic.end() || it2 != topics.end() ) + 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; } - it1++;it2++; + it1++; it2++; } @@ -531,7 +531,7 @@ TEST_F(NotificationServiceConsumerTest, ExpectEQTopicList) TEST_F(NotificationServiceConsumerTest, ExpectFailUpdateTopicOnConsumer) { - OIC::Service::NSTopicsList * retTopic = g_provider->getTopicList(); + OIC::Service::NSTopicsList *retTopic = g_provider->getTopicList(); for (auto it : retTopic->getTopicsList()) { it->setState(OIC::Service::NSTopic::NSTopicState::SUBSCRIBED); diff --git a/service/notification/cpp-wrapper/unittest/NSProviderServiceSimulator.h b/service/notification/cpp-wrapper/unittest/NSProviderServiceSimulator.h old mode 100644 new mode 100755 index 2245567de..d39477537 --- a/service/notification/cpp-wrapper/unittest/NSProviderServiceSimulator.h +++ b/service/notification/cpp-wrapper/unittest/NSProviderServiceSimulator.h @@ -47,298 +47,298 @@ namespace class NSProviderSimulator { -public: - enum class TopicAllowState - { - DENY, - ALLOW - }; - typedef std::pair NS_TopicState; - typedef std::map NS_TopicStateList; - - typedef std::list NS_TopicList; -private: - OCResourceHandle m_notificationHandle; - OCResourceHandle m_messageHandle; - OCResourceHandle m_syncHandle; - OCResourceHandle m_topicHandle; - OC::OCRepresentation m_syncRep; - OC::OCRepresentation m_messageRep; - int m_accepter; - - std::string m_notificationUri; - std::string m_messageUri; - std::string m_syncUri; - std::string m_topicUri; - NS_TopicList m_topicList; - NS_TopicStateList m_allowedTopicList; - - OC::ObservationIds m_syncObservers; - -public: - NSProviderSimulator() - : m_notificationHandle(), m_messageHandle(), m_syncHandle(), m_topicHandle(), - m_syncRep(), m_messageRep(), m_accepter(0), - m_notificationUri(std::string("/notification")), - m_messageUri(std::string("/message")), - m_syncUri(std::string("/sync")), - m_topicUri(std::string("/topic")), - m_topicList(), - m_allowedTopicList(), - m_syncObservers() - { + public: + enum class TopicAllowState + { + DENY, + ALLOW + }; + typedef std::pair NS_TopicState; + typedef std::map NS_TopicStateList; + + typedef std::list NS_TopicList; + private: + OCResourceHandle m_notificationHandle; + OCResourceHandle m_messageHandle; + OCResourceHandle m_syncHandle; + OCResourceHandle m_topicHandle; + OC::OCRepresentation m_syncRep; + OC::OCRepresentation m_messageRep; + int m_accepter; + + std::string m_notificationUri; + std::string m_messageUri; + std::string m_syncUri; + std::string m_topicUri; + NS_TopicList m_topicList; + NS_TopicStateList m_allowedTopicList; + + OC::ObservationIds m_syncObservers; + + public: + NSProviderSimulator() + : m_notificationHandle(), m_messageHandle(), m_syncHandle(), m_topicHandle(), + m_syncRep(), m_messageRep(), m_accepter(0), + m_notificationUri(std::string("/notification")), + m_messageUri(std::string("/message")), + m_syncUri(std::string("/sync")), + m_topicUri(std::string("/topic")), + m_topicList(), + m_allowedTopicList(), + m_syncObservers() + { - }; + }; - ~NSProviderSimulator() = default; + ~NSProviderSimulator() = default; - NSProviderSimulator(const NSProviderSimulator &) = delete; - NSProviderSimulator & operator = (const NSProviderSimulator &) = delete; + NSProviderSimulator(const NSProviderSimulator &) = delete; + NSProviderSimulator &operator = (const NSProviderSimulator &) = delete; - NSProviderSimulator(NSProviderSimulator &&) = delete; - NSProviderSimulator & operator = (NSProviderSimulator &&) = delete; + NSProviderSimulator(NSProviderSimulator &&) = delete; + NSProviderSimulator &operator = (NSProviderSimulator &&) = delete; -private: - std::shared_ptr getResponse( + private: + std::shared_ptr getResponse( std::shared_ptr< OC::OCResourceRequest > requests, requestType type) - { - auto response = std::make_shared(); - response->setRequestHandle(requests->getRequestHandle()); - response->setResourceHandle(requests->getResourceHandle()); - - int requestFlag = requests->getRequestHandlerFlag(); - if (requestFlag == OC::RequestHandlerFlag::RequestFlag) { - std::string request = requests->getRequestType(); - - response->setErrorCode(200); - response->setResponseResult(OC_EH_OK); + auto response = std::make_shared(); + response->setRequestHandle(requests->getRequestHandle()); + response->setResourceHandle(requests->getResourceHandle()); - if (request == "GET") + int requestFlag = requests->getRequestHandlerFlag(); + if (requestFlag == OC::RequestHandlerFlag::RequestFlag) { - OC::OCRepresentation rep; + std::string request = requests->getRequestType(); - if (type == requestType::NS_NOTIFICATION) - { - std::string msgUri = m_notificationUri + m_messageUri; - std::string syncUri = m_notificationUri + m_syncUri; - std::string topicUri = m_notificationUri + m_topicUri; - std::string providerId = "123456789012345678901234567890123456"; - rep.setValue("subControllability", m_accepter); - rep.setValue("messageUri", msgUri); - rep.setValue("syncUri", syncUri); - rep.setValue("topicUri", topicUri); - rep.setValue("providerId", providerId); - } - else if (type == requestType::NS_SYNC) - { - rep = m_syncRep; - } - else if (type == requestType::NS_MESSAGE) - { - rep = m_messageRep; - } - else if (type == requestType::NS_TOPIC) + response->setErrorCode(200); + response->setResponseResult(OC_EH_OK); + + if (request == "GET") { - if (m_allowedTopicList.empty()) + OC::OCRepresentation rep; + + if (type == requestType::NS_NOTIFICATION) + { + std::string msgUri = m_notificationUri + m_messageUri; + std::string syncUri = m_notificationUri + m_syncUri; + std::string topicUri = m_notificationUri + m_topicUri; + std::string providerId = "123456789012345678901234567890123456"; + rep.setValue("subControllability", m_accepter); + rep.setValue("messageUri", msgUri); + rep.setValue("syncUri", syncUri); + rep.setValue("topicUri", topicUri); + rep.setValue("providerId", providerId); + } + else if (type == requestType::NS_SYNC) { - std::for_each (m_topicList.begin(), m_topicList.end(), - [this](const std::string & topic) + rep = m_syncRep; + } + else if (type == requestType::NS_MESSAGE) + { + rep = m_messageRep; + } + else if (type == requestType::NS_TOPIC) + { + if (m_allowedTopicList.empty()) + { + std::for_each (m_topicList.begin(), m_topicList.end(), + [this](const std::string & topic) { m_allowedTopicList.insert( - std::make_pair( - std::string(topic), TopicAllowState::DENY)); + std::make_pair( + std::string(topic), TopicAllowState::DENY)); } - ); - } + ); + } - std::vector topicArr; + std::vector topicArr; - std::for_each (m_allowedTopicList.begin(), m_allowedTopicList.end(), - [& topicArr](const NS_TopicState & topicState) + std::for_each (m_allowedTopicList.begin(), m_allowedTopicList.end(), + [& topicArr](const NS_TopicState & topicState) { OC::OCRepresentation topic; topic.setValue("topicName", topicState.first); topic.setValue("topicState", (int) topicState.second); topicArr.push_back(topic); } - ); + ); - rep.setValue> - ("topicList", topicArr); - } - else - { - return NULL; - } + rep.setValue> + ("topicList", topicArr); + } + else + { + return NULL; + } - response->setResourceRepresentation(rep); - return response; - } + response->setResourceRepresentation(rep); + return response; + } - else if (request == "POST") - { - if (type == requestType::NS_SYNC) + else if (request == "POST") { - m_syncRep = requests->getResourceRepresentation(); + if (type == requestType::NS_SYNC) + { + m_syncRep = requests->getResourceRepresentation(); - std::cout << "Receive POST for Sync" << std::endl; - std::cout << "provider Id : " << m_syncRep.getValueToString("providerId") << std::endl; - std::cout << "Sync State : " << m_syncRep.getValueToString("state") << std::endl; + std::cout << "Receive POST for Sync" << std::endl; + std::cout << "provider Id : " << m_syncRep.getValueToString("providerId") << std::endl; + std::cout << "Sync State : " << m_syncRep.getValueToString("state") << std::endl; - response->setResourceRepresentation(m_syncRep); + response->setResourceRepresentation(m_syncRep); - OC::OCPlatform::notifyAllObservers(m_syncHandle); + OC::OCPlatform::notifyAllObservers(m_syncHandle); - return response; - } - else if (type == requestType::NS_TOPIC) - { - auto receivePayload = + return response; + } + else if (type == requestType::NS_TOPIC) + { + auto receivePayload = requests->getResourceRepresentation() .getValue>("topicList"); - std::for_each (receivePayload.begin(), receivePayload.end(), - [this](const OC::OCRepresentation & rep) - { - auto tmp = m_allowedTopicList.find(rep.getValueToString("topicName")); - if (tmp != m_allowedTopicList.end()) - { - tmp->second = (TopicAllowState) rep.getValue("topicState"); - } - } - ); + std::for_each (receivePayload.begin(), receivePayload.end(), + [this](const OC::OCRepresentation & rep) + { + auto tmp = m_allowedTopicList.find(rep.getValueToString("topicName")); + if (tmp != m_allowedTopicList.end()) + { + tmp->second = (TopicAllowState) rep.getValue("topicState"); + } + } + ); + } } } - } - return NULL; - } + return NULL; + } - void setObserver(std::shared_ptr< OC::OCResourceRequest > requests, requestType type) - { - if (type == requestType::NS_SYNC) + void setObserver(std::shared_ptr< OC::OCResourceRequest > requests, requestType type) { - OC::ObservationInfo observationInfo = requests->getObservationInfo(); - if (OC::ObserveAction::ObserveRegister == observationInfo.action) + if (type == requestType::NS_SYNC) { - m_syncObservers.push_back(observationInfo.obsId); + OC::ObservationInfo observationInfo = requests->getObservationInfo(); + if (OC::ObserveAction::ObserveRegister == observationInfo.action) + { + m_syncObservers.push_back(observationInfo.obsId); + } + else if (OC::ObserveAction::ObserveUnregister == observationInfo.action) + { + m_syncObservers.erase(std::remove( + m_syncObservers.begin(), m_syncObservers.end(), + observationInfo.obsId), m_syncObservers.end()); + } } - else if (OC::ObserveAction::ObserveUnregister == observationInfo.action) + else if (type == requestType::NS_MESSAGE) { - m_syncObservers.erase(std::remove( - m_syncObservers.begin(), m_syncObservers.end(), - observationInfo.obsId), m_syncObservers.end()); + OC::OCRepresentation rep; + std::string providerId = "123456789012345678901234567890123456"; + rep.setValue("messageId", (int)messageType::NS_ALLOW); + rep.setValue("providerId", providerId); + + auto response = std::make_shared(); + response->setRequestHandle(requests->getRequestHandle()); + response->setResourceHandle(requests->getResourceHandle()); + response->setErrorCode(200); + response->setResponseResult(OC_EH_OK); + response->setResourceRepresentation(rep); + + OC::ObservationIds ids; + ids.push_back(requests->getObservationInfo().obsId); + + OC::OCPlatform::notifyListOfObservers(m_messageHandle, ids, response); } } - else if (type == requestType::NS_MESSAGE) + + OCEntityHandlerResult entityHandler( + std::shared_ptr< OC::OCResourceRequest > request, requestType type) { - OC::OCRepresentation rep; - std::string providerId = "123456789012345678901234567890123456"; - rep.setValue("messageId", (int)messageType::NS_ALLOW); - rep.setValue("providerId", providerId); + if (!request) + { + return OC_EH_ERROR; + } - auto response = std::make_shared(); - response->setRequestHandle(requests->getRequestHandle()); - response->setResourceHandle(requests->getResourceHandle()); - response->setErrorCode(200); - response->setResponseResult(OC_EH_OK); - response->setResourceRepresentation(rep); + std::cout << "Provider : Income request : " << request->getRequestHandlerFlag() << std::endl; + if ((request->getRequestHandlerFlag() & OC::RequestHandlerFlag::ObserverFlag)) + { + std::cout << "Provider : Income Observe : " << std::endl; + setObserver(request, type); + return OC_EH_OK; + } - OC::ObservationIds ids; - ids.push_back(requests->getObservationInfo().obsId); + auto pResponse = getResponse(request, type); + if (pResponse == nullptr) + { + return OC_EH_ERROR; + } - OC::OCPlatform::notifyListOfObservers(m_messageHandle, ids, response); + try + { + OC::OCPlatform::sendResponse(pResponse); + } + catch (std::exception &e) + { + return OC_EH_ERROR; + } + + return OC_EH_OK; } - } - OCEntityHandlerResult entityHandler( - std::shared_ptr< OC::OCResourceRequest > request, requestType type) - { - if (!request) + public: + + void setAccepter(int accepter) { - return OC_EH_ERROR; + m_accepter = accepter; } - std::cout << "Provider : Income request : " << request->getRequestHandlerFlag() << std::endl; - if ((request->getRequestHandlerFlag() & OC::RequestHandlerFlag::ObserverFlag)) + void notifyMessage() { - std::cout << "Provider : Income Observe : " << std::endl; - setObserver(request, type); - return OC_EH_OK; + std::cout << "Provider : notify~" << std::endl; + OC::OCPlatform::notifyAllObservers(m_messageHandle); } - auto pResponse = getResponse(request, type); - if (pResponse == nullptr) + void notifyMessage(const uint64_t &id, const std::string &title, const std::string &content) { - return OC_EH_ERROR; + setMessage(id, title, content); + notifyMessage(); } - try + void sendRead(const uint64_t &id) { - OC::OCPlatform::sendResponse(pResponse); + std::string providerId = "123456789012345678901234567890123456"; + m_syncRep.setValue("messageId", id); + m_syncRep.setValue("state", (int)1); + m_syncRep.setValue("providerId", providerId); + OC::OCPlatform::notifyAllObservers(m_syncHandle); } - catch (std::exception & e) + void sendDismiss(const uint64_t &id) { - return OC_EH_ERROR; + std::string providerId = "123456789012345678901234567890123456"; + m_syncRep.setValue("messageId", id); + m_syncRep.setValue("state", (int)2); + m_syncRep.setValue("providerId", providerId); + OC::OCPlatform::notifyAllObservers(m_syncHandle); } - return OC_EH_OK; - } - -public: - - void setAccepter(int accepter) - { - m_accepter = accepter; - } - - void notifyMessage() - { - std::cout << "Provider : notify~" << std::endl; - OC::OCPlatform::notifyAllObservers(m_messageHandle); - } - - void notifyMessage(const uint64_t & id, const std::string & title, const std::string & content) - { - setMessage(id, title, content); - notifyMessage(); - } + void setMessage(const uint64_t &id, const std::string &title, const std::string &content) + { + std::string providerId = "123456789012345678901234567890123456"; + m_messageRep.setValue("messageId", id); + m_messageRep.setValue("title", title); + m_messageRep.setValue("contentText", content); + m_messageRep.setValue("providerId", providerId); + } - void sendRead(const uint64_t & id) - { - std::string providerId = "123456789012345678901234567890123456"; - m_syncRep.setValue("messageId", id); - m_syncRep.setValue("state", (int)1); - m_syncRep.setValue("providerId", providerId); - OC::OCPlatform::notifyAllObservers(m_syncHandle); - } - void sendDismiss(const uint64_t & id) - { - std::string providerId = "123456789012345678901234567890123456"; - m_syncRep.setValue("messageId", id); - m_syncRep.setValue("state", (int)2); - m_syncRep.setValue("providerId", providerId); - OC::OCPlatform::notifyAllObservers(m_syncHandle); - } - - void setMessage(const uint64_t & id, const std::string & title, const std::string & content) - { - std::string providerId = "123456789012345678901234567890123456"; - m_messageRep.setValue("messageId", id); - m_messageRep.setValue("title", title); - m_messageRep.setValue("contentText", content); - m_messageRep.setValue("providerId", providerId); - } - - void setTopics(const NS_TopicList & topics) - { - bool isChanged = false; - std::for_each (topics.begin(), topics.end(), - [this, & isChanged](const std::string & topic) + void setTopics(const NS_TopicList &topics) + { + bool isChanged = false; + std::for_each (topics.begin(), topics.end(), + [this, & isChanged](const std::string & topic) { auto found = std::find( - this->m_topicList.begin(), this->m_topicList.end(), topic); + this->m_topicList.begin(), this->m_topicList.end(), topic); if (found == this->m_topicList.end()) { this->m_topicList.push_back(topic); @@ -346,21 +346,21 @@ public: } }); - if (isChanged) - { - this->notifyMessage((uint64_t)messageType::NS_TOPIC, "", ""); + if (isChanged) + { + this->notifyMessage((uint64_t)messageType::NS_TOPIC, "", ""); + } } - } - NS_TopicList getTopics() const - { - return m_topicList; - } + NS_TopicList getTopics() const + { + return m_topicList; + } - void updateTopicState(const NS_TopicStateList & allowedTopics) - { - std::for_each (allowedTopics.begin(), allowedTopics.end(), - [this](const NS_TopicState & allowedTopic) + void updateTopicState(const NS_TopicStateList &allowedTopics) + { + std::for_each (allowedTopics.begin(), allowedTopics.end(), + [this](const NS_TopicState & allowedTopic) { auto found = this->m_allowedTopicList.find(allowedTopic.first); if (found != this->m_allowedTopicList.end()) @@ -368,106 +368,106 @@ public: found->second = allowedTopic.second; } }); - } + } - NS_TopicStateList getTopicAllowState() const - { - return m_allowedTopicList; - } + NS_TopicStateList getTopicAllowState() const + { + return m_allowedTopicList; + } - void deleteNotificationResource() - { - OC::OCPlatform::unregisterResource(m_notificationHandle); - OC::OCPlatform::unregisterResource(m_messageHandle); - OC::OCPlatform::unregisterResource(m_syncHandle); - OC::OCPlatform::unregisterResource(m_topicHandle); - m_allowedTopicList.clear(); - m_topicList.clear(); - } - - void createNotificationResource() - { - createNotificationResource(m_notificationUri); - } + void deleteNotificationResource() + { + OC::OCPlatform::unregisterResource(m_notificationHandle); + OC::OCPlatform::unregisterResource(m_messageHandle); + OC::OCPlatform::unregisterResource(m_syncHandle); + OC::OCPlatform::unregisterResource(m_topicHandle); + m_allowedTopicList.clear(); + m_topicList.clear(); + } - void createNotificationResource(const std::string & uri) - { - if (m_notificationUri != uri) + void createNotificationResource() { - m_notificationUri = uri; + createNotificationResource(m_notificationUri); } - OC::OCPlatform::startPresence(30); + void createNotificationResource(const std::string &uri) + { + if (m_notificationUri != uri) + { + m_notificationUri = uri; + } + + OC::OCPlatform::startPresence(30); - std::string notificationUri = m_notificationUri; - std::string resourceTypeName = "oic.wk.notification.topic"; - std::string resourceInterface = OC::DEFAULT_INTERFACE; + std::string notificationUri = m_notificationUri; + std::string resourceTypeName = "oic.wk.notification.topic"; + std::string resourceInterface = OC::DEFAULT_INTERFACE; - uint8_t resourceProperty = OC_OBSERVABLE; - std::string childUri = uri + m_topicUri; - try - { - OC::OCPlatform::registerResource( + uint8_t resourceProperty = OC_OBSERVABLE; + std::string childUri = uri + m_topicUri; + try + { + OC::OCPlatform::registerResource( m_topicHandle, childUri, resourceTypeName, resourceInterface, std::bind(& NSProviderSimulator::entityHandler, this, - std::placeholders::_1, requestType::NS_TOPIC), - resourceProperty); - } - catch(std::exception & e) - { - std::cout << e.what() << std::endl; - } + std::placeholders::_1, requestType::NS_TOPIC), + resourceProperty); + } + catch (std::exception &e) + { + std::cout << e.what() << std::endl; + } - //resourceProperty |= OC_OBSERVABLE; - resourceTypeName = "oic.wk.notification.message"; - childUri = uri + m_messageUri; - try - { - OC::OCPlatform::registerResource( + //resourceProperty |= OC_OBSERVABLE; + resourceTypeName = "oic.wk.notification.message"; + childUri = uri + m_messageUri; + try + { + OC::OCPlatform::registerResource( m_messageHandle, childUri, resourceTypeName, resourceInterface, std::bind(& NSProviderSimulator::entityHandler, this, - std::placeholders::_1, requestType::NS_MESSAGE), - resourceProperty); - } - catch (std::exception & e) - { - std::cout << e.what() << std::endl; - } + std::placeholders::_1, requestType::NS_MESSAGE), + resourceProperty); + } + catch (std::exception &e) + { + std::cout << e.what() << std::endl; + } - resourceTypeName = "oic.wk.notification.sync"; - childUri = uri + m_syncUri; - try - { - OC::OCPlatform::registerResource( + resourceTypeName = "oic.wk.notification.sync"; + childUri = uri + m_syncUri; + try + { + OC::OCPlatform::registerResource( m_syncHandle, childUri, resourceTypeName, resourceInterface, std::bind(& NSProviderSimulator::entityHandler, this, - std::placeholders::_1, requestType::NS_SYNC), - resourceProperty); - } - catch (std::exception & e) - { - std::cout << e.what() << std::endl; - } + std::placeholders::_1, requestType::NS_SYNC), + resourceProperty); + } + catch (std::exception &e) + { + std::cout << e.what() << std::endl; + } - resourceProperty |= OC_DISCOVERABLE; - resourceTypeName = "oic.wk.notification"; - try - { - OC::OCPlatform::registerResource( + resourceProperty |= OC_DISCOVERABLE; + resourceTypeName = "oic.wk.notification"; + try + { + OC::OCPlatform::registerResource( m_notificationHandle, notificationUri, resourceTypeName, resourceInterface, std::bind(& NSProviderSimulator::entityHandler, this, - std::placeholders::_1, requestType::NS_NOTIFICATION), - resourceProperty); - } - catch (std::exception & e) - { - std::cout << e.what() << std::endl; + std::placeholders::_1, requestType::NS_NOTIFICATION), + resourceProperty); + } + catch (std::exception &e) + { + std::cout << e.what() << std::endl; + } } - } }; #endif /* _NS_PROVIDER_SERVICE_SIMULATOR_H_ */ diff --git a/service/notification/cpp-wrapper/unittest/NSProviderServiceTest.cpp b/service/notification/cpp-wrapper/unittest/NSProviderServiceTest.cpp index be8e4161a..81cb1e8d8 100755 --- a/service/notification/cpp-wrapper/unittest/NSProviderServiceTest.cpp +++ b/service/notification/cpp-wrapper/unittest/NSProviderServiceTest.cpp @@ -199,7 +199,7 @@ TEST_F(NotificationProviderServiceTest, std::unique_lock< std::mutex > lock { mutexForCondition }; responseCon.wait_for(lock, g_waitForResponse); - EXPECT_NE((void*)g_consumer, (void*)NULL); + EXPECT_NE((void *)g_consumer, (void *)NULL); } TEST_F(NotificationProviderServiceTest, NeverCallNotifyOnConsumerByAcceptIsFalse) @@ -208,7 +208,7 @@ TEST_F(NotificationProviderServiceTest, NeverCallNotifyOnConsumerByAcceptIsFalse int msgID = 0; mocks.OnCallFunc(MessageCallbackFromConsumerEmpty).Do( - [& expectTrue, &msgID](const int & id, const std::string &, const std::string &, + [& expectTrue, &msgID](const int &id, const std::string &, const std::string &, const std::string &) { if (id == msgID) @@ -246,7 +246,7 @@ TEST_F(NotificationProviderServiceTest, ExpectCallNotifyOnConsumerByAcceptIsTrue int msgID = 0; mocks.ExpectCallFunc(MessageCallbackFromConsumerEmpty).Do( - [&msgID](const int & id, const std::string &, const std::string &, const std::string &) + [&msgID](const int &id, const std::string &, const std::string &, const std::string &) { if (id == msgID) { @@ -275,7 +275,7 @@ TEST_F(NotificationProviderServiceTest, ExpectCallbackSyncOnReadToConsumer) int id = 0; mocks.ExpectCallFunc(SyncCallbackFromConsumerEmpty).Do( - [& id](int & type, int & syncId) + [& id](int &type, int &syncId) { std::cout << "MessageSynchronizedCallbackEmpty" << std::endl; if (syncId == id && @@ -363,7 +363,7 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualAddedTopicsAndRegisteredTopic OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1); OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2); - if(topicList != nullptr) + if (topicList != nullptr) { delete topicList; } @@ -400,7 +400,7 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualUnregisteredTopicsAndRegister EXPECT_EQ(isSame, true); OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1); - if(topicList != nullptr) + if (topicList != nullptr) { delete topicList; } @@ -448,7 +448,7 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualSetConsumerTopicsAndGetConsum OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1); OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2); - if(topicList != nullptr) + if (topicList != nullptr) { delete topicList; } @@ -498,8 +498,8 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualUnSetConsumerTopicsAndGetCons OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1); OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2); - - if(topicList != nullptr) + + if (topicList != nullptr) { delete topicList; }