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"
34 GroupSynchronization* GroupSynchronization::groupSyncnstance = NULL;
35 bool GroupSynchronization::bIsFinding = false;
37 GroupSynchronization* GroupSynchronization::getInstance()
39 if (groupSyncnstance == NULL)
41 groupSyncnstance = new GroupSynchronization();
43 return groupSyncnstance;
46 void GroupSynchronization::deleteInstance()
50 delete groupSyncnstance;
51 groupSyncnstance = NULL;
55 OCStackResult GroupSynchronization::findGroup(
56 std::vector< std::string > collectionResourceTypes, FindCallback callback)
58 cout << "GroupSynchronization::findGroup" << endl;
61 return OC_STACK_ERROR;
63 foundGroupResourceList.clear();
64 findCallback = callback;
66 if (findCallback == NULL)
68 return OC_STACK_ERROR;
71 for (unsigned int i = 0; i < collectionResourceTypes.size(); ++i)
74 std::string query = OC_WELL_KNOWN_QUERY;
76 query.append(collectionResourceTypes.at(i));
78 cout << query << endl;
79 cout << "GroupSynchronization::findGroup - " << query << endl;
81 OCPlatform::findResource("", query,
83 std::bind(&GroupSynchronization::onFindGroup, this, std::placeholders::_1));
85 OCPlatform::findResource("", query,
87 std::bind(&GroupSynchronization::onFindGroup, this, std::placeholders::_1));
92 // thread to check if GroupSynchronization::onFoundGroup is called or not.
93 std::thread t(std::bind(&GroupSynchronization::checkFindGroup, this));
99 OCStackResult GroupSynchronization::createGroup(std::string collectionResourceType)
101 foundGroupResourceList.clear();
103 OCResourceHandle collectionResHandle = NULL;
104 OCResourceHandle groupSyncResHandle = NULL;
106 if (0 == collectionResourceType.length())
108 cout << "GroupSynchronization::createGroup : Error! Input params are wrong." << endl;
109 return OC_STACK_INVALID_PARAM;
112 cout << "GroupSynchronization::createGroup - The created group is added." << endl;
114 OCStackResult result;
116 // creating master collection resource
117 std::string collectionUri = "/" + collectionResourceType;
119 while ((i = collectionUri.find(".")) != std::string::npos)
121 collectionUri.replace(i, 1, "/");
123 cout << "GroupSynchronization::createGroup : collection uri - " << collectionUri
124 << ", type - " << collectionResourceType << endl;
126 std::string resourceInterface = DEFAULT_INTERFACE;
128 result = OCPlatform::registerResource(collectionResHandle, collectionUri,
129 collectionResourceType, resourceInterface, NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
130 if (result != OC_STACK_OK)
132 cout << "To register resource (" << collectionUri << ") was unsuccessful. result - "
138 OCPlatform::bindInterfaceToResource(collectionResHandle, GROUP_INTERFACE);
139 if (result != OC_STACK_OK)
141 cout << "To bind Interface (collection) was unsuccessful. result - " << result
145 collectionResourceHandleList[collectionResourceType] = collectionResHandle;
147 // creating master group sync resource
148 std::string groupSyncUri = collectionUri + "/groupsync";
149 std::string groupSyncResType = collectionResourceType + ".groupsync";
151 // cout << "GroupSynchronization::createGroup : groupSync uri - " << groupSyncUri
152 // << ", type - " << collectionResourceType << endl;
154 result = OCPlatform::registerResource(groupSyncResHandle, groupSyncUri,
155 groupSyncResType, resourceInterface,
156 std::bind(&GroupSynchronization::groupEntityHandler, this,
157 std::placeholders::_1), OC_DISCOVERABLE | OC_OBSERVABLE);
158 if (result != OC_STACK_OK)
160 cout << "To register resource (groupsync) was unsuccessful. result - " << result
165 groupSyncResourceHandleList[collectionResourceType] = groupSyncResHandle;
171 if (collectionResHandle)
173 OCPlatform::unregisterResource(collectionResHandle);
174 auto iterator = collectionResourceHandleList.find(collectionResourceType);
175 if (iterator != collectionResourceHandleList.end())
177 collectionResourceHandleList.erase(iterator);
181 if (groupSyncResHandle)
183 OCPlatform::unregisterResource(groupSyncResHandle);
184 auto iterator = groupSyncResourceHandleList.find(collectionResourceType);
185 if (iterator != groupSyncResourceHandleList.end())
187 groupSyncResourceHandleList.erase(iterator);
191 return OC_STACK_NO_RESOURCE;
194 OCStackResult GroupSynchronization::joinGroup(std::string collectionResourceType,
195 OCResourceHandle resourceHandle)
197 if ((0 == collectionResourceType.length()) || (!resourceHandle))
199 cout << "GroupSynchronization::joinGroup : Error! input params are wrong." << endl;
200 return OC_STACK_INVALID_PARAM;
202 auto resIt = collectionResourceHandleList.find(collectionResourceType);
203 if (resIt == collectionResourceHandleList.end())
205 cout << "GroupSynchronization::joinGroup : error! There is no collection to join"
207 return OC_STACK_INVALID_PARAM;
210 OCResourceHandle collectionResHandle = resIt->second;
212 OCStackResult result = OCPlatform::bindResource(collectionResHandle, resourceHandle);
213 if (result != OC_STACK_OK)
215 cout << "GroupSynchronization::joinGroup : To bind resource was unsuccessful."
216 << "result - " << result << endl;
217 return OC_STACK_ERROR;
219 } catch(OCException &e) {
221 return OC_STACK_INVALID_PARAM;
224 cout << "GroupSynchronization::joinGroup : "
225 << "To bind collectionResHandle and resourceHandle" << endl;
227 std::vector< OCResourceHandle > childHandleList;
229 auto childIt = childResourceHandleList.find(collectionResHandle);
230 if (childIt != childResourceHandleList.end())
232 childHandleList = childIt->second;
235 childHandleList.push_back(resourceHandle);
236 childResourceHandleList[collectionResHandle] = childHandleList;
238 deviceResourceHandleList.push_back(resourceHandle);
245 OCStackResult GroupSynchronization::joinGroup(const std::shared_ptr< OCResource > resource,
246 OCResourceHandle resourceHandle)
248 if ((!resource) || (!resourceHandle))
250 cout << "GroupSynchronization::joinGroup : Error! Input params are wrong." << endl;
251 return OC_STACK_INVALID_PARAM;
254 cout << "GroupSynchronization::joinGroup" << endl;
256 // making representation to join group
257 std::string method = "joinGroup";
258 std::vector< std::string > type = resource->getResourceTypes();
259 std::string resourceType;
260 resourceType.append(OCGetResourceTypeName(resourceHandle, 0));
262 OCRepresentation rep;
263 rep.setValue("method", method);
264 rep.setValue("collectionResourceType", type[0]);
265 rep.setValue("resourceType", resourceType);
267 cout << "\tmethod - " << method << endl;
268 cout << "\tcollectionResourceType - " << type[0] << endl;
269 cout << "\tresourceType - " << resourceType << endl;
271 // creating group sync resource with the received collection resource.
272 // entity handler of group sync is used to join group.
273 std::string host = resource->host();
274 std::string uri = resource->uri() + "/groupsync";
276 std::vector< std::string > resourceTypes;
278 for (unsigned int i = 0; i < type.size(); ++i)
280 temp = type[0] + ".groupsync";
281 resourceTypes.push_back(temp);
284 std::vector< std::string > resourceInterface;
285 resourceInterface.push_back(DEFAULT_INTERFACE);
287 OCResource::Ptr groupSyncResource = OCPlatform::constructResourceObject(host, uri,
288 OC_ETHERNET, false, resourceTypes, resourceInterface);
290 // OCResource::Ptr groupSyncResource = OCPlatform::constructResourceObject(host, uri,
291 // OC_WIFI, false, resourceTypes, resourceInterface);
293 groupSyncResourceList[type[0]] = groupSyncResource;
295 cout << "GroupSynchronization::joinGroup : creating groupSyncResource." << endl;
297 // Create QueryParameters Map and add query params (if any)
298 QueryParamsMap queryParamsMap;
300 // request to join group to the remote group sync resource
301 OCStackResult result = groupSyncResource->put(rep, queryParamsMap,
302 std::bind(&GroupSynchronization::onJoinGroup, this, std::placeholders::_1,
303 std::placeholders::_2, std::placeholders::_3));
304 if (OC_STACK_OK == result)
306 cout << "GroupSynchronization::joinGroup : groupSyncResource->put was successful."
311 cout << "GroupSynchronization::joinGroup : "
312 << "groupSyncResource->put was unsuccessful. result - " << result << endl;
315 // saving the remote collection resource.
316 // It is used in onJoinGroup() and onGetJoinedRemoteChild().
317 remoteCollectionResource = resource;
319 // saving the resource handle to join. It is used in onGetJoinedRemoteChild()
320 deviceResourceHandle = resourceHandle;
325 OCStackResult GroupSynchronization::leaveGroup(std::string collectionResourceType,
326 OCResourceHandle resourceHandle)
328 if ((0 == collectionResourceType.length()) || (!resourceHandle))
330 cout << "GroupSynchronization::leaveGroup : Error! Input params are wrong." << endl;
331 return OC_STACK_INVALID_PARAM;
334 cout << "GroupSynchronization::leaveGroup : collectionResourceType - "
335 << collectionResourceType << endl;
337 OCResourceHandle collectionResHandle;
338 auto handleIt = groupSyncResourceHandleList.find(collectionResourceType);
340 // if groupSyncResourceHandleList has resourceType,
341 // this app created collection resource handle.
342 if (handleIt != groupSyncResourceHandleList.end())
344 handleIt = collectionResourceHandleList.find(collectionResourceType);
345 if (handleIt == collectionResourceHandleList.end())
347 cout << "GroupSynchronization::leaveGroup : "
348 << "Error! There is no collection resource handle to leave." << endl;
349 return OC_STACK_INVALID_PARAM;
352 collectionResHandle = handleIt->second;
353 // cout << "GroupSynchronization::leaveGroup : collection handle uri - "
354 // << OCGetResourceUri(collectionResHandle) << endl;
355 if(collectionResHandle == NULL)
356 return OC_STACK_INVALID_PARAM;
358 OCStackResult result;
361 result = OCPlatform::unbindResource(collectionResHandle, resourceHandle);
362 if (OC_STACK_OK == result)
364 cout << "GroupSynchronization::leaveGroup : "
365 << "To unbind resource was successful." << endl;
369 cout << "GroupSynchronization::leaveGroup : "
370 << "To unbind resource was unsuccessful. result - " << result << endl;
373 } catch(OCException &e) {
374 cout << "ERROR : " << e.reason() << endl;
375 return OC_STACK_NO_RESOURCE;
378 auto It = std::find(deviceResourceHandleList.begin(), deviceResourceHandleList.end(),
380 if (It == deviceResourceHandleList.end()) // there is no resource handle to find
384 result = OCPlatform::unregisterResource(resourceHandle);
385 if (OC_STACK_OK == result)
387 cout << "GroupSynchronization::leaveGroup : "
388 << "To unregister resource was successful." << endl;
392 cout << "GroupSynchronization::leaveGroup : "
393 << "To unregister resource was unsuccessful. result - " << result
397 } catch(OCException &e)
399 cout << "ERROR : " << e.reason() << endl;
400 return OC_STACK_NO_RESOURCE;
405 cout << "GroupSynchronization::leaveGroup : "
406 << "This resource cannot be unregistered." << endl;
407 deviceResourceHandleList.erase(It);
410 auto handleListIt = childResourceHandleList.find(collectionResHandle);
411 if (handleListIt == childResourceHandleList.end())
413 cout << "GroupSynchronization::leaveGroup : "
414 << "Error! There is no child resource list to delete." << endl;
415 return OC_STACK_INVALID_PARAM;
418 std::vector< OCResourceHandle > childList = handleListIt->second;
419 auto childIt = std::find(childList.begin(), childList.end(), resourceHandle);
420 if (childIt != childList.end())
422 cout << "GroupSynchronization::groupEntityHandler : "
423 << "Found! The resource to leave is found." << endl;
424 childList.erase(childIt);
427 childResourceHandleList[collectionResHandle] = childList;
435 OCStackResult GroupSynchronization::leaveGroup(
436 const std::shared_ptr<OCResource> resource,
437 std::string collectionResourceType, OCResourceHandle resourceHandle)
439 if ((!resource) || (!resourceHandle))
441 cout << "GroupSynchronization::joinGroup : Error! Input params are wrong." << endl;
442 return OC_STACK_INVALID_PARAM;
445 cout << "GroupSynchronization::joinGroup" << endl;
447 // making representation to join group
448 std::vector< std::string > type = resource->getResourceTypes();
449 std::string host = resource->host();
450 std::string uri = resource->uri() + "/groupsync";
452 std::vector< std::string > resourceTypes;
454 for (unsigned int i = 0; i < type.size(); ++i)
456 temp = type[0] + ".groupsync";
457 resourceTypes.push_back(temp);
460 std::vector< std::string > resourceInterface;
461 resourceInterface.push_back(DEFAULT_INTERFACE);
463 OCResource::Ptr groupSyncResource;
464 groupSyncResource = OCPlatform::constructResourceObject(host, uri,
465 OC_ETHERNET, false, resourceTypes, resourceInterface);
466 // groupSyncResource = OCPlatform::constructResourceObject(host, uri,
467 // OC_WIFI, false, resourceTypes, resourceInterface);
469 // making representation to leave group
470 std::string method = "leaveGroup";
471 // std::string type = OCGetResourceTypeName(collectionResourceType, 0);
472 std::string resourceType;
473 resourceType.append(OCGetResourceTypeName(resourceHandle, 0));
475 OCRepresentation rep;
476 rep.setValue("method", method);
477 rep.setValue("collectionResourceType", collectionResourceType);
478 rep.setValue("resourceType", resourceType);
480 cout << "\tmethod - " << method << endl;
481 cout << "\tcollectionResourceType - " << collectionResourceType << endl;
482 cout << "\tresourceType - " << resourceType << endl;
484 QueryParamsMap queryParamsMap;
486 // request to leave group to the remote group sync resource
487 OCStackResult result = groupSyncResource->put(rep, queryParamsMap,
488 std::bind(&GroupSynchronization::onLeaveGroup, this, std::placeholders::_1,
489 std::placeholders::_2, std::placeholders::_3));
490 if (OC_STACK_OK == result)
492 cout << "GroupSynchronization::leaveGroup : "
493 << "groupSyncResource->put was successful." << endl;
497 cout << "GroupSynchronization::leaveGroup : "
498 << "groupSyncResource->put was unsuccessful. result - " << result << endl;
503 void GroupSynchronization::deleteGroup(std::string collectionResourceType)
505 if (0 == collectionResourceType.length())
507 cout << "GroupSynchronization::deleteGroup : Error! Input params are wrong." << endl;
511 cout << "GroupSynchronization::deleteGroup" << endl;
513 OCStackResult result;
514 OCResourceHandle resourceHandle;
516 auto handleIt = groupSyncResourceHandleList.find(collectionResourceType);
518 // if groupSyncResourceHandleList has resourceType,
519 // group sync of this app created collection resource.
520 if (handleIt != groupSyncResourceHandleList.end())
522 resourceHandle = handleIt->second; // group sync resource handle
523 result = OCPlatform::unregisterResource(resourceHandle);
524 if (OC_STACK_OK == result)
526 cout << "GroupSynchronization::deleteGroup : "
527 << "To unregister group sync resource handle was successful." << endl;
531 cout << "GroupSynchronization::deleteGroup : "
532 << "To unregister group sync resource handle was unsuccessful. "
533 << "result - " << result << endl;
536 groupSyncResourceHandleList.erase(handleIt);
539 auto resourceIt = groupSyncResourceList.find(collectionResourceType);
540 if (resourceIt != groupSyncResourceList.end())
542 groupSyncResourceList.erase(resourceIt);
545 handleIt = collectionResourceHandleList.find(collectionResourceType);
546 if (handleIt == collectionResourceHandleList.end())
548 cout << "GroupSynchronization::deleteGroup : "
549 << "Error! There is no collection resource handle to delete." << endl;
552 OCResourceHandle collectionResHandle = handleIt->second;
554 collectionResourceHandleList.erase(handleIt);
556 auto handleListIt = childResourceHandleList.find(collectionResHandle);
557 if (handleListIt == childResourceHandleList.end())
559 cout << "GroupSynchronization::deleteGroup : "
560 << "There is no child resource list to delete." << endl;
562 result = OCPlatform::unregisterResource(collectionResHandle);
563 if (result == OC_STACK_OK)
565 cout << "GroupSynchronization::deleteGroup : "
566 << "To unregister collection resource handle was successful." << endl;
570 cout << "GroupSynchronization::deleteGroup : "
571 << " To unregister collection resource handle was unsuccessful. result - "
579 std::vector< OCResourceHandle > childList = handleListIt->second;
581 childResourceHandleList.erase(handleListIt);
583 result = OCPlatform::unbindResources(collectionResHandle, childList);
584 if (OC_STACK_OK == result)
586 cout << "GroupSynchronization::deleteGroup : " << "To unbind resources was successful."
591 cout << "GroupSynchronization::deleteGroup : "
592 << "To unbind resources was unsuccessful. result - " << result << endl;
595 result = OCPlatform::unregisterResource(collectionResHandle);
596 if (result == OC_STACK_OK)
598 cout << "GroupSynchronization::deleteGroup : "
599 << "To unregister collection resource handle was successful." << endl;
603 cout << "GroupSynchronization::deleteGroup : "
604 << " To unregister collection resource handle was unsuccessful. result - "
608 std::vector< OCResourceHandle >::iterator It;
610 for (unsigned int i = 0; i < childList.size(); i++)
612 resourceHandle = childList.at(i);
614 It = std::find(deviceResourceHandleList.begin(), deviceResourceHandleList.end(),
616 if (It != deviceResourceHandleList.end()) // find !!
618 deviceResourceHandleList.erase(It);
622 result = OCPlatform::unregisterResource(resourceHandle);
623 if (OC_STACK_OK == result)
625 cout << "GroupSynchronization::deleteGroup : UnregisterResource(" << i + 1
626 << ") was successful." << endl;
630 cout << "GroupSynchronization::deleteGroup : UnregisterResource(" << i + 1
631 << ") was unsuccessful. result - " << result << endl;
639 std::map< std::string, OCResourceHandle > GroupSynchronization::getGroupList()
641 return collectionResourceHandleList;
644 OCEntityHandlerResult GroupSynchronization::groupEntityHandler(
645 const std::shared_ptr< OCResourceRequest > request)
647 cout << "GroupSynchronization::groupEntityHandler\n";
651 // Get the request type and request flag
652 std::string requestType = request->getRequestType();
653 int requestFlag = request->getRequestHandlerFlag();
655 if (requestFlag == RequestHandlerFlag::InitFlag)
657 cout << "\trequestFlag : Init\n";
659 // entity handler to perform resource initialization operations
661 else if (requestFlag == RequestHandlerFlag::RequestFlag)
663 cout << "\trequestFlag : Request\n";
665 // If the request type is GET
666 if (requestType == "GET")
668 cout << "\t\trequestType : GET\n";
670 else if (requestType == "PUT")
672 cout << "\t\trequestType : PUT\n";
674 //get method name, group resource type and resource type to join group
675 OCRepresentation rp = request->getResourceRepresentation();
676 std::string methodType = rp.getValue< std::string >("method");
677 std::string collectionResourceType = rp.getValue< std::string >(
678 "collectionResourceType");
679 std::string resourceType = rp.getValue< std::string >("resourceType");
681 cout << "\t\t\tmethod : " << methodType << endl;
682 cout << "\t\t\tcollection resourceType : " << collectionResourceType << endl;
683 cout << "\t\t\tresourceType : " << resourceType << endl;
685 auto handleIt = collectionResourceHandleList.find(collectionResourceType);
686 if (handleIt == collectionResourceHandleList.end())
688 cout << "GroupSynchronization::groupEntityHandler : "
689 << "Error! There is no collection resource handle to delete."
693 collectionResourceHandle = handleIt->second;
694 // in case of join group it is used in onFindResource()
696 if (methodType == "joinGroup")
698 std::string resourceName = "coap://224.0.1.187/oc/core?rt=";
699 resourceName += resourceType;
700 cout << "\t\t\tresourceName : " << resourceName << endl;
702 resourceRequest = request;
704 OCPlatform::findResource("", resourceName,
706 std::bind(&GroupSynchronization::onFindResource, this,
707 std::placeholders::_1));
709 // OCPlatform::findResource("", resourceName,
711 // std::bind(&GroupSynchronization::onFindResource, this,
712 // std::placeholders::_1));
714 else if (methodType == "leaveGroup")
716 auto it = childResourceHandleList.find(collectionResourceHandle);
717 if (it == childResourceHandleList.end())
719 cout << "GroupSynchronization::groupEntityHandler : "
720 << "Error! There is no child resource list." << endl;
724 std::vector< OCResourceHandle > childList = it->second;
725 OCResourceHandle resourceHandle;
726 for (auto childIt = childList.begin(); childIt != childList.end();)
728 resourceHandle = (*childIt);
729 char* type = (char*) OCGetResourceTypeName(resourceHandle, 0);
731 if (0 == resourceType.compare(type))
733 cout << "GroupSynchronization::groupEntityHandler : "
734 << "Found! The resource to leave is found. - " << type
737 childIt = childList.erase(childIt++);
739 OCStackResult result = OCPlatform::unbindResource(
740 collectionResourceHandle, resourceHandle);
741 if (OC_STACK_OK == result)
743 cout << "GroupSynchronization::groupEntityHandler : "
744 << "To unbind resource was successful." << endl;
748 cout << "GroupSynchronization::groupEntityHandler : "
749 << "To unbind resource was unsuccessful. result - "
753 result = OCPlatform::unregisterResource(resourceHandle);
754 if (OC_STACK_OK == result)
756 cout << "GroupSynchronization::groupEntityHandler : "
757 << "To unregister resource was successful." << endl;
761 cout << "GroupSynchronization::groupEntityHandler : "
762 << "To unregister resource was unsuccessful. result - "
775 childResourceHandleList[collectionResourceHandle] = childList;
779 auto pResponse = std::make_shared< OC::OCResourceResponse >();
780 pResponse->setRequestHandle(request->getRequestHandle());
781 pResponse->setResourceHandle(request->getResourceHandle());
782 pResponse->setErrorCode(200);
783 pResponse->setResponseResult(OC_EH_OK);
785 OCRepresentation rep = request->getResourceRepresentation();
786 pResponse->setResourceRepresentation(rep, DEFAULT_INTERFACE);
787 if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
789 cout << "GroupSynchronization::groupEntityHandler : "
790 << "sendResponse is successful." << endl;
794 if (methodType != "") //TODO: Check groupmethodtype NULL
798 else if (requestType == "POST")
800 // POST request operations
802 else if (requestType == "DELETE")
804 // DELETE request operations
807 else if (requestFlag == RequestHandlerFlag::ObserverFlag)
809 cout << "\trequestFlag : Observer\n";
814 std::cout << "Request invalid" << std::endl;
820 void GroupSynchronization::onFindGroup(std::shared_ptr< OCResource > resource)
822 cout << "GroupSynchronization::onFindGroup" << endl;
829 std::string resourceURI;
830 std::string hostAddress;
832 // Get the resource URI
833 resourceURI = resource->uri();
834 cout << "\tURI of the resource: " << resourceURI << endl;
836 // Get the resource host address
837 hostAddress = resource->host();
838 cout << "\tHost address of the resource: " << hostAddress << endl;
840 hostAddress.append(resourceURI);
842 // Get the resource types
843 cout << "\tList of resource types: " << endl;
845 for (auto &resourceTypes : resource->getResourceTypes())
847 cout << "\t\t" << resourceTypes << endl;
850 // Get the resource interfaces
851 cout << "\tList of resource interfaces: " << endl;
852 for (auto &resourceInterfaces : resource->getResourceInterfaces())
854 cout << "\t\t" << resourceInterfaces << endl;
857 if (false == IsSameGroup(resource))
860 findCallback(resource);
865 // Resource is invalid
866 cout << "Resource is invalid" << endl;
872 catch (std::exception& e)
878 void GroupSynchronization::checkFindGroup(void)
880 cout << "GroupSynchronization::checkFindGroup" << endl;
882 for (int i = 0; i < 15; i++)
884 std::chrono::milliseconds workTime(300);
885 std::this_thread::sleep_for(workTime);
887 std::lock_guard < std::mutex > guard(foundGroupMutex);
889 if (false == foundGroupResourceList.empty())
891 cout << "GroupSynchronization::checkFoundGroup : " << "Some group is received."
897 cout << "GroupSynchronization::checkFoundGroup : "
898 << "It is failed to find resource within 3s." << endl;
904 bool GroupSynchronization::IsSameGroup(std::shared_ptr< OCResource > resource)
906 std::lock_guard < std::mutex > guard(foundGroupMutex);
908 if (true == foundGroupResourceList.empty())
910 cout << "GroupSynchronization::IsSameGroup : There is no found group." << endl;
914 std::string foundHostAddress, savedHostAddress;
915 foundHostAddress = resource->host();
916 // foundHostAddress.append (resource->uri());
918 for (unsigned int i = 0; i < foundGroupResourceList.size(); ++i)
920 savedHostAddress = (foundGroupResourceList.at(i))->host();
921 // savedHostAddress.append ((foundGroupResourceList.at(i))->uri());
922 // cout << "GroupSynchronization::IsSameGroup : foundHostAddress - " << foundHostAddress
923 // << ", savedHostAddress - " << savedHostAddress << endl;
925 if (0 == foundHostAddress.compare(savedHostAddress.c_str()))
927 cout << "GroupSynchronization::IsSameGroup : Found! The same group is found."
933 cout << "GroupSynchronization::IsSameGroup : There is no same group." << endl;
937 void GroupSynchronization::saveGroup(std::shared_ptr< OCResource > resource)
939 cout << "GroupSynchronization::saveGroup" << endl;
941 std::lock_guard < std::mutex > guard(foundGroupMutex);
943 foundGroupResourceList.push_back(resource);
946 void GroupSynchronization::onJoinGroup(const HeaderOptions& headerOptions,
947 const OCRepresentation& rep, const int eCode)
949 if (eCode != OC_STACK_OK)
951 cout << "GroupSynchronization::onJoinGroup : error - " << eCode << endl;
955 // cout << "GroupSynchronization::onJoinGroup : " << endl;
957 // if (remoteCollectionResource)
959 // std::string resourceInterface = DEFAULT_INTERFACE;
960 // QueryParamsMap queryParamsMap;
962 // OCStackResult result = remoteCollectionResource->get("", resourceInterface,
964 // std::bind(&GroupSynchronization::onGetJoinedRemoteChild, this,
965 // std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
966 // if (OC_STACK_OK == result)
968 // cout << "GroupSynchronization::onJoinGroup : "
969 // << "remoteCollectionResource->get was successful." << endl;
973 // cout << "GroupSynchronization::onJoinGroup : "
974 // << "remoteCollectionResource->get was unsuccessful. result - " << result
980 void GroupSynchronization::onFindResource(std::shared_ptr< OCResource > resource)
982 cout << "GroupSynchronization::onFindResource" << endl;
986 // start of debugging
987 std::string resourceURI;
988 std::string hostAddress;
990 // Get the resource URI
991 resourceURI = resource->uri();
992 cout << "\tURI of the resource: " << resourceURI << endl;
994 // Get the resource host address
995 hostAddress = resource->host();
996 cout << "\tHost address of the resource: " << hostAddress << endl;
998 hostAddress.append(resourceURI);
1000 // Get the resource types
1001 cout << "\tList of resource types: " << endl;
1003 for (auto &resourceTypes : resource->getResourceTypes())
1005 cout << "\t\t" << resourceTypes << endl;
1008 // Get the resource interfaces
1009 cout << "\tList of resource interfaces: " << endl;
1010 for (auto &resourceInterfaces : resource->getResourceInterfaces())
1012 cout << "\t\t" << resourceInterfaces << endl;
1016 OCResourceHandle resourceHandle;
1017 OCStackResult result = OCPlatform::registerResource(resourceHandle, resource);
1018 if (result != OC_STACK_OK)
1020 cout << "GroupSynchronization::"
1021 << "onFindResource - Resource to join creation was unsuccessful. result - "
1025 // cout << "GroupSynchronization::onFindResource : creating resourceHandle. resource type - "
1026 // << OCGetResourceTypeName(resourceHandle, 0) << endl;
1028 result = OCPlatform::bindResource(collectionResourceHandle, resourceHandle);
1029 if (result != OC_STACK_OK)
1031 cout << "GroupSynchronization::onFindResource : "
1032 << "To bind resource was unsuccessful. result - " << result << endl;
1036 cout << "GroupSynchronization::onFindResource : "
1037 << "To bind joinGroupHandle and resourceHandle was successful." << endl;
1039 auto it = childResourceHandleList.find(collectionResourceHandle);
1040 std::vector< OCResourceHandle > childHandleList;
1041 if (it != childResourceHandleList.end())
1043 childHandleList = it->second;
1046 childHandleList.push_back(resourceHandle);
1047 childResourceHandleList[collectionResourceHandle] = childHandleList;
1049 auto pResponse = std::make_shared< OC::OCResourceResponse >();
1050 pResponse->setRequestHandle(resourceRequest->getRequestHandle());
1051 pResponse->setResourceHandle(resourceRequest->getResourceHandle());
1052 pResponse->setErrorCode(200);
1053 pResponse->setResponseResult(OC_EH_OK);
1055 OCRepresentation rep = resourceRequest->getResourceRepresentation();
1056 pResponse->setResourceRepresentation(rep, DEFAULT_INTERFACE);
1057 if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
1059 cout << "GroupSynchronization::onFindResource : sendResponse is successful."
1065 cout << "GroupSynchronization::onFindResource : "
1066 << "Resource is invalid. So a new Group Resource has to be created." << endl;
1072 void GroupSynchronization::onGetJoinedRemoteChild(const HeaderOptions& headerOptions,
1073 const OCRepresentation& rep, const int eCode)
1075 if (eCode != OC_STACK_OK)
1077 cout << "GroupSynchronization::onGetJoinedRemoteChild : error - " << eCode << endl;
1081 cout << "GroupSynchronization::onGetJoinedRemoteChild" << endl;
1084 std::string resourceURI;
1086 // Get the resource URI
1087 resourceURI = rep.getUri();
1088 cout << "\tURI of the resource: " << resourceURI << endl;
1090 // Get the resource types
1091 cout << "\tList of resource types: " << endl;
1093 for (auto &resourceTypes : rep.getResourceTypes())
1095 cout << "\t\t" << resourceTypes << endl;
1098 // Get the resource interfaces
1099 cout << "\tList of resource interfaces: " << endl;
1100 for (auto &resourceInterfaces : rep.getResourceInterfaces())
1102 cout << "\t\t" << resourceInterfaces << endl;
1105 std::vector< OCRepresentation > childList = rep.getChildren();
1106 OCRepresentation child;
1107 for (unsigned int i = 0; i < childList.size(); ++i)
1109 cout << "\n\tchild resource - " << i + 1 << endl;
1111 child = childList.at(i);
1112 resourceURI = child.getUri();
1113 cout << "\t\tURI of the resource: " << resourceURI << endl;
1115 cout << "\t\tList of resource types: " << endl;
1116 for (auto &types : child.getResourceTypes())
1118 cout << "\t\t\t" << types << endl;
1121 cout << "\tList of resource interfaces: " << endl;
1122 for (auto &interfaces : child.getResourceInterfaces())
1124 cout << "\t\t\t" << interfaces << endl;
1128 // creating remote collection resource handle
1129 OCResourceHandle remoteCollectionResourceHandle;
1130 resourceURI = remoteCollectionResource->uri();
1131 std::vector< std::string > types = remoteCollectionResource->getResourceTypes();
1132 std::vector< std::string > interfaces = remoteCollectionResource->getResourceInterfaces();
1134 OCStackResult result = OCPlatform::registerResource(remoteCollectionResourceHandle,
1135 resourceURI, types[0], interfaces[0], NULL, OC_OBSERVABLE);
1136 if (result != OC_STACK_OK)
1138 cout << "GroupSynchronization::onGetJoinedRemoteChild - "
1139 << "To register remoteCollectionResourceHandle"
1140 << " was unsuccessful. result - " << result << endl;
1143 cout << "GroupSynchronization::onGetJoinedRemoteChild : "
1144 "To register remoteCollectionResourceHandle was successful." << endl;
1146 // binding remote collection resource handle and resource handle to join
1147 collectionResourceHandleList[types[0]] = remoteCollectionResourceHandle;
1149 result = OCPlatform::bindResource(remoteCollectionResourceHandle, deviceResourceHandle);
1150 if (OC_STACK_OK == result)
1152 cout << "GroupSynchronization::onGetJoinedRemoteChild : "
1153 << "binding remoteCollectionResourceHandle and deviceResourceHandle" << endl;
1157 cout << "GroupSynchronization::onGetJoinedRemoteChild - "
1158 << "To bind remoteCollectionResourceHandle and deviceResourceHandle "
1159 << "was unsuccessful. result - " << result << endl;
1162 std::vector< OCResourceHandle > childHandleList;
1163 childHandleList.push_back(deviceResourceHandle);
1164 deviceResourceHandleList.push_back(deviceResourceHandle);
1166 // binding copied remote collection resource handle and copied remote resource
1167 OCResourceHandle resourceHandle;
1168 for (unsigned int i = 0; i < childList.size(); ++i)
1170 cout << "\tremote resource - " << i + 1 << endl;
1172 child = childList.at(i);
1173 resourceURI = child.getUri();
1174 types = child.getResourceTypes();
1175 interfaces = child.getResourceInterfaces();
1177 if (0 == types[0].compare(OCGetResourceTypeName(deviceResourceHandle, 0)))
1179 cout << "GroupSynchronization::onGetJoinedRemoteChild : " << types[0]
1180 << " is bind already." << endl;
1184 result = OCPlatform::registerResource(resourceHandle, resourceURI, types[0],
1185 interfaces[0], NULL, OC_OBSERVABLE);
1186 if (OC_STACK_OK == result)
1188 result = OCPlatform::bindResource(remoteCollectionResourceHandle, resourceHandle);
1189 if (result != OC_STACK_OK)
1191 cout << "GroupSynchronization::onGetJoinedRemoteChild - "
1192 << "binding remoteCollectionResourceHandle and resourceHandle "
1193 << "was unsuccessful. result - " << result << endl;
1194 OCPlatform::unregisterResource(resourceHandle);
1197 childHandleList.push_back(resourceHandle);
1198 cout << "GroupSynchronization::onGetJoinedRemoteChild : "
1199 << "binding remoteCollectionResourceHandle and resourceHandle" << endl;
1203 cout << "GroupSynchronization::onGetJoinedRemoteChild - "
1204 << "To register remoteCollectionResourceHandle was unsuccessful."
1205 << " result - " << result << endl;
1209 childResourceHandleList[remoteCollectionResourceHandle] = childHandleList;
1210 // this handle list is used to leave group
1215 void GroupSynchronization::onLeaveGroup(const HeaderOptions& headerOptions,
1216 const OCRepresentation& rep, const int eCode)
1218 if (eCode != OC_STACK_OK)
1220 cout << "GroupSynchronization::onLeaveGroup : error - " << eCode << endl;
1224 cout << "GroupSynchronization::onLeaveGroup" << endl;
1228 void GroupSynchronization::debugGroupSync(void)
1230 cout << "GroupSynchronization::debugGroupSync" << endl;
1233 std::map< std::string, OCResourceHandle >::iterator handleIt;
1234 std::map< OCResourceHandle, std::vector< OCResourceHandle > >::iterator childIt;
1236 OCResourceHandle resourceHandle;
1237 std::vector< OCResourceHandle > handleList;
1238 std::shared_ptr< OCResource > resource;
1240 cout << "Resource Handle Created by App" << endl;
1241 for (i = 0; i < deviceResourceHandleList.size(); i++)
1243 resourceHandle = deviceResourceHandleList.at(i);
1245 cout << i + 1 << ". details" << endl;
1246 cout << " uri - " << OCGetResourceUri(resourceHandle) << endl;
1247 cout << " resource type - " << OCGetResourceTypeName(resourceHandle, 0) << endl;
1248 cout << " resource interface - " << OCGetResourceInterfaceName(resourceHandle, 0)
1252 cout << "\nGroup Sync Resource Handle List. The number is "
1253 << groupSyncResourceHandleList.size() << endl;
1255 for (handleIt = groupSyncResourceHandleList.begin();
1256 handleIt != groupSyncResourceHandleList.end(); ++handleIt)
1258 type = handleIt->first;
1259 cout << "\t" << i << ". group sync resource type - " << type << endl;
1260 cout << "\t details" << endl;
1262 resourceHandle = handleIt->second;
1263 cout << "\t uri - " << OCGetResourceUri(resourceHandle) << endl;
1264 cout << "\t resource type - " << OCGetResourceTypeName(resourceHandle, 0) << endl;
1265 cout << "\t resource interface - " << OCGetResourceInterfaceName(resourceHandle, 0)
1271 cout << "Copied Remote Group Sync Resource List. The number is "
1272 << groupSyncResourceList.size() << endl;
1273 std::vector< std::string > list;
1275 for (auto resourceIt = groupSyncResourceList.begin();
1276 resourceIt != groupSyncResourceList.end(); ++resourceIt)
1278 type = resourceIt->first;
1279 cout << "\t" << i << ". group sync resource type - " << type << endl;
1280 cout << "\t details" << endl;
1282 resource = resourceIt->second;
1283 cout << "\t host - " << resource->host() << endl;
1284 cout << "\t uri - " << resource->uri() << endl;
1285 list = resource->getResourceTypes();
1286 cout << "\t resource type - " << list[0] << endl;
1287 list = resource->getResourceInterfaces();
1288 cout << "\t resource interface - " << list[0] << endl << endl;
1292 // cout << "The number of collection Resource Handle is " << collectionResourceHandleList.size()
1294 // cout << "The number of child resource handle list is " << childResourceHandleList.size()
1297 cout << "Collection Resource Handle List" << endl;
1299 for (handleIt = collectionResourceHandleList.begin();
1300 handleIt != collectionResourceHandleList.end(); ++handleIt)
1302 type = handleIt->first;
1303 cout << "\t" << i << ". collection resource type - " << type << endl;
1304 cout << "\t details" << endl;
1306 resourceHandle = handleIt->second;
1307 cout << "\t uri - " << OCGetResourceUri(resourceHandle) << endl;
1308 cout << "\t resource type - " << OCGetResourceTypeName(resourceHandle, 0) << endl;
1309 cout << "\t resource interface - " << OCGetResourceInterfaceName(resourceHandle, 0)
1312 childIt = childResourceHandleList.find(resourceHandle);
1313 if (childIt != childResourceHandleList.end())
1315 handleList = childIt->second;
1316 for (unsigned int j = 0; j < handleList.size(); j++)
1319 cout << "\t\t" << j + 1 << ". child resource details" << endl;
1321 resourceHandle = handleList.at(j);
1322 cout << "\t\t uri - " << OCGetResourceUri(resourceHandle) << endl;
1323 cout << "\t\t resource type - " << OCGetResourceTypeName(resourceHandle, 0)
1325 cout << "\t\t resource interface - "
1326 << OCGetResourceInterfaceName(resourceHandle, 0) << endl << endl;