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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
26 #include "GroupSynchronization.h"
33 GroupSynchronization* GroupSynchronization::groupSyncnstance = NULL;
34 bool GroupSynchronization::bIsFinding = false;
36 GroupSynchronization* GroupSynchronization::getInstance()
38 if (groupSyncnstance == NULL)
40 groupSyncnstance = new GroupSynchronization();
42 return groupSyncnstance;
45 void GroupSynchronization::deleteInstance()
49 delete groupSyncnstance;
50 groupSyncnstance = NULL;
54 OCStackResult GroupSynchronization::findGroup(
55 std::vector< std::string > collectionResourceTypes, FindCallback callback)
57 cout << "GroupSynchronization::findGroup" << endl;
60 return OC_STACK_ERROR;
62 foundGroupResourceList.clear();
63 findCallback = callback;
65 if (findCallback == NULL)
67 return OC_STACK_ERROR;
70 for (unsigned int i = 0; i < collectionResourceTypes.size(); ++i)
72 std::string query = "coap://224.0.1.187/oc/core?rt=";
73 query.append(collectionResourceTypes.at(i));
74 cout << "GroupSynchronization::findGroup - " << query << endl;
76 OCPlatform::findResource("", query,
77 std::bind(&GroupSynchronization::onFindGroup, this, std::placeholders::_1));
82 // thread to check if GroupSynchronization::onFoundGroup is called or not.
83 std::thread t(std::bind(&GroupSynchronization::checkFindGroup, this));
89 OCStackResult GroupSynchronization::createGroup(std::string collectionResourceType)
91 foundGroupResourceList.clear();
93 OCResourceHandle collectionResHandle = NULL;
94 OCResourceHandle groupSyncResHandle = NULL;
96 if (0 == collectionResourceType.length())
98 cout << "GroupSynchronization::createGroup : Error! Input params are wrong." << endl;
99 return OC_STACK_INVALID_PARAM;
102 cout << "GroupSynchronization::createGroup - The created group is added." << endl;
104 OCStackResult result;
106 // creating master collection resource
107 std::string collectionUri = "/" + collectionResourceType;
109 while ((i = collectionUri.find(".")) != std::string::npos)
111 collectionUri.replace(i, 1, "/");
113 cout << "GroupSynchronization::createGroup : collection uri - " << collectionUri
114 << ", type - " << collectionResourceType << endl;
116 std::string resourceInterface = DEFAULT_INTERFACE;
118 result = OCPlatform::registerResource(collectionResHandle, collectionUri,
119 collectionResourceType, resourceInterface, NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
120 if (result != OC_STACK_OK)
122 cout << "To register resource (" << collectionUri << ") was unsuccessful. result - "
128 OCPlatform::bindInterfaceToResource(collectionResHandle, GROUP_INTERFACE);
129 if (result != OC_STACK_OK)
131 cout << "To bind Interface (collection) was unsuccessful. result - " << result
135 collectionResourceHandleList[collectionResourceType] = collectionResHandle;
137 // creating master group sync resource
138 std::string groupSyncUri = collectionUri + "/groupsync";
139 std::string groupSyncResType = collectionResourceType + ".groupsync";
141 // cout << "GroupSynchronization::createGroup : groupSync uri - " << groupSyncUri
142 // << ", type - " << collectionResourceType << endl;
144 result = OCPlatform::registerResource(groupSyncResHandle, groupSyncUri,
145 groupSyncResType, resourceInterface,
146 std::bind(&GroupSynchronization::groupEntityHandler, this,
147 std::placeholders::_1), OC_DISCOVERABLE | OC_OBSERVABLE);
148 if (result != OC_STACK_OK)
150 cout << "To register resource (groupsync) was unsuccessful. result - " << result
155 groupSyncResourceHandleList[collectionResourceType] = groupSyncResHandle;
161 if (collectionResHandle)
163 OCPlatform::unregisterResource(collectionResHandle);
164 auto iterator = collectionResourceHandleList.find(collectionResourceType);
165 if (iterator != collectionResourceHandleList.end())
167 collectionResourceHandleList.erase(iterator);
171 if (groupSyncResHandle)
173 OCPlatform::unregisterResource(groupSyncResHandle);
174 auto iterator = groupSyncResourceHandleList.find(collectionResourceType);
175 if (iterator != groupSyncResourceHandleList.end())
177 groupSyncResourceHandleList.erase(iterator);
181 return OC_STACK_NO_RESOURCE;
184 OCStackResult GroupSynchronization::joinGroup(std::string collectionResourceType,
185 OCResourceHandle resourceHandle)
187 if ((0 == collectionResourceType.length()) || (!resourceHandle))
189 cout << "GroupSynchronization::joinGroup : Error! input params are wrong." << endl;
190 return OC_STACK_INVALID_PARAM;
192 auto resIt = collectionResourceHandleList.find(collectionResourceType);
193 if (resIt == collectionResourceHandleList.end())
195 cout << "GroupSynchronization::joinGroup : error! There is no collection to join"
197 return OC_STACK_INVALID_PARAM;
200 OCResourceHandle collectionResHandle = resIt->second;
203 OCStackResult result = OCPlatform::bindResource(collectionResHandle, resourceHandle);
204 if (result != OC_STACK_OK)
206 cout << "GroupSynchronization::joinGroup : To bind resource was unsuccessful."
207 << "result - " << result << endl;
208 return OC_STACK_ERROR;
210 } catch(OCException &e) {
212 return OC_STACK_INVALID_PARAM;
215 cout << "GroupSynchronization::joinGroup : "
216 << "To bind collectionResHandle and resourceHandle" << endl;
218 std::vector< OCResourceHandle > childHandleList;
220 auto childIt = childResourceHandleList.find(collectionResHandle);
221 if (childIt != childResourceHandleList.end())
223 childHandleList = childIt->second;
226 childHandleList.push_back(resourceHandle);
227 childResourceHandleList[collectionResHandle] = childHandleList;
229 deviceResourceHandleList.push_back(resourceHandle);
236 OCStackResult GroupSynchronization::joinGroup(const std::shared_ptr< OCResource > resource,
237 OCResourceHandle resourceHandle)
239 if ((!resource) || (!resourceHandle))
241 cout << "GroupSynchronization::joinGroup : Error! Input params are wrong." << endl;
242 return OC_STACK_INVALID_PARAM;
245 cout << "GroupSynchronization::joinGroup" << endl;
247 // making representation to join group
248 std::string method = "joinGroup";
249 std::vector< std::string > type = resource->getResourceTypes();
250 std::string resourceType;
251 resourceType.append(OCGetResourceTypeName(resourceHandle, 0));
253 OCRepresentation rep;
254 rep.setValue("method", method);
255 rep.setValue("collectionResourceType", type[0]);
256 rep.setValue("resourceType", resourceType);
258 cout << "\tmethod - " << method << endl;
259 cout << "\tcollectionResourceType - " << type[0] << endl;
260 cout << "\tresourceType - " << resourceType << endl;
262 // creating group sync resource with the received collection resource.
263 // entity handler of group sync is used to join group.
264 std::string host = resource->host();
265 std::string uri = resource->uri() + "/groupsync";
267 std::vector< std::string > resourceTypes;
269 for (unsigned int i = 0; i < type.size(); ++i)
271 temp = type[0] + ".groupsync";
272 resourceTypes.push_back(temp);
275 std::vector< std::string > resourceInterface;
276 resourceInterface.push_back(DEFAULT_INTERFACE);
280 OCResource::Ptr groupSyncResource = OCPlatform::constructResourceObject(host, uri,
281 OC_ETHERNET | OC_WIFI, 1, resourceTypes, resourceInterface);
283 OCResource::Ptr groupSyncResource = OCPlatform::constructResourceObject(host, uri, 1,
284 resourceTypes, resourceInterface);
286 groupSyncResourceList[type[0]] = groupSyncResource;
288 cout << "GroupSynchronization::joinGroup : creating groupSyncResource." << endl;
290 // Create QueryParameters Map and add query params (if any)
291 QueryParamsMap queryParamsMap;
293 // request to join group to the remote group sync resource
294 OCStackResult result = groupSyncResource->put(rep, queryParamsMap,
295 std::bind(&GroupSynchronization::onJoinGroup, this, std::placeholders::_1,
296 std::placeholders::_2, std::placeholders::_3));
297 if (OC_STACK_OK == result)
299 cout << "GroupSynchronization::joinGroup : groupSyncResource->put was successful."
304 cout << "GroupSynchronization::joinGroup : "
305 << "groupSyncResource->put was unsuccessful. result - " << result << endl;
308 // saving the remote collection resource.
309 // It is used in onJoinGroup() and onGetJoinedRemoteChild().
310 remoteCollectionResource = resource;
312 // saving the resource handle to join. It is used in onGetJoinedRemoteChild()
313 deviceResourceHandle = resourceHandle;
318 OCStackResult GroupSynchronization::leaveGroup(std::string collectionResourceType,
319 OCResourceHandle resourceHandle)
321 if ((0 == collectionResourceType.length()) || (!resourceHandle))
323 cout << "GroupSynchronization::leaveGroup : Error! Input params are wrong." << endl;
324 return OC_STACK_INVALID_PARAM;
327 cout << "GroupSynchronization::leaveGroup : collectionResourceType - "
328 << collectionResourceType << endl;
330 OCResourceHandle collectionResHandle;
331 auto handleIt = groupSyncResourceHandleList.find(collectionResourceType);
333 // if groupSyncResourceHandleList has resourceType,
334 // this app created collection resource handle.
335 if (handleIt != groupSyncResourceHandleList.end())
337 handleIt = collectionResourceHandleList.find(collectionResourceType);
338 if (handleIt == collectionResourceHandleList.end())
340 cout << "GroupSynchronization::leaveGroup : "
341 << "Error! There is no collection resource handle to leave." << endl;
342 return OC_STACK_INVALID_PARAM;
345 collectionResHandle = handleIt->second;
346 // cout << "GroupSynchronization::leaveGroup : collection handle uri - "
347 // << OCGetResourceUri(collectionResHandle) << endl;
348 if(collectionResHandle == NULL)
349 return OC_STACK_INVALID_PARAM;
351 OCStackResult result;
354 result = OCPlatform::unbindResource(collectionResHandle, resourceHandle);
355 if (OC_STACK_OK == result)
357 cout << "GroupSynchronization::leaveGroup : "
358 << "To unbind resource was successful." << endl;
362 cout << "GroupSynchronization::leaveGroup : "
363 << "To unbind resource was unsuccessful. result - " << result << endl;
366 } catch(OCException &e) {
367 cout << "ERROR : " << e.reason() << endl;
368 return OC_STACK_NO_RESOURCE;
371 auto It = std::find(deviceResourceHandleList.begin(), deviceResourceHandleList.end(),
373 if (It == deviceResourceHandleList.end()) // there is no resource handle to find
377 result = OCPlatform::unregisterResource(resourceHandle);
378 if (OC_STACK_OK == result)
380 cout << "GroupSynchronization::leaveGroup : "
381 << "To unregister resource was successful." << endl;
385 cout << "GroupSynchronization::leaveGroup : "
386 << "To unregister resource was unsuccessful. result - " << result
390 } catch(OCException &e)
392 cout << "ERROR : " << e.reason() << endl;
393 return OC_STACK_NO_RESOURCE;
398 cout << "GroupSynchronization::leaveGroup : "
399 << "This resource cannot be unregistered." << endl;
400 deviceResourceHandleList.erase(It);
403 auto handleListIt = childResourceHandleList.find(collectionResHandle);
404 if (handleListIt == childResourceHandleList.end())
406 cout << "GroupSynchronization::leaveGroup : "
407 << "Error! There is no child resource list to delete." << endl;
408 return OC_STACK_INVALID_PARAM;
411 std::vector< OCResourceHandle > childList = handleListIt->second;
412 auto childIt = std::find(childList.begin(), childList.end(), resourceHandle);
413 if (childIt != childList.end())
415 cout << "GroupSynchronization::groupEntityHandler : "
416 << "Found! The resource to leave is found." << endl;
417 childList.erase(childIt);
420 childResourceHandleList[collectionResHandle] = childList;
424 else // requesting to unbind this resourceHandle to the remote collection resource
426 auto resourceIt = groupSyncResourceList.find(collectionResourceType);
428 if (resourceIt == groupSyncResourceList.end())
430 cout << "GroupSynchronization::leaveGroup : "
431 << "Error! There is no collectin resource type to leave." << endl;
432 return OC_STACK_INVALID_PARAM;
435 std::shared_ptr< OCResource > resource = resourceIt->second;
437 return OC_STACK_NO_RESOURCE;
438 // cout << "GroupSynchronization::leaveGroup : group sync resource uri - "
439 // << resource->uri() << endl;
441 handleIt = collectionResourceHandleList.find(collectionResourceType);
442 if (handleIt == collectionResourceHandleList.end())
444 cout << "GroupSynchronization::leaveGroup : "
445 << "Error! There is no collection resource handle to leave." << endl;
446 return OC_STACK_INVALID_PARAM;
449 collectionResHandle = handleIt->second;
451 // making representation to leave group
452 std::string method = "leaveGroup";
453 std::string type = OCGetResourceTypeName(collectionResHandle, 0);
454 std::string resourceType;
455 resourceType.append(OCGetResourceTypeName(resourceHandle, 0));
457 OCRepresentation rep;
458 rep.setValue("method", method);
459 rep.setValue("collectionResourceType", type);
460 rep.setValue("resourceType", resourceType);
462 cout << "\tmethod - " << method << endl;
463 cout << "\tcollectionResourceType - " << type << endl;
464 cout << "\tresourceType - " << resourceType << endl;
466 QueryParamsMap queryParamsMap;
468 // request to leave group to the remote group sync resource
469 OCStackResult result = resource->put(rep, queryParamsMap,
470 std::bind(&GroupSynchronization::onLeaveGroup, this, std::placeholders::_1,
471 std::placeholders::_2, std::placeholders::_3));
472 if (OC_STACK_OK == result)
474 cout << "GroupSynchronization::leaveGroup : "
475 << "groupSyncResource->put was successful." << endl;
479 cout << "GroupSynchronization::leaveGroup : "
480 << "groupSyncResource->put was unsuccessful. result - " << result << endl;
483 // deleting all remote resources. These are copied in onGetJoinedRemoteChild()
484 deleteGroup(collectionResourceType);
490 void GroupSynchronization::deleteGroup(std::string collectionResourceType)
492 if (0 == collectionResourceType.length())
494 cout << "GroupSynchronization::deleteGroup : Error! Input params are wrong." << endl;
498 cout << "GroupSynchronization::deleteGroup" << endl;
500 OCStackResult result;
501 OCResourceHandle resourceHandle;
503 auto handleIt = groupSyncResourceHandleList.find(collectionResourceType);
505 // if groupSyncResourceHandleList has resourceType,
506 // group sync of this app created collection resource.
507 if (handleIt != groupSyncResourceHandleList.end())
509 resourceHandle = handleIt->second; // group sync resource handle
510 result = OCPlatform::unregisterResource(resourceHandle);
511 if (OC_STACK_OK == result)
513 cout << "GroupSynchronization::deleteGroup : "
514 << "To unregister group sync resource handle was successful." << endl;
518 cout << "GroupSynchronization::deleteGroup : "
519 << "To unregister group sync resource handle was unsuccessful. "
520 << "result - " << result << endl;
523 groupSyncResourceHandleList.erase(handleIt);
526 auto resourceIt = groupSyncResourceList.find(collectionResourceType);
527 if (resourceIt != groupSyncResourceList.end())
529 groupSyncResourceList.erase(resourceIt);
532 handleIt = collectionResourceHandleList.find(collectionResourceType);
533 if (handleIt == collectionResourceHandleList.end())
535 cout << "GroupSynchronization::deleteGroup : "
536 << "Error! There is no collection resource handle to delete." << endl;
539 OCResourceHandle collectionResHandle = handleIt->second;
541 collectionResourceHandleList.erase(handleIt);
543 auto handleListIt = childResourceHandleList.find(collectionResHandle);
544 if (handleListIt == childResourceHandleList.end())
546 cout << "GroupSynchronization::deleteGroup : "
547 << "There is no child resource list to delete." << endl;
549 result = OCPlatform::unregisterResource(collectionResHandle);
550 if (result == OC_STACK_OK)
552 cout << "GroupSynchronization::deleteGroup : "
553 << "To unregister collection resource handle was successful." << endl;
557 cout << "GroupSynchronization::deleteGroup : "
558 << " To unregister collection resource handle was unsuccessful. result - "
566 std::vector< OCResourceHandle > childList = handleListIt->second;
568 childResourceHandleList.erase(handleListIt);
570 result = OCPlatform::unbindResources(collectionResHandle, childList);
571 if (OC_STACK_OK == result)
573 cout << "GroupSynchronization::deleteGroup : " << "To unbind resources was successful."
578 cout << "GroupSynchronization::deleteGroup : "
579 << "To unbind resources was unsuccessful. result - " << result << endl;
582 result = OCPlatform::unregisterResource(collectionResHandle);
583 if (result == OC_STACK_OK)
585 cout << "GroupSynchronization::deleteGroup : "
586 << "To unregister collection resource handle was successful." << endl;
590 cout << "GroupSynchronization::deleteGroup : "
591 << " To unregister collection resource handle was unsuccessful. result - "
595 std::vector< OCResourceHandle >::iterator It;
597 for (unsigned int i = 0; i < childList.size(); i++)
599 resourceHandle = childList.at(i);
601 It = std::find(deviceResourceHandleList.begin(), deviceResourceHandleList.end(),
603 if (It != deviceResourceHandleList.end()) // find !!
605 deviceResourceHandleList.erase(It);
609 result = OCPlatform::unregisterResource(resourceHandle);
610 if (OC_STACK_OK == result)
612 cout << "GroupSynchronization::deleteGroup : UnregisterResource(" << i + 1
613 << ") was successful." << endl;
617 cout << "GroupSynchronization::deleteGroup : UnregisterResource(" << i + 1
618 << ") was unsuccessful. result - " << result << endl;
626 std::map< std::string, OCResourceHandle > GroupSynchronization::getGroupList()
628 return collectionResourceHandleList;
631 OCEntityHandlerResult GroupSynchronization::groupEntityHandler(
632 const std::shared_ptr< OCResourceRequest > request)
634 cout << "GroupSynchronization::groupEntityHandler\n";
638 // Get the request type and request flag
639 std::string requestType = request->getRequestType();
640 int requestFlag = request->getRequestHandlerFlag();
642 if (requestFlag == RequestHandlerFlag::InitFlag)
644 cout << "\trequestFlag : Init\n";
646 // entity handler to perform resource initialization operations
648 else if (requestFlag == RequestHandlerFlag::RequestFlag)
650 cout << "\trequestFlag : Request\n";
652 // If the request type is GET
653 if (requestType == "GET")
655 cout << "\t\trequestType : GET\n";
657 else if (requestType == "PUT")
659 cout << "\t\trequestType : PUT\n";
661 //get method name, group resource type and resource type to join group
662 OCRepresentation rp = request->getResourceRepresentation();
663 std::string methodType = rp.getValue< std::string >("method");
664 std::string collectionResourceType = rp.getValue< std::string >(
665 "collectionResourceType");
666 std::string resourceType = rp.getValue< std::string >("resourceType");
668 cout << "\t\t\tmethod : " << methodType << endl;
669 cout << "\t\t\tcollection resourceType : " << collectionResourceType << endl;
670 cout << "\t\t\tresourceType : " << resourceType << endl;
672 auto handleIt = collectionResourceHandleList.find(collectionResourceType);
673 if (handleIt == collectionResourceHandleList.end())
675 cout << "GroupSynchronization::groupEntityHandler : "
676 << "Error! There is no collection resource handle to delete."
680 collectionResourceHandle = handleIt->second;
681 // in case of join group it is used in onFindResource()
683 if (methodType == "joinGroup")
685 std::string resourceName = "coap://224.0.1.187/oc/core?rt=";
686 resourceName += resourceType;
687 cout << "\t\t\tresourceName : " << resourceName << endl;
689 resourceRequest = request;
691 OCPlatform::findResource("", resourceName,
692 std::bind(&GroupSynchronization::onFindResource, this,
693 std::placeholders::_1));
695 else if (methodType == "leaveGroup")
697 auto it = childResourceHandleList.find(collectionResourceHandle);
698 if (it == childResourceHandleList.end())
700 cout << "GroupSynchronization::groupEntityHandler : "
701 << "Error! There is no child resource list." << endl;
705 std::vector< OCResourceHandle > childList = it->second;
706 OCResourceHandle resourceHandle;
707 for (auto childIt = childList.begin(); childIt != childList.end();)
709 resourceHandle = (*childIt);
710 char* type = (char*) OCGetResourceTypeName(resourceHandle, 0);
712 if (0 == resourceType.compare(type))
714 cout << "GroupSynchronization::groupEntityHandler : "
715 << "Found! The resource to leave is found. - " << type
718 childIt = childList.erase(childIt++);
720 OCStackResult result = OCPlatform::unbindResource(
721 collectionResourceHandle, resourceHandle);
722 if (OC_STACK_OK == result)
724 cout << "GroupSynchronization::groupEntityHandler : "
725 << "To unbind resource was successful." << endl;
729 cout << "GroupSynchronization::groupEntityHandler : "
730 << "To unbind resource was unsuccessful. result - "
734 result = OCPlatform::unregisterResource(resourceHandle);
735 if (OC_STACK_OK == result)
737 cout << "GroupSynchronization::groupEntityHandler : "
738 << "To unregister resource was successful." << endl;
742 cout << "GroupSynchronization::groupEntityHandler : "
743 << "To unregister resource was unsuccessful. result - "
756 childResourceHandleList[collectionResourceHandle] = childList;
760 auto pResponse = std::make_shared< OC::OCResourceResponse >();
761 pResponse->setRequestHandle(request->getRequestHandle());
762 pResponse->setResourceHandle(request->getResourceHandle());
763 pResponse->setErrorCode(200);
764 pResponse->setResponseResult(OC_EH_OK);
766 OCRepresentation rep = request->getResourceRepresentation();
767 pResponse->setResourceRepresentation(rep, DEFAULT_INTERFACE);
768 if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
770 cout << "GroupSynchronization::groupEntityHandler : "
771 << "sendResponse is successful." << endl;
775 if (methodType != "") //TODO: Check groupmethodtype NULL
779 else if (requestType == "POST")
781 // POST request operations
783 else if (requestType == "DELETE")
785 // DELETE request operations
788 else if (requestFlag == RequestHandlerFlag::ObserverFlag)
790 cout << "\trequestFlag : Observer\n";
795 std::cout << "Request invalid" << std::endl;
801 void GroupSynchronization::onFindGroup(std::shared_ptr< OCResource > resource)
803 cout << "GroupSynchronization::onFindGroup" << endl;
810 std::string resourceURI;
811 std::string hostAddress;
813 // Get the resource URI
814 resourceURI = resource->uri();
815 cout << "\tURI of the resource: " << resourceURI << endl;
817 // Get the resource host address
818 hostAddress = resource->host();
819 cout << "\tHost address of the resource: " << hostAddress << endl;
821 hostAddress.append(resourceURI);
823 // Get the resource types
824 cout << "\tList of resource types: " << endl;
826 for (auto &resourceTypes : resource->getResourceTypes())
828 cout << "\t\t" << resourceTypes << endl;
831 // Get the resource interfaces
832 cout << "\tList of resource interfaces: " << endl;
833 for (auto &resourceInterfaces : resource->getResourceInterfaces())
835 cout << "\t\t" << resourceInterfaces << endl;
838 if (false == IsSameGroup(resource))
841 findCallback(resource);
846 // Resource is invalid
847 cout << "Resource is invalid" << endl;
853 catch (std::exception& e)
859 void GroupSynchronization::checkFindGroup(void)
861 cout << "GroupSynchronization::checkFindGroup" << endl;
863 for (int i = 0; i < 15; i++)
865 std::chrono::milliseconds workTime(300);
866 std::this_thread::sleep_for(workTime);
868 std::lock_guard < std::mutex > guard(foundGroupMutex);
870 if (false == foundGroupResourceList.empty())
872 cout << "GroupSynchronization::checkFoundGroup : " << "Some group is received."
878 cout << "GroupSynchronization::checkFoundGroup : "
879 << "It is failed to find resource within 3s." << endl;
885 bool GroupSynchronization::IsSameGroup(std::shared_ptr< OCResource > resource)
887 std::lock_guard < std::mutex > guard(foundGroupMutex);
889 if (true == foundGroupResourceList.empty())
891 cout << "GroupSynchronization::IsSameGroup : There is no found group." << endl;
895 std::string foundHostAddress, savedHostAddress;
896 foundHostAddress = resource->host();
897 // foundHostAddress.append (resource->uri());
899 for (unsigned int i = 0; i < foundGroupResourceList.size(); ++i)
901 savedHostAddress = (foundGroupResourceList.at(i))->host();
902 // savedHostAddress.append ((foundGroupResourceList.at(i))->uri());
903 // cout << "GroupSynchronization::IsSameGroup : foundHostAddress - " << foundHostAddress
904 // << ", savedHostAddress - " << savedHostAddress << endl;
906 if (0 == foundHostAddress.compare(savedHostAddress.c_str()))
908 cout << "GroupSynchronization::IsSameGroup : Found! The same group is found."
914 cout << "GroupSynchronization::IsSameGroup : There is no same group." << endl;
918 void GroupSynchronization::saveGroup(std::shared_ptr< OCResource > resource)
920 cout << "GroupSynchronization::saveGroup" << endl;
922 std::lock_guard < std::mutex > guard(foundGroupMutex);
924 foundGroupResourceList.push_back(resource);
927 void GroupSynchronization::onJoinGroup(const HeaderOptions& headerOptions,
928 const OCRepresentation& rep, const int eCode)
930 if (eCode != OC_STACK_OK)
932 cout << "GroupSynchronization::onJoinGroup : error - " << eCode << endl;
936 cout << "GroupSynchronization::onJoinGroup : " << endl;
938 if (remoteCollectionResource)
940 std::string resourceInterface = DEFAULT_INTERFACE;
941 QueryParamsMap queryParamsMap;
943 OCStackResult result = remoteCollectionResource->get("", resourceInterface,
945 std::bind(&GroupSynchronization::onGetJoinedRemoteChild, this,
946 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
947 if (OC_STACK_OK == result)
949 cout << "GroupSynchronization::onJoinGroup : "
950 << "remoteCollectionResource->get was successful." << endl;
954 cout << "GroupSynchronization::onJoinGroup : "
955 << "remoteCollectionResource->get was unsuccessful. result - " << result
961 void GroupSynchronization::onFindResource(std::shared_ptr< OCResource > resource)
963 cout << "GroupSynchronization::onFindResource" << endl;
967 // start of debugging
968 std::string resourceURI;
969 std::string hostAddress;
971 // Get the resource URI
972 resourceURI = resource->uri();
973 cout << "\tURI of the resource: " << resourceURI << endl;
975 // Get the resource host address
976 hostAddress = resource->host();
977 cout << "\tHost address of the resource: " << hostAddress << endl;
979 hostAddress.append(resourceURI);
981 // Get the resource types
982 cout << "\tList of resource types: " << endl;
984 for (auto &resourceTypes : resource->getResourceTypes())
986 cout << "\t\t" << resourceTypes << endl;
989 // Get the resource interfaces
990 cout << "\tList of resource interfaces: " << endl;
991 for (auto &resourceInterfaces : resource->getResourceInterfaces())
993 cout << "\t\t" << resourceInterfaces << endl;
997 OCResourceHandle resourceHandle;
998 OCStackResult result = OCPlatform::registerResource(resourceHandle, resource);
999 if (result != OC_STACK_OK)
1001 cout << "GroupSynchronization::"
1002 << "onFindResource - Resource to join creation was unsuccessful. result - "
1006 // cout << "GroupSynchronization::onFindResource : creating resourceHandle. resource type - "
1007 // << OCGetResourceTypeName(resourceHandle, 0) << endl;
1009 result = OCPlatform::bindResource(collectionResourceHandle, resourceHandle);
1010 if (result != OC_STACK_OK)
1012 cout << "GroupSynchronization::onFindResource : "
1013 << "To bind resource was unsuccessful. result - " << result << endl;
1017 cout << "GroupSynchronization::onFindResource : "
1018 << "To bind joinGroupHandle and resourceHandle was successful." << endl;
1020 auto it = childResourceHandleList.find(collectionResourceHandle);
1021 std::vector< OCResourceHandle > childHandleList;
1022 if (it != childResourceHandleList.end())
1024 childHandleList = it->second;
1027 childHandleList.push_back(resourceHandle);
1028 childResourceHandleList[collectionResourceHandle] = childHandleList;
1030 auto pResponse = std::make_shared< OC::OCResourceResponse >();
1031 pResponse->setRequestHandle(resourceRequest->getRequestHandle());
1032 pResponse->setResourceHandle(resourceRequest->getResourceHandle());
1033 pResponse->setErrorCode(200);
1034 pResponse->setResponseResult(OC_EH_OK);
1036 OCRepresentation rep = resourceRequest->getResourceRepresentation();
1037 pResponse->setResourceRepresentation(rep, DEFAULT_INTERFACE);
1038 if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
1040 cout << "GroupSynchronization::onFindResource : sendResponse is successful."
1046 cout << "GroupSynchronization::onFindResource : "
1047 << "Resource is invalid. So a new Group Resource has to be created." << endl;
1053 void GroupSynchronization::onGetJoinedRemoteChild(const HeaderOptions& headerOptions,
1054 const OCRepresentation& rep, const int eCode)
1056 if (eCode != OC_STACK_OK)
1058 cout << "GroupSynchronization::onGetJoinedRemoteChild : error - " << eCode << endl;
1062 cout << "GroupSynchronization::onGetJoinedRemoteChild" << endl;
1065 std::string resourceURI;
1067 // Get the resource URI
1068 resourceURI = rep.getUri();
1069 cout << "\tURI of the resource: " << resourceURI << endl;
1071 // Get the resource types
1072 cout << "\tList of resource types: " << endl;
1074 for (auto &resourceTypes : rep.getResourceTypes())
1076 cout << "\t\t" << resourceTypes << endl;
1079 // Get the resource interfaces
1080 cout << "\tList of resource interfaces: " << endl;
1081 for (auto &resourceInterfaces : rep.getResourceInterfaces())
1083 cout << "\t\t" << resourceInterfaces << endl;
1086 std::vector< OCRepresentation > childList = rep.getChildren();
1087 OCRepresentation child;
1088 for (unsigned int i = 0; i < childList.size(); ++i)
1090 cout << "\n\tchild resource - " << i + 1 << endl;
1092 child = childList.at(i);
1093 resourceURI = child.getUri();
1094 cout << "\t\tURI of the resource: " << resourceURI << endl;
1096 cout << "\t\tList of resource types: " << endl;
1097 for (auto &types : child.getResourceTypes())
1099 cout << "\t\t\t" << types << endl;
1102 cout << "\tList of resource interfaces: " << endl;
1103 for (auto &interfaces : child.getResourceInterfaces())
1105 cout << "\t\t\t" << interfaces << endl;
1109 // creating remote collection resource handle
1110 OCResourceHandle remoteCollectionResourceHandle;
1111 resourceURI = remoteCollectionResource->uri();
1112 std::vector< std::string > types = remoteCollectionResource->getResourceTypes();
1113 std::vector< std::string > interfaces = remoteCollectionResource->getResourceInterfaces();
1115 OCStackResult result = OCPlatform::registerResource(remoteCollectionResourceHandle,
1116 resourceURI, types[0], interfaces[0], NULL, OC_OBSERVABLE);
1117 if (result != OC_STACK_OK)
1119 cout << "GroupSynchronization::onGetJoinedRemoteChild - "
1120 << "To register remoteCollectionResourceHandle"
1121 << " was unsuccessful. result - " << result << endl;
1124 cout << "GroupSynchronization::onGetJoinedRemoteChild : "
1125 "To register remoteCollectionResourceHandle was successful." << endl;
1127 // binding remote collection resource handle and resource handle to join
1128 collectionResourceHandleList[types[0]] = remoteCollectionResourceHandle;
1130 result = OCPlatform::bindResource(remoteCollectionResourceHandle, deviceResourceHandle);
1131 if (OC_STACK_OK == result)
1133 cout << "GroupSynchronization::onGetJoinedRemoteChild : "
1134 << "binding remoteCollectionResourceHandle and deviceResourceHandle" << endl;
1138 cout << "GroupSynchronization::onGetJoinedRemoteChild - "
1139 << "To bind remoteCollectionResourceHandle and deviceResourceHandle "
1140 << "was unsuccessful. result - " << result << endl;
1143 std::vector< OCResourceHandle > childHandleList;
1144 childHandleList.push_back(deviceResourceHandle);
1145 deviceResourceHandleList.push_back(deviceResourceHandle);
1147 // binding copied remote collection resource handle and copied remote resource
1148 OCResourceHandle resourceHandle;
1149 for (unsigned int i = 0; i < childList.size(); ++i)
1151 cout << "\tremote resource - " << i + 1 << endl;
1153 child = childList.at(i);
1154 resourceURI = child.getUri();
1155 types = child.getResourceTypes();
1156 interfaces = child.getResourceInterfaces();
1158 if (0 == types[0].compare(OCGetResourceTypeName(deviceResourceHandle, 0)))
1160 cout << "GroupSynchronization::onGetJoinedRemoteChild : " << types[0]
1161 << " is bind already." << endl;
1165 result = OCPlatform::registerResource(resourceHandle, resourceURI, types[0],
1166 interfaces[0], NULL, OC_OBSERVABLE);
1167 if (OC_STACK_OK == result)
1169 result = OCPlatform::bindResource(remoteCollectionResourceHandle, resourceHandle);
1170 if (result != OC_STACK_OK)
1172 cout << "GroupSynchronization::onGetJoinedRemoteChild - "
1173 << "binding remoteCollectionResourceHandle and resourceHandle "
1174 << "was unsuccessful. result - " << result << endl;
1175 OCPlatform::unregisterResource(resourceHandle);
1178 childHandleList.push_back(resourceHandle);
1179 cout << "GroupSynchronization::onGetJoinedRemoteChild : "
1180 << "binding remoteCollectionResourceHandle and resourceHandle" << endl;
1184 cout << "GroupSynchronization::onGetJoinedRemoteChild - "
1185 << "To register remoteCollectionResourceHandle was unsuccessful."
1186 << " result - " << result << endl;
1190 childResourceHandleList[remoteCollectionResourceHandle] = childHandleList;
1191 // this handle list is used to leave group
1196 void GroupSynchronization::onLeaveGroup(const HeaderOptions& headerOptions,
1197 const OCRepresentation& rep, const int eCode)
1199 if (eCode != OC_STACK_OK)
1201 cout << "GroupSynchronization::onLeaveGroup : error - " << eCode << endl;
1205 cout << "GroupSynchronization::onLeaveGroup" << endl;
1209 void GroupSynchronization::debugGroupSync(void)
1211 cout << "GroupSynchronization::debugGroupSync" << endl;
1214 std::map< std::string, OCResourceHandle >::iterator handleIt;
1215 std::map< OCResourceHandle, std::vector< OCResourceHandle > >::iterator childIt;
1217 OCResourceHandle resourceHandle;
1218 std::vector< OCResourceHandle > handleList;
1219 std::shared_ptr< OCResource > resource;
1221 cout << "Resource Handle Created by App" << endl;
1222 for (i = 0; i < deviceResourceHandleList.size(); i++)
1224 resourceHandle = deviceResourceHandleList.at(i);
1226 cout << i + 1 << ". details" << endl;
1227 cout << " uri - " << OCGetResourceUri(resourceHandle) << endl;
1228 cout << " resource type - " << OCGetResourceTypeName(resourceHandle, 0) << endl;
1229 cout << " resource interface - " << OCGetResourceInterfaceName(resourceHandle, 0)
1233 cout << "\nGroup Sync Resource Handle List. The number is "
1234 << groupSyncResourceHandleList.size() << endl;
1236 for (handleIt = groupSyncResourceHandleList.begin();
1237 handleIt != groupSyncResourceHandleList.end(); ++handleIt)
1239 type = handleIt->first;
1240 cout << "\t" << i << ". group sync resource type - " << type << endl;
1241 cout << "\t details" << endl;
1243 resourceHandle = handleIt->second;
1244 cout << "\t uri - " << OCGetResourceUri(resourceHandle) << endl;
1245 cout << "\t resource type - " << OCGetResourceTypeName(resourceHandle, 0) << endl;
1246 cout << "\t resource interface - " << OCGetResourceInterfaceName(resourceHandle, 0)
1252 cout << "Copied Remote Group Sync Resource List. The number is "
1253 << groupSyncResourceList.size() << endl;
1254 std::vector< std::string > list;
1256 for (auto resourceIt = groupSyncResourceList.begin();
1257 resourceIt != groupSyncResourceList.end(); ++resourceIt)
1259 type = resourceIt->first;
1260 cout << "\t" << i << ". group sync resource type - " << type << endl;
1261 cout << "\t details" << endl;
1263 resource = resourceIt->second;
1264 cout << "\t host - " << resource->host() << endl;
1265 cout << "\t uri - " << resource->uri() << endl;
1266 list = resource->getResourceTypes();
1267 cout << "\t resource type - " << list[0] << endl;
1268 list = resource->getResourceInterfaces();
1269 cout << "\t resource interface - " << list[0] << endl << endl;
1273 // cout << "The number of collection Resource Handle is " << collectionResourceHandleList.size()
1275 // cout << "The number of child resource handle list is " << childResourceHandleList.size()
1278 cout << "Collection Resource Handle List" << endl;
1280 for (handleIt = collectionResourceHandleList.begin();
1281 handleIt != collectionResourceHandleList.end(); ++handleIt)
1283 type = handleIt->first;
1284 cout << "\t" << i << ". collection resource type - " << type << endl;
1285 cout << "\t details" << endl;
1287 resourceHandle = handleIt->second;
1288 cout << "\t uri - " << OCGetResourceUri(resourceHandle) << endl;
1289 cout << "\t resource type - " << OCGetResourceTypeName(resourceHandle, 0) << endl;
1290 cout << "\t resource interface - " << OCGetResourceInterfaceName(resourceHandle, 0)
1293 childIt = childResourceHandleList.find(resourceHandle);
1294 if (childIt != childResourceHandleList.end())
1296 handleList = childIt->second;
1297 for (unsigned int j = 0; j < handleList.size(); j++)
1300 cout << "\t\t" << j + 1 << ". child resource details" << endl;
1302 resourceHandle = handleList.at(j);
1303 cout << "\t\t uri - " << OCGetResourceUri(resourceHandle) << endl;
1304 cout << "\t\t resource type - " << OCGetResourceTypeName(resourceHandle, 0)
1306 cout << "\t\t resource interface - "
1307 << OCGetResourceInterfaceName(resourceHandle, 0) << endl << endl;