replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / cpp-wrapper / consumer / src / NSProvider.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 "NSProvider.h"\r
22 #include <cstring>\r
23 #include "NSConsumerService.h"\r
24 #include "NSConsumerInterface.h"\r
25 #include "NSAcceptedProviders.h"\r
26 #include "NSConstants.h"\r
27 #include "NSCommon.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         ::NSProvider *NSProvider::getNSProvider()\r
36         {\r
37             ::NSProvider *provider = new ::NSProvider;\r
38             OICStrcpy(provider->providerId, NS_UTILS_UUID_STRING_SIZE, m_providerId.c_str());\r
39             return provider;\r
40         }\r
41 \r
42         NSProvider::NSProvider(::NSProvider *provider)\r
43         {\r
44             m_stateCb = NULL;\r
45             m_messageCb = NULL;\r
46             m_syncInfoCb = NULL;\r
47             m_state = NSProviderState::DENY;\r
48             m_subscribedState = NSProviderSubscribedState::DENY;\r
49 \r
50             m_topicList = std::make_shared<NSTopicsList>();\r
51 \r
52             if (provider != nullptr)\r
53             {\r
54                 m_providerId.assign(provider->providerId, NS_UTILS_UUID_STRING_SIZE - 1);\r
55             }\r
56         }\r
57 \r
58         NSProvider::NSProvider(const NSProvider &provider)\r
59         {\r
60             m_providerId = provider.getProviderId();\r
61             m_topicList = std::make_shared<NSTopicsList>();\r
62             auto topicsList = provider.getTopicList();\r
63             if (topicsList != nullptr)\r
64             {\r
65                 for (auto it : topicsList->getTopicsList())\r
66                 {\r
67                     m_topicList->addTopic(it.getTopicName(), it.getState());\r
68                 }\r
69             }\r
70             m_topicList->unsetModifiability();\r
71             setListener(provider.getProviderStateReceivedCb(), provider.getMessageReceivedCb(),\r
72                         provider.getSyncInfoReceivedCb());\r
73             setProviderState(provider.getProviderState());\r
74             setProviderSubscribedState(provider.getProviderSubscribedState());\r
75         }\r
76 \r
77         NSProvider &NSProvider::operator=(const NSProvider &provider)\r
78         {\r
79             this->m_providerId = provider.getProviderId();\r
80             this->m_topicList = std::make_shared<NSTopicsList>();\r
81             auto topicsList = provider.getTopicList();\r
82             if (topicsList != nullptr)\r
83             {\r
84                 for (auto it : topicsList->getTopicsList())\r
85                 {\r
86                     this->m_topicList->addTopic(it.getTopicName(), it.getState());\r
87                 }\r
88             }\r
89             m_topicList->unsetModifiability();\r
90             this->setListener(provider.getProviderStateReceivedCb(), provider.getMessageReceivedCb(),\r
91                               provider.getSyncInfoReceivedCb());\r
92             this->setProviderState(provider.getProviderState());\r
93             this->setProviderSubscribedState(provider.getProviderSubscribedState());\r
94             return *this;\r
95         }\r
96 \r
97         std::string NSProvider::getProviderId() const\r
98         {\r
99             return m_providerId;\r
100         }\r
101 \r
102         std::shared_ptr<NSTopicsList> NSProvider::getTopicList() const  throw (NSException)\r
103         {\r
104             NS_LOG(DEBUG, "getTopicList - IN");\r
105             if (!isValid())\r
106             {\r
107                 throw NSException("Invalid Operation on provider. Provider is already Stopped.");\r
108             }\r
109             std::shared_ptr<NSTopicsList> topicList = std::make_shared<NSTopicsList>();\r
110             for (auto it : m_topicList->getTopicsList())\r
111             {\r
112                 topicList->addTopic(it.getTopicName(), it.getState());\r
113             }\r
114             topicList->unsetModifiability();\r
115             return topicList;\r
116         }\r
117 \r
118         NSResult NSProvider::updateTopicList(std::shared_ptr<NSTopicsList> topicList)  throw (NSException)\r
119         {\r
120             NS_LOG(DEBUG, "updateTopicList - IN");\r
121             if (!isValid())\r
122             {\r
123                 throw NSException("Invalid Operation on provider. Provider is already Stopped.");\r
124             }\r
125             if (topicList == nullptr)\r
126             {\r
127                 return NSResult::ERROR;\r
128             }\r
129             NS_LOG(DEBUG, "Creating TopicLL from TopicList");\r
130             NSTopicLL *topicLL = NULL;\r
131             for (auto it : topicList->getTopicsList())\r
132             {\r
133                 NSTopicLL *topic = (NSTopicLL *) OICMalloc(sizeof(NSTopicLL));\r
134                 if (topic == nullptr)\r
135                 {\r
136                     NS_LOG(ERROR, "new NSTopicLL failed");\r
137                     return NSResult::ERROR;\r
138                 }\r
139                 topic->topicName = NULL;\r
140                 topic->topicName = OICStrdup(it.getTopicName().c_str());\r
141                 topic->state = (::NSTopicState)it.getState();\r
142                 topic->next = NULL;\r
143                 if (topicLL == NULL)\r
144                 {\r
145                     topicLL = topic;\r
146                 }\r
147                 else\r
148                 {\r
149                     NSTopicLL *iter = topicLL;\r
150                     NSTopicLL *prev = NULL;\r
151                     while (iter)\r
152                     {\r
153                         prev = iter;\r
154                         iter = (NSTopicLL *) iter->next;\r
155                     }\r
156                     prev->next = topic;\r
157                     topic->next = NULL;\r
158                 }\r
159             }\r
160             if (topicLL)\r
161             {\r
162                 NSTopicLL *iter = topicLL;\r
163                 while (iter)\r
164                 {\r
165                     NS_LOG_V(DEBUG, "Topic Name : %s", iter->topicName);\r
166                     NS_LOG_V(DEBUG, "Topic State : %d", (int) iter->state);\r
167                     iter = iter->next;\r
168                 }\r
169             }\r
170             NS_LOG_V(INFO_PRIVATE, "calling Lower layer UpdateTopicList for Provider Id : %s",\r
171                      getProviderId().c_str());\r
172             NSResult result = (NSResult) NSConsumerUpdateTopicList(getProviderId().c_str(), topicLL);\r
173 \r
174             if (topicLL)\r
175             {\r
176                 NSTopicLL *iter = topicLL;\r
177                 NSTopicLL *following = NULL;\r
178 \r
179                 while (iter)\r
180                 {\r
181                     following = iter->next;\r
182                     if (iter)\r
183                     {\r
184                         NSOICFree(iter->topicName);\r
185                         iter->next = NULL;\r
186                         NSOICFree(iter);\r
187                     }\r
188                     iter = following;\r
189                 }\r
190             }\r
191             NS_LOG(DEBUG, "updateTopicList - OUT");\r
192             return result;\r
193         }\r
194 \r
195         NSProviderState NSProvider::getProviderState() const\r
196         {\r
197             NS_LOG_V(DEBUG, "getProviderState  state : %d", (int)m_state);\r
198             if (!isValid())\r
199             {\r
200                 return NSProviderState::STOPPED;\r
201             }\r
202             return m_state;\r
203         }\r
204 \r
205         NSProviderSubscribedState NSProvider::getProviderSubscribedState() const  throw (NSException)\r
206         {\r
207             NS_LOG_V(DEBUG, "getProviderSubscribedState  state : %d", (int)m_subscribedState);\r
208             if (!isValid())\r
209             {\r
210                 throw NSException("Invalid Operation on provider. Provider is already Stopped.");\r
211             }\r
212             return m_subscribedState;\r
213         }\r
214 \r
215         NSResult NSProvider::subscribe()  throw (NSException)\r
216         {\r
217             NS_LOG(DEBUG, "Subscribe - IN");\r
218             if (!isValid())\r
219             {\r
220                 throw NSException("Invalid Operation on provider. Provider is already Stopped.");\r
221             }\r
222             NSResult result = (NSResult) NSSubscribe(getProviderId().c_str());\r
223             NS_LOG(DEBUG, "Subscribe - OUT");\r
224             return result;\r
225         }\r
226 \r
227         NSResult NSProvider::unsubscribe()  throw (NSException)\r
228         {\r
229             NS_LOG(DEBUG, "unsubscribe - IN");\r
230             if (!isValid())\r
231             {\r
232                 throw NSException("Invalid Operation on provider. Provider is already Stopped.");\r
233             }\r
234             NSResult result = (NSResult) NSUnsubscribe(getProviderId().c_str());\r
235             NS_LOG(DEBUG, "unsubscribe - OUT");\r
236             return result;\r
237         }\r
238 \r
239         bool NSProvider::isSubscribed()  throw (NSException)\r
240         {\r
241             NS_LOG(DEBUG, "isSubscribed - IN");\r
242             if (!isValid())\r
243             {\r
244                 throw NSException("Invalid Operation on provider. Provider is already Stopped.");\r
245             }\r
246             NS_LOG_V(DEBUG, "Subscribed state : %d", (int)getProviderSubscribedState());\r
247             if (getProviderSubscribedState() == NSProviderSubscribedState::SUBSCRIBED)\r
248             {\r
249                 return true;\r
250             }\r
251             return false;\r
252         }\r
253 \r
254         NSResult NSProvider::sendSyncInfo(uint64_t messageId, NSSyncInfo::NSSyncType type)  throw (NSException)\r
255         {\r
256             NS_LOG(DEBUG, "SendSyncInfo - IN");\r
257             if (!isValid())\r
258             {\r
259                 throw NSException("Invalid Operation on provider. Provider is already Stopped.");\r
260             }\r
261             NSResult result = (NSResult) NSConsumerSendSyncInfo(getProviderId().c_str(), messageId,\r
262                               (::NSSyncType)type);\r
263             NS_LOG(DEBUG, "SendSyncInfo - OUT");\r
264             return result;\r
265         }\r
266 \r
267         void NSProvider::setListener(NSProvider::ProviderStateCallback stateHandle,\r
268                                      NSProvider::MessageReceivedCallback messageHandle,\r
269                                      NSProvider::SyncInfoReceivedCallback syncHandle)\r
270         {\r
271             NS_LOG(DEBUG, "setListener - IN");\r
272             m_stateCb = stateHandle;\r
273             m_messageCb = messageHandle;\r
274             m_syncInfoCb = syncHandle;\r
275             NS_LOG(DEBUG, "setListener - OUT");\r
276         }\r
277 \r
278         NSProvider::ProviderStateCallback NSProvider::getProviderStateReceivedCb() const\r
279         {\r
280             return m_stateCb;\r
281         }\r
282 \r
283         NSProvider::MessageReceivedCallback NSProvider::getMessageReceivedCb() const\r
284         {\r
285             return m_messageCb;\r
286         }\r
287 \r
288         NSProvider::SyncInfoReceivedCallback NSProvider::getSyncInfoReceivedCb() const\r
289         {\r
290             return m_syncInfoCb;\r
291         }\r
292 \r
293         void NSProvider::setTopicList(std::shared_ptr<NSTopicsList> topicsList)\r
294         {\r
295             m_topicList = topicsList;\r
296         }\r
297 \r
298         void NSProvider::setProviderState(const NSProviderState &providerState)\r
299         {\r
300             m_state = providerState;\r
301         }\r
302 \r
303         void NSProvider::setProviderSubscribedState(const NSProviderSubscribedState &subscribedState)\r
304         {\r
305             m_subscribedState = subscribedState;\r
306         }\r
307 \r
308         bool NSProvider::isValid() const\r
309         {\r
310             if (!NSConsumerService::getInstance()->getAcceptedProviders()->isAccepted(getProviderId()))\r
311             {\r
312                 NS_LOG(DEBUG, "Invalid Operation with stale reference of Provider. Provider ID doesnot exist");\r
313                 return false;\r
314             }\r
315             return true;\r
316         }\r
317     }\r
318 }\r