1 //******************************************************************
\r
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
\r
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\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
11 // http://www.apache.org/licenses/LICENSE-2.0
\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
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
21 #include "NSProvider.h"
\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
35 ::NSProvider *NSProvider::getNSProvider()
\r
37 ::NSProvider *provider = new ::NSProvider;
\r
38 OICStrcpy(provider->providerId, NS_UTILS_UUID_STRING_SIZE, m_providerId.c_str());
\r
42 NSProvider::NSProvider(::NSProvider *provider)
\r
46 m_syncInfoCb = NULL;
\r
47 m_state = NSProviderState::DENY;
\r
48 m_subscribedState = NSProviderSubscribedState::DENY;
\r
50 m_topicList = new NSTopicsList();
\r
52 if (provider != nullptr)
\r
54 m_providerId.assign(provider->providerId, NS_UTILS_UUID_STRING_SIZE - 1);
\r
58 NSProvider::NSProvider(const NSProvider &provider)
\r
60 m_providerId = provider.getProviderId();
\r
61 m_topicList = new NSTopicsList();
\r
62 auto topicsList = provider.getTopicList();
\r
63 if (topicsList != nullptr)
\r
65 for (auto it : topicsList->getTopicsList())
\r
67 getTopicList()->addTopic(it->getTopicName(), it->getState());
\r
70 setListener(provider.getProviderStateReceivedCb(), provider.getMessageReceivedCb(),
\r
71 provider.getSyncInfoReceivedCb());
\r
72 setProviderState(provider.getProviderState());
\r
73 setProviderSubscribedState(provider.getProviderSubscribedState());
\r
76 NSProvider &NSProvider::operator=(const NSProvider &provider)
\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
83 for (auto it : topicsList->getTopicsList())
\r
85 this->getTopicList()->addTopic(it->getTopicName(), it->getState());
\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
95 NSProvider::~NSProvider()
\r
97 if (m_topicList != nullptr)
\r
103 std::string NSProvider::getProviderId() const
\r
105 return m_providerId;
\r
108 NSTopicsList *NSProvider::getTopicList() const
\r
110 NS_LOG(DEBUG, "getTopicList - IN");
\r
115 NSTopicsList *topicList = new NSTopicsList();
\r
116 for (auto it : m_topicList->getTopicsList())
\r
118 topicList->addTopic(it->getTopicName(), it->getState());
\r
123 NSResult NSProvider::updateTopicList(NSTopicsList *topicList)
\r
125 NS_LOG(DEBUG, "updateTopicList - IN");
\r
128 return NSResult::FAIL;
\r
130 if (topicList == nullptr)
\r
132 return NSResult::ERROR;
\r
134 NS_LOG(DEBUG, "Creating TopicLL from TopicList");
\r
135 NSTopicLL *topicLL = NULL;
\r
136 for (auto it : topicList->getTopicsList())
\r
138 NSTopicLL *topic = (NSTopicLL *) OICMalloc(sizeof(NSTopicLL));
\r
139 if (topic == nullptr)
\r
141 NS_LOG(ERROR, "new NSTopicLL failed");
\r
142 return NSResult::ERROR;
\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
154 NSTopicLL *iter = topicLL;
\r
155 NSTopicLL *prev = NULL;
\r
159 iter = (NSTopicLL *) iter->next;
\r
161 prev->next = topic;
\r
162 topic->next = NULL;
\r
167 NSTopicLL *iter = topicLL;
\r
170 NS_LOG_V(DEBUG, "Topic Name : %s", iter->topicName);
\r
171 NS_LOG_V(DEBUG, "Topic State : %d", (int) iter->state);
\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
181 NSTopicLL *iter = topicLL;
\r
182 NSTopicLL *following = NULL;
\r
186 following = iter->next;
\r
189 NSOICFree(iter->topicName);
\r
196 NS_LOG(DEBUG, "updateTopicList - OUT");
\r
200 NSProviderState NSProvider::getProviderState() const
\r
202 NS_LOG_V(DEBUG, "getProviderState state : %d", (int)m_state);
\r
205 return NSProviderState::STOPPED;
\r
210 NSProviderSubscribedState NSProvider::getProviderSubscribedState() const
\r
212 NS_LOG_V(DEBUG, "getProviderSubscribedState state : %d", (int)m_subscribedState);
\r
215 return NSProviderSubscribedState::DENY;
\r
217 return m_subscribedState;
\r
220 NSResult NSProvider::subscribe()
\r
222 NS_LOG(DEBUG, "Subscribe - IN");
\r
225 return NSResult::FAIL;
\r
227 NSResult result = (NSResult) NSSubscribe(getProviderId().c_str());
\r
228 NS_LOG(DEBUG, "Subscribe - OUT");
\r
232 NSResult NSProvider::unsubscribe()
\r
234 NS_LOG(DEBUG, "unsubscribe - IN");
\r
237 return NSResult::FAIL;
\r
239 NSResult result = (NSResult) NSUnsubscribe(getProviderId().c_str());
\r
240 NS_LOG(DEBUG, "unsubscribe - OUT");
\r
244 bool NSProvider::isSubscribed()
\r
246 NS_LOG(DEBUG, "isSubscribed - IN");
\r
251 NS_LOG_V(DEBUG, "Subscribed state : %d", (int)getProviderSubscribedState());
\r
252 if (getProviderSubscribedState() == NSProviderSubscribedState::SUBSCRIBED)
\r
259 NSResult NSProvider::sendSyncInfo(uint64_t messageId, NSSyncInfo::NSSyncType type)
\r
261 NS_LOG(DEBUG, "SendSyncInfo - IN");
\r
264 return NSResult::FAIL;
\r
266 NSResult result = (NSResult) NSConsumerSendSyncInfo(getProviderId().c_str(), messageId,
\r
267 (::NSSyncType)type);
\r
268 NS_LOG(DEBUG, "SendSyncInfo - OUT");
\r
272 void NSProvider::setListener(NSProvider::ProviderStateCallback stateHandle,
\r
273 NSProvider::MessageReceivedCallback messageHandle,
\r
274 NSProvider::SyncInfoReceivedCallback syncHandle)
\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
283 NSProvider::ProviderStateCallback NSProvider::getProviderStateReceivedCb() const
\r
288 NSProvider::MessageReceivedCallback NSProvider::getMessageReceivedCb() const
\r
290 return m_messageCb;
\r
293 NSProvider::SyncInfoReceivedCallback NSProvider::getSyncInfoReceivedCb() const
\r
295 return m_syncInfoCb;
\r
298 void NSProvider::setTopicList(NSTopicsList *topicsList)
\r
300 if (m_topicList != nullptr)
\r
302 delete m_topicList;
\r
304 m_topicList = topicsList;
\r
307 void NSProvider::setProviderState(const NSProviderState &providerState)
\r
309 m_state = providerState;
\r
312 void NSProvider::setProviderSubscribedState(const NSProviderSubscribedState &subscribedState)
\r
314 m_subscribedState = subscribedState;
\r
317 bool NSProvider::isValid() const
\r
319 if (!NSConsumerService::getInstance()->getAcceptedProviders()->isAccepted(getProviderId()))
\r
321 NS_LOG(DEBUG, "Invalid Operation with stale reference of Provider. Provider ID doesnot exist");
\r