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(new NSTopicsList(topicLL));
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(new NSTopicsList(topicLL));
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 NSConsumerService::getInstance()->getAcceptedProviders()->removeProvider(
151 oldProvider->getProviderId());
152 NS_LOG(DEBUG, "initiating the State callback : NS_STOPPED");
153 if (changeCallback != NULL)
155 NS_LOG(DEBUG, "initiating the callback for Response : NS_STOPPED");
156 changeCallback((NSProviderState)state);
160 NS_LOG(DEBUG, "onNSProviderStateChanged - OUT");
163 void NSConsumerService::onNSMessageReceived(::NSMessage *message)
165 NS_LOG(DEBUG, "onNSMessageReceived - IN");
166 NS_LOG_V(DEBUG, "message->providerId : %s", message->providerId);
168 NSMessage nsMessage(message);
170 if (NSConsumerService::getInstance()->getAcceptedProviders()->isAccepted(
171 nsMessage.getProviderId()))
173 auto provider = NSConsumerService::getInstance()->getProvider(
174 nsMessage.getProviderId());
175 if (provider != nullptr)
177 NS_LOG(DEBUG, "Found Provider with given ID");
178 auto callback = provider->getMessageReceivedCb();
179 if (callback != NULL)
181 NS_LOG(DEBUG, "initiating the callback for messageReceived");
186 NS_LOG(DEBUG, "onNSMessageReceived - OUT");
189 void NSConsumerService::onNSSyncInfoReceived(::NSSyncInfo *syncInfo)
191 NS_LOG(DEBUG, "onNSSyncInfoReceived - IN");
192 NS_LOG_V(DEBUG, "syncInfo->providerId : %s", syncInfo->providerId);
194 NSSyncInfo nsSyncInfo(syncInfo);
196 if (NSConsumerService::getInstance()->getAcceptedProviders()->isAccepted(
197 nsSyncInfo.getProviderId()))
199 auto provider = NSConsumerService::getInstance()->getProvider(
200 nsSyncInfo.getProviderId());
201 if (provider != nullptr)
203 NS_LOG(DEBUG, "Found Provider with given ID");
204 auto callback = provider->getSyncInfoReceivedCb();
205 if (callback != NULL)
207 NS_LOG(DEBUG, "initiating the callback for SyncInfoReceived");
208 callback(nsSyncInfo);
212 NS_LOG(DEBUG, "onNSSyncInfoReceived - OUT");
215 NSConsumerService::NSConsumerService()
217 m_providerDiscoveredCb = NULL;
218 m_acceptedProviders = new NSAcceptedProviders();
221 NSConsumerService::~NSConsumerService()
223 m_acceptedProviders->removeProviders();
224 delete m_acceptedProviders;
227 NSConsumerService *NSConsumerService::getInstance()
229 static NSConsumerService s_instance;
233 NSResult NSConsumerService::start(NSConsumerService::ProviderDiscoveredCallback providerDiscovered)
235 NS_LOG(DEBUG, "start - IN");
236 m_acceptedProviders->removeProviders();
238 m_providerDiscoveredCb = providerDiscovered;
239 NSConsumerConfig nsConfig;
240 nsConfig.changedCb = onProviderStateReceived;
241 nsConfig.messageCb = onNSMessageReceived;
242 nsConfig.syncInfoCb = onNSSyncInfoReceived;
244 NSResult result = (NSResult) NSStartConsumer(nsConfig);
245 NS_LOG(DEBUG, "start - OUT");
249 NSResult NSConsumerService::stop()
251 NS_LOG(DEBUG, "stop - IN");
252 m_providerDiscoveredCb = NULL;
253 m_acceptedProviders->removeProviders();
255 NSResult result = (NSResult) NSStopConsumer();
256 NS_LOG(DEBUG, "stop - OUT");
260 NSResult NSConsumerService::enableRemoteService(const std::string &serverAddress)
262 NS_LOG(DEBUG, "enableRemoteService - IN");
263 NS_LOG_V(DEBUG, "Server Address : %s", serverAddress.c_str());
264 NSResult result = NSResult::ERROR;
266 result = (NSResult) NSConsumerEnableRemoteService(OICStrdup(serverAddress.c_str()));
268 NS_LOG(ERROR, "Remote Services feature is not enabled in the Build");
269 (void) serverAddress;
271 NS_LOG(DEBUG, "enableRemoteService - OUT");
275 NSResult NSConsumerService::subscribeMQService(const std::string &serverAddress,
276 const std::string &topicName)
278 NS_LOG(DEBUG, "subscribeMQService - IN");
279 NS_LOG_V(DEBUG, "Server Address : %s", serverAddress.c_str());
280 NSResult result = NSResult::ERROR;
282 result = (NSResult) NSConsumerSubscribeMQService(
283 serverAddress.c_str(), topicName.c_str());
285 NS_LOG(ERROR, "MQ Services feature is not enabled in the Build");
286 (void) serverAddress;
289 NS_LOG(DEBUG, "subscribeMQService - OUT");
293 NSResult NSConsumerService::rescanProvider()
295 NS_LOG(DEBUG, "rescanProvider - IN");
296 NSResult result = (NSResult) NSRescanProvider();
297 NS_LOG(DEBUG, "rescanProvider - OUT");
301 NSConsumerService::ProviderDiscoveredCallback NSConsumerService::getProviderDiscoveredCb()
303 return m_providerDiscoveredCb;
306 std::shared_ptr<NSProvider> NSConsumerService::getProvider(const std::string &id)
308 return m_acceptedProviders->getProvider(id);
311 NSAcceptedProviders *NSConsumerService::getAcceptedProviders()
313 return m_acceptedProviders;