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