86649e37550a12a18ae06735efbda05ec9399f41
[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 "NSConstants.h"\r
27 #include "oic_string.h"\r
28 #include "oic_malloc.h"\r
29 \r
30 namespace OIC\r
31 {\r
32     namespace Service\r
33     {\r
34         ::NSConsumer *NSConsumer::getNSConsumer()\r
35         {\r
36             ::NSConsumer *nsCon = new ::NSConsumer;\r
37             OICStrcpy(nsCon->consumerId, NS_UTILS_UUID_STRING_SIZE, m_consumerId.c_str());\r
38             return nsCon;\r
39         }\r
40 \r
41         NSConsumer::NSConsumer(::NSConsumer *consumer)\r
42         {\r
43             if (consumer != nullptr)\r
44             {\r
45                 m_consumerId.assign(consumer->consumerId, NS_UTILS_UUID_STRING_SIZE - 1);\r
46             }\r
47         }\r
48 \r
49         std::string NSConsumer::getConsumerId() const\r
50         {\r
51             return m_consumerId;\r
52         }\r
53 \r
54         NSResult NSConsumer::acceptSubscription(bool accepted)\r
55         {\r
56             NS_LOG(DEBUG, "acceptSubscription - IN");\r
57             if (!isValid())\r
58             {\r
59                 return NSResult::FAIL;\r
60             }\r
61             NSResult result = (NSResult) NSAcceptSubscription(getConsumerId().c_str(), accepted);\r
62             NS_LOG(DEBUG, "acceptSubscription - OUT");\r
63             return result;\r
64         }\r
65 \r
66         NSResult NSConsumer::setTopic(const std::string &topicName)\r
67         {\r
68             NS_LOG(DEBUG, "setTopic - IN");\r
69             if (!isValid())\r
70             {\r
71                 return NSResult::FAIL;\r
72             }\r
73             NSResult result = (NSResult) NSProviderSetConsumerTopic(getConsumerId().c_str(),\r
74                               topicName.c_str());\r
75             NS_LOG(DEBUG, "setTopic - OUT");\r
76             return result;\r
77         }\r
78 \r
79         NSResult NSConsumer::unsetTopic(const std::string &topicName)\r
80         {\r
81             NS_LOG(DEBUG, "unsetTopic - IN");\r
82             if (!isValid())\r
83             {\r
84                 return NSResult::FAIL;\r
85             }\r
86             NSResult result = (NSResult) NSProviderUnsetConsumerTopic(getConsumerId().c_str(),\r
87                               topicName.c_str());\r
88             NS_LOG(DEBUG, "unsetTopic - OUT");\r
89             return result;\r
90         }\r
91 \r
92         NSTopicsList *NSConsumer::getConsumerTopicList()\r
93         {\r
94             NS_LOG(DEBUG, "getConsumerTopicList - IN");\r
95             if (!isValid())\r
96             {\r
97                 return nullptr;\r
98             }\r
99             ::NSTopicLL *topics = NSProviderGetConsumerTopics(getConsumerId().c_str());\r
100 \r
101             NSTopicsList *nsTopics = new NSTopicsList(topics);\r
102             NS_LOG(DEBUG, "getConsumerTopicList - OUT");\r
103             return nsTopics;\r
104         }\r
105 \r
106         bool NSConsumer::isValid() const\r
107         {\r
108             if (!NSProviderService::getInstance()->getAcceptedConsumers()->isAccepted(getConsumerId()))\r
109             {\r
110                 NS_LOG(DEBUG, "Invalid Operation with stale reference of Consumer. Consumer ID doesnot exist");\r
111                 return false;\r
112             }\r
113             return true;\r
114         }\r
115     }\r
116 }\r