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;
33 GroupSynchronization* GroupSynchronization::getInstance()
35 if (groupSyncnstance == NULL)
37 groupSyncnstance = new GroupSynchronization();
39 return groupSyncnstance;
42 void GroupSynchronization::deleteInstance()
46 delete groupSyncnstance;
47 groupSyncnstance = NULL;
51 OCStackResult GroupSynchronization::findGroup(
52 std::vector< std::string > collectionResourceTypes, FindCallback callback)
54 cout << "GroupSynchronization::findGroup" << endl;
56 foundGroupResourceList.clear();
58 findCallback = callback;
60 for (unsigned int i = 0; i < collectionResourceTypes.size(); ++i)
62 std::string query = "coap://224.0.1.187/oc/core?rt=";
63 query.append(collectionResourceTypes.at(i));
64 cout << "GroupSynchronization::findGroup - " << query << endl;
66 OCPlatform::findResource("", query,
67 std::bind(&GroupSynchronization::onFindGroup, this, std::placeholders::_1));
70 // thread to check if GroupSynchronization::onFoundGroup is called or not.
71 std::thread t(std::bind(&GroupSynchronization::checkFindGroup, this));
77 OCStackResult GroupSynchronization::createGroup(std::string collectionResourceType)
79 foundGroupResourceList.clear();
81 OCResourceHandle collectionResHandle = NULL;
82 OCResourceHandle groupSyncResHandle = NULL;
84 if (0 != collectionResourceType.length())
86 cout << "GroupSynchronization::createGroup - The created group is added." << endl;
90 // creating master collection resource
91 std::string collectionUri = "/" + collectionResourceType;
93 while ((i = collectionUri.find(".")) != std::string::npos)
95 collectionUri.replace(i, 1, "/");
97 cout << "GroupSynchronization::createGroup : collection uri - " << collectionUri
98 << ", type - " << collectionResourceType << endl;
100 std::string resourceInterface = DEFAULT_INTERFACE;
102 result = OCPlatform::registerResource(collectionResHandle, collectionUri,
103 collectionResourceType, resourceInterface, NULL,
104 OC_DISCOVERABLE | OC_OBSERVABLE);
105 if (result != OC_STACK_OK)
107 cout << "To register resource (" << collectionUri << ") was unsuccessful. result - "
112 OCPlatform::bindInterfaceToResource(collectionResHandle, GROUP_INTERFACE);
113 if (result != OC_STACK_OK)
115 cout << "To bind Interface (collection) was unsuccessful. result - " << result
119 collectionResourceHandleList[collectionResourceType] = collectionResHandle;
121 // creating master group sync resource
122 std::string groupSyncUri = collectionUri + "/groupsync";
123 std::string groupSyncResType = collectionResourceType + ".groupsync";
125 // cout << "GroupSynchronization::createGroup : groupSync uri - " << groupSyncUri
126 // << ", type - " << collectionResourceType << endl;
128 result = OCPlatform::registerResource(groupSyncResHandle, groupSyncUri,
129 groupSyncResType, resourceInterface,
130 std::bind(&GroupSynchronization::groupEntityHandler, this,
131 std::placeholders::_1), OC_DISCOVERABLE | OC_OBSERVABLE);
132 if (result != OC_STACK_OK)
134 cout << "To register resource (groupsync) was unsuccessful. result - " << result
139 groupSyncResourceHandleList[collectionResourceType] = groupSyncResHandle;
145 cout << "GroupSynchronization::createGroup : Error! Input params are wrong." << endl;
146 return OC_STACK_INVALID_PARAM;
151 if (collectionResHandle)
153 OCPlatform::unregisterResource(collectionResHandle);
154 auto iterator = collectionResourceHandleList.find(collectionResourceType);
155 if (iterator != collectionResourceHandleList.end())
157 collectionResourceHandleList.erase(iterator);
161 if (groupSyncResHandle)
163 OCPlatform::unregisterResource(groupSyncResHandle);
164 auto iterator = groupSyncResourceHandleList.find(collectionResourceType);
165 if (iterator != groupSyncResourceHandleList.end())
167 groupSyncResourceHandleList.erase(iterator);
171 return OC_STACK_NO_RESOURCE;
174 OCStackResult GroupSynchronization::joinGroup(std::string collectionResourceType,
175 OCResourceHandle resourceHandle)
177 if ((0 != collectionResourceType.length()) && (resourceHandle))
179 auto resIt = collectionResourceHandleList.find(collectionResourceType);
180 if (resIt == groupSyncResourceHandleList.end())
182 cout << "GroupSynchronization::joinGroup : error! There is no collection to join"
184 return OC_STACK_INVALID_PARAM;
187 OCResourceHandle collectionResHandle = resIt->second;
189 OCStackResult result = OCPlatform::bindResource(collectionResHandle, resourceHandle);
190 if (result != OC_STACK_OK)
192 cout << "GroupSynchronization::joinGroup : To bind resource was unsuccessful."
193 << "result - " << result << endl;
194 return OC_STACK_ERROR;
196 cout << "GroupSynchronization::joinGroup : "
197 << "To bind collectionResHandle and resourceHandle" << endl;
199 std::vector< OCResourceHandle > childHandleList;
201 auto childIt = childResourceHandleList.find(collectionResHandle);
202 if (childIt != childResourceHandleList.end())
204 childHandleList = childIt->second;
207 childHandleList.push_back(resourceHandle);
208 childResourceHandleList[collectionResHandle] = childHandleList;
210 deviceResourceHandleList.push_back(resourceHandle);
216 cout << "GroupSynchronization::joinGroup : Error! input params are wrong." << endl;
217 return OC_STACK_INVALID_PARAM;
223 OCStackResult GroupSynchronization::joinGroup(const std::shared_ptr< OCResource > resource,
224 OCResourceHandle resourceHandle)
226 if ((resource) && (resourceHandle))
228 cout << "GroupSynchronization::joinGroup" << endl;
230 // making representation to join group
231 std::string method = "joinGroup";
232 std::vector< std::string > type = resource->getResourceTypes();
233 std::string resourceType;
234 resourceType.append(OCGetResourceTypeName(resourceHandle, 0));
236 OCRepresentation rep;
237 rep.setValue("method", method);
238 rep.setValue("collectionResourceType", type[0]);
239 rep.setValue("resourceType", resourceType);
241 cout << "\tmethod - " << method << endl;
242 cout << "\tcollectionResourceType - " << type[0] << endl;
243 cout << "\tresourceType - " << resourceType << endl;
245 // creating group sync resource with the received collection resource.
246 // entity handler of group sync is used to join group.
247 std::string host = resource->host();
248 std::string uri = resource->uri() + "/groupsync";
250 std::vector< std::string > resourceTypes;
252 for (unsigned int i = 0; i < type.size(); ++i)
254 temp = type[0] + ".groupsync";
255 resourceTypes.push_back(temp);
258 std::vector< std::string > resourceInterface;
259 resourceInterface.push_back(DEFAULT_INTERFACE);
261 OCResource::Ptr groupSyncResource = OCPlatform::constructResourceObject(host, uri, 1,
262 resourceTypes, resourceInterface);
263 groupSyncResourceList[type[0]] = groupSyncResource;
265 cout << "GroupSynchronization::joinGroup : creating groupSyncResource." << endl;
267 // Create QueryParameters Map and add query params (if any)
268 QueryParamsMap queryParamsMap;
270 // request to join group to the remote group sync resource
271 OCStackResult result = groupSyncResource->put(rep, queryParamsMap,
272 std::bind(&GroupSynchronization::onJoinGroup, this, std::placeholders::_1,
273 std::placeholders::_2, std::placeholders::_3));
274 if (OC_STACK_OK == result)
276 cout << "GroupSynchronization::joinGroup : groupSyncResource->put was successful."
281 cout << "GroupSynchronization::joinGroup : "
282 << "groupSyncResource->put was unsuccessful. result - " << result << endl;
285 // saving the remote collection resource.
286 // It is used in onJoinGroup() and onGetJoinedRemoteChild().
287 remoteCollectionResource = resource;
289 // saving the resource handle to join. It is used in onGetJoinedRemoteChild()
290 deviceResourceHandle = resourceHandle;
296 cout << "GroupSynchronization::joinGroup : Error! Input params are wrong." << endl;
297 return OC_STACK_INVALID_PARAM;
301 OCStackResult GroupSynchronization::leaveGroup(std::string collectionResourceType,
302 OCResourceHandle resourceHandle)
304 if ((0 != collectionResourceType.length()) && (resourceHandle))
306 cout << "GroupSynchronization::leaveGroup : collectionResourceType - "
307 << collectionResourceType << endl;
309 OCResourceHandle collectionResHandle;
311 auto handleIt = groupSyncResourceHandleList.find(collectionResourceType);
313 // if groupSyncResourceHandleList has resourceType,
314 // this app created collection resource handle.
315 if (handleIt != groupSyncResourceHandleList.end())
317 handleIt = collectionResourceHandleList.find(collectionResourceType);
318 if (handleIt == collectionResourceHandleList.end())
320 cout << "GroupSynchronization::leaveGroup : "
321 << "Error! There is no collection resource handle to leave." << endl;
322 return OC_STACK_INVALID_PARAM;
325 collectionResHandle = handleIt->second;
326 // cout << "GroupSynchronization::leaveGroup : collection handle uri - "
327 // << OCGetResourceUri(collectionResHandle) << endl;
329 OCStackResult result = OCPlatform::unbindResource(collectionResHandle,
331 if (OC_STACK_OK == result)
333 cout << "GroupSynchronization::leaveGroup : "
334 << "To unbind resource was successful." << endl;
338 cout << "GroupSynchronization::leaveGroup : "
339 << "To unbind resource was unsuccessful. result - " << result << endl;
342 auto It = std::find(deviceResourceHandleList.begin(),
343 deviceResourceHandleList.end(), resourceHandle);
344 if (It == deviceResourceHandleList.end()) // there is no resource handle to find
346 result = OCPlatform::unregisterResource(resourceHandle);
347 if (OC_STACK_OK == result)
349 cout << "GroupSynchronization::leaveGroup : "
350 << "To unregister resource was successful." << endl;
354 cout << "GroupSynchronization::leaveGroup : "
355 << "To unregister resource was unsuccessful. result - " << result
361 cout << "GroupSynchronization::leaveGroup : "
362 << "This resource cannot be unregistered." << endl;
363 deviceResourceHandleList.erase(It);
366 auto handleListIt = childResourceHandleList.find(collectionResHandle);
367 if (handleListIt == childResourceHandleList.end())
369 cout << "GroupSynchronization::leaveGroup : "
370 << "Error! There is no child resource list to delete." << endl;
371 return OC_STACK_INVALID_PARAM;
374 std::vector< OCResourceHandle > childList = handleListIt->second;
375 auto childIt = std::find(childList.begin(), childList.end(), resourceHandle);
376 if (childIt != childList.end())
378 cout << "GroupSynchronization::groupEntityHandler : "
379 << "Found! The resource to leave is found." << endl;
380 childList.erase(childIt);
383 childResourceHandleList[collectionResHandle] = childList;
387 else // requesting to unbind this resourceHandle to the remote collection resource
389 auto resourceIt = groupSyncResourceList.find(collectionResourceType);
391 if (resourceIt == groupSyncResourceList.end())
393 cout << "GroupSynchronization::leaveGroup : "
394 << "Error! There is no collectin resource type to leave." << endl;
395 return OC_STACK_INVALID_PARAM;
398 std::shared_ptr< OCResource > resource = resourceIt->second;
399 // cout << "GroupSynchronization::leaveGroup : group sync resource uri - "
400 // << resource->uri() << endl;
402 handleIt = collectionResourceHandleList.find(collectionResourceType);
403 if (handleIt == collectionResourceHandleList.end())
405 cout << "GroupSynchronization::leaveGroup : "
406 << "Error! There is no collection resource handle to leave." << endl;
407 return OC_STACK_INVALID_PARAM;
410 collectionResHandle = handleIt->second;
412 // making representation to leave group
413 std::string method = "leaveGroup";
414 std::string type = OCGetResourceTypeName(collectionResHandle, 0);
415 std::string resourceType;
416 resourceType.append(OCGetResourceTypeName(resourceHandle, 0));
418 OCRepresentation rep;
419 rep.setValue("method", method);
420 rep.setValue("collectionResourceType", type);
421 rep.setValue("resourceType", resourceType);
423 cout << "\tmethod - " << method << endl;
424 cout << "\tcollectionResourceType - " << type << endl;
425 cout << "\tresourceType - " << resourceType << endl;
427 QueryParamsMap queryParamsMap;
429 // request to leave group to the remote group sync resource
430 OCStackResult result = resource->put(rep, queryParamsMap,
431 std::bind(&GroupSynchronization::onLeaveGroup, this, std::placeholders::_1,
432 std::placeholders::_2, std::placeholders::_3));
433 if (OC_STACK_OK == result)
435 cout << "GroupSynchronization::leaveGroup : "
436 << "groupSyncResource->put was successful." << endl;
440 cout << "GroupSynchronization::leaveGroup : "
441 << "groupSyncResource->put was unsuccessful. result - " << result
445 // deleting all remote resources. These are copied in onGetJoinedRemoteChild()
446 deleteGroup(collectionResourceType);
451 cout << "GroupSynchronization::leaveGroup : Error! Input params are wrong." << endl;
452 return OC_STACK_INVALID_PARAM;
458 void GroupSynchronization::deleteGroup(std::string collectionResourceType)
460 if (0 != collectionResourceType.length())
462 cout << "GroupSynchronization::deleteGroup" << endl;
464 OCStackResult result;
466 auto handleIt = collectionResourceHandleList.find(collectionResourceType);
467 if (handleIt == collectionResourceHandleList.end())
469 cout << "GroupSynchronization::deleteGroup : "
470 << "Error! There is no collection resource handle to delete." << endl;
473 OCResourceHandle collectionResHandle = handleIt->second;
475 collectionResourceHandleList.erase(handleIt);
477 auto handleListIt = childResourceHandleList.find(collectionResHandle);
478 if (handleListIt == childResourceHandleList.end())
480 cout << "GroupSynchronization::deleteGroup : "
481 << "Error! There is no child resource list to delete." << endl;
484 std::vector< OCResourceHandle > childList = handleListIt->second;
486 childResourceHandleList.erase(handleListIt);
488 result = OCPlatform::unbindResources(collectionResHandle, childList);
489 if (OC_STACK_OK == result)
491 cout << "GroupSynchronization::deleteGroup : "
492 << "To unbind resources was successful." << endl;
496 cout << "GroupSynchronization::deleteGroup : "
497 << "To unbind resources was unsuccessful. result - " << result << endl;
500 result = OCPlatform::unregisterResource(collectionResHandle);
501 if (result != OC_STACK_OK)
503 cout << "GroupSynchronization::deleteGroup : "
504 << "To unregister collection resource handle was successful." << endl;
508 cout << "GroupSynchronization::deleteGroup : "
509 << " To unregister collection resource handle was unsuccessful. result - "
513 OCResourceHandle resourceHandle;
514 std::vector< OCResourceHandle >::iterator It;
516 for (unsigned int i = 0; i < childList.size(); i++)
518 resourceHandle = childList.at(i);
520 It = std::find(deviceResourceHandleList.begin(), deviceResourceHandleList.end(),
522 if (It != deviceResourceHandleList.end()) // find !!
524 deviceResourceHandleList.erase(It);
528 result = OCPlatform::unregisterResource(resourceHandle);
529 if (OC_STACK_OK == result)
531 cout << "GroupSynchronization::deleteGroup : UnregisterResource(" << i + 1
532 << ") was successful." << endl;
536 cout << "GroupSynchronization::deleteGroup : UnregisterResource(" << i + 1
537 << ") was unsuccessful. result - " << result << endl;
542 handleIt = groupSyncResourceHandleList.find(collectionResourceType);
544 // if groupSyncResourceHandleList has resourceType,
545 // group sync of this app created collection resource.
546 if (handleIt != groupSyncResourceHandleList.end())
548 resourceHandle = handleIt->second; // group sync resource handle
549 result = OCPlatform::unregisterResource(resourceHandle);
550 if (OC_STACK_OK == result)
552 cout << "GroupSynchronization::deleteGroup : "
553 << "To unregister group sync resource handle was successful." << endl;
557 cout << "GroupSynchronization::deleteGroup : "
558 << "To unregister group sync resource handle was unsuccessful. "
559 << "result - " << result << endl;
562 groupSyncResourceHandleList.erase(handleIt);
565 auto resourceIt = groupSyncResourceList.find(collectionResourceType);
566 if (resourceIt != groupSyncResourceList.end())
568 groupSyncResourceList.erase(resourceIt);
575 cout << "GroupSynchronization::deleteGroup : Error! Input params are wrong." << endl;
579 std::map< std::string, OCResourceHandle > GroupSynchronization::getGroupList()
581 return collectionResourceHandleList;
584 OCEntityHandlerResult GroupSynchronization::groupEntityHandler(
585 const std::shared_ptr< OCResourceRequest > request)
587 cout << "GroupSynchronization::groupEntityHandler\n";
591 // Get the request type and request flag
592 std::string requestType = request->getRequestType();
593 int requestFlag = request->getRequestHandlerFlag();
595 if (requestFlag == RequestHandlerFlag::InitFlag)
597 cout << "\trequestFlag : Init\n";
599 // entity handler to perform resource initialization operations
601 else if (requestFlag == RequestHandlerFlag::RequestFlag)
603 cout << "\trequestFlag : Request\n";
605 // If the request type is GET
606 if (requestType == "GET")
608 cout << "\t\trequestType : GET\n";
610 else if (requestType == "PUT")
612 cout << "\t\trequestType : PUT\n";
614 //get method name, group resource type and resource type to join group
615 OCRepresentation rp = request->getResourceRepresentation();
616 std::string methodType = rp.getValue< std::string >("method");
617 std::string collectionResourceType = rp.getValue< std::string >(
618 "collectionResourceType");
619 std::string resourceType = rp.getValue< std::string >("resourceType");
621 cout << "\t\t\tmethod : " << methodType << endl;
622 cout << "\t\t\tcollection resourceType : " << collectionResourceType << endl;
623 cout << "\t\t\tresourceType : " << resourceType << endl;
625 auto handleIt = collectionResourceHandleList.find(collectionResourceType);
626 if (handleIt == collectionResourceHandleList.end())
628 cout << "GroupSynchronization::groupEntityHandler : "
629 << "Error! There is no collection resource handle to delete."
633 collectionResourceHandle = handleIt->second;
634 // in case of join group it is used in onFindResource()
636 if (methodType == "joinGroup")
638 std::string resourceName = "coap://224.0.1.187/oc/core?rt=";
639 resourceName += resourceType;
640 cout << "\t\t\tresourceName : " << resourceName << endl;
642 resourceRequest = request;
644 OCPlatform::findResource("", resourceName,
645 std::bind(&GroupSynchronization::onFindResource, this,
646 std::placeholders::_1));
648 else if (methodType == "leaveGroup")
650 auto it = childResourceHandleList.find(collectionResourceHandle);
651 if (it == childResourceHandleList.end())
653 cout << "GroupSynchronization::groupEntityHandler : "
654 << "Error! There is no child resource list." << endl;
658 std::vector< OCResourceHandle > childList = it->second;
659 OCResourceHandle resourceHandle;
660 for (auto childIt = childList.begin(); childIt != childList.end();)
662 resourceHandle = (*childIt);
663 char* type = (char*) OCGetResourceTypeName(resourceHandle, 0);
665 if (0 == resourceType.compare(type))
667 cout << "GroupSynchronization::groupEntityHandler : "
668 << "Found! The resource to leave is found. - " << type
671 childIt = childList.erase(childIt++);
673 OCStackResult result = OCPlatform::unbindResource(
674 collectionResourceHandle, resourceHandle);
675 if (OC_STACK_OK == result)
677 cout << "GroupSynchronization::groupEntityHandler : "
678 << "To unbind resource was successful." << endl;
682 cout << "GroupSynchronization::groupEntityHandler : "
683 << "To unbind resource was unsuccessful. result - "
687 result = OCPlatform::unregisterResource(resourceHandle);
688 if (OC_STACK_OK == result)
690 cout << "GroupSynchronization::groupEntityHandler : "
691 << "To unregister resource was successful." << endl;
695 cout << "GroupSynchronization::groupEntityHandler : "
696 << "To unregister resource was unsuccessful. result - "
709 childResourceHandleList[collectionResourceHandle] = childList;
713 auto pResponse = std::make_shared< OC::OCResourceResponse >();
714 pResponse->setRequestHandle(request->getRequestHandle());
715 pResponse->setResourceHandle(request->getResourceHandle());
716 pResponse->setErrorCode(200);
717 pResponse->setResponseResult(OC_EH_OK);
719 OCRepresentation rep = request->getResourceRepresentation();
720 pResponse->setResourceRepresentation(rep, DEFAULT_INTERFACE);
721 if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
723 cout << "GroupSynchronization::groupEntityHandler : "
724 << "sendResponse is successful." << endl;
728 if (methodType != "") //TODO: Check groupmethodtype NULL
732 else if (requestType == "POST")
734 // POST request operations
736 else if (requestType == "DELETE")
738 // DELETE request operations
741 else if (requestFlag == RequestHandlerFlag::ObserverFlag)
743 cout << "\trequestFlag : Observer\n";
748 std::cout << "Request invalid" << std::endl;
754 void GroupSynchronization::onFindGroup(std::shared_ptr< OCResource > resource)
756 cout << "GroupSynchronization::onFindGroup" << endl;
762 //////////////////////////////////////////////////////////////////////////////////////////////////
763 ////////////debugging
764 std::string resourceURI;
765 std::string hostAddress;
767 // Get the resource URI
768 resourceURI = resource->uri();
769 cout << "\tURI of the resource: " << resourceURI << endl;
771 // Get the resource host address
772 hostAddress = resource->host();
773 cout << "\tHost address of the resource: " << hostAddress << endl;
775 hostAddress.append(resourceURI);
777 // Get the resource types
778 cout << "\tList of resource types: " << endl;
780 for (auto &resourceTypes : resource->getResourceTypes())
782 cout << "\t\t" << resourceTypes << endl;
785 // Get the resource interfaces
786 cout << "\tList of resource interfaces: " << endl;
787 for (auto &resourceInterfaces : resource->getResourceInterfaces())
789 cout << "\t\t" << resourceInterfaces << endl;
791 //////////////////////////////////////////////////////////////////////////////////////////////////
793 if (false == IsSameGroup(resource))
796 findCallback(resource);
801 // Resource is invalid
802 cout << "Resource is invalid" << endl;
807 catch (std::exception& e)
813 void GroupSynchronization::checkFindGroup(void)
815 cout << "GroupSynchronization::checkFindGroup" << endl;
817 for (int i = 0; i < 15; i++)
819 std::chrono::milliseconds workTime(300);
820 std::this_thread::sleep_for(workTime);
822 std::lock_guard < std::mutex > guard(foundGroupMutex);
824 if (false == foundGroupResourceList.empty())
826 cout << "GroupSynchronization::checkFoundGroup : " << "Some group is received."
832 cout << "GroupSynchronization::checkFoundGroup : "
833 << "It is failed to find resource within 3s." << endl;
839 bool GroupSynchronization::IsSameGroup(std::shared_ptr< OCResource > resource)
841 std::lock_guard < std::mutex > guard(foundGroupMutex);
843 if (true == foundGroupResourceList.empty())
845 cout << "GroupSynchronization::IsSameGroup : There is no found group." << endl;
849 std::string foundHostAddress, savedHostAddress;
850 foundHostAddress = resource->host();
851 // foundHostAddress.append (resource->uri());
853 for (unsigned int i = 0; i < foundGroupResourceList.size(); ++i)
855 savedHostAddress = (foundGroupResourceList.at(i))->host();
856 // savedHostAddress.append ((foundGroupResourceList.at(i))->uri());
857 // cout << "GroupSynchronization::IsSameGroup : foundHostAddress - " << foundHostAddress
858 // << ", savedHostAddress - " << savedHostAddress << endl;
860 if (0 == foundHostAddress.compare(savedHostAddress.c_str()))
862 cout << "GroupSynchronization::IsSameGroup : Found! The same group is found."
868 cout << "GroupSynchronization::IsSameGroup : There is no same group." << endl;
872 void GroupSynchronization::saveGroup(std::shared_ptr< OCResource > resource)
874 cout << "GroupSynchronization::saveGroup" << endl;
876 std::lock_guard < std::mutex > guard(foundGroupMutex);
878 foundGroupResourceList.push_back(resource);
881 void GroupSynchronization::onJoinGroup(const HeaderOptions& headerOptions,
882 const OCRepresentation& rep, const int eCode)
884 if (eCode == OC_STACK_OK)
886 cout << "GroupSynchronization::onJoinGroup : " << endl;
888 if (remoteCollectionResource)
890 std::string resourceInterface = DEFAULT_INTERFACE;
891 QueryParamsMap queryParamsMap;
893 OCStackResult result = remoteCollectionResource->get("", resourceInterface,
895 std::bind(&GroupSynchronization::onGetJoinedRemoteChild, this,
896 std::placeholders::_1, std::placeholders::_2,
897 std::placeholders::_3));
898 if (OC_STACK_OK == result)
900 cout << "GroupSynchronization::onJoinGroup : "
901 << "remoteCollectionResource->get was successful." << endl;
905 cout << "GroupSynchronization::onJoinGroup : "
906 << "remoteCollectionResource->get was unsuccessful. result - " << result
913 cout << "GroupSynchronization::onJoinGroup : error - " << eCode << endl;
917 void GroupSynchronization::onFindResource(std::shared_ptr< OCResource > resource)
919 cout << "GroupSynchronization::onFindResource" << endl;
923 //////////////////////////////////////////////////////////////////////////////////////////////////
925 std::string resourceURI;
926 std::string hostAddress;
928 // Get the resource URI
929 resourceURI = resource->uri();
930 cout << "\tURI of the resource: " << resourceURI << endl;
932 // Get the resource host address
933 hostAddress = resource->host();
934 cout << "\tHost address of the resource: " << hostAddress << endl;
936 hostAddress.append(resourceURI);
938 // Get the resource types
939 cout << "\tList of resource types: " << endl;
941 for (auto &resourceTypes : resource->getResourceTypes())
943 cout << "\t\t" << resourceTypes << endl;
946 // Get the resource interfaces
947 cout << "\tList of resource interfaces: " << endl;
948 for (auto &resourceInterfaces : resource->getResourceInterfaces())
950 cout << "\t\t" << resourceInterfaces << endl;
952 //////////////////////////////////////////////////////////////////////////////////////////////////
954 OCResourceHandle resourceHandle;
955 OCStackResult result = OCPlatform::registerResource(resourceHandle, resource);
956 if (result != OC_STACK_OK)
958 cout << "GroupSynchronization::"
959 << "onFindResource - Resource to join creation was unsuccessful. result - "
963 // cout << "GroupSynchronization::onFindResource : creating resourceHandle. resource type - "
964 // << OCGetResourceTypeName(resourceHandle, 0) << endl;
966 result = OCPlatform::bindResource(collectionResourceHandle, resourceHandle);
967 if (result != OC_STACK_OK)
969 cout << "GroupSynchronization::onFindResource : "
970 << "To bind resource was unsuccessful. result - " << result << endl;
973 cout << "GroupSynchronization::onFindResource : "
974 << "To bind joinGroupHandle and resourceHandle was successful." << endl;
976 auto it = childResourceHandleList.find(collectionResourceHandle);
977 std::vector< OCResourceHandle > childHandleList;
978 if (it != childResourceHandleList.end())
980 childHandleList = it->second;
983 childHandleList.push_back(resourceHandle);
984 childResourceHandleList[collectionResourceHandle] = childHandleList;
986 auto pResponse = std::make_shared< OC::OCResourceResponse >();
987 pResponse->setRequestHandle(resourceRequest->getRequestHandle());
988 pResponse->setResourceHandle(resourceRequest->getResourceHandle());
989 pResponse->setErrorCode(200);
990 pResponse->setResponseResult(OC_EH_OK);
992 OCRepresentation rep = resourceRequest->getResourceRepresentation();
993 pResponse->setResourceRepresentation(rep, DEFAULT_INTERFACE);
994 if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
996 cout << "GroupSynchronization::onFindResource : sendResponse is successful."
1002 cout << "GroupSynchronization::onFindResource : "
1003 << "Resource is invalid. So a new Group Resource has to be created." << endl;
1009 void GroupSynchronization::onGetJoinedRemoteChild(const HeaderOptions& headerOptions,
1010 const OCRepresentation& rep, const int eCode)
1012 if (eCode == OC_STACK_OK)
1014 cout << "GroupSynchronization::onGetJoinedRemoteChild" << endl;
1016 //////////////////////////////////////////////////////////////////////////////////////////////////
1017 ////////// debugging
1018 std::string resourceURI;
1020 // Get the resource URI
1021 resourceURI = rep.getUri();
1022 cout << "\tURI of the resource: " << resourceURI << endl;
1024 // Get the resource types
1025 cout << "\tList of resource types: " << endl;
1027 for (auto &resourceTypes : rep.getResourceTypes())
1029 cout << "\t\t" << resourceTypes << endl;
1032 // Get the resource interfaces
1033 cout << "\tList of resource interfaces: " << endl;
1034 for (auto &resourceInterfaces : rep.getResourceInterfaces())
1036 cout << "\t\t" << resourceInterfaces << endl;
1039 std::vector< OCRepresentation > childList = rep.getChildren();
1040 OCRepresentation child;
1041 for (unsigned int i = 0; i < childList.size(); ++i)
1043 cout << "\n\tchild resource - " << i + 1 << endl;
1045 child = childList.at(i);
1046 resourceURI = child.getUri();
1047 cout << "\t\tURI of the resource: " << resourceURI << endl;
1049 cout << "\t\tList of resource types: " << endl;
1050 for (auto &types : child.getResourceTypes())
1052 cout << "\t\t\t" << types << endl;
1055 cout << "\tList of resource interfaces: " << endl;
1056 for (auto &interfaces : child.getResourceInterfaces())
1058 cout << "\t\t\t" << interfaces << endl;
1061 //////////////////////////////////////////////////////////////////////////////////////////////////
1063 // creating remote collection resource handle
1064 OCResourceHandle remoteCollectionResourceHandle;
1065 resourceURI = remoteCollectionResource->uri();
1066 std::vector< std::string > types = remoteCollectionResource->getResourceTypes();
1067 std::vector< std::string > interfaces =
1068 remoteCollectionResource->getResourceInterfaces();
1070 OCStackResult result = OCPlatform::registerResource(remoteCollectionResourceHandle,
1071 resourceURI, types[0], interfaces[0], NULL, OC_OBSERVABLE);
1072 if (result != OC_STACK_OK)
1074 cout << "GroupSynchronization::onGetJoinedRemoteChild - "
1075 << "To register remoteCollectionResourceHandle"
1076 << " was unsuccessful. result - " << result << endl;
1079 cout << "GroupSynchronization::onGetJoinedRemoteChild : "
1080 "To register remoteCollectionResourceHandle was successful." << endl;
1082 // binding remote collection resource handle and resource handle to join
1083 collectionResourceHandleList[types[0]] = remoteCollectionResourceHandle;
1085 result = OCPlatform::bindResource(remoteCollectionResourceHandle, deviceResourceHandle);
1086 if (OC_STACK_OK == result)
1088 cout << "GroupSynchronization::onGetJoinedRemoteChild : "
1089 << "binding remoteCollectionResourceHandle and deviceResourceHandle"
1094 cout << "GroupSynchronization::onGetJoinedRemoteChild - "
1095 << "To bind remoteCollectionResourceHandle and deviceResourceHandle "
1096 << "was unsuccessful. result - " << result << endl;
1099 std::vector< OCResourceHandle > childHandleList;
1100 childHandleList.push_back(deviceResourceHandle);
1101 deviceResourceHandleList.push_back(deviceResourceHandle);
1103 // binding copied remote collection resource handle and copied remote resource
1104 OCResourceHandle resourceHandle;
1105 for (unsigned int i = 0; i < childList.size(); ++i)
1107 cout << "\tremote resource - " << i + 1 << endl;
1109 child = childList.at(i);
1110 resourceURI = child.getUri();
1111 types = child.getResourceTypes();
1112 interfaces = child.getResourceInterfaces();
1114 if (0 == types[0].compare(OCGetResourceTypeName(deviceResourceHandle, 0)))
1116 cout << "GroupSynchronization::onGetJoinedRemoteChild : " << types[0]
1117 << " is bind already." << endl;
1121 result = OCPlatform::registerResource(resourceHandle, resourceURI, types[0],
1122 interfaces[0], NULL, OC_OBSERVABLE);
1123 if (OC_STACK_OK == result)
1125 result = OCPlatform::bindResource(remoteCollectionResourceHandle,
1127 if (result != OC_STACK_OK)
1129 cout << "GroupSynchronization::onGetJoinedRemoteChild - "
1130 << "binding remoteCollectionResourceHandle and resourceHandle "
1131 << "was unsuccessful. result - " << result << endl;
1132 OCPlatform::unregisterResource(resourceHandle);
1135 childHandleList.push_back(resourceHandle);
1136 cout << "GroupSynchronization::onGetJoinedRemoteChild : "
1137 << "binding remoteCollectionResourceHandle and resourceHandle" << endl;
1141 cout << "GroupSynchronization::onGetJoinedRemoteChild - "
1142 << "To register remoteCollectionResourceHandle was unsuccessful."
1143 << " result - " << result << endl;
1147 childResourceHandleList[remoteCollectionResourceHandle] = childHandleList;
1148 // this handle list is used to leave group
1152 cout << "GroupSynchronization::onGetJoinedRemoteChild : error - " << eCode << endl;
1158 void GroupSynchronization::onLeaveGroup(const HeaderOptions& headerOptions,
1159 const OCRepresentation& rep, const int eCode)
1161 if (eCode == OC_STACK_OK)
1163 cout << "GroupSynchronization::onLeaveGroup" << endl;
1167 cout << "GroupSynchronization::onLeaveGroup : error - " << eCode << endl;
1172 void GroupSynchronization::debugGroupSync(void)
1174 cout << "GroupSynchronization::debugGroupSync" << endl;
1177 std::map< std::string, OCResourceHandle >::iterator handleIt;
1178 std::map< OCResourceHandle, std::vector< OCResourceHandle > >::iterator childIt;
1180 OCResourceHandle resourceHandle;
1181 std::vector< OCResourceHandle > handleList;
1182 std::shared_ptr< OCResource > resource;
1184 cout << "Resource Handle Created by App" << endl;
1185 for (i = 0; i < deviceResourceHandleList.size(); i++)
1187 resourceHandle = deviceResourceHandleList.at(i);
1189 cout << i + 1 << ". details" << endl;
1190 cout << " uri - " << OCGetResourceUri(resourceHandle) << endl;
1191 cout << " resource type - " << OCGetResourceTypeName(resourceHandle, 0) << endl;
1192 cout << " resource interface - " << OCGetResourceInterfaceName(resourceHandle, 0)
1196 cout << "\nGroup Sync Resource Handle List. The number is "
1197 << groupSyncResourceHandleList.size() << endl;
1199 for (handleIt = groupSyncResourceHandleList.begin();
1200 handleIt != groupSyncResourceHandleList.end(); ++handleIt)
1202 type = handleIt->first;
1203 cout << "\t" << i << ". group sync resource type - " << type << endl;
1204 cout << "\t details" << endl;
1206 resourceHandle = handleIt->second;
1207 cout << "\t uri - " << OCGetResourceUri(resourceHandle) << endl;
1208 cout << "\t resource type - " << OCGetResourceTypeName(resourceHandle, 0) << endl;
1209 cout << "\t resource interface - " << OCGetResourceInterfaceName(resourceHandle, 0)
1215 cout << "Copied Remote Group Sync Resource List. The number is "
1216 << groupSyncResourceList.size() << endl;
1217 std::vector< std::string > list;
1219 for (auto resourceIt = groupSyncResourceList.begin();
1220 resourceIt != groupSyncResourceList.end(); ++resourceIt)
1222 type = resourceIt->first;
1223 cout << "\t" << i << ". group sync resource type - " << type << endl;
1224 cout << "\t details" << endl;
1226 resource = resourceIt->second;
1227 cout << "\t host - " << resource->host() << endl;
1228 cout << "\t uri - " << resource->uri() << endl;
1229 list = resource->getResourceTypes();
1230 cout << "\t resource type - " << list[0] << endl;
1231 list = resource->getResourceInterfaces();
1232 cout << "\t resource interface - " << list[0] << endl << endl;
1236 // cout << "The number of collection Resource Handle is " << collectionResourceHandleList.size()
1238 // cout << "The number of child resource handle list is " << childResourceHandleList.size()
1241 cout << "Collection Resource Handle List" << endl;
1243 for (handleIt = collectionResourceHandleList.begin();
1244 handleIt != collectionResourceHandleList.end(); ++handleIt)
1246 type = handleIt->first;
1247 cout << "\t" << i << ". collection resource type - " << type << endl;
1248 cout << "\t details" << endl;
1250 resourceHandle = handleIt->second;
1251 cout << "\t uri - " << OCGetResourceUri(resourceHandle) << endl;
1252 cout << "\t resource type - " << OCGetResourceTypeName(resourceHandle, 0) << endl;
1253 cout << "\t resource interface - " << OCGetResourceInterfaceName(resourceHandle, 0)
1256 childIt = childResourceHandleList.find(resourceHandle);
1257 if (childIt != childResourceHandleList.end())
1259 handleList = childIt->second;
1260 for (unsigned int j = 0; j < handleList.size(); j++)
1263 cout << "\t\t" << j + 1 << ". child resource details" << endl;
1265 resourceHandle = handleList.at(j);
1266 cout << "\t\t uri - " << OCGetResourceUri(resourceHandle) << endl;
1267 cout << "\t\t resource type - " << OCGetResourceTypeName(resourceHandle, 0)
1269 cout << "\t\t resource interface - "
1270 << OCGetResourceInterfaceName(resourceHandle, 0) << endl << endl;