1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "NSConsumerService.h"
23 #include "NSConsumerInterface.h"
24 #include "NSAcceptedProviders.h"
25 #include "NSMessage.h"
27 #include "NSConstants.h"
28 #include "oic_string.h"
29 #include "oic_malloc.h"
35 void NSConsumerService::onProviderStateReceived(::NSProvider *provider, ::NSProviderState state)
37 NS_LOG(DEBUG, "onNSProviderStateChanged - IN");
38 NS_LOG_V(DEBUG, "provider Id : %s", provider->providerId);
39 NS_LOG_V(DEBUG, "state : %d", (int)state);
42 provId.assign(provider->providerId, NS_UTILS_UUID_STRING_SIZE - 1);
43 std::shared_ptr<NSProvider> oldProvider =
44 NSConsumerService::getInstance()->getProvider(provId);
46 if (oldProvider == nullptr)
48 //Callback received for First time from new Provider as there is no reference to
49 //previous Provider with same ID in internal map.
50 //States received during discovery of Provider : NS_DISCOVERED and NS_ALLOW
51 std::shared_ptr<NSProvider> nsProvider = std::make_shared<NSProvider>(provider);
52 NS_LOG(DEBUG, "Provider with same Id do not exist. updating Data for New Provider");
53 auto discoveredCallback = NSConsumerService::getInstance()->getProviderDiscoveredCb();
54 nsProvider->setProviderState((NSProviderState)state);
55 auto topicLL = NSConsumerGetTopicList(provider->providerId);
56 nsProvider->setTopicList(std::make_shared<NSTopicsList>(topicLL, false));
57 NSConsumerService::getInstance()->getAcceptedProviders()->addProvider(nsProvider);
58 if (state == NS_DISCOVERED)
60 nsProvider->setProviderSubscribedState(NSProviderSubscribedState::DISCOVERED);
61 if (discoveredCallback != NULL)
64 "initiating the Discovered callback : NS_DISCOVERED, policy false, Accepter: Consumer");
65 discoveredCallback(nsProvider);
68 else if (state == NS_ALLOW)
70 nsProvider->setProviderSubscribedState(NSProviderSubscribedState::SUBSCRIBED);
71 if (discoveredCallback != NULL)
73 NS_LOG(DEBUG, "initiating the Discovered callback : NS_ALLOW, policy true, Accepter: Provider");
74 discoveredCallback(nsProvider);
80 //Callback received from already discovered and existing Provider in internal map.
81 //States received Post discovery of Provider.
82 NS_LOG(DEBUG, "Provider with same Id exists. updating the old Provider data");
83 auto changeCallback = oldProvider->getProviderStateReceivedCb();
84 auto prevState = oldProvider->getProviderState();
85 oldProvider->setProviderState((NSProviderState)state);
86 if (state == NS_ALLOW)
88 oldProvider->setProviderSubscribedState(NSProviderSubscribedState::SUBSCRIBED);
89 if (changeCallback != NULL)
91 NS_LOG(DEBUG, "initiating the callback for Response : NS_ALLOW");
92 changeCallback((NSProviderState)state);
96 oldProvider->setProviderSubscribedState(NSProviderSubscribedState::SUBSCRIBED);
97 auto discoveredCallback = NSConsumerService::getInstance()->getProviderDiscoveredCb();
98 if (discoveredCallback != NULL)
100 discoveredCallback(oldProvider);
102 auto changeCallback = oldProvider->getProviderStateReceivedCb();
103 if (changeCallback != NULL)
105 changeCallback(prevState);
109 else if (state == NS_DENY)
111 oldProvider->setProviderSubscribedState(NSProviderSubscribedState::DENY);
112 NSConsumerService::getInstance()->getAcceptedProviders()->removeProvider(
113 oldProvider->getProviderId());
114 if (changeCallback != NULL)
116 NS_LOG(DEBUG, "initiating the callback for Response : NS_DENY");
117 changeCallback((NSProviderState)state);
120 else if (state == NS_TOPIC)
122 auto topicLL = NSConsumerGetTopicList(provider->providerId);
123 oldProvider->setTopicList(std::make_shared<NSTopicsList>(topicLL, false));
124 if (changeCallback != NULL)
126 NS_LOG(DEBUG, "initiating the callback for Response : NS_TOPIC");
127 changeCallback((NSProviderState)state);
131 NSTopicLL *iter = topicLL;
132 NSTopicLL *following = NULL;
136 following = iter->next;
139 NSOICFree(iter->topicName);
147 else if (state == NS_STOPPED)
149 oldProvider->setProviderSubscribedState(NSProviderSubscribedState::DENY);
150 oldProvider->getTopicList()->unsetModifiability();
151 NSConsumerService::getInstance()->getAcceptedProviders()->removeProvider(
152 oldProvider->getProviderId());
153 NS_LOG(DEBUG, "initiating the State callback : NS_STOPPED");
154 if (changeCallback != NULL)
156 NS_LOG(DEBUG, "initiating the callback for Response : NS_STOPPED");
157 changeCallback((NSProviderState)state);
161 NS_LOG(DEBUG, "onNSProviderStateChanged - OUT");
164 void NSConsumerService::onNSMessageReceived(::NSMessage *message)
166 NS_LOG(DEBUG, "onNSMessageReceived - IN");
167 NS_LOG_V(DEBUG, "message->providerId : %s", message->providerId);
169 NSMessage nsMessage(message);
171 if (NSConsumerService::getInstance()->getAcceptedProviders()->isAccepted(
172 nsMessage.getProviderId()))
174 auto provider = NSConsumerService::getInstance()->getProvider(
175 nsMessage.getProviderId());
176 if (provider != nullptr)
178 NS_LOG(DEBUG, "Found Provider with given ID");
179 auto callback = provider->getMessageReceivedCb();
180 if (callback != NULL)
182 NS_LOG(DEBUG, "initiating the callback for messageReceived");
187 NS_LOG(DEBUG, "onNSMessageReceived - OUT");
190 void NSConsumerService::onNSSyncInfoReceived(::NSSyncInfo *syncInfo)
192 NS_LOG(DEBUG, "onNSSyncInfoReceived - IN");
193 NS_LOG_V(DEBUG, "syncInfo->providerId : %s", syncInfo->providerId);
195 NSSyncInfo nsSyncInfo(syncInfo);
197 if (NSConsumerService::getInstance()->getAcceptedProviders()->isAccepted(
198 nsSyncInfo.getProviderId()))
200 auto provider = NSConsumerService::getInstance()->getProvider(
201 nsSyncInfo.getProviderId());
202 if (provider != nullptr)
204 NS_LOG(DEBUG, "Found Provider with given ID");
205 auto callback = provider->getSyncInfoReceivedCb();
206 if (callback != NULL)
208 NS_LOG(DEBUG, "initiating the callback for SyncInfoReceived");
209 callback(nsSyncInfo);
213 NS_LOG(DEBUG, "onNSSyncInfoReceived - OUT");
216 NSConsumerService::NSConsumerService()
218 m_providerDiscoveredCb = NULL;
219 m_acceptedProviders = new NSAcceptedProviders();
222 NSConsumerService::~NSConsumerService()
224 m_acceptedProviders->removeProviders();
225 delete m_acceptedProviders;
228 NSConsumerService *NSConsumerService::getInstance()
230 static NSConsumerService s_instance;
234 NSResult NSConsumerService::start(NSConsumerService::ProviderDiscoveredCallback providerDiscovered)
236 NS_LOG(DEBUG, "start - IN");
237 m_acceptedProviders->removeProviders();
239 m_providerDiscoveredCb = providerDiscovered;
240 NSConsumerConfig nsConfig;
241 nsConfig.changedCb = onProviderStateReceived;
242 nsConfig.messageCb = onNSMessageReceived;
243 nsConfig.syncInfoCb = onNSSyncInfoReceived;
245 NSResult result = (NSResult) NSStartConsumer(nsConfig);
246 NS_LOG(DEBUG, "start - OUT");
250 NSResult NSConsumerService::stop()
252 NS_LOG(DEBUG, "stop - IN");
253 m_providerDiscoveredCb = NULL;
254 m_acceptedProviders->removeProviders();
256 NSResult result = (NSResult) NSStopConsumer();
257 NS_LOG(DEBUG, "stop - OUT");
261 NSResult NSConsumerService::enableRemoteService(const std::string &serverAddress)
263 NS_LOG(DEBUG, "enableRemoteService - IN");
264 NS_LOG_V(DEBUG, "Server Address : %s", serverAddress.c_str());
265 NSResult result = NSResult::ERROR;
267 result = (NSResult) NSConsumerEnableRemoteService(OICStrdup(serverAddress.c_str()));
269 NS_LOG(ERROR, "Remote Services feature is not enabled in the Build");
270 (void) serverAddress;
272 NS_LOG(DEBUG, "enableRemoteService - OUT");
276 NSResult NSConsumerService::subscribeMQService(const std::string &serverAddress,
277 const std::string &topicName)
279 NS_LOG(DEBUG, "subscribeMQService - IN");
280 NS_LOG_V(DEBUG, "Server Address : %s", serverAddress.c_str());
281 NSResult result = NSResult::ERROR;
283 result = (NSResult) NSConsumerSubscribeMQService(
284 serverAddress.c_str(), topicName.c_str());
286 NS_LOG(ERROR, "MQ Services feature is not enabled in the Build");
287 (void) serverAddress;
290 NS_LOG(DEBUG, "subscribeMQService - OUT");
294 NSResult NSConsumerService::rescanProvider()
296 NS_LOG(DEBUG, "rescanProvider - IN");
297 NSResult result = (NSResult) NSRescanProvider();
298 NS_LOG(DEBUG, "rescanProvider - OUT");
302 NSConsumerService::ProviderDiscoveredCallback NSConsumerService::getProviderDiscoveredCb()
304 return m_providerDiscoveredCb;
307 std::shared_ptr<NSProvider> NSConsumerService::getProvider(const std::string &id)
309 return m_acceptedProviders->getProvider(id);
312 NSAcceptedProviders *NSConsumerService::getAcceptedProviders()
314 return m_acceptedProviders;