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);
278 OCResource::Ptr groupSyncResource = OCPlatform::constructResourceObject(host, uri,
279 OC_ETHERNET | OC_WIFI, 1, resourceTypes, resourceInterface);
281 OCResource::Ptr groupSyncResource = OCPlatform::constructResourceObject(host, uri, 1,
282 resourceTypes, resourceInterface);
284 groupSyncResourceList[type[0]] = groupSyncResource;
286 cout << "GroupSynchronization::joinGroup : creating groupSyncResource." << endl;
288 // Create QueryParameters Map and add query params (if any)
289 QueryParamsMap queryParamsMap;
291 // request to join group to the remote group sync resource
292 OCStackResult result = groupSyncResource->put(rep, queryParamsMap,
293 std::bind(&GroupSynchronization::onJoinGroup, this, std::placeholders::_1,
294 std::placeholders::_2, std::placeholders::_3));
295 if (OC_STACK_OK == result)
297 cout << "GroupSynchronization::joinGroup : groupSyncResource->put was successful."
302 cout << "GroupSynchronization::joinGroup : "
303 << "groupSyncResource->put was unsuccessful. result - " << result << endl;
306 // saving the remote collection resource.
307 // It is used in onJoinGroup() and onGetJoinedRemoteChild().
308 remoteCollectionResource = resource;
310 // saving the resource handle to join. It is used in onGetJoinedRemoteChild()
311 deviceResourceHandle = resourceHandle;
316 OCStackResult GroupSynchronization::leaveGroup(std::string collectionResourceType,
317 OCResourceHandle resourceHandle)
319 if ((0 == collectionResourceType.length()) || (!resourceHandle))
321 cout << "GroupSynchronization::leaveGroup : Error! Input params are wrong." << endl;
322 return OC_STACK_INVALID_PARAM;
325 cout << "GroupSynchronization::leaveGroup : collectionResourceType - "
326 << collectionResourceType << endl;
328 OCResourceHandle collectionResHandle;
329 auto handleIt = groupSyncResourceHandleList.find(collectionResourceType);
331 // if groupSyncResourceHandleList has resourceType,
332 // this app created collection resource handle.
333 if (handleIt != groupSyncResourceHandleList.end())
335 handleIt = collectionResourceHandleList.find(collectionResourceType);
336 if (handleIt == collectionResourceHandleList.end())
338 cout << "GroupSynchronization::leaveGroup : "
339 << "Error! There is no collection resource handle to leave." << endl;
340 return OC_STACK_INVALID_PARAM;
343 collectionResHandle = handleIt->second;
344 // cout << "GroupSynchronization::leaveGroup : collection handle uri - "
345 // << OCGetResourceUri(collectionResHandle) << endl;
346 if(collectionResHandle == NULL)
347 return OC_STACK_INVALID_PARAM;
349 OCStackResult result;
352 result = OCPlatform::unbindResource(collectionResHandle, resourceHandle);
353 if (OC_STACK_OK == result)
355 cout << "GroupSynchronization::leaveGroup : "
356 << "To unbind resource was successful." << endl;
360 cout << "GroupSynchronization::leaveGroup : "
361 << "To unbind resource was unsuccessful. result - " << result << endl;
364 } catch(OCException &e) {
365 cout << "ERROR : " << e.reason() << endl;
366 return OC_STACK_NO_RESOURCE;
369 auto It = std::find(deviceResourceHandleList.begin(), deviceResourceHandleList.end(),
371 if (It == deviceResourceHandleList.end()) // there is no resource handle to find
375 result = OCPlatform::unregisterResource(resourceHandle);
376 if (OC_STACK_OK == result)
378 cout << "GroupSynchronization::leaveGroup : "
379 << "To unregister resource was successful." << endl;
383 cout << "GroupSynchronization::leaveGroup : "
384 << "To unregister resource was unsuccessful. result - " << result
388 } catch(OCException &e)
390 cout << "ERROR : " << e.reason() << endl;
391 return OC_STACK_NO_RESOURCE;
396 cout << "GroupSynchronization::leaveGroup : "
397 << "This resource cannot be unregistered." << endl;
398 deviceResourceHandleList.erase(It);
401 auto handleListIt = childResourceHandleList.find(collectionResHandle);
402 if (handleListIt == childResourceHandleList.end())
404 cout << "GroupSynchronization::leaveGroup : "
405 << "Error! There is no child resource list to delete." << endl;
406 return OC_STACK_INVALID_PARAM;
409 std::vector< OCResourceHandle > childList = handleListIt->second;
410 auto childIt = std::find(childList.begin(), childList.end(), resourceHandle);
411 if (childIt != childList.end())
413 cout << "GroupSynchronization::groupEntityHandler : "
414 << "Found! The resource to leave is found." << endl;
415 childList.erase(childIt);
418 childResourceHandleList[collectionResHandle] = childList;
422 else // requesting to unbind this resourceHandle to the remote collection resource
424 auto resourceIt = groupSyncResourceList.find(collectionResourceType);
426 if (resourceIt == groupSyncResourceList.end())
428 cout << "GroupSynchronization::leaveGroup : "
429 << "Error! There is no collectin resource type to leave." << endl;
430 return OC_STACK_INVALID_PARAM;
433 std::shared_ptr< OCResource > resource = resourceIt->second;
435 return OC_STACK_NO_RESOURCE;
436 // cout << "GroupSynchronization::leaveGroup : group sync resource uri - "
437 // << resource->uri() << endl;
439 handleIt = collectionResourceHandleList.find(collectionResourceType);
440 if (handleIt == collectionResourceHandleList.end())
442 cout << "GroupSynchronization::leaveGroup : "
443 << "Error! There is no collection resource handle to leave." << endl;
444 return OC_STACK_INVALID_PARAM;
447 collectionResHandle = handleIt->second;
449 // making representation to leave group
450 std::string method = "leaveGroup";
451 std::string type = OCGetResourceTypeName(collectionResHandle, 0);
452 std::string resourceType;
453 resourceType.append(OCGetResourceTypeName(resourceHandle, 0));
455 OCRepresentation rep;
456 rep.setValue("method", method);
457 rep.setValue("collectionResourceType", type);
458 rep.setValue("resourceType", resourceType);
460 cout << "\tmethod - " << method << endl;
461 cout << "\tcollectionResourceType - " << type << endl;
462 cout << "\tresourceType - " << resourceType << endl;
464 QueryParamsMap queryParamsMap;
466 // request to leave group to the remote group sync resource
467 OCStackResult result = resource->put(rep, queryParamsMap,
468 std::bind(&GroupSynchronization::onLeaveGroup, this, std::placeholders::_1,
469 std::placeholders::_2, std::placeholders::_3));
470 if (OC_STACK_OK == result)
472 cout << "GroupSynchronization::leaveGroup : "
473 << "groupSyncResource->put was successful." << endl;
477 cout << "GroupSynchronization::leaveGroup : "
478 << "groupSyncResource->put was unsuccessful. result - " << result << endl;
481 // deleting all remote resources. These are copied in onGetJoinedRemoteChild()
482 deleteGroup(collectionResourceType);
488 void GroupSynchronization::deleteGroup(std::string collectionResourceType)
490 if (0 == collectionResourceType.length())
492 cout << "GroupSynchronization::deleteGroup : Error! Input params are wrong." << endl;
496 cout << "GroupSynchronization::deleteGroup" << endl;
498 OCStackResult result;
499 OCResourceHandle resourceHandle;
501 auto handleIt = groupSyncResourceHandleList.find(collectionResourceType);
503 // if groupSyncResourceHandleList has resourceType,
504 // group sync of this app created collection resource.
505 if (handleIt != groupSyncResourceHandleList.end())
507 resourceHandle = handleIt->second; // group sync resource handle
508 result = OCPlatform::unregisterResource(resourceHandle);
509 if (OC_STACK_OK == result)
511 cout << "GroupSynchronization::deleteGroup : "
512 << "To unregister group sync resource handle was successful." << endl;
516 cout << "GroupSynchronization::deleteGroup : "
517 << "To unregister group sync resource handle was unsuccessful. "
518 << "result - " << result << endl;
521 groupSyncResourceHandleList.erase(handleIt);
524 auto resourceIt = groupSyncResourceList.find(collectionResourceType);
525 if (resourceIt != groupSyncResourceList.end())
527 groupSyncResourceList.erase(resourceIt);
530 handleIt = collectionResourceHandleList.find(collectionResourceType);
531 if (handleIt == collectionResourceHandleList.end())
533 cout << "GroupSynchronization::deleteGroup : "
534 << "Error! There is no collection resource handle to delete." << endl;
537 OCResourceHandle collectionResHandle = handleIt->second;
539 collectionResourceHandleList.erase(handleIt);
541 auto handleListIt = childResourceHandleList.find(collectionResHandle);
542 if (handleListIt == childResourceHandleList.end())
544 cout << "GroupSynchronization::deleteGroup : "
545 << "There is no child resource list to delete." << endl;
547 result = OCPlatform::unregisterResource(collectionResHandle);
548 if (result == OC_STACK_OK)
550 cout << "GroupSynchronization::deleteGroup : "
551 << "To unregister collection resource handle was successful." << endl;
555 cout << "GroupSynchronization::deleteGroup : "
556 << " To unregister collection resource handle was unsuccessful. result - "
564 std::vector< OCResourceHandle > childList = handleListIt->second;
566 childResourceHandleList.erase(handleListIt);
568 result = OCPlatform::unbindResources(collectionResHandle, childList);
569 if (OC_STACK_OK == result)
571 cout << "GroupSynchronization::deleteGroup : " << "To unbind resources was successful."
576 cout << "GroupSynchronization::deleteGroup : "
577 << "To unbind resources was unsuccessful. result - " << result << endl;
580 result = OCPlatform::unregisterResource(collectionResHandle);
581 if (result == OC_STACK_OK)
583 cout << "GroupSynchronization::deleteGroup : "
584 << "To unregister collection resource handle was successful." << endl;
588 cout << "GroupSynchronization::deleteGroup : "
589 << " To unregister collection resource handle was unsuccessful. result - "
593 std::vector< OCResourceHandle >::iterator It;
595 for (unsigned int i = 0; i < childList.size(); i++)
597 resourceHandle = childList.at(i);
599 It = std::find(deviceResourceHandleList.begin(), deviceResourceHandleList.end(),
601 if (It != deviceResourceHandleList.end()) // find !!
603 deviceResourceHandleList.erase(It);
607 result = OCPlatform::unregisterResource(resourceHandle);
608 if (OC_STACK_OK == result)
610 cout << "GroupSynchronization::deleteGroup : UnregisterResource(" << i + 1
611 << ") was successful." << endl;
615 cout << "GroupSynchronization::deleteGroup : UnregisterResource(" << i + 1
616 << ") was unsuccessful. result - " << result << endl;
624 std::map< std::string, OCResourceHandle > GroupSynchronization::getGroupList()
626 return collectionResourceHandleList;
629 OCEntityHandlerResult GroupSynchronization::groupEntityHandler(
630 const std::shared_ptr< OCResourceRequest > request)
632 cout << "GroupSynchronization::groupEntityHandler\n";
636 // Get the request type and request flag
637 std::string requestType = request->getRequestType();
638 int requestFlag = request->getRequestHandlerFlag();
640 if (requestFlag == RequestHandlerFlag::InitFlag)
642 cout << "\trequestFlag : Init\n";
644 // entity handler to perform resource initialization operations
646 else if (requestFlag == RequestHandlerFlag::RequestFlag)
648 cout << "\trequestFlag : Request\n";
650 // If the request type is GET
651 if (requestType == "GET")
653 cout << "\t\trequestType : GET\n";
655 else if (requestType == "PUT")
657 cout << "\t\trequestType : PUT\n";
659 //get method name, group resource type and resource type to join group
660 OCRepresentation rp = request->getResourceRepresentation();
661 std::string methodType = rp.getValue< std::string >("method");
662 std::string collectionResourceType = rp.getValue< std::string >(
663 "collectionResourceType");
664 std::string resourceType = rp.getValue< std::string >("resourceType");
666 cout << "\t\t\tmethod : " << methodType << endl;
667 cout << "\t\t\tcollection resourceType : " << collectionResourceType << endl;
668 cout << "\t\t\tresourceType : " << resourceType << endl;
670 auto handleIt = collectionResourceHandleList.find(collectionResourceType);
671 if (handleIt == collectionResourceHandleList.end())
673 cout << "GroupSynchronization::groupEntityHandler : "
674 << "Error! There is no collection resource handle to delete."
678 collectionResourceHandle = handleIt->second;
679 // in case of join group it is used in onFindResource()
681 if (methodType == "joinGroup")
683 std::string resourceName = "coap://224.0.1.187/oc/core?rt=";
684 resourceName += resourceType;
685 cout << "\t\t\tresourceName : " << resourceName << endl;
687 resourceRequest = request;
689 OCPlatform::findResource("", resourceName,
690 std::bind(&GroupSynchronization::onFindResource, this,
691 std::placeholders::_1));
693 else if (methodType == "leaveGroup")
695 auto it = childResourceHandleList.find(collectionResourceHandle);
696 if (it == childResourceHandleList.end())
698 cout << "GroupSynchronization::groupEntityHandler : "
699 << "Error! There is no child resource list." << endl;
703 std::vector< OCResourceHandle > childList = it->second;
704 OCResourceHandle resourceHandle;
705 for (auto childIt = childList.begin(); childIt != childList.end();)
707 resourceHandle = (*childIt);
708 char* type = (char*) OCGetResourceTypeName(resourceHandle, 0);
710 if (0 == resourceType.compare(type))
712 cout << "GroupSynchronization::groupEntityHandler : "
713 << "Found! The resource to leave is found. - " << type
716 childIt = childList.erase(childIt++);
718 OCStackResult result = OCPlatform::unbindResource(
719 collectionResourceHandle, resourceHandle);
720 if (OC_STACK_OK == result)
722 cout << "GroupSynchronization::groupEntityHandler : "
723 << "To unbind resource was successful." << endl;
727 cout << "GroupSynchronization::groupEntityHandler : "
728 << "To unbind resource was unsuccessful. result - "
732 result = OCPlatform::unregisterResource(resourceHandle);
733 if (OC_STACK_OK == result)
735 cout << "GroupSynchronization::groupEntityHandler : "
736 << "To unregister resource was successful." << endl;
740 cout << "GroupSynchronization::groupEntityHandler : "
741 << "To unregister resource was unsuccessful. result - "
754 childResourceHandleList[collectionResourceHandle] = childList;
758 auto pResponse = std::make_shared< OC::OCResourceResponse >();
759 pResponse->setRequestHandle(request->getRequestHandle());
760 pResponse->setResourceHandle(request->getResourceHandle());
761 pResponse->setErrorCode(200);
762 pResponse->setResponseResult(OC_EH_OK);
764 OCRepresentation rep = request->getResourceRepresentation();
765 pResponse->setResourceRepresentation(rep, DEFAULT_INTERFACE);
766 if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
768 cout << "GroupSynchronization::groupEntityHandler : "
769 << "sendResponse is successful." << endl;
773 if (methodType != "") //TODO: Check groupmethodtype NULL
777 else if (requestType == "POST")
779 // POST request operations
781 else if (requestType == "DELETE")
783 // DELETE request operations
786 else if (requestFlag == RequestHandlerFlag::ObserverFlag)
788 cout << "\trequestFlag : Observer\n";
793 std::cout << "Request invalid" << std::endl;
799 void GroupSynchronization::onFindGroup(std::shared_ptr< OCResource > resource)
801 cout << "GroupSynchronization::onFindGroup" << endl;
808 std::string resourceURI;
809 std::string hostAddress;
811 // Get the resource URI
812 resourceURI = resource->uri();
813 cout << "\tURI of the resource: " << resourceURI << endl;
815 // Get the resource host address
816 hostAddress = resource->host();
817 cout << "\tHost address of the resource: " << hostAddress << endl;
819 hostAddress.append(resourceURI);
821 // Get the resource types
822 cout << "\tList of resource types: " << endl;
824 for (auto &resourceTypes : resource->getResourceTypes())
826 cout << "\t\t" << resourceTypes << endl;
829 // Get the resource interfaces
830 cout << "\tList of resource interfaces: " << endl;
831 for (auto &resourceInterfaces : resource->getResourceInterfaces())
833 cout << "\t\t" << resourceInterfaces << endl;
836 if (false == IsSameGroup(resource))
839 findCallback(resource);
844 // Resource is invalid
845 cout << "Resource is invalid" << endl;
851 catch (std::exception& e)
857 void GroupSynchronization::checkFindGroup(void)
859 cout << "GroupSynchronization::checkFindGroup" << endl;
861 for (int i = 0; i < 15; i++)
863 std::chrono::milliseconds workTime(300);
864 std::this_thread::sleep_for(workTime);
866 std::lock_guard < std::mutex > guard(foundGroupMutex);
868 if (false == foundGroupResourceList.empty())
870 cout << "GroupSynchronization::checkFoundGroup : " << "Some group is received."
876 cout << "GroupSynchronization::checkFoundGroup : "
877 << "It is failed to find resource within 3s." << endl;
883 bool GroupSynchronization::IsSameGroup(std::shared_ptr< OCResource > resource)
885 std::lock_guard < std::mutex > guard(foundGroupMutex);
887 if (true == foundGroupResourceList.empty())
889 cout << "GroupSynchronization::IsSameGroup : There is no found group." << endl;
893 std::string foundHostAddress, savedHostAddress;
894 foundHostAddress = resource->host();
895 // foundHostAddress.append (resource->uri());
897 for (unsigned int i = 0; i < foundGroupResourceList.size(); ++i)
899 savedHostAddress = (foundGroupResourceList.at(i))->host();
900 // savedHostAddress.append ((foundGroupResourceList.at(i))->uri());
901 // cout << "GroupSynchronization::IsSameGroup : foundHostAddress - " << foundHostAddress
902 // << ", savedHostAddress - " << savedHostAddress << endl;
904 if (0 == foundHostAddress.compare(savedHostAddress.c_str()))
906 cout << "GroupSynchronization::IsSameGroup : Found! The same group is found."
912 cout << "GroupSynchronization::IsSameGroup : There is no same group." << endl;
916 void GroupSynchronization::saveGroup(std::shared_ptr< OCResource > resource)
918 cout << "GroupSynchronization::saveGroup" << endl;
920 std::lock_guard < std::mutex > guard(foundGroupMutex);
922 foundGroupResourceList.push_back(resource);
925 void GroupSynchronization::onJoinGroup(const HeaderOptions& headerOptions,
926 const OCRepresentation& rep, const int eCode)
928 if (eCode != OC_STACK_OK)
930 cout << "GroupSynchronization::onJoinGroup : error - " << eCode << endl;
934 cout << "GroupSynchronization::onJoinGroup : " << endl;
936 if (remoteCollectionResource)
938 std::string resourceInterface = DEFAULT_INTERFACE;
939 QueryParamsMap queryParamsMap;
941 OCStackResult result = remoteCollectionResource->get("", resourceInterface,
943 std::bind(&GroupSynchronization::onGetJoinedRemoteChild, this,
944 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
945 if (OC_STACK_OK == result)
947 cout << "GroupSynchronization::onJoinGroup : "
948 << "remoteCollectionResource->get was successful." << endl;
952 cout << "GroupSynchronization::onJoinGroup : "
953 << "remoteCollectionResource->get was unsuccessful. result - " << result
959 void GroupSynchronization::onFindResource(std::shared_ptr< OCResource > resource)
961 cout << "GroupSynchronization::onFindResource" << endl;
965 // start of debugging
966 std::string resourceURI;
967 std::string hostAddress;
969 // Get the resource URI
970 resourceURI = resource->uri();
971 cout << "\tURI of the resource: " << resourceURI << endl;
973 // Get the resource host address
974 hostAddress = resource->host();
975 cout << "\tHost address of the resource: " << hostAddress << endl;
977 hostAddress.append(resourceURI);
979 // Get the resource types
980 cout << "\tList of resource types: " << endl;
982 for (auto &resourceTypes : resource->getResourceTypes())
984 cout << "\t\t" << resourceTypes << endl;
987 // Get the resource interfaces
988 cout << "\tList of resource interfaces: " << endl;
989 for (auto &resourceInterfaces : resource->getResourceInterfaces())
991 cout << "\t\t" << resourceInterfaces << endl;
995 OCResourceHandle resourceHandle;
996 OCStackResult result = OCPlatform::registerResource(resourceHandle, resource);
997 if (result != OC_STACK_OK)
999 cout << "GroupSynchronization::"
1000 << "onFindResource - Resource to join creation was unsuccessful. result - "
1004 // cout << "GroupSynchronization::onFindResource : creating resourceHandle. resource type - "
1005 // << OCGetResourceTypeName(resourceHandle, 0) << endl;
1007 result = OCPlatform::bindResource(collectionResourceHandle, resourceHandle);
1008 if (result != OC_STACK_OK)
1010 cout << "GroupSynchronization::onFindResource : "
1011 << "To bind resource was unsuccessful. result - " << result << endl;
1015 cout << "GroupSynchronization::onFindResource : "
1016 << "To bind joinGroupHandle and resourceHandle was successful." << endl;
1018 auto it = childResourceHandleList.find(collectionResourceHandle);
1019 std::vector< OCResourceHandle > childHandleList;
1020 if (it != childResourceHandleList.end())
1022 childHandleList = it->second;
1025 childHandleList.push_back(resourceHandle);
1026 childResourceHandleList[collectionResourceHandle] = childHandleList;
1028 auto pResponse = std::make_shared< OC::OCResourceResponse >();
1029 pResponse->setRequestHandle(resourceRequest->getRequestHandle());
1030 pResponse->setResourceHandle(resourceRequest->getResourceHandle());
1031 pResponse->setErrorCode(200);
1032 pResponse->setResponseResult(OC_EH_OK);
1034 OCRepresentation rep = resourceRequest->getResourceRepresentation();
1035 pResponse->setResourceRepresentation(rep, DEFAULT_INTERFACE);
1036 if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
1038 cout << "GroupSynchronization::onFindResource : sendResponse is successful."
1044 cout << "GroupSynchronization::onFindResource : "
1045 << "Resource is invalid. So a new Group Resource has to be created." << endl;
1051 void GroupSynchronization::onGetJoinedRemoteChild(const HeaderOptions& headerOptions,
1052 const OCRepresentation& rep, const int eCode)
1054 if (eCode != OC_STACK_OK)
1056 cout << "GroupSynchronization::onGetJoinedRemoteChild : error - " << eCode << endl;
1060 cout << "GroupSynchronization::onGetJoinedRemoteChild" << endl;
1063 std::string resourceURI;
1065 // Get the resource URI
1066 resourceURI = rep.getUri();
1067 cout << "\tURI of the resource: " << resourceURI << endl;
1069 // Get the resource types
1070 cout << "\tList of resource types: " << endl;
1072 for (auto &resourceTypes : rep.getResourceTypes())
1074 cout << "\t\t" << resourceTypes << endl;
1077 // Get the resource interfaces
1078 cout << "\tList of resource interfaces: " << endl;
1079 for (auto &resourceInterfaces : rep.getResourceInterfaces())
1081 cout << "\t\t" << resourceInterfaces << endl;
1084 std::vector< OCRepresentation > childList = rep.getChildren();
1085 OCRepresentation child;
1086 for (unsigned int i = 0; i < childList.size(); ++i)
1088 cout << "\n\tchild resource - " << i + 1 << endl;
1090 child = childList.at(i);
1091 resourceURI = child.getUri();
1092 cout << "\t\tURI of the resource: " << resourceURI << endl;
1094 cout << "\t\tList of resource types: " << endl;
1095 for (auto &types : child.getResourceTypes())
1097 cout << "\t\t\t" << types << endl;
1100 cout << "\tList of resource interfaces: " << endl;
1101 for (auto &interfaces : child.getResourceInterfaces())
1103 cout << "\t\t\t" << interfaces << endl;
1107 // creating remote collection resource handle
1108 OCResourceHandle remoteCollectionResourceHandle;
1109 resourceURI = remoteCollectionResource->uri();
1110 std::vector< std::string > types = remoteCollectionResource->getResourceTypes();
1111 std::vector< std::string > interfaces = remoteCollectionResource->getResourceInterfaces();
1113 OCStackResult result = OCPlatform::registerResource(remoteCollectionResourceHandle,
1114 resourceURI, types[0], interfaces[0], NULL, OC_OBSERVABLE);
1115 if (result != OC_STACK_OK)
1117 cout << "GroupSynchronization::onGetJoinedRemoteChild - "
1118 << "To register remoteCollectionResourceHandle"
1119 << " was unsuccessful. result - " << result << endl;
1122 cout << "GroupSynchronization::onGetJoinedRemoteChild : "
1123 "To register remoteCollectionResourceHandle was successful." << endl;
1125 // binding remote collection resource handle and resource handle to join
1126 collectionResourceHandleList[types[0]] = remoteCollectionResourceHandle;
1128 result = OCPlatform::bindResource(remoteCollectionResourceHandle, deviceResourceHandle);
1129 if (OC_STACK_OK == result)
1131 cout << "GroupSynchronization::onGetJoinedRemoteChild : "
1132 << "binding remoteCollectionResourceHandle and deviceResourceHandle" << endl;
1136 cout << "GroupSynchronization::onGetJoinedRemoteChild - "
1137 << "To bind remoteCollectionResourceHandle and deviceResourceHandle "
1138 << "was unsuccessful. result - " << result << endl;
1141 std::vector< OCResourceHandle > childHandleList;
1142 childHandleList.push_back(deviceResourceHandle);
1143 deviceResourceHandleList.push_back(deviceResourceHandle);
1145 // binding copied remote collection resource handle and copied remote resource
1146 OCResourceHandle resourceHandle;
1147 for (unsigned int i = 0; i < childList.size(); ++i)
1149 cout << "\tremote resource - " << i + 1 << endl;
1151 child = childList.at(i);
1152 resourceURI = child.getUri();
1153 types = child.getResourceTypes();
1154 interfaces = child.getResourceInterfaces();
1156 if (0 == types[0].compare(OCGetResourceTypeName(deviceResourceHandle, 0)))
1158 cout << "GroupSynchronization::onGetJoinedRemoteChild : " << types[0]
1159 << " is bind already." << endl;
1163 result = OCPlatform::registerResource(resourceHandle, resourceURI, types[0],
1164 interfaces[0], NULL, OC_OBSERVABLE);
1165 if (OC_STACK_OK == result)
1167 result = OCPlatform::bindResource(remoteCollectionResourceHandle, resourceHandle);
1168 if (result != OC_STACK_OK)
1170 cout << "GroupSynchronization::onGetJoinedRemoteChild - "
1171 << "binding remoteCollectionResourceHandle and resourceHandle "
1172 << "was unsuccessful. result - " << result << endl;
1173 OCPlatform::unregisterResource(resourceHandle);
1176 childHandleList.push_back(resourceHandle);
1177 cout << "GroupSynchronization::onGetJoinedRemoteChild : "
1178 << "binding remoteCollectionResourceHandle and resourceHandle" << endl;
1182 cout << "GroupSynchronization::onGetJoinedRemoteChild - "
1183 << "To register remoteCollectionResourceHandle was unsuccessful."
1184 << " result - " << result << endl;
1188 childResourceHandleList[remoteCollectionResourceHandle] = childHandleList;
1189 // this handle list is used to leave group
1194 void GroupSynchronization::onLeaveGroup(const HeaderOptions& headerOptions,
1195 const OCRepresentation& rep, const int eCode)
1197 if (eCode != OC_STACK_OK)
1199 cout << "GroupSynchronization::onLeaveGroup : error - " << eCode << endl;
1203 cout << "GroupSynchronization::onLeaveGroup" << endl;
1207 void GroupSynchronization::debugGroupSync(void)
1209 cout << "GroupSynchronization::debugGroupSync" << endl;
1212 std::map< std::string, OCResourceHandle >::iterator handleIt;
1213 std::map< OCResourceHandle, std::vector< OCResourceHandle > >::iterator childIt;
1215 OCResourceHandle resourceHandle;
1216 std::vector< OCResourceHandle > handleList;
1217 std::shared_ptr< OCResource > resource;
1219 cout << "Resource Handle Created by App" << endl;
1220 for (i = 0; i < deviceResourceHandleList.size(); i++)
1222 resourceHandle = deviceResourceHandleList.at(i);
1224 cout << i + 1 << ". details" << endl;
1225 cout << " uri - " << OCGetResourceUri(resourceHandle) << endl;
1226 cout << " resource type - " << OCGetResourceTypeName(resourceHandle, 0) << endl;
1227 cout << " resource interface - " << OCGetResourceInterfaceName(resourceHandle, 0)
1231 cout << "\nGroup Sync Resource Handle List. The number is "
1232 << groupSyncResourceHandleList.size() << endl;
1234 for (handleIt = groupSyncResourceHandleList.begin();
1235 handleIt != groupSyncResourceHandleList.end(); ++handleIt)
1237 type = handleIt->first;
1238 cout << "\t" << i << ". group sync resource type - " << type << endl;
1239 cout << "\t details" << endl;
1241 resourceHandle = handleIt->second;
1242 cout << "\t uri - " << OCGetResourceUri(resourceHandle) << endl;
1243 cout << "\t resource type - " << OCGetResourceTypeName(resourceHandle, 0) << endl;
1244 cout << "\t resource interface - " << OCGetResourceInterfaceName(resourceHandle, 0)
1250 cout << "Copied Remote Group Sync Resource List. The number is "
1251 << groupSyncResourceList.size() << endl;
1252 std::vector< std::string > list;
1254 for (auto resourceIt = groupSyncResourceList.begin();
1255 resourceIt != groupSyncResourceList.end(); ++resourceIt)
1257 type = resourceIt->first;
1258 cout << "\t" << i << ". group sync resource type - " << type << endl;
1259 cout << "\t details" << endl;
1261 resource = resourceIt->second;
1262 cout << "\t host - " << resource->host() << endl;
1263 cout << "\t uri - " << resource->uri() << endl;
1264 list = resource->getResourceTypes();
1265 cout << "\t resource type - " << list[0] << endl;
1266 list = resource->getResourceInterfaces();
1267 cout << "\t resource interface - " << list[0] << endl << endl;
1271 // cout << "The number of collection Resource Handle is " << collectionResourceHandleList.size()
1273 // cout << "The number of child resource handle list is " << childResourceHandleList.size()
1276 cout << "Collection Resource Handle List" << endl;
1278 for (handleIt = collectionResourceHandleList.begin();
1279 handleIt != collectionResourceHandleList.end(); ++handleIt)
1281 type = handleIt->first;
1282 cout << "\t" << i << ". collection resource type - " << type << endl;
1283 cout << "\t details" << endl;
1285 resourceHandle = handleIt->second;
1286 cout << "\t uri - " << OCGetResourceUri(resourceHandle) << endl;
1287 cout << "\t resource type - " << OCGetResourceTypeName(resourceHandle, 0) << endl;
1288 cout << "\t resource interface - " << OCGetResourceInterfaceName(resourceHandle, 0)
1291 childIt = childResourceHandleList.find(resourceHandle);
1292 if (childIt != childResourceHandleList.end())
1294 handleList = childIt->second;
1295 for (unsigned int j = 0; j < handleList.size(); j++)
1298 cout << "\t\t" << j + 1 << ". child resource details" << endl;
1300 resourceHandle = handleList.at(j);
1301 cout << "\t\t uri - " << OCGetResourceUri(resourceHandle) << endl;
1302 cout << "\t\t resource type - " << OCGetResourceTypeName(resourceHandle, 0)
1304 cout << "\t\t resource interface - "
1305 << OCGetResourceInterfaceName(resourceHandle, 0) << endl << endl;