1 //******************************************************************
3 // Copyright 2014 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 /// @file GroupManager.cpp
24 #include "GroupManager.h"
31 #define PLAIN_DELIMITER "\""
32 #define ACTION_DELIMITER "*"
33 #define DESC_DELIMITER "|"
34 #define ATTR_DELIMITER "="
40 std::map< std::vector< std::string >, CandidateCallback > candidateRequest;
41 std::map< std::vector< std::string >, CandidateCallback > candidateRequestForTimer;
42 std::map< std::string, std::map< std::string, std::shared_ptr< OCResource > > > rtForResourceList;
43 std::vector< std::string > allFoundResourceTypes;
44 std::mutex callbackLock;
47 template< typename T >
48 bool IsSubset(std::vector< T > full, std::vector< T > sub)
50 std::sort(full.begin(), full.end());
51 std::sort(sub.begin(), sub.end());
52 return std::includes(full.begin(), full.end(), sub.begin(), sub.end());
54 std::vector< std::string > &str_split(const std::string &s, char delim,
55 std::vector< std::string > &elems)
57 std::stringstream ss(s);
59 while (std::getline(ss, item, delim))
61 elems.push_back(item);
66 std::vector< std::string > str_split(const std::string &s, char delim)
68 std::vector< std::string > elems;
69 str_split(s, delim, elems);
73 void GroupManager::onFoundResource(std::shared_ptr< OCResource > resource, int waitsec)
76 std::string resourceURI;
77 std::string hostAddress;
80 // Do some operations with resource object.
84 std::cout << "DISCOVERED Resource:" << std::endl;
85 // Get the resource URI
86 resourceURI = resource->uri();
87 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
89 // Get the resource host address
90 hostAddress = resource->host();
91 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
93 // Get the resource types
94 std::cout << "\tList of resource types: " << std::endl;
96 hostAddress.append(resourceURI);
98 for (auto &resourceTypes : resource->getResourceTypes())
100 std::cout << "\t\t" << resourceTypes << std::endl;
102 if (std::find(allFoundResourceTypes.begin(), allFoundResourceTypes.end(),
103 resourceTypes) == allFoundResourceTypes.end())
105 allFoundResourceTypes.push_back(resourceTypes);
108 rtForResourceList[resourceTypes][hostAddress] = resource;
111 // Get the resource interfaces
112 std::cout << "\tList of resource interfaces: " << std::endl;
113 for (auto &resourceInterfaces : resource->getResourceInterfaces())
115 std::cout << "\t\t" << resourceInterfaces << std::endl;
120 findPreparedRequest(candidateRequest);
125 // Resource is invalid
126 std::cout << "Resource is invalid" << std::endl;
130 catch (std::exception& e)
136 GroupManager::GroupManager(void)
144 GroupManager::~GroupManager(void)
146 candidateRequest.clear();
147 candidateRequestForTimer.clear();
148 rtForResourceList.clear();
149 allFoundResourceTypes.clear();
152 void GroupManager::findPreparedRequest(
153 std::map< std::vector< std::string >, CandidateCallback > &request)
155 std::lock_guard<std::mutex> lock(callbackLock);
156 std::vector< std::shared_ptr< OCResource > > resources;
158 for (auto it = request.begin(); it != request.end();)
161 if (IsSubset(allFoundResourceTypes, it->first))
163 for (unsigned int i = 0; i < it->first.size(); ++i)
166 for (auto secondIt = rtForResourceList[it->first.at(i)].begin();
167 secondIt != rtForResourceList[it->first.at(i)].end(); ++secondIt)
169 //insert resource related to request
170 resources.push_back(secondIt->second);
174 it->second(resources);
176 //TODO : decide policy - callback only once
188 void GroupManager::lazyCallback(int second)
191 findPreparedRequest(candidateRequestForTimer);
195 OCStackResult GroupManager::findCandidateResources(std::vector< std::string > resourceTypes,
196 CandidateCallback callback, int waitsec)
198 if (resourceTypes.size() < 1)
200 return OC_STACK_ERROR;
204 return OC_STACK_ERROR;
207 std::sort(resourceTypes.begin(), resourceTypes.end());
208 resourceTypes.erase(std::unique(resourceTypes.begin(), resourceTypes.end()),
209 resourceTypes.end());
213 candidateRequestForTimer.insert(std::make_pair(resourceTypes, callback));
217 candidateRequest.insert(std::make_pair(resourceTypes, callback));
220 for (unsigned int i = 0; i < resourceTypes.size(); ++i)
222 std::cout << "resourceTypes : " << resourceTypes.at(i) << std::endl;
223 std::string query = OC_WELL_KNOWN_QUERY;
225 query += resourceTypes.at(i);
227 OCPlatform::findResource("", query, OC_WIFI,
228 std::function < void(std::shared_ptr < OCResource > resource)
229 > (std::bind(&GroupManager::onFoundResource, this,
230 std::placeholders::_1, waitsec)));
236 std::function< void(int second) >(
237 std::bind(&GroupManager::lazyCallback, this, std::placeholders::_1)),
246 OCStackResult GroupManager::bindResourceToGroup(OCResourceHandle& childHandle, std::shared_ptr< OCResource > resource, OCResourceHandle& collectionHandle)
249 OCStackResult result = OCPlatform::registerResource(childHandle, resource);
251 cout << "\tresource registed!" << endl;
253 if(result == OC_STACK_OK)
255 OCPlatform::bindResource(collectionHandle, childHandle);
259 cout << "\tresource Error!" << endl;
271 std::map< std::string, CollectionPresenceCallback > presenceCallbacks;
273 // Callback to presence
274 void GroupManager::collectionPresenceHandler(OCStackResult result, const unsigned int nonce,
275 const std::string& hostAddress, std::string host, std::string uri)
277 std::cout << "uri : " << uri << std::endl;
278 std::cout << "host : " << host << std::endl;
279 std::cout << "result : " << result << std::endl;
283 std::cout << "Nonce# " << nonce << std::endl;
285 case OC_STACK_PRESENCE_STOPPED:
286 std::cout << "Presence Stopped\n";
288 case OC_STACK_PRESENCE_DO_NOT_HANDLE:
289 std::cout << "Presence do not handle\n";
291 case OC_STACK_PRESENCE_TIMEOUT:
292 std::cout << "Presence TIMEOUT\n";
295 std::cout << "Error\n";
299 if (presenceCallbacks.find(uri) != presenceCallbacks.end())
301 (presenceCallbacks.find(uri)->second)(uri, result);
305 void GroupManager::checkCollectionRepresentation(const OCRepresentation& rep,
306 CollectionPresenceCallback callback)
308 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
311 if(rep.hasAttribute("name"))
313 std::cout << "\tRoom name: " << rep.getValue<std::string>("name") << std::endl;
316 std::vector< OCRepresentation > children = rep.getChildren();
318 if(children.size() == 0 )
320 callback("", OC_STACK_ERROR);
324 for (auto oit = children.begin(); oit != children.end(); ++oit)
326 std::cout << "\t\tChild Resource URI: " << oit->getUri() << std::endl;
327 std::vector< std::string > hostAddressVector = str_split(oit->getUri(), '/');
328 std::string hostAddress = "";
329 for (unsigned int i = 0; i < hostAddressVector.size(); ++i)
333 hostAddress.append(hostAddressVector.at(i));
336 hostAddress.append("/");
341 std::vector< std::string > resourceTypes = oit->getResourceTypes();
342 for (unsigned int i = 0; i < resourceTypes.size(); ++i)
344 std::cout << "\t\t\tresourcetype :" << resourceTypes.at(i) << std::endl;
347 std::string resourceType = "core.";
348 resourceType.append(str_split(oit->getUri(), '/').at(4));
349 std::cout << "\t\tconvertRT : " << resourceType << std::endl;
350 std::cout << "\t\thost : " << hostAddress << std::endl;
351 OCPlatform::OCPresenceHandle presenceHandle;
353 OCStackResult result = OCPlatform::subscribePresence(presenceHandle, hostAddress,
354 resourceType, OC_WIFI,
356 void(OCStackResult result, const unsigned int nonce,
357 const std::string& hostAddress) >(
358 std::bind(&GroupManager::collectionPresenceHandler, this,
359 std::placeholders::_1, std::placeholders::_2,
360 std::placeholders::_3, hostAddress, oit->getUri())));
362 if (result == OC_STACK_OK)
364 std::cout << "\t\tOK!" << std::endl;
365 presenceCallbacks.insert(std::make_pair(oit->getUri(), callback));
369 callback("", OC_STACK_ERROR);
375 void GroupManager::onGetForPresence(const HeaderOptions& headerOptions,
376 const OCRepresentation& rep, const int eCode, CollectionPresenceCallback callback)
378 if (eCode == OC_STACK_OK)
380 std::cout << "GET request was successful" << std::endl;
381 std::cout << "Resource URI: " << rep.getUri() << std::endl;
383 checkCollectionRepresentation(rep, callback);
388 std::cout << "onGET Response error: " << eCode << std::endl;
389 callback("", OC_STACK_ERROR);
394 OCStackResult GroupManager::subscribeCollectionPresence(
395 std::shared_ptr< OCResource > collectionResource, CollectionPresenceCallback callback)
397 if(callback == NULL || collectionResource == NULL)
399 return OC_STACK_ERROR;
402 OCStackResult result = OC_STACK_OK;
403 //callback("core.room",OC_STACK_OK);
405 QueryParamsMap queryParam;
407 //parameter 1 = resourceType
408 collectionResource->get("", DEFAULT_INTERFACE, queryParam,
410 void(const HeaderOptions& headerOptions, const OCRepresentation& rep,
412 std::bind(&GroupManager::onGetForPresence, this, std::placeholders::_1,
413 std::placeholders::_2, std::placeholders::_3, callback)));
422 std::string GroupManager::getStringFromActionSet(const ActionSet *newActionSet)
424 std::string message = "";
426 if(newActionSet == NULL)
429 message = newActionSet->actionsetName;
431 for (auto iterAction = newActionSet->listOfAction.begin();
432 iterAction != newActionSet->listOfAction.end(); iterAction++)
434 message.append("uri=");
435 message.append((*iterAction)->target);
438 for (auto iterCapa = (*iterAction)->listOfCapability.begin();
439 iterCapa != (*iterAction)->listOfCapability.end(); iterCapa++)
441 message.append((*iterCapa)->capability);
443 message.append((*iterCapa)->status);
445 if (iterCapa + 1 != (*iterAction)->listOfCapability.end())
449 if (iterAction + 1 != newActionSet->listOfAction.end())
458 ActionSet* GroupManager::getActionSetfromString(std::string desc)
462 char *plainText = NULL;
463 char *plainPtr = NULL;
465 ActionSet *actionset = new ActionSet();
466 plainText = new char[(desc.length() + 1)];
467 strcpy(plainText, desc.c_str());
469 token = strtok_r(plainText, ACTION_DELIMITER, &plainPtr);
473 actionset->actionsetName = std::string(token);
474 token = strtok_r(NULL, ACTION_DELIMITER, &plainPtr);
485 char *descPtr = NULL;
486 char *desc = new char[(strlen(token) + 1)];
490 Action *action = NULL;
492 token = strtok_r(desc, DESC_DELIMITER, &descPtr);
494 // cout << "desc :: " << token << endl;
495 while (token != NULL)
497 char *attrPtr = NULL;
498 char *attr = new char[(strlen(token) + 1)];
502 // cout << "attr :: " << attr << endl;
504 token = strtok_r(attr, ATTR_DELIMITER, &attrPtr);
505 while (token != NULL)
507 if (strcmp(token, "uri") == 0)
509 token = strtok_r(NULL, ATTR_DELIMITER, &attrPtr);
510 action = new Action();
514 action->target = std::string(token);
527 Capability *capa = new Capability();
528 capa->capability = std::string(token);
529 token = strtok_r(NULL, ATTR_DELIMITER, &attrPtr);
530 capa->status = std::string(token);
534 action->listOfCapability.push_back(capa);
547 token = strtok_r(NULL, ATTR_DELIMITER, &attrPtr);
551 token = strtok_r(NULL, DESC_DELIMITER, &descPtr);
554 actionset->listOfAction.push_back(action);
566 token = strtok_r(NULL, ACTION_DELIMITER, &plainPtr);
573 OCStackResult GroupManager::addActionSet(std::shared_ptr< OCResource > resource,
574 const ActionSet* newActionSet, PutCallback cb)
576 // BUILD message of ActionSet which it is included delimiter.
577 if ((resource != NULL) && (newActionSet != NULL))
579 std::string message = getStringFromActionSet(newActionSet);
580 OCRepresentation rep;
582 rep.setValue("ActionSet", message);
584 return resource->put(resource->getResourceTypes().front(), GROUP_INTERFACE, rep,
585 QueryParamsMap(), cb);
589 return OC_STACK_ERROR;
593 OCStackResult GroupManager::executeActionSet(std::shared_ptr< OCResource > resource,
594 std::string actionsetName, PostCallback cb)
596 if (resource != NULL)
598 OCRepresentation rep;
600 rep.setValue("DoAction", actionsetName);
601 return resource->post(resource->getResourceTypes().front(), GROUP_INTERFACE, rep,
602 QueryParamsMap(), cb);
606 return OC_STACK_ERROR;
610 OCStackResult GroupManager::getActionSet(std::shared_ptr< OCResource > resource,
611 std::string actionsetName, PostCallback cb)
613 if (resource != NULL)
615 OCRepresentation rep;
617 rep.setValue("GetActionSet", actionsetName);
619 return resource->post(resource->getResourceTypes().front(), GROUP_INTERFACE, rep,
620 QueryParamsMap(), cb);
624 return OC_STACK_ERROR;
628 OCStackResult GroupManager::deleteActionSet(std::shared_ptr< OCResource > resource,
629 std::string actionsetName, PutCallback cb)
631 if (resource != NULL)
633 OCRepresentation rep;
635 rep.setValue("DelActionSet", actionsetName);
637 return resource->put(resource->getResourceTypes().front(), GROUP_INTERFACE, rep,
638 QueryParamsMap(), cb);
642 return OC_STACK_ERROR;