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
22 #include "NSProviderService.h"
\r
24 #include "NSCommon.h"
\r
25 #include "NSProviderInterface.h"
\r
26 #include "NSConsumer.h"
\r
27 #include "NSSyncInfo.h"
\r
28 #include "NSConstants.h"
\r
29 #include "oic_string.h"
\r
30 #include "oic_malloc.h"
\r
36 void onConsumerSubscribedCallback(::NSConsumer *consumer)
\r
38 NS_LOG(DEBUG, "onConsumerSubscribedCallback - IN");
\r
39 NSConsumer *nsConsumer = new NSConsumer(consumer);
\r
40 NSProviderService::getInstance()->getAcceptedConsumers().push_back(nsConsumer);
\r
41 if (NSProviderService::getInstance()->getProviderConfig().m_subscribeRequestCb != NULL)
\r
43 NS_LOG(DEBUG, "initiating the callback for consumer subscribed");
\r
44 NSProviderService::getInstance()->getProviderConfig().m_subscribeRequestCb(nsConsumer);
\r
46 NS_LOG(DEBUG, "onConsumerSubscribedCallback - OUT");
\r
49 void onMessageSynchronizedCallback(::NSSyncInfo *syncInfo)
\r
51 NS_LOG(DEBUG, "onMessageSynchronizedCallback - IN");
\r
52 NSSyncInfo *nsSyncInfo = new NSSyncInfo(syncInfo);
\r
53 if (NSProviderService::getInstance()->getProviderConfig().m_syncInfoCb != NULL)
\r
55 NS_LOG(DEBUG, "initiating the callback for synchronized");
\r
56 NSProviderService::getInstance()->getProviderConfig().m_syncInfoCb(nsSyncInfo);
\r
59 NS_LOG(DEBUG, "onMessageSynchronizedCallback - OUT");
\r
62 ::NSMessage *NSProviderService::getNSMessage(NSMessage *msg)
\r
64 ::NSMessage *nsMsg = new ::NSMessage;
\r
65 nsMsg->messageId = msg->getMessageId();
\r
66 OICStrcpy(nsMsg->providerId, NS_UTILS_UUID_STRING_SIZE, msg->getProviderId().c_str());
\r
67 nsMsg->sourceName = OICStrdup(msg->getSourceName().c_str());
\r
68 nsMsg->type = (::NSMessageType) msg->getType();
\r
69 nsMsg->dateTime = OICStrdup(msg->getTime().c_str());
\r
70 nsMsg->ttl = msg->getTTL();
\r
71 nsMsg->title = OICStrdup(msg->getTitle().c_str());
\r
72 nsMsg->contentText = OICStrdup(msg->getContentText().c_str());
\r
73 nsMsg->topic = OICStrdup(msg->getTopic().c_str());
\r
75 nsMsg->mediaContents = new ::NSMediaContents;
\r
76 if (msg->getMediaContents() != nullptr)
\r
77 nsMsg->mediaContents->iconImage = OICStrdup(msg->getMediaContents()->getIconImage().c_str());
\r
79 nsMsg->mediaContents->iconImage = nullptr;
\r
83 NSProviderService::~NSProviderService()
\r
85 for (auto it : getAcceptedConsumers())
\r
89 getAcceptedConsumers().clear();
\r
92 NSProviderService *NSProviderService::getInstance()
\r
94 static NSProviderService s_instance;
\r
98 NSResult NSProviderService::start(NSProviderService::ProviderConfig config)
\r
100 NS_LOG(DEBUG, "start - IN");
\r
103 NSProviderConfig nsConfig;
\r
104 nsConfig.subRequestCallback = onConsumerSubscribedCallback;
\r
105 nsConfig.syncInfoCallback = onMessageSynchronizedCallback;
\r
106 nsConfig.subControllability = config.subControllability;
\r
107 nsConfig.userInfo = OICStrdup(config.userInfo.c_str());
\r
109 NSResult result = (NSResult) NSStartProvider(nsConfig);
\r
110 NS_LOG(DEBUG, "start - OUT");
\r
114 NSResult NSProviderService::stop()
\r
116 NS_LOG(DEBUG, "stop - IN");
\r
117 NSResult result = (NSResult) NSStopProvider();
\r
118 NS_LOG(DEBUG, "stop - OUT");
\r
122 NSResult NSProviderService::enableRemoteService(const std::string &serverAddress)
\r
124 NS_LOG(DEBUG, "enableRemoteService - IN");
\r
125 NS_LOG_V(DEBUG, "Server Address : %s", serverAddress.c_str());
\r
126 NSResult result = NSResult::ERROR;
\r
128 result = (NSResult) NSProviderEnableRemoteService(OICStrdup(serverAddress.c_str()));
\r
130 (void) serverAddress;
\r
131 NS_LOG(ERROR, "Remote Services feature is not enabled in the Build");
\r
133 NS_LOG(DEBUG, "enableRemoteService - OUT");
\r
137 NSResult NSProviderService::disableRemoteService(const std::string &serverAddress)
\r
139 NS_LOG(DEBUG, "disableRemoteService - IN");
\r
140 NS_LOG_V(DEBUG, "Server Address : %s", serverAddress.c_str());
\r
141 NSResult result = NSResult::ERROR;
\r
143 result = (NSResult) NSProviderDisableRemoteService(OICStrdup(serverAddress.c_str()));
\r
145 (void) serverAddress;
\r
146 NS_LOG(ERROR, "Remote Services feature is not enabled in the Build");
\r
148 NS_LOG(DEBUG, "disableRemoteService - OUT");
\r
152 NSResult NSProviderService::sendMessage(NSMessage *msg)
\r
154 NS_LOG(DEBUG, "sendMessage - IN");
\r
155 NSResult result = NSResult::ERROR;
\r
156 if (msg != nullptr)
\r
158 ::NSMessage *nsMsg = getNSMessage(msg);
\r
160 NS_LOG_V(DEBUG, "nsMsg->providerId : %s", nsMsg->providerId);
\r
161 result = (NSResult) NSSendMessage(nsMsg);
\r
162 delete nsMsg->mediaContents;
\r
167 NS_LOG(DEBUG, "Empty Message");
\r
169 NS_LOG(DEBUG, "sendMessage - OUT");
\r
173 void NSProviderService::sendSyncInfo(uint64_t messageId,
\r
174 NSSyncInfo::NSSyncType type)
\r
176 NS_LOG(DEBUG, "sendSyncInfo - IN");
\r
177 NSProviderSendSyncInfo(messageId, (NSSyncType)type);
\r
178 NS_LOG(DEBUG, "sendSyncInfo - OUT");
\r
182 NSMessage *NSProviderService::createMessage()
\r
184 NS_LOG(DEBUG, "createMessage - IN");
\r
186 ::NSMessage *message = NSCreateMessage();
\r
187 NSMessage *nsMessage = new NSMessage(message);
\r
189 NS_LOG_V(DEBUG, "Message ID : %lld", (long long int) nsMessage->getMessageId());
\r
190 NS_LOG_V(DEBUG, "Provider ID : %s", nsMessage->getProviderId().c_str());
\r
191 NS_LOG(DEBUG, "createMessage - OUT");
\r
196 NSResult NSProviderService::registerTopic(const std::string &topicName)
\r
198 NS_LOG(DEBUG, "registerTopic - IN");
\r
199 NSResult result = (NSResult) NSProviderRegisterTopic(topicName.c_str());
\r
200 NS_LOG(DEBUG, "registerTopic - OUT");
\r
204 NSResult NSProviderService::unregisterTopic(const std::string &topicName)
\r
206 NS_LOG(DEBUG, "unregisterTopic - IN");
\r
207 NSResult result = (NSResult) NSProviderUnregisterTopic(topicName.c_str());
\r
208 NS_LOG(DEBUG, "unregisterTopic - OUT");
\r
212 NSTopicsList *NSProviderService::getRegisteredTopicList()
\r
214 NS_LOG(DEBUG, "getRegisteredTopicList - IN");
\r
215 ::NSTopicLL *topics = NSProviderGetTopics();
\r
217 NSTopicsList *nsTopics = new NSTopicsList(topics);
\r
218 NS_LOG(DEBUG, "getRegisteredTopicList - OUT");
\r
222 NSProviderService::ProviderConfig NSProviderService::getProviderConfig()
\r
227 NSConsumer *NSProviderService::getConsumer(const std::string &id)
\r
229 for (auto it : getAcceptedConsumers())
\r
231 if (it->getConsumerId() == id)
\r
233 NS_LOG(DEBUG, "getConsumer : Found Consumer with given ID");
\r
237 NS_LOG(DEBUG, "getConsumer : Not Found Consumer with given ID");
\r
241 std::list<NSConsumer *> &NSProviderService::getAcceptedConsumers()
\r
243 return m_acceptedConsumers;
\r