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 GroupSynchronization.cpp
24 #include "GroupSynchronization.h"
31 GroupSynchronization* GroupSynchronization::groupSyncnstance = NULL;
32 bool GroupSynchronization::bIsFinding = false;
34 GroupSynchronization* GroupSynchronization::getInstance()
36 if (groupSyncnstance == NULL)
38 groupSyncnstance = new GroupSynchronization();
40 return groupSyncnstance;
43 void GroupSynchronization::deleteInstance()
47 delete groupSyncnstance;
48 groupSyncnstance = NULL;
52 OCStackResult GroupSynchronization::findGroup(
53 std::vector< std::string > collectionResourceTypes, FindCallback callback)
55 cout << "GroupSynchronization::findGroup" << endl;
58 return OC_STACK_ERROR;
60 foundGroupResourceList.clear();
61 findCallback = callback;
63 if (findCallback == NULL)
65 return OC_STACK_ERROR;
68 for (unsigned int i = 0; i < collectionResourceTypes.size(); ++i)
70 std::string query = "coap://224.0.1.187/oc/core?rt=";
71 query.append(collectionResourceTypes.at(i));
72 cout << "GroupSynchronization::findGroup - " << query << endl;
74 OCPlatform::findResource("", query,
75 std::bind(&GroupSynchronization::onFindGroup, this, std::placeholders::_1));
80 // thread to check if GroupSynchronization::onFoundGroup is called or not.
81 std::thread t(std::bind(&GroupSynchronization::checkFindGroup, this));
87 OCStackResult GroupSynchronization::createGroup(std::string collectionResourceType)
89 foundGroupResourceList.clear();
91 OCResourceHandle collectionResHandle = NULL;
92 OCResourceHandle groupSyncResHandle = NULL;
94 if (0 == collectionResourceType.length())
96 cout << "GroupSynchronization::createGroup : Error! Input params are wrong." << endl;
97 return OC_STACK_INVALID_PARAM;
100 cout << "GroupSynchronization::createGroup - The created group is added." << endl;
102 OCStackResult result;
104 // creating master collection resource
105 std::string collectionUri = "/" + collectionResourceType;
107 while ((i = collectionUri.find(".")) != std::string::npos)
109 collectionUri.replace(i, 1, "/");
111 cout << "GroupSynchronization::createGroup : collection uri - " << collectionUri
112 << ", type - " << collectionResourceType << endl;
114 std::string resourceInterface = DEFAULT_INTERFACE;
116 result = OCPlatform::registerResource(collectionResHandle, collectionUri,
117 collectionResourceType, resourceInterface, NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
118 if (result != OC_STACK_OK)
120 cout << "To register resource (" << collectionUri << ") was unsuccessful. result - "
126 OCPlatform::bindInterfaceToResource(collectionResHandle, GROUP_INTERFACE);
127 if (result != OC_STACK_OK)
129 cout << "To bind Interface (collection) was unsuccessful. result - " << result
133 collectionResourceHandleList[collectionResourceType] = collectionResHandle;
135 // creating master group sync resource
136 std::string groupSyncUri = collectionUri + "/groupsync";
137 std::string groupSyncResType = collectionResourceType + ".groupsync";
139 // cout << "GroupSynchronization::createGroup : groupSync uri - " << groupSyncUri
140 // << ", type - " << collectionResourceType << endl;
142 result = OCPlatform::registerResource(groupSyncResHandle, groupSyncUri,
143 groupSyncResType, resourceInterface,
144 std::bind(&GroupSynchronization::groupEntityHandler, this,
145 std::placeholders::_1), OC_DISCOVERABLE | OC_OBSERVABLE);
146 if (result != OC_STACK_OK)
148 cout << "To register resource (groupsync) was unsuccessful. result - " << result
153 groupSyncResourceHandleList[collectionResourceType] = groupSyncResHandle;
159 if (collectionResHandle)
161 OCPlatform::unregisterResource(collectionResHandle);
162 auto iterator = collectionResourceHandleList.find(collectionResourceType);
163 if (iterator != collectionResourceHandleList.end())
165 collectionResourceHandleList.erase(iterator);
169 if (groupSyncResHandle)
171 OCPlatform::unregisterResource(groupSyncResHandle);
172 auto iterator = groupSyncResourceHandleList.find(collectionResourceType);
173 if (iterator != groupSyncResourceHandleList.end())
175 groupSyncResourceHandleList.erase(iterator);
179 return OC_STACK_NO_RESOURCE;
182 OCStackResult GroupSynchronization::joinGroup(std::string collectionResourceType,
183 OCResourceHandle resourceHandle)
185 if ((0 == collectionResourceType.length()) || (!resourceHandle))
187 cout << "GroupSynchronization::joinGroup : Error! input params are wrong." << endl;
188 return OC_STACK_INVALID_PARAM;
190 auto resIt = collectionResourceHandleList.find(collectionResourceType);
191 if (resIt == collectionResourceHandleList.end())
193 cout << "GroupSynchronization::joinGroup : error! There is no collection to join"
195 return OC_STACK_INVALID_PARAM;
198 OCResourceHandle collectionResHandle = resIt->second;
201 OCStackResult result = OCPlatform::bindResource(collectionResHandle, resourceHandle);
202 if (result != OC_STACK_OK)
204 cout << "GroupSynchronization::joinGroup : To bind resource was unsuccessful."
205 << "result - " << result << endl;
206 return OC_STACK_ERROR;
208 } catch(OCException &e) {
210 return OC_STACK_INVALID_PARAM;
213 cout << "GroupSynchronization::joinGroup : "
214 << "To bind collectionResHandle and resourceHandle" << endl;
216 std::vector< OCResourceHandle > childHandleList;
218 auto childIt = childResourceHandleList.find(collectionResHandle);
219 if (childIt != childResourceHandleList.end())
221 childHandleList = childIt->second;
224 childHandleList.push_back(resourceHandle);
225 childResourceHandleList[collectionResHandle] = childHandleList;
227 deviceResourceHandleList.push_back(resourceHandle);
234 OCStackResult GroupSynchronization::joinGroup(const std::shared_ptr< OCResource > resource,
235 OCResourceHandle resourceHandle)
237 if ((!resource) || (!resourceHandle))
239 cout << "GroupSynchronization::joinGroup : Error! Input params are wrong." << endl;
240 return OC_STACK_INVALID_PARAM;
243 cout << "GroupSynchronization::joinGroup" << endl;
245 // making representation to join group
246 std::string method = "joinGroup";
247 std::vector< std::string > type = resource->getResourceTypes();
248 std::string resourceType;
249 resourceType.append(OCGetResourceTypeName(resourceHandle, 0));
251 OCRepresentation rep;
252 rep.setValue("method", method);
253 rep.setValue("collectionResourceType", type[0]);
254 rep.setValue("resourceType", resourceType);
256 cout << "\tmethod - " << method << endl;
257 cout << "\tcollectionResourceType - " << type[0] << endl;
258 cout << "\tresourceType - " << resourceType << endl;
260 // creating group sync resource with the received collection resource.
261 // entity handler of group sync is used to join group.
262 std::string host = resource->host();
263 std::string uri = resource->uri() + "/groupsync";
265 std::vector< std::string > resourceTypes;
267 for (unsigned int i = 0; i < type.size(); ++i)
269 temp = type[0] + ".groupsync";
270 resourceTypes.push_back(temp);
273 std::vector< std::string > resourceInterface;
274 resourceInterface.push_back(DEFAULT_INTERFACE);
276 OCResource::Ptr groupSyncResource = OCPlatform::constructResourceObject(host, uri, 1,
277 resourceTypes, resourceInterface);
278 groupSyncResourceList[type[0]] = groupSyncResource;
280 cout << "GroupSynchronization::joinGroup : creating groupSyncResource." << endl;
282 // Create QueryParameters Map and add query params (if any)
283 QueryParamsMap queryParamsMap;
285 // request to join group to the remote group sync resource
286 OCStackResult result = groupSyncResource->put(rep, queryParamsMap,
287 std::bind(&GroupSynchronization::onJoinGroup, this, std::placeholders::_1,
288 std::placeholders::_2, std::placeholders::_3));
289 if (OC_STACK_OK == result)
291 cout << "GroupSynchronization::joinGroup : groupSyncResource->put was successful."
296 cout << "GroupSynchronization::joinGroup : "
297 << "groupSyncResource->put was unsuccessful. result - " << result << endl;
300 // saving the remote collection resource.
301 // It is used in onJoinGroup() and onGetJoinedRemoteChild().
302 remoteCollectionResource = resource;
304 // saving the resource handle to join. It is used in onGetJoinedRemoteChild()
305 deviceResourceHandle = resourceHandle;
310 OCStackResult GroupSynchronization::leaveGroup(std::string collectionResourceType,
311 OCResourceHandle resourceHandle)
313 if ((0 == collectionResourceType.length()) || (!resourceHandle))
315 cout << "GroupSynchronization::leaveGroup : Error! Input params are wrong." << endl;
316 return OC_STACK_INVALID_PARAM;
319 cout << "GroupSynchronization::leaveGroup : collectionResourceType - "
320 << collectionResourceType << endl;
322 OCResourceHandle collectionResHandle;
323 auto handleIt = groupSyncResourceHandleList.find(collectionResourceType);
325 // if groupSyncResourceHandleList has resourceType,
326 // this app created collection resource handle.
327 if (handleIt != groupSyncResourceHandleList.end())
329 handleIt = collectionResourceHandleList.find(collectionResourceType);
330 if (handleIt == collectionResourceHandleList.end())
332 cout << "GroupSynchronization::leaveGroup : "
333 << "Error! There is no collection resource handle to leave." << endl;
334 return OC_STACK_INVALID_PARAM;
337 collectionResHandle = handleIt->second;
338 // cout << "GroupSynchronization::leaveGroup : collection handle uri - "
339 // << OCGetResourceUri(collectionResHandle) << endl;
340 if(collectionResHandle == NULL)
341 return OC_STACK_INVALID_PARAM;
343 OCStackResult result;
346 result = OCPlatform::unbindResource(collectionResHandle, resourceHandle);
347 if (OC_STACK_OK == result)
349 cout << "GroupSynchronization::leaveGroup : "
350 << "To unbind resource was successful." << endl;
354 cout << "GroupSynchronization::leaveGroup : "
355 << "To unbind resource was unsuccessful. result - " << result << endl;
358 } catch(OCException &e) {
359 cout << "ERROR : " << e.reason() << endl;
360 return OC_STACK_NO_RESOURCE;
363 auto It = std::find(deviceResourceHandleList.begin(), deviceResourceHandleList.end(),
365 if (It == deviceResourceHandleList.end()) // there is no resource handle to find
369 result = OCPlatform::unregisterResource(resourceHandle);
370 if (OC_STACK_OK == result)
372 cout << "GroupSynchronization::leaveGroup : "
373 << "To unregister resource was successful." << endl;
377 cout << "GroupSynchronization::leaveGroup : "
378 << "To unregister resource was unsuccessful. result - " << result
382 } catch(OCException &e)
384 cout << "ERROR : " << e.reason() << endl;
385 return OC_STACK_NO_RESOURCE;
390 cout << "GroupSynchronization::leaveGroup : "
391 << "This resource cannot be unregistered." << endl;
392 deviceResourceHandleList.erase(It);
395 auto handleListIt = childResourceHandleList.find(collectionResHandle);
396 if (handleListIt == childResourceHandleList.end())
398 cout << "GroupSynchronization::leaveGroup : "
399 << "Error! There is no child resource list to delete." << endl;
400 return OC_STACK_INVALID_PARAM;
403 std::vector< OCResourceHandle > childList = handleListIt->second;
404 auto childIt = std::find(childList.begin(), childList.end(), resourceHandle);
405 if (childIt != childList.end())
407 cout << "GroupSynchronization::groupEntityHandler : "
408 << "Found! The resource to leave is found." << endl;
409 childList.erase(childIt);
412 childResourceHandleList[collectionResHandle] = childList;
416 else // requesting to unbind this resourceHandle to the remote collection resource
418 auto resourceIt = groupSyncResourceList.find(collectionResourceType);
420 if (resourceIt == groupSyncResourceList.end())
422 cout << "GroupSynchronization::leaveGroup : "
423 << "Error! There is no collectin resource type to leave." << endl;
424 return OC_STACK_INVALID_PARAM;
427 std::shared_ptr< OCResource > resource = resourceIt->second;
429 return OC_STACK_NO_RESOURCE;
430 // cout << "GroupSynchronization::leaveGroup : group sync resource uri - "
431 // << resource->uri() << endl;
433 handleIt = collectionResourceHandleList.find(collectionResourceType);
434 if (handleIt == collectionResourceHandleList.end())
436 cout << "GroupSynchronization::leaveGroup : "
437 << "Error! There is no collection resource handle to leave." << endl;
438 return OC_STACK_INVALID_PARAM;
441 collectionResHandle = handleIt->second;
443 // making representation to leave group
444 std::string method = "leaveGroup";
445 std::string type = OCGetResourceTypeName(collectionResHandle, 0);
446 std::string resourceType;
447 resourceType.append(OCGetResourceTypeName(resourceHandle, 0));
449 OCRepresentation rep;
450 rep.setValue("method", method);
451 rep.setValue("collectionResourceType", type);
452 rep.setValue("resourceType", resourceType);
454 cout << "\tmethod - " << method << endl;
455 cout << "\tcollectionResourceType - " << type << endl;
456 cout << "\tresourceType - " << resourceType << endl;
458 QueryParamsMap queryParamsMap;
460 // request to leave group to the remote group sync resource
461 OCStackResult result = resource->put(rep, queryParamsMap,
462 std::bind(&GroupSynchronization::onLeaveGroup, this, std::placeholders::_1,
463 std::placeholders::_2, std::placeholders::_3));
464 if (OC_STACK_OK == result)
466 cout << "GroupSynchronization::leaveGroup : "
467 << "groupSyncResource->put was successful." << endl;
471 cout << "GroupSynchronization::leaveGroup : "
472 << "groupSyncResource->put was unsuccessful. result - " << result << endl;
475 // deleting all remote resources. These are copied in onGetJoinedRemoteChild()
476 deleteGroup(collectionResourceType);
482 void GroupSynchronization::deleteGroup(std::string collectionResourceType)
484 if (0 == collectionResourceType.length())
486 cout << "GroupSynchronization::deleteGroup : Error! Input params are wrong." << endl;
490 cout << "GroupSynchronization::deleteGroup" << endl;
492 OCStackResult result;
493 OCResourceHandle resourceHandle;
495 auto handleIt = groupSyncResourceHandleList.find(collectionResourceType);
497 // if groupSyncResourceHandleList has resourceType,
498 // group sync of this app created collection resource.
499 if (handleIt != groupSyncResourceHandleList.end())
501 resourceHandle = handleIt->second; // group sync resource handle
502 result = OCPlatform::unregisterResource(resourceHandle);
503 if (OC_STACK_OK == result)
505 cout << "GroupSynchronization::deleteGroup : "
506 << "To unregister group sync resource handle was successful." << endl;
510 cout << "GroupSynchronization::deleteGroup : "
511 << "To unregister group sync resource handle was unsuccessful. "
512 << "result - " << result << endl;
515 groupSyncResourceHandleList.erase(handleIt);
518 auto resourceIt = groupSyncResourceList.find(collectionResourceType);
519 if (resourceIt != groupSyncResourceList.end())
521 groupSyncResourceList.erase(resourceIt);
524 handleIt = collectionResourceHandleList.find(collectionResourceType);
525 if (handleIt == collectionResourceHandleList.end())
527 cout << "GroupSynchronization::deleteGroup : "
528 << "Error! There is no collection resource handle to delete." << endl;
531 OCResourceHandle collectionResHandle = handleIt->second;
533 collectionResourceHandleList.erase(handleIt);
535 auto handleListIt = childResourceHandleList.find(collectionResHandle);
536 if (handleListIt == childResourceHandleList.end())
538 cout << "GroupSynchronization::deleteGroup : "
539 << "There is no child resource list to delete." << endl;
541 result = OCPlatform::unregisterResource(collectionResHandle);
542 if (result == OC_STACK_OK)
544 cout << "GroupSynchronization::deleteGroup : "
545 << "To unregister collection resource handle was successful." << endl;
549 cout << "GroupSynchronization::deleteGroup : "
550 << " To unregister collection resource handle was unsuccessful. result - "
558 std::vector< OCResourceHandle > childList = handleListIt->second;
560 childResourceHandleList.erase(handleListIt);
562 result = OCPlatform::unbindResources(collectionResHandle, childList);
563 if (OC_STACK_OK == result)
565 cout << "GroupSynchronization::deleteGroup : " << "To unbind resources was successful."
570 cout << "GroupSynchronization::deleteGroup : "
571 << "To unbind resources was unsuccessful. result - " << result << endl;
574 result = OCPlatform::unregisterResource(collectionResHandle);
575 if (result == OC_STACK_OK)
577 cout << "GroupSynchronization::deleteGroup : "
578 << "To unregister collection resource handle was successful." << endl;
582 cout << "GroupSynchronization::deleteGroup : "
583 << " To unregister collection resource handle was unsuccessful. result - "
587 std::vector< OCResourceHandle >::iterator It;
589 for (unsigned int i = 0; i < childList.size(); i++)
591 resourceHandle = childList.at(i);
593 It = std::find(deviceResourceHandleList.begin(), deviceResourceHandleList.end(),
595 if (It != deviceResourceHandleList.end()) // find !!
597 deviceResourceHandleList.erase(It);
601 result = OCPlatform::unregisterResource(resourceHandle);
602 if (OC_STACK_OK == result)
604 cout << "GroupSynchronization::deleteGroup : UnregisterResource(" << i + 1
605 << ") was successful." << endl;
609 cout << "GroupSynchronization::deleteGroup : UnregisterResource(" << i + 1
610 << ") was unsuccessful. result - " << result << endl;
618 std::map< std::string, OCResourceHandle > GroupSynchronization::getGroupList()
620 return collectionResourceHandleList;
623 OCEntityHandlerResult GroupSynchronization::groupEntityHandler(
624 const std::shared_ptr< OCResourceRequest > request)
626 cout << "GroupSynchronization::groupEntityHandler\n";
630 // Get the request type and request flag
631 std::string requestType = request->getRequestType();
632 int requestFlag = request->getRequestHandlerFlag();
634 if (requestFlag == RequestHandlerFlag::InitFlag)
636 cout << "\trequestFlag : Init\n";
638 // entity handler to perform resource initialization operations
640 else if (requestFlag == RequestHandlerFlag::RequestFlag)
642 cout << "\trequestFlag : Request\n";
644 // If the request type is GET
645 if (requestType == "GET")
647 cout << "\t\trequestType : GET\n";
649 else if (requestType == "PUT")
651 cout << "\t\trequestType : PUT\n";
653 //get method name, group resource type and resource type to join group
654 OCRepresentation rp = request->getResourceRepresentation();
655 std::string methodType = rp.getValue< std::string >("method");
656 std::string collectionResourceType = rp.getValue< std::string >(
657 "collectionResourceType");
658 std::string resourceType = rp.getValue< std::string >("resourceType");
660 cout << "\t\t\tmethod : " << methodType << endl;
661 cout << "\t\t\tcollection resourceType : " << collectionResourceType << endl;
662 cout << "\t\t\tresourceType : " << resourceType << endl;
664 auto handleIt = collectionResourceHandleList.find(collectionResourceType);
665 if (handleIt == collectionResourceHandleList.end())
667 cout << "GroupSynchronization::groupEntityHandler : "
668 << "Error! There is no collection resource handle to delete."
672 collectionResourceHandle = handleIt->second;
673 // in case of join group it is used in onFindResource()
675 if (methodType == "joinGroup")
677 std::string resourceName = "coap://224.0.1.187/oc/core?rt=";
678 resourceName += resourceType;
679 cout << "\t\t\tresourceName : " << resourceName << endl;
681 resourceRequest = request;
683 OCPlatform::findResource("", resourceName,
684 std::bind(&GroupSynchronization::onFindResource, this,
685 std::placeholders::_1));
687 else if (methodType == "leaveGroup")
689 auto it = childResourceHandleList.find(collectionResourceHandle);
690 if (it == childResourceHandleList.end())
692 cout << "GroupSynchronization::groupEntityHandler : "
693 << "Error! There is no child resource list." << endl;
697 std::vector< OCResourceHandle > childList = it->second;
698 OCResourceHandle resourceHandle;
699 for (auto childIt = childList.begin(); childIt != childList.end();)
701 resourceHandle = (*childIt);
702 char* type = (char*) OCGetResourceTypeName(resourceHandle, 0);
704 if (0 == resourceType.compare(type))
706 cout << "GroupSynchronization::groupEntityHandler : "
707 << "Found! The resource to leave is found. - " << type
710 childIt = childList.erase(childIt++);
712 OCStackResult result = OCPlatform::unbindResource(
713 collectionResourceHandle, resourceHandle);
714 if (OC_STACK_OK == result)
716 cout << "GroupSynchronization::groupEntityHandler : "
717 << "To unbind resource was successful." << endl;
721 cout << "GroupSynchronization::groupEntityHandler : "
722 << "To unbind resource was unsuccessful. result - "
726 result = OCPlatform::unregisterResource(resourceHandle);
727 if (OC_STACK_OK == result)
729 cout << "GroupSynchronization::groupEntityHandler : "
730 << "To unregister resource was successful." << endl;
734 cout << "GroupSynchronization::groupEntityHandler : "
735 << "To unregister resource was unsuccessful. result - "
748 childResourceHandleList[collectionResourceHandle] = childList;
752 auto pResponse = std::make_shared< OC::OCResourceResponse >();
753 pResponse->setRequestHandle(request->getRequestHandle());
754 pResponse->setResourceHandle(request->getResourceHandle());
755 pResponse->setErrorCode(200);
756 pResponse->setResponseResult(OC_EH_OK);
758 OCRepresentation rep = request->getResourceRepresentation();
759 pResponse->setResourceRepresentation(rep, DEFAULT_INTERFACE);
760 if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
762 cout << "GroupSynchronization::groupEntityHandler : "
763 << "sendResponse is successful." << endl;
767 if (methodType != "") //TODO: Check groupmethodtype NULL
771 else if (requestType == "POST")
773 // POST request operations
775 else if (requestType == "DELETE")
777 // DELETE request operations
780 else if (requestFlag == RequestHandlerFlag::ObserverFlag)
782 cout << "\trequestFlag : Observer\n";
787 std::cout << "Request invalid" << std::endl;
793 void GroupSynchronization::onFindGroup(std::shared_ptr< OCResource > resource)
795 cout << "GroupSynchronization::onFindGroup" << endl;
802 std::string resourceURI;
803 std::string hostAddress;
805 // Get the resource URI
806 resourceURI = resource->uri();
807 cout << "\tURI of the resource: " << resourceURI << endl;
809 // Get the resource host address
810 hostAddress = resource->host();
811 cout << "\tHost address of the resource: " << hostAddress << endl;
813 hostAddress.append(resourceURI);
815 // Get the resource types
816 cout << "\tList of resource types: " << endl;
818 for (auto &resourceTypes : resource->getResourceTypes())
820 cout << "\t\t" << resourceTypes << endl;
823 // Get the resource interfaces
824 cout << "\tList of resource interfaces: " << endl;
825 for (auto &resourceInterfaces : resource->getResourceInterfaces())
827 cout << "\t\t" << resourceInterfaces << endl;
830 if (false == IsSameGroup(resource))
833 findCallback(resource);
838 // Resource is invalid
839 cout << "Resource is invalid" << endl;
845 catch (std::exception& e)
851 void GroupSynchronization::checkFindGroup(void)
853 cout << "GroupSynchronization::checkFindGroup" << endl;
855 for (int i = 0; i < 15; i++)
857 std::chrono::milliseconds workTime(300);
858 std::this_thread::sleep_for(workTime);
860 std::lock_guard < std::mutex > guard(foundGroupMutex);
862 if (false == foundGroupResourceList.empty())
864 cout << "GroupSynchronization::checkFoundGroup : " << "Some group is received."
870 cout << "GroupSynchronization::checkFoundGroup : "
871 << "It is failed to find resource within 3s." << endl;
877 bool GroupSynchronization::IsSameGroup(std::shared_ptr< OCResource > resource)
879 std::lock_guard < std::mutex > guard(foundGroupMutex);
881 if (true == foundGroupResourceList.empty())
883 cout << "GroupSynchronization::IsSameGroup : There is no found group." << endl;
887 std::string foundHostAddress, savedHostAddress;
888 foundHostAddress = resource->host();
889 // foundHostAddress.append (resource->uri());
891 for (unsigned int i = 0; i < foundGroupResourceList.size(); ++i)
893 savedHostAddress = (foundGroupResourceList.at(i))->host();
894 // savedHostAddress.append ((foundGroupResourceList.at(i))->uri());
895 // cout << "GroupSynchronization::IsSameGroup : foundHostAddress - " << foundHostAddress
896 // << ", savedHostAddress - " << savedHostAddress << endl;
898 if (0 == foundHostAddress.compare(savedHostAddress.c_str()))
900 cout << "GroupSynchronization::IsSameGroup : Found! The same group is found."
906 cout << "GroupSynchronization::IsSameGroup : There is no same group." << endl;
910 void GroupSynchronization::saveGroup(std::shared_ptr< OCResource > resource)
912 cout << "GroupSynchronization::saveGroup" << endl;
914 std::lock_guard < std::mutex > guard(foundGroupMutex);
916 foundGroupResourceList.push_back(resource);
919 void GroupSynchronization::onJoinGroup(const HeaderOptions& headerOptions,
920 const OCRepresentation& rep, const int eCode)
922 if (eCode != OC_STACK_OK)
924 cout << "GroupSynchronization::onJoinGroup : error - " << eCode << endl;
928 cout << "GroupSynchronization::onJoinGroup : " << endl;
930 if (remoteCollectionResource)
932 std::string resourceInterface = DEFAULT_INTERFACE;
933 QueryParamsMap queryParamsMap;
935 OCStackResult result = remoteCollectionResource->get("", resourceInterface,
937 std::bind(&GroupSynchronization::onGetJoinedRemoteChild, this,
938 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
939 if (OC_STACK_OK == result)
941 cout << "GroupSynchronization::onJoinGroup : "
942 << "remoteCollectionResource->get was successful." << endl;
946 cout << "GroupSynchronization::onJoinGroup : "
947 << "remoteCollectionResource->get was unsuccessful. result - " << result
953 void GroupSynchronization::onFindResource(std::shared_ptr< OCResource > resource)
955 cout << "GroupSynchronization::onFindResource" << endl;
959 // start of debugging
960 std::string resourceURI;
961 std::string hostAddress;
963 // Get the resource URI
964 resourceURI = resource->uri();
965 cout << "\tURI of the resource: " << resourceURI << endl;
967 // Get the resource host address
968 hostAddress = resource->host();
969 cout << "\tHost address of the resource: " << hostAddress << endl;
971 hostAddress.append(resourceURI);
973 // Get the resource types
974 cout << "\tList of resource types: " << endl;
976 for (auto &resourceTypes : resource->getResourceTypes())
978 cout << "\t\t" << resourceTypes << endl;
981 // Get the resource interfaces
982 cout << "\tList of resource interfaces: " << endl;
983 for (auto &resourceInterfaces : resource->getResourceInterfaces())
985 cout << "\t\t" << resourceInterfaces << endl;
989 OCResourceHandle resourceHandle;
990 OCStackResult result = OCPlatform::registerResource(resourceHandle, resource);
991 if (result != OC_STACK_OK)
993 cout << "GroupSynchronization::"
994 << "onFindResource - Resource to join creation was unsuccessful. result - "
998 // cout << "GroupSynchronization::onFindResource : creating resourceHandle. resource type - "
999 // << OCGetResourceTypeName(resourceHandle, 0) << endl;
1001 result = OCPlatform::bindResource(collectionResourceHandle, resourceHandle);
1002 if (result != OC_STACK_OK)
1004 cout << "GroupSynchronization::onFindResource : "
1005 << "To bind resource was unsuccessful. result - " << result << endl;
1009 cout << "GroupSynchronization::onFindResource : "
1010 << "To bind joinGroupHandle and resourceHandle was successful." << endl;
1012 auto it = childResourceHandleList.find(collectionResourceHandle);
1013 std::vector< OCResourceHandle > childHandleList;
1014 if (it != childResourceHandleList.end())
1016 childHandleList = it->second;
1019 childHandleList.push_back(resourceHandle);
1020 childResourceHandleList[collectionResourceHandle] = childHandleList;
1022 auto pResponse = std::make_shared< OC::OCResourceResponse >();
1023 pResponse->setRequestHandle(resourceRequest->getRequestHandle());
1024 pResponse->setResourceHandle(resourceRequest->getResourceHandle());
1025 pResponse->setErrorCode(200);
1026 pResponse->setResponseResult(OC_EH_OK);
1028 OCRepresentation rep = resourceRequest->getResourceRepresentation();
1029 pResponse->setResourceRepresentation(rep, DEFAULT_INTERFACE);
1030 if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
1032 cout << "GroupSynchronization::onFindResource : sendResponse is successful."
1038 cout << "GroupSynchronization::onFindResource : "
1039 << "Resource is invalid. So a new Group Resource has to be created." << endl;
1045 void GroupSynchronization::onGetJoinedRemoteChild(const HeaderOptions& headerOptions,
1046 const OCRepresentation& rep, const int eCode)
1048 if (eCode != OC_STACK_OK)
1050 cout << "GroupSynchronization::onGetJoinedRemoteChild : error - " << eCode << endl;
1054 cout << "GroupSynchronization::onGetJoinedRemoteChild" << endl;
1057 std::string resourceURI;
1059 // Get the resource URI
1060 resourceURI = rep.getUri();
1061 cout << "\tURI of the resource: " << resourceURI << endl;
1063 // Get the resource types
1064 cout << "\tList of resource types: " << endl;
1066 for (auto &resourceTypes : rep.getResourceTypes())
1068 cout << "\t\t" << resourceTypes << endl;
1071 // Get the resource interfaces
1072 cout << "\tList of resource interfaces: " << endl;
1073 for (auto &resourceInterfaces : rep.getResourceInterfaces())
1075 cout << "\t\t" << resourceInterfaces << endl;
1078 std::vector< OCRepresentation > childList = rep.getChildren();
1079 OCRepresentation child;
1080 for (unsigned int i = 0; i < childList.size(); ++i)
1082 cout << "\n\tchild resource - " << i + 1 << endl;
1084 child = childList.at(i);
1085 resourceURI = child.getUri();
1086 cout << "\t\tURI of the resource: " << resourceURI << endl;
1088 cout << "\t\tList of resource types: " << endl;
1089 for (auto &types : child.getResourceTypes())
1091 cout << "\t\t\t" << types << endl;
1094 cout << "\tList of resource interfaces: " << endl;
1095 for (auto &interfaces : child.getResourceInterfaces())
1097 cout << "\t\t\t" << interfaces << endl;
1101 // creating remote collection resource handle
1102 OCResourceHandle remoteCollectionResourceHandle;
1103 resourceURI = remoteCollectionResource->uri();
1104 std::vector< std::string > types = remoteCollectionResource->getResourceTypes();
1105 std::vector< std::string > interfaces = remoteCollectionResource->getResourceInterfaces();
1107 OCStackResult result = OCPlatform::registerResource(remoteCollectionResourceHandle,
1108 resourceURI, types[0], interfaces[0], NULL, OC_OBSERVABLE);
1109 if (result != OC_STACK_OK)
1111 cout << "GroupSynchronization::onGetJoinedRemoteChild - "
1112 << "To register remoteCollectionResourceHandle"
1113 << " was unsuccessful. result - " << result << endl;
1116 cout << "GroupSynchronization::onGetJoinedRemoteChild : "
1117 "To register remoteCollectionResourceHandle was successful." << endl;
1119 // binding remote collection resource handle and resource handle to join
1120 collectionResourceHandleList[types[0]] = remoteCollectionResourceHandle;
1122 result = OCPlatform::bindResource(remoteCollectionResourceHandle, deviceResourceHandle);
1123 if (OC_STACK_OK == result)
1125 cout << "GroupSynchronization::onGetJoinedRemoteChild : "
1126 << "binding remoteCollectionResourceHandle and deviceResourceHandle" << endl;
1130 cout << "GroupSynchronization::onGetJoinedRemoteChild - "
1131 << "To bind remoteCollectionResourceHandle and deviceResourceHandle "
1132 << "was unsuccessful. result - " << result << endl;
1135 std::vector< OCResourceHandle > childHandleList;
1136 childHandleList.push_back(deviceResourceHandle);
1137 deviceResourceHandleList.push_back(deviceResourceHandle);
1139 // binding copied remote collection resource handle and copied remote resource
1140 OCResourceHandle resourceHandle;
1141 for (unsigned int i = 0; i < childList.size(); ++i)
1143 cout << "\tremote resource - " << i + 1 << endl;
1145 child = childList.at(i);
1146 resourceURI = child.getUri();
1147 types = child.getResourceTypes();
1148 interfaces = child.getResourceInterfaces();
1150 if (0 == types[0].compare(OCGetResourceTypeName(deviceResourceHandle, 0)))
1152 cout << "GroupSynchronization::onGetJoinedRemoteChild : " << types[0]
1153 << " is bind already." << endl;
1157 result = OCPlatform::registerResource(resourceHandle, resourceURI, types[0],
1158 interfaces[0], NULL, OC_OBSERVABLE);
1159 if (OC_STACK_OK == result)
1161 result = OCPlatform::bindResource(remoteCollectionResourceHandle, resourceHandle);
1162 if (result != OC_STACK_OK)
1164 cout << "GroupSynchronization::onGetJoinedRemoteChild - "
1165 << "binding remoteCollectionResourceHandle and resourceHandle "
1166 << "was unsuccessful. result - " << result << endl;
1167 OCPlatform::unregisterResource(resourceHandle);
1170 childHandleList.push_back(resourceHandle);
1171 cout << "GroupSynchronization::onGetJoinedRemoteChild : "
1172 << "binding remoteCollectionResourceHandle and resourceHandle" << endl;
1176 cout << "GroupSynchronization::onGetJoinedRemoteChild - "
1177 << "To register remoteCollectionResourceHandle was unsuccessful."
1178 << " result - " << result << endl;
1182 childResourceHandleList[remoteCollectionResourceHandle] = childHandleList;
1183 // this handle list is used to leave group
1188 void GroupSynchronization::onLeaveGroup(const HeaderOptions& headerOptions,
1189 const OCRepresentation& rep, const int eCode)
1191 if (eCode != OC_STACK_OK)
1193 cout << "GroupSynchronization::onLeaveGroup : error - " << eCode << endl;
1197 cout << "GroupSynchronization::onLeaveGroup" << endl;
1201 void GroupSynchronization::debugGroupSync(void)
1203 cout << "GroupSynchronization::debugGroupSync" << endl;
1206 std::map< std::string, OCResourceHandle >::iterator handleIt;
1207 std::map< OCResourceHandle, std::vector< OCResourceHandle > >::iterator childIt;
1209 OCResourceHandle resourceHandle;
1210 std::vector< OCResourceHandle > handleList;
1211 std::shared_ptr< OCResource > resource;
1213 cout << "Resource Handle Created by App" << endl;
1214 for (i = 0; i < deviceResourceHandleList.size(); i++)
1216 resourceHandle = deviceResourceHandleList.at(i);
1218 cout << i + 1 << ". details" << endl;
1219 cout << " uri - " << OCGetResourceUri(resourceHandle) << endl;
1220 cout << " resource type - " << OCGetResourceTypeName(resourceHandle, 0) << endl;
1221 cout << " resource interface - " << OCGetResourceInterfaceName(resourceHandle, 0)
1225 cout << "\nGroup Sync Resource Handle List. The number is "
1226 << groupSyncResourceHandleList.size() << endl;
1228 for (handleIt = groupSyncResourceHandleList.begin();
1229 handleIt != groupSyncResourceHandleList.end(); ++handleIt)
1231 type = handleIt->first;
1232 cout << "\t" << i << ". group sync resource type - " << type << endl;
1233 cout << "\t details" << endl;
1235 resourceHandle = handleIt->second;
1236 cout << "\t uri - " << OCGetResourceUri(resourceHandle) << endl;
1237 cout << "\t resource type - " << OCGetResourceTypeName(resourceHandle, 0) << endl;
1238 cout << "\t resource interface - " << OCGetResourceInterfaceName(resourceHandle, 0)
1244 cout << "Copied Remote Group Sync Resource List. The number is "
1245 << groupSyncResourceList.size() << endl;
1246 std::vector< std::string > list;
1248 for (auto resourceIt = groupSyncResourceList.begin();
1249 resourceIt != groupSyncResourceList.end(); ++resourceIt)
1251 type = resourceIt->first;
1252 cout << "\t" << i << ". group sync resource type - " << type << endl;
1253 cout << "\t details" << endl;
1255 resource = resourceIt->second;
1256 cout << "\t host - " << resource->host() << endl;
1257 cout << "\t uri - " << resource->uri() << endl;
1258 list = resource->getResourceTypes();
1259 cout << "\t resource type - " << list[0] << endl;
1260 list = resource->getResourceInterfaces();
1261 cout << "\t resource interface - " << list[0] << endl << endl;
1265 // cout << "The number of collection Resource Handle is " << collectionResourceHandleList.size()
1267 // cout << "The number of child resource handle list is " << childResourceHandleList.size()
1270 cout << "Collection Resource Handle List" << endl;
1272 for (handleIt = collectionResourceHandleList.begin();
1273 handleIt != collectionResourceHandleList.end(); ++handleIt)
1275 type = handleIt->first;
1276 cout << "\t" << i << ". collection resource type - " << type << endl;
1277 cout << "\t details" << endl;
1279 resourceHandle = handleIt->second;
1280 cout << "\t uri - " << OCGetResourceUri(resourceHandle) << endl;
1281 cout << "\t resource type - " << OCGetResourceTypeName(resourceHandle, 0) << endl;
1282 cout << "\t resource interface - " << OCGetResourceInterfaceName(resourceHandle, 0)
1285 childIt = childResourceHandleList.find(resourceHandle);
1286 if (childIt != childResourceHandleList.end())
1288 handleList = childIt->second;
1289 for (unsigned int j = 0; j < handleList.size(); j++)
1292 cout << "\t\t" << j + 1 << ". child resource details" << endl;
1294 resourceHandle = handleList.at(j);
1295 cout << "\t\t uri - " << OCGetResourceUri(resourceHandle) << endl;
1296 cout << "\t\t resource type - " << OCGetResourceTypeName(resourceHandle, 0)
1298 cout << "\t\t resource interface - "
1299 << OCGetResourceInterfaceName(resourceHandle, 0) << endl << endl;