replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / cpp-wrapper / provider / src / NSConsumer.cpp
1 //******************************************************************\r
2 //\r
3 // Copyright 2016 Samsung Electronics All Rights Reserved.\r
4 //\r
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
6 //\r
7 // Licensed under the Apache License, Version 2.0 (the "License");\r
8 // you may not use this file except in compliance with the License.\r
9 // You may obtain a copy of the License at\r
10 //\r
11 //      http://www.apache.org/licenses/LICENSE-2.0\r
12 //\r
13 // Unless required by applicable law or agreed to in writing, software\r
14 // distributed under the License is distributed on an "AS IS" BASIS,\r
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16 // See the License for the specific language governing permissions and\r
17 // limitations under the License.\r
18 //\r
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
20 \r
21 #include "NSConsumer.h"\r
22 #include <cstring>\r
23 #include "NSProviderService.h"\r
24 #include "NSAcceptedConsumers.h"\r
25 #include "NSProviderInterface.h"\r
26 #include "NSException.h"\r
27 #include "NSConstants.h"\r
28 #include "oic_string.h"\r
29 #include "oic_malloc.h"\r
30 \r
31 namespace OIC\r
32 {\r
33     namespace Service\r
34     {\r
35         ::NSConsumer *NSConsumer::getNSConsumer()\r
36         {\r
37             ::NSConsumer *nsCon = new ::NSConsumer;\r
38             OICStrcpy(nsCon->consumerId, NS_UTILS_UUID_STRING_SIZE, m_consumerId.c_str());\r
39             return nsCon;\r
40         }\r
41 \r
42         NSConsumer::NSConsumer(::NSConsumer *consumer)\r
43         {\r
44             if (consumer != nullptr)\r
45             {\r
46                 m_consumerId.assign(consumer->consumerId, NS_UTILS_UUID_STRING_SIZE - 1);\r
47             }\r
48         }\r
49 \r
50         std::string NSConsumer::getConsumerId() const\r
51         {\r
52             return m_consumerId;\r
53         }\r
54 \r
55         NSResult NSConsumer::acceptSubscription(bool accepted)\r
56         {\r
57             NS_LOG(DEBUG, "acceptSubscription - IN");\r
58             if (!isValid())\r
59             {\r
60                 throw NSException("Invalid Operation with stale reference of Consumer");\r
61             }\r
62             NSResult result = (NSResult) NSAcceptSubscription(getConsumerId().c_str(), accepted);\r
63             NS_LOG(DEBUG, "acceptSubscription - OUT");\r
64             return result;\r
65         }\r
66 \r
67         NSResult NSConsumer::setTopic(const std::string &topicName)\r
68         {\r
69             NS_LOG(DEBUG, "setTopic - IN");\r
70             if (!isValid())\r
71             {\r
72                 throw NSException("Invalid Operation with stale reference of Consumer");\r
73             }\r
74             NSResult result = (NSResult) NSProviderSetConsumerTopic(getConsumerId().c_str(),\r
75                               topicName.c_str());\r
76             NS_LOG(DEBUG, "setTopic - OUT");\r
77             return result;\r
78         }\r
79 \r
80         NSResult NSConsumer::unsetTopic(const std::string &topicName)\r
81         {\r
82             NS_LOG(DEBUG, "unsetTopic - IN");\r
83             if (!isValid())\r
84             {\r
85                 throw NSException("Invalid Operation with stale reference of Consumer");\r
86             }\r
87             NSResult result = (NSResult) NSProviderUnsetConsumerTopic(getConsumerId().c_str(),\r
88                               topicName.c_str());\r
89             NS_LOG(DEBUG, "unsetTopic - OUT");\r
90             return result;\r
91         }\r
92 \r
93         std::shared_ptr<NSTopicsList> NSConsumer::getConsumerTopicList()\r
94         {\r
95             NS_LOG(DEBUG, "getConsumerTopicList - IN");\r
96             if (!isValid())\r
97             {\r
98                 throw NSException("Invalid Operation with stale reference of Consumer");\r
99             }\r
100             ::NSTopicLL *topics = NSProviderGetConsumerTopics(getConsumerId().c_str());\r
101 \r
102             std::shared_ptr<NSTopicsList> nsTopics = std::make_shared<NSTopicsList>(topics, false);\r
103             NS_LOG(DEBUG, "getConsumerTopicList - OUT");\r
104             return nsTopics;\r
105         }\r
106 \r
107         bool NSConsumer::isValid() const\r
108         {\r
109             if (!NSProviderService::getInstance()->getAcceptedConsumers()->isAccepted(getConsumerId()))\r
110             {\r
111                 NS_LOG(DEBUG, "Invalid Operation with stale reference of Consumer. Consumer ID doesnot exist");\r
112                 return false;\r
113             }\r
114             return true;\r
115         }\r
116     }\r
117 }\r