replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / cpp-wrapper / provider / src / NSConsumer.cpp
index a81f875..5d3c472 100755 (executable)
 \r
 #include "NSConsumer.h"\r
 #include <cstring>\r
+#include "NSProviderService.h"\r
+#include "NSAcceptedConsumers.h"\r
 #include "NSProviderInterface.h"\r
+#include "NSException.h"\r
 #include "NSConstants.h"\r
 #include "oic_string.h"\r
 #include "oic_malloc.h"\r
@@ -49,17 +52,25 @@ namespace OIC
             return m_consumerId;\r
         }\r
 \r
-        int NSConsumer::acceptSubscription(bool accepted)\r
+        NSResult NSConsumer::acceptSubscription(bool accepted)\r
         {\r
             NS_LOG(DEBUG, "acceptSubscription - IN");\r
+            if (!isValid())\r
+            {\r
+                throw NSException("Invalid Operation with stale reference of Consumer");\r
+            }\r
             NSResult result = (NSResult) NSAcceptSubscription(getConsumerId().c_str(), accepted);\r
             NS_LOG(DEBUG, "acceptSubscription - OUT");\r
-            return (int) result;\r
+            return result;\r
         }\r
 \r
         NSResult NSConsumer::setTopic(const std::string &topicName)\r
         {\r
             NS_LOG(DEBUG, "setTopic - IN");\r
+            if (!isValid())\r
+            {\r
+                throw NSException("Invalid Operation with stale reference of Consumer");\r
+            }\r
             NSResult result = (NSResult) NSProviderSetConsumerTopic(getConsumerId().c_str(),\r
                               topicName.c_str());\r
             NS_LOG(DEBUG, "setTopic - OUT");\r
@@ -69,20 +80,38 @@ namespace OIC
         NSResult NSConsumer::unsetTopic(const std::string &topicName)\r
         {\r
             NS_LOG(DEBUG, "unsetTopic - IN");\r
+            if (!isValid())\r
+            {\r
+                throw NSException("Invalid Operation with stale reference of Consumer");\r
+            }\r
             NSResult result = (NSResult) NSProviderUnsetConsumerTopic(getConsumerId().c_str(),\r
                               topicName.c_str());\r
             NS_LOG(DEBUG, "unsetTopic - OUT");\r
             return result;\r
         }\r
 \r
-        NSTopicsList *NSConsumer::getConsumerTopicList()\r
+        std::shared_ptr<NSTopicsList> NSConsumer::getConsumerTopicList()\r
         {\r
             NS_LOG(DEBUG, "getConsumerTopicList - IN");\r
+            if (!isValid())\r
+            {\r
+                throw NSException("Invalid Operation with stale reference of Consumer");\r
+            }\r
             ::NSTopicLL *topics = NSProviderGetConsumerTopics(getConsumerId().c_str());\r
 \r
-            NSTopicsList *nsTopics = new NSTopicsList(topics);\r
+            std::shared_ptr<NSTopicsList> nsTopics = std::make_shared<NSTopicsList>(topics, false);\r
             NS_LOG(DEBUG, "getConsumerTopicList - OUT");\r
             return nsTopics;\r
         }\r
+\r
+        bool NSConsumer::isValid() const\r
+        {\r
+            if (!NSProviderService::getInstance()->getAcceptedConsumers()->isAccepted(getConsumerId()))\r
+            {\r
+                NS_LOG(DEBUG, "Invalid Operation with stale reference of Consumer. Consumer ID doesnot exist");\r
+                return false;\r
+            }\r
+            return true;\r
+        }\r
     }\r
 }\r