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 #include "GroupSynchronization.h"
27 #define TAG "GROUP_SYNC"
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 OC_LOG(DEBUG, TAG, "GroupSynchronization::findGroup");
59 OC_LOG(DEBUG, TAG, "It was searching already.");
60 return OC_STACK_ERROR;
63 std::lock_guard < std::mutex > guard(foundGroupMutex);
64 foundGroupResourceList.clear();
65 findCallback = callback;
67 if (findCallback == NULL)
69 OC_LOG(DEBUG, TAG, "Find callback is NULL.");
70 return OC_STACK_ERROR;
73 for (unsigned int i = 0; i < collectionResourceTypes.size(); ++i)
76 std::string query = OC_MULTICAST_DISCOVERY_URI;
78 query.append(collectionResourceTypes.at(i));
80 OCPlatform::findResource("", query,
82 std::bind(&GroupSynchronization::onFindGroup, this,
83 std::placeholders::_1));
88 // thread to check if GroupSynchronization::onFoundGroup is called or not.
89 std::thread t(std::bind(&GroupSynchronization::checkFindGroup, this));
95 OCStackResult GroupSynchronization::createGroup(std::string collectionResourceType)
97 std::lock_guard < std::mutex > guard(foundGroupMutex);
98 foundGroupResourceList.clear();
100 OCResourceHandle collectionResHandle = NULL;
101 OCResourceHandle groupSyncResHandle = NULL;
103 if (0 == collectionResourceType.length())
106 "GroupSynchronization::createGroup : Error! Input params are wrong.");
107 return OC_STACK_INVALID_PARAM;
110 OC_LOG(DEBUG, TAG, "GroupSynchronization::createGroup - The created group is added.");
112 OCStackResult result;
114 // creating master collection resource
115 std::string collectionUri = "/" + collectionResourceType;
117 while ((i = collectionUri.find(".")) != std::string::npos)
119 collectionUri.replace(i, 1, "/");
122 OC_LOG_V(DEBUG, TAG, "GroupSynchronization::createGroup : collection uri - %s, type - %s",
123 collectionUri.c_str(), collectionResourceType.c_str());
125 std::string resourceInterface = DEFAULT_INTERFACE;
127 result = OCPlatform::registerResource(collectionResHandle, collectionUri,
128 collectionResourceType, resourceInterface, NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
129 if (result != OC_STACK_OK)
131 OC_LOG_V(DEBUG, TAG, "To register resource (%s) was unsuccessful. result - %d",
132 collectionUri.c_str(), result);
137 OCPlatform::bindInterfaceToResource(collectionResHandle, GROUP_INTERFACE);
138 if (result != OC_STACK_OK)
141 "To bind Interface (collection) was unsuccessful. result - %d",
145 collectionResourceHandleList[collectionResourceType] = collectionResHandle;
147 // creating master group sync resource
148 std::string groupSyncUri = collectionUri + "/groupsync";
149 std::string groupSyncResType = collectionResourceType + ".groupsync";
151 result = OCPlatform::registerResource(groupSyncResHandle, groupSyncUri,
152 groupSyncResType, resourceInterface,
153 std::bind(&GroupSynchronization::groupEntityHandler, this,
154 std::placeholders::_1), OC_DISCOVERABLE | OC_OBSERVABLE);
155 if (result != OC_STACK_OK)
158 "To register resource (groupsync) was unsuccessful. result - %d", result);
162 groupSyncResourceHandleList[collectionResourceType] = groupSyncResHandle;
168 if (collectionResHandle)
170 OCPlatform::unregisterResource(collectionResHandle);
171 auto iterator = collectionResourceHandleList.find(collectionResourceType);
172 if (iterator != collectionResourceHandleList.end())
174 collectionResourceHandleList.erase(iterator);
178 if (groupSyncResHandle)
180 OCPlatform::unregisterResource(groupSyncResHandle);
181 auto iterator = groupSyncResourceHandleList.find(collectionResourceType);
182 if (iterator != groupSyncResourceHandleList.end())
184 groupSyncResourceHandleList.erase(iterator);
188 return OC_STACK_NO_RESOURCE;
191 OCStackResult GroupSynchronization::joinGroup(std::string collectionResourceType,
192 OCResourceHandle resourceHandle)
194 std::lock_guard < std::mutex > guard(foundGroupMutex);
195 if ((0 == collectionResourceType.length()) || (!resourceHandle))
198 "GroupSynchronization::joinGroup : Error! input params are wrong.");
199 return OC_STACK_INVALID_PARAM;
202 auto resIt = collectionResourceHandleList.find(collectionResourceType);
203 if (resIt == collectionResourceHandleList.end())
206 "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)
216 "GroupSynchronization::joinGroup : To bind resource was unsuccessful." \
217 " result - %d", result);
218 return OC_STACK_ERROR;
220 } catch(OCException &e) {
221 return OC_STACK_INVALID_PARAM;
225 "GroupSynchronization::joinGroup : To bind collectionResHandle and resourceHandle");
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 std::lock_guard < std::mutex > guard(foundGroupMutex);
249 if ((!resource) || (!resourceHandle))
251 OC_LOG(DEBUG, TAG, "GroupSynchronization::joinGroup : Error! Input params are wrong.");
252 return OC_STACK_INVALID_PARAM;
255 OC_LOG(DEBUG, TAG, "GroupSynchronization::joinGroup");
257 // making representation to join group
258 std::string method = "joinGroup";
259 std::vector< std::string > type = resource->getResourceTypes();
260 std::string resourceType;
261 resourceType.append(OCGetResourceTypeName(resourceHandle, 0));
263 OCRepresentation rep;
264 rep.setValue("method", method);
265 rep.setValue("collectionResourceType", type[0]);
266 rep.setValue("resourceType", resourceType);
268 OC_LOG_V(DEBUG, TAG, "\tmethod - %s", method.c_str());
269 OC_LOG_V(DEBUG, TAG, "\tcollectionResourceType - %s", type[0].c_str());
270 OC_LOG_V(DEBUG, TAG, "\tresourceType - %s", resourceType.c_str());
272 // creating group sync resource with the received collection resource.
273 // entity handler of group sync is used to join group.
274 std::string host = resource->host();
275 std::string uri = resource->uri() + "/groupsync";
277 std::vector< std::string > resourceTypes;
279 for (unsigned int i = 0; i < type.size(); ++i)
281 temp = type[0] + ".groupsync";
282 resourceTypes.push_back(temp);
285 std::vector< std::string > resourceInterface;
286 resourceInterface.push_back(DEFAULT_INTERFACE);
288 OCResource::Ptr groupSyncResource =
289 OCPlatform::constructResourceObject(host, uri,
292 resourceTypes, resourceInterface);
294 // OCResource::Ptr groupSyncResource = OCPlatform::constructResourceObject(host, uri,
295 // OC_WIFI, false, resourceTypes, resourceInterface);
297 groupSyncResourceList[type[0]] = groupSyncResource;
299 OC_LOG(DEBUG, TAG, "GroupSynchronization::joinGroup : creating groupSyncResource.");
301 // Create QueryParameters Map and add query params (if any)
302 QueryParamsMap queryParamsMap;
304 // request to join group to the remote group sync resource
305 OCStackResult result = groupSyncResource->put(rep, queryParamsMap,
306 std::bind(&GroupSynchronization::onJoinGroup, this, std::placeholders::_1,
307 std::placeholders::_2, std::placeholders::_3));
308 if (OC_STACK_OK == result)
311 "GroupSynchronization::joinGroup : groupSyncResource->put was successful.");
316 "GroupSynchronization::joinGroup : groupSyncResource->put was unsuccessful." \
317 "result - %d", result);
320 // saving the remote collection resource.
321 // It is used in onJoinGroup() and onGetJoinedRemoteChild().
322 remoteCollectionResource = resource;
324 // saving the resource handle to join. It is used in onGetJoinedRemoteChild()
325 deviceResourceHandle = resourceHandle;
330 OCStackResult GroupSynchronization::leaveGroup(std::string collectionResourceType,
331 OCResourceHandle resourceHandle)
333 if ((0 == collectionResourceType.length()) || (!resourceHandle))
336 "GroupSynchronization::leaveGroup : Error! Input params are wrong.");
337 return OC_STACK_INVALID_PARAM;
341 "GroupSynchronization::leaveGroup : collectionResourceType - %s",
342 collectionResourceType.c_str());
344 OCResourceHandle collectionResHandle;
345 auto handleIt = groupSyncResourceHandleList.find(collectionResourceType);
347 // if groupSyncResourceHandleList has resourceType,
348 // this app created collection resource handle.
349 if (handleIt != groupSyncResourceHandleList.end())
351 handleIt = collectionResourceHandleList.find(collectionResourceType);
352 if (handleIt == collectionResourceHandleList.end())
354 OC_LOG(DEBUG, TAG, "GroupSynchronization::leaveGroup : Error!" \
355 "There is no collection resource handle to leave.");
356 return OC_STACK_INVALID_PARAM;
359 collectionResHandle = handleIt->second;
360 if(collectionResHandle == NULL)
361 return OC_STACK_INVALID_PARAM;
363 OCStackResult result;
366 result = OCPlatform::unbindResource(collectionResHandle, resourceHandle);
367 if (OC_STACK_OK == result)
370 "GroupSynchronization::leaveGroup : To unbind resource was successful.");
374 OC_LOG_V(DEBUG, TAG, "GroupSynchronization::leaveGroup : " \
375 "To unbind resource was unsuccessful. result - %d", result);
378 } catch(OCException &e) {
379 OC_LOG_V(DEBUG, TAG, "ERROR : %s", e.reason().c_str());
380 return OC_STACK_NO_RESOURCE;
383 auto It = std::find(deviceResourceHandleList.begin(), deviceResourceHandleList.end(),
385 if (It == deviceResourceHandleList.end()) // there is no resource handle to find
389 result = OCPlatform::unregisterResource(resourceHandle);
390 if (OC_STACK_OK == result)
392 OC_LOG(DEBUG, TAG, "GroupSynchronization::leaveGroup : " \
393 "To unregister resource was successful.");
397 OC_LOG_V(DEBUG, TAG, "GroupSynchronization::leaveGroup : " \
398 "To unregister resource was unsuccessful. result - %d", result);
401 } catch(OCException &e)
403 OC_LOG_V(DEBUG, TAG, "ERROR : %s", e.reason().c_str());
404 return OC_STACK_NO_RESOURCE;
409 OC_LOG(DEBUG, TAG, "GroupSynchronization::leaveGroup : " \
410 "This resource cannot be unregistered.");
411 deviceResourceHandleList.erase(It);
414 auto handleListIt = childResourceHandleList.find(collectionResHandle);
415 if (handleListIt == childResourceHandleList.end())
417 OC_LOG(DEBUG, TAG, "GroupSynchronization::leaveGroup : " \
418 "Error! There is no child resource list to delete.");
419 return OC_STACK_INVALID_PARAM;
422 std::vector< OCResourceHandle > childList = handleListIt->second;
423 auto childIt = std::find(childList.begin(), childList.end(), resourceHandle);
424 if (childIt != childList.end())
426 OC_LOG(DEBUG, TAG, "GroupSynchronization::groupEntityHandler : " \
427 "Found! The resource to leave is found.");
428 childList.erase(childIt);
431 childResourceHandleList[collectionResHandle] = childList;
439 OCStackResult GroupSynchronization::leaveGroup(
440 const std::shared_ptr<OCResource> resource,
441 std::string collectionResourceType, OCResourceHandle resourceHandle)
443 if ((!resource) || (!resourceHandle))
445 OC_LOG(DEBUG, TAG, "GroupSynchronization::joinGroup : Error! Input params are wrong.");
446 return OC_STACK_INVALID_PARAM;
449 OC_LOG(DEBUG, TAG, "GroupSynchronization::joinGroup");
451 // making representation to join group
452 std::vector< std::string > type = resource->getResourceTypes();
453 std::string host = resource->host();
454 std::string uri = resource->uri() + "/groupsync";
456 std::vector< std::string > resourceTypes;
458 for (unsigned int i = 0; i < type.size(); ++i)
460 temp = type[0] + ".groupsync";
461 resourceTypes.push_back(temp);
464 std::vector< std::string > resourceInterface;
465 resourceInterface.push_back(DEFAULT_INTERFACE);
467 OCResource::Ptr groupSyncResource;
468 groupSyncResource = OCPlatform::constructResourceObject(host, uri,
470 resourceTypes, resourceInterface);
471 // groupSyncResource = OCPlatform::constructResourceObject(host, uri,
472 // OC_WIFI, false, resourceTypes, resourceInterface);
474 // making representation to leave group
475 std::string method = "leaveGroup";
476 // std::string type = OCGetResourceTypeName(collectionResourceType, 0);
477 std::string resourceType;
478 resourceType.append(OCGetResourceTypeName(resourceHandle, 0));
480 OCRepresentation rep;
481 rep.setValue("method", method);
482 rep.setValue("collectionResourceType", collectionResourceType);
483 rep.setValue("resourceType", resourceType);
485 OC_LOG_V(DEBUG, TAG, "\tmethod - %s", method.c_str());
486 OC_LOG_V(DEBUG, TAG, "\tcollectionResourceType - %s", collectionResourceType.c_str());
487 OC_LOG_V(DEBUG, TAG, "\tresourceType - %s", resourceType.c_str());
489 QueryParamsMap queryParamsMap;
491 // request to leave group to the remote group sync resource
492 OCStackResult result = groupSyncResource->put(rep, queryParamsMap,
493 std::bind(&GroupSynchronization::onLeaveGroup, this, std::placeholders::_1,
494 std::placeholders::_2, std::placeholders::_3));
495 if (OC_STACK_OK == result)
497 OC_LOG(DEBUG, TAG, "GroupSynchronization::leaveGroup : " \
498 "groupSyncResource->put was successful.");
502 OC_LOG_V(DEBUG, TAG, "GroupSynchronization::leaveGroup : " \
503 "groupSyncResource->put was unsuccessful. result - %d", result);
508 void GroupSynchronization::deleteGroup(std::string collectionResourceType)
510 if (0 == collectionResourceType.length())
513 "GroupSynchronization::deleteGroup : Error! Input params are wrong.");
517 OC_LOG(DEBUG, TAG, "GroupSynchronization::deleteGroup");
519 OCStackResult result;
520 OCResourceHandle resourceHandle;
522 auto handleIt = groupSyncResourceHandleList.find(collectionResourceType);
524 // if groupSyncResourceHandleList has resourceType,
525 // group sync of this app created collection resource.
526 if (handleIt != groupSyncResourceHandleList.end())
528 resourceHandle = handleIt->second; // group sync resource handle
529 result = OCPlatform::unregisterResource(resourceHandle);
530 if (OC_STACK_OK == result)
532 OC_LOG(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \
533 "To unregister group sync resource handle was successful.");
537 OC_LOG_V(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \
538 "To unregister group sync resource handle was unsuccessful. " \
539 "result - %d", result);
542 groupSyncResourceHandleList.erase(handleIt);
545 auto resourceIt = groupSyncResourceList.find(collectionResourceType);
546 if (resourceIt != groupSyncResourceList.end())
548 groupSyncResourceList.erase(resourceIt);
551 handleIt = collectionResourceHandleList.find(collectionResourceType);
552 if (handleIt == collectionResourceHandleList.end())
554 OC_LOG(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \
555 "Error! There is no collection resource handle to delete.");
558 OCResourceHandle collectionResHandle = handleIt->second;
560 collectionResourceHandleList.erase(handleIt);
562 auto handleListIt = childResourceHandleList.find(collectionResHandle);
563 if (handleListIt == childResourceHandleList.end())
565 OC_LOG(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \
566 "There is no child resource list to delete.");
568 result = OCPlatform::unregisterResource(collectionResHandle);
569 if (result == OC_STACK_OK)
571 OC_LOG(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \
572 "To unregister collection resource handle was successful.");
576 OC_LOG_V(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \
577 " To unregister collection resource handle was unsuccessful. result - %d",
585 std::vector< OCResourceHandle > childList = handleListIt->second;
587 childResourceHandleList.erase(handleListIt);
589 result = OCPlatform::unbindResources(collectionResHandle, childList);
590 if (OC_STACK_OK == result)
592 OC_LOG(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \
593 "To unbind resources was successful.");
597 OC_LOG_V(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \
598 "To unbind resources was unsuccessful. result - %d", result);
601 result = OCPlatform::unregisterResource(collectionResHandle);
602 if (result == OC_STACK_OK)
604 OC_LOG(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \
605 "To unregister collection resource handle was successful.");
609 OC_LOG_V(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \
610 " To unregister collection resource handle was unsuccessful. result - %d",
614 std::vector< OCResourceHandle >::iterator It;
616 for (unsigned int i = 0; i < childList.size(); i++)
618 resourceHandle = childList.at(i);
620 It = std::find(deviceResourceHandleList.begin(), deviceResourceHandleList.end(),
622 if (It != deviceResourceHandleList.end()) // find !!
624 deviceResourceHandleList.erase(It);
628 result = OCPlatform::unregisterResource(resourceHandle);
629 if (OC_STACK_OK == result)
632 "GroupSynchronization::deleteGroup : UnregisterResource(%d)" \
633 " was successful.", i + 1);
638 "GroupSynchronization::deleteGroup : UnregisterResource(%d)" \
639 " was unsuccessful. result - ", i + 1, result);
647 std::map< std::string, OCResourceHandle > GroupSynchronization::getGroupList()
649 return collectionResourceHandleList;
652 OCEntityHandlerResult GroupSynchronization::groupEntityHandler(
653 const std::shared_ptr< OCResourceRequest > request)
655 OC_LOG(DEBUG, TAG, "GroupSynchronization::groupEntityHandler");
659 // Get the request type and request flag
660 std::string requestType = request->getRequestType();
661 int requestFlag = request->getRequestHandlerFlag();
663 if (requestFlag == RequestHandlerFlag::RequestFlag)
665 OC_LOG(DEBUG, TAG, "\trequestFlag : Request");
667 // If the request type is GET
668 if (requestType == "GET")
670 OC_LOG(DEBUG, TAG, "\t\trequestType : GET");
672 else if (requestType == "PUT")
674 OC_LOG(DEBUG, TAG, "\t\trequestType : PUT");
676 //get method name, group resource type and resource type to join group
677 OCRepresentation rp = request->getResourceRepresentation();
678 std::string methodType = rp.getValue< std::string >("method");
679 std::string collectionResourceType = rp.getValue< std::string >(
680 "collectionResourceType");
681 std::string resourceType = rp.getValue< std::string >("resourceType");
683 OC_LOG_V(DEBUG, TAG, "\t\t\tmethod : %s", methodType.c_str());
684 OC_LOG_V(DEBUG, TAG, "\t\t\tcollection resourceType : %s",
685 collectionResourceType.c_str());
686 OC_LOG_V(DEBUG, TAG, "\t\t\tresourceType : %s", resourceType.c_str());
688 auto handleIt = collectionResourceHandleList.find(collectionResourceType);
689 if (handleIt == collectionResourceHandleList.end())
691 OC_LOG(DEBUG, TAG, "GroupSynchronization::groupEntityHandler : " \
692 "Error! There is no collection resource handle to delete.");
695 collectionResourceHandle = handleIt->second;
696 // in case of join group it is used in onFindResource()
698 if (methodType == "joinGroup")
700 std::string resourceName = OC_MULTICAST_DISCOVERY_URI;
701 resourceName.append("?rt=");
702 resourceName.append(resourceType);
703 OC_LOG_V(DEBUG, TAG, "\t\t\tresourceName : %s", resourceName.c_str());
705 resourceRequest = request;
707 OCPlatform::findResource("", resourceName,
709 std::bind(&GroupSynchronization::onFindResource, this,
710 std::placeholders::_1));
714 else if (methodType == "leaveGroup")
716 auto it = childResourceHandleList.find(collectionResourceHandle);
717 if (it == childResourceHandleList.end())
719 OC_LOG(DEBUG, TAG, "GroupSynchronization::groupEntityHandler : " \
720 "Error! There is no child resource list.");
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))
734 "GroupSynchronization::groupEntityHandler : " \
735 "Found! The resource to leave is found. - %s", type);
737 childIt = childList.erase(childIt++);
739 OCStackResult result = OCPlatform::unbindResource(
740 collectionResourceHandle, resourceHandle);
741 if (OC_STACK_OK == result)
744 "GroupSynchronization::groupEntityHandler : " \
745 "To unbind resource was successful.");
750 "GroupSynchronization::groupEntityHandler : " \
751 "To unbind resource was unsuccessful. result - %d",
755 result = OCPlatform::unregisterResource(resourceHandle);
756 if (OC_STACK_OK == result)
759 "GroupSynchronization::groupEntityHandler : " \
760 "To unregister resource was successful.");
765 "GroupSynchronization::groupEntityHandler : "
766 "To unregister resource was unsuccessful. result - %d",
777 childResourceHandleList[collectionResourceHandle] = childList;
781 auto pResponse = std::make_shared< OC::OCResourceResponse >();
782 pResponse->setRequestHandle(request->getRequestHandle());
783 pResponse->setResourceHandle(request->getResourceHandle());
784 pResponse->setErrorCode(200);
785 pResponse->setResponseResult(OC_EH_OK);
787 OCRepresentation rep = request->getResourceRepresentation();
788 pResponse->setResourceRepresentation(rep, DEFAULT_INTERFACE);
789 if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
791 OC_LOG(DEBUG, TAG, "GroupSynchronization::groupEntityHandler : " \
792 "sendResponse is successful.");
796 if (methodType != "") //TODO: Check groupmethodtype NULL
800 else if (requestType == "POST")
802 // POST request operations
804 else if (requestType == "DELETE")
806 // DELETE request operations
809 else if (requestFlag == RequestHandlerFlag::ObserverFlag)
811 OC_LOG(DEBUG, TAG, "\trequestFlag : Observer");
816 OC_LOG(DEBUG, TAG, "Request invalid");
822 void GroupSynchronization::onFindGroup(std::shared_ptr< OCResource > resource)
824 OC_LOG(DEBUG, TAG, "GroupSynchronization::onFindGroup");
831 std::string resourceURI;
832 std::string hostAddress;
834 // Get the resource URI
835 resourceURI = resource->uri();
836 OC_LOG_V(DEBUG, TAG, "\tURI of the resource: %s", resourceURI.c_str());
838 // Get the resource host address
839 hostAddress = resource->host();
840 OC_LOG_V(DEBUG, TAG, "\tHost address of the resource: %s", hostAddress.c_str());
842 hostAddress.append(resourceURI);
845 // Get the resource types
846 OC_LOG(DEBUG, TAG, "\tList of resource types: ");
847 for (auto &resourceTypes : resource->getResourceTypes())
849 OC_LOG_V(DEBUG, TAG, "\t\t", resourceTypes.c_str());
852 // Get the resource interfaces
853 OC_LOG(DEBUG, TAG, "\tList of resource interfaces: ");
854 for (auto &resourceInterfaces : resource->getResourceInterfaces())
856 OC_LOG_V(DEBUG, TAG, "\t\t", resourceInterfaces.c_str());
860 if (false == IsSameGroup(resource))
863 findCallback(resource);
868 // Resource is invalid
869 OC_LOG(DEBUG, TAG, "Resource is invalid");
875 catch (std::exception& e)
881 void GroupSynchronization::checkFindGroup(void)
883 OC_LOG(DEBUG, TAG, "GroupSynchronization::checkFindGroup");
885 for (int i = 0; i < 15; i++)
887 std::chrono::milliseconds workTime(300);
888 std::this_thread::sleep_for(workTime);
890 std::lock_guard < std::mutex > guard(foundGroupMutex);
892 if (false == foundGroupResourceList.empty())
894 OC_LOG(DEBUG, TAG, "GroupSynchronization::checkFoundGroup : " \
895 "Some group is received.");
900 OC_LOG(DEBUG, TAG, "GroupSynchronization::checkFoundGroup : " \
901 "It is failed to find resource within 3s.");
907 bool GroupSynchronization::IsSameGroup(std::shared_ptr< OCResource > resource)
909 std::lock_guard < std::mutex > guard(foundGroupMutex);
911 if (true == foundGroupResourceList.empty())
913 OC_LOG(DEBUG, TAG, "GroupSynchronization::IsSameGroup : There is no found group.");
917 std::string foundHostAddress, savedHostAddress;
918 std::string foundHostUri, savedHostUri;
919 foundHostAddress = resource->host();
920 foundHostUri = resource->uri();
922 for (unsigned int i = 0; i < foundGroupResourceList.size(); ++i)
924 savedHostAddress = (foundGroupResourceList.at(i))->host();
925 savedHostUri = (foundGroupResourceList.at(i))->uri();
927 if (0 == foundHostAddress.compare(savedHostAddress.c_str()))
929 if( 0 == foundHostUri.compare(savedHostAddress) )
932 "GroupSynchronization::IsSameGroup : Found! The same group is found.");
938 OC_LOG(DEBUG, TAG, "GroupSynchronization::IsSameGroup : There is no same group.");
942 void GroupSynchronization::saveGroup(std::shared_ptr< OCResource > resource)
944 OC_LOG(DEBUG, TAG, "GroupSynchronization::saveGroup");
946 std::lock_guard < std::mutex > guard(foundGroupMutex);
948 foundGroupResourceList.push_back(resource);
951 void GroupSynchronization::onJoinGroup(const HeaderOptions& headerOptions,
952 const OCRepresentation& rep, const int eCode)
954 if (eCode != OC_STACK_OK)
956 OC_LOG_V(DEBUG, TAG, "GroupSynchronization::onJoinGroup : error - %d", eCode);
961 void GroupSynchronization::onFindResource(std::shared_ptr< OCResource > resource)
963 OC_LOG(DEBUG, TAG, "GroupSynchronization::onFindResource");
967 if(NULL == collectionResourceHandle)
970 // start of debugging
971 std::string resourceURI;
972 std::string hostAddress;
974 // Get the resource URI
975 resourceURI = resource->uri();
976 OC_LOG_V(DEBUG, TAG, "\tURI of the resource: %s", resourceURI.c_str());
978 // Get the resource host address
979 hostAddress = resource->host();
980 OC_LOG_V(DEBUG, TAG, "\tHost address of the resource: %s", hostAddress.c_str());
982 hostAddress.append(resourceURI);
985 // Get the resource types
986 OC_LOG(DEBUG, TAG, "\tList of resource types: ");
987 for (auto &resourceTypes : resource->getResourceTypes())
989 OC_LOG_V(DEBUG, TAG, "\t\t%s", resourceTypes.c_str());
992 // Get the resource interfaces
993 OC_LOG(DEBUG, TAG, "\tList of resource interfaces: ");
994 for (auto &resourceInterfaces : resource->getResourceInterfaces())
996 OC_LOG_V(DEBUG, TAG, "\t\t%s", resourceInterfaces.c_str());
1001 OCResourceHandle resourceHandle;
1002 OCStackResult result = OCPlatform::registerResource(resourceHandle, resource);
1003 if (result != OC_STACK_OK)
1005 OC_LOG_V(DEBUG, TAG, "GroupSynchronization::" \
1006 "onFindResource - Resource to join creation was unsuccessful. result - %d",
1011 result = OCPlatform::bindResource(collectionResourceHandle, resourceHandle);
1012 if (result != OC_STACK_OK)
1014 OC_LOG_V(DEBUG, TAG, "GroupSynchronization::onFindResource : " \
1015 "To bind resource was unsuccessful. result - %d", result);
1019 OC_LOG(DEBUG, TAG, "GroupSynchronization::onFindResource : " \
1020 "To bind joinGroupHandle and resourceHandle was successful.");
1022 auto it = childResourceHandleList.find(collectionResourceHandle);
1023 std::vector< OCResourceHandle > childHandleList;
1024 if (it != childResourceHandleList.end())
1026 childHandleList = it->second;
1029 childHandleList.push_back(resourceHandle);
1030 childResourceHandleList[collectionResourceHandle] = childHandleList;
1032 auto pResponse = std::make_shared< OC::OCResourceResponse >();
1033 pResponse->setRequestHandle(resourceRequest->getRequestHandle());
1034 pResponse->setResourceHandle(resourceRequest->getResourceHandle());
1035 pResponse->setErrorCode(200);
1036 pResponse->setResponseResult(OC_EH_OK);
1038 OCRepresentation rep = resourceRequest->getResourceRepresentation();
1039 pResponse->setResourceRepresentation(rep);
1041 if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
1044 "GroupSynchronization::onFindResource : sendResponse is successful.");
1047 catch( OCException &e )
1049 // OC_LOG(DEBUG, TAG, e.what);
1055 OC_LOG(DEBUG, TAG, "GroupSynchronization::onFindResource : " \
1056 "Resource is invalid. So a new Group Resource has to be created.");
1062 void GroupSynchronization::onGetJoinedRemoteChild(const HeaderOptions& headerOptions,
1063 const OCRepresentation& rep, const int eCode)
1065 if (eCode != OC_STACK_OK)
1067 OC_LOG_V(DEBUG, TAG,
1068 "GroupSynchronization::onGetJoinedRemoteChild : error - %d", eCode);
1072 std::string resourceURI;
1073 std::vector< OCRepresentation > childList;
1074 OCRepresentation child;
1076 OC_LOG(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild");
1079 // Get the resource URI
1080 resourceURI = rep.getUri();
1081 OC_LOG_V(DEBUG, TAG, "\tURI of the resource: %s", resourceURI.c_str());
1083 // Get the resource types
1084 OC_LOG(DEBUG, TAG, "\tList of resource types: ");
1086 for (auto &resourceTypes : rep.getResourceTypes())
1088 OC_LOG_V(DEBUG, TAG, "\t\t%s", resourceTypes.c_str());
1091 // Get the resource interfaces
1092 OC_LOG(DEBUG, TAG, "\tList of resource interfaces: ");
1093 for (auto &resourceInterfaces : rep.getResourceInterfaces())
1095 OC_LOG_V(DEBUG, TAG, "\t\t%s", resourceInterfaces.c_str());
1098 childList = rep.getChildren();
1099 for (unsigned int i = 0; i < childList.size(); ++i)
1101 OC_LOG_V(DEBUG, TAG, "\tchild resource - %d", i + 1);
1103 child = childList.at(i);
1104 resourceURI = child.getUri();
1105 OC_LOG_V(DEBUG, TAG, "\t\tURI of the resource: %s", resourceURI.c_str());
1107 OC_LOG_V(DEBUG, TAG, "\t\tList of resource types: ");
1108 for (auto &types : child.getResourceTypes())
1110 OC_LOG_V(DEBUG, TAG, "\t\t\t%s", types.c_str());
1113 OC_LOG(DEBUG, TAG, "\tList of resource interfaces: ");
1114 for (auto &interfaces : child.getResourceInterfaces())
1116 OC_LOG_V(DEBUG, TAG, "\t\t\t%s", interfaces.c_str());
1121 // creating remote collection resource handle
1122 OCResourceHandle remoteCollectionResourceHandle;
1123 resourceURI = remoteCollectionResource->uri();
1124 std::vector< std::string > types = remoteCollectionResource->getResourceTypes();
1125 std::vector< std::string > interfaces = remoteCollectionResource->getResourceInterfaces();
1127 OCStackResult result = OCPlatform::registerResource(remoteCollectionResourceHandle,
1128 resourceURI, types[0], interfaces[0], NULL, OC_OBSERVABLE);
1129 if (result != OC_STACK_OK)
1131 OC_LOG_V(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild - " \
1132 "To register remoteCollectionResourceHandle" \
1133 " was unsuccessful. result - %d", result);
1136 OC_LOG(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild : " \
1137 "To register remoteCollectionResourceHandle was successful.");
1139 // binding remote collection resource handle and resource handle to join
1140 collectionResourceHandleList[types[0]] = remoteCollectionResourceHandle;
1142 result = OCPlatform::bindResource(remoteCollectionResourceHandle, deviceResourceHandle);
1143 if (OC_STACK_OK == result)
1145 OC_LOG(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild : " \
1146 "binding remoteCollectionResourceHandle and deviceResourceHandle");
1150 OC_LOG_V(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild - " \
1151 "To bind remoteCollectionResourceHandle and deviceResourceHandle " \
1152 "was unsuccessful. result - %d", result);
1155 std::vector< OCResourceHandle > childHandleList;
1156 childHandleList.push_back(deviceResourceHandle);
1157 deviceResourceHandleList.push_back(deviceResourceHandle);
1159 // binding copied remote collection resource handle and copied remote resource
1160 OCResourceHandle resourceHandle;
1161 for (unsigned int i = 0; i < childList.size(); ++i)
1163 OC_LOG_V(DEBUG, TAG, "\tremote resource - %d", i + 1);
1165 child = childList.at(i);
1166 resourceURI = child.getUri();
1167 types = child.getResourceTypes();
1168 interfaces = child.getResourceInterfaces();
1170 if (0 == types[0].compare(OCGetResourceTypeName(deviceResourceHandle, 0)))
1172 OC_LOG_V(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild : %s" \
1173 " is bind already.", types[0].c_str());
1177 result = OCPlatform::registerResource(resourceHandle, resourceURI, types[0],
1178 interfaces[0], NULL, OC_OBSERVABLE);
1179 if (OC_STACK_OK == result)
1181 result = OCPlatform::bindResource(remoteCollectionResourceHandle, resourceHandle);
1182 if (result != OC_STACK_OK)
1184 OC_LOG_V(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild - " \
1185 "binding remoteCollectionResourceHandle and resourceHandle " \
1186 "was unsuccessful. result - %d", result);
1187 OCPlatform::unregisterResource(resourceHandle);
1190 childHandleList.push_back(resourceHandle);
1191 OC_LOG(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild : " \
1192 "binding remoteCollectionResourceHandle and resourceHandle");
1196 OC_LOG_V(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild - " \
1197 "To register remoteCollectionResourceHandle was unsuccessful." \
1198 " result - %d", result);
1202 childResourceHandleList[remoteCollectionResourceHandle] = childHandleList;
1203 // this handle list is used to leave group
1208 void GroupSynchronization::onLeaveGroup(const HeaderOptions& headerOptions,
1209 const OCRepresentation& rep, const int eCode)
1211 if (eCode != OC_STACK_OK)
1213 OC_LOG_V(DEBUG, TAG, "GroupSynchronization::onLeaveGroup : error - %d", eCode);
1217 OC_LOG(DEBUG, TAG, "GroupSynchronization::onLeaveGroup");
1221 void GroupSynchronization::debugGroupSync(void)
1224 OC_LOG(DEBUG, TAG, "GroupSynchronization::debugGroupSync");
1227 std::map< std::string, OCResourceHandle >::iterator handleIt;
1228 std::map< OCResourceHandle, std::vector< OCResourceHandle > >::iterator childIt;
1230 OCResourceHandle resourceHandle;
1231 std::vector< OCResourceHandle > handleList;
1232 std::shared_ptr< OCResource > resource;
1234 OC_LOG(DEBUG, TAG, "Resource Handle Created by App");
1235 for (i = 0; i < deviceResourceHandleList.size(); i++)
1237 resourceHandle = deviceResourceHandleList.at(i);
1239 OC_LOG_V(DEBUG, TAG, "%d. details", i + 1);
1240 OC_LOG_V(DEBUG, TAG, "\turi - %s", OCGetResourceUri(resourceHandle));
1241 OC_LOG_V(DEBUG, TAG, "\tresource type - %s",
1242 OCGetResourceTypeName(resourceHandle, 0));
1243 OC_LOG_V(DEBUG, TAG, "\tresource interface - %s",
1244 OCGetResourceInterfaceName(resourceHandle, 0));
1247 OC_LOG_V(DEBUG, TAG, "Group Sync Resource Handle List. The number is %d",
1248 groupSyncResourceHandleList.size());
1251 for (handleIt = groupSyncResourceHandleList.begin();
1252 handleIt != groupSyncResourceHandleList.end(); ++handleIt)
1254 type = handleIt->first;
1255 OC_LOG_V(DEBUG, TAG, "\t%d. group sync resource type - %s", i, type.c_str());
1256 OC_LOG(DEBUG, TAG, "\tdetails");
1258 resourceHandle = handleIt->second;
1259 OC_LOG_V(DEBUG, TAG, "\turi - %s",
1260 OCGetResourceUri(resourceHandle));
1261 OC_LOG_V(DEBUG, TAG, "\tresource type - %s",
1262 OCGetResourceTypeName(resourceHandle, 0));
1263 OC_LOG_V(DEBUG, TAG, "\tresource interface - %s",
1264 OCGetResourceInterfaceName(resourceHandle, 0));
1269 OC_LOG_V(DEBUG, TAG, "Copied Remote Group Sync Resource List. The number is %d",
1270 groupSyncResourceList.size());
1271 std::vector< std::string > list;
1273 for (auto resourceIt = groupSyncResourceList.begin();
1274 resourceIt != groupSyncResourceList.end(); ++resourceIt)
1276 type = resourceIt->first;
1277 OC_LOG_V(DEBUG, TAG, "\t%d. group sync resource type - %s", i, type.c_str());
1278 OC_LOG(DEBUG, TAG, "\tdetails");
1280 resource = resourceIt->second;
1281 OC_LOG_V(DEBUG, TAG, "\thost - %s", resource->host().c_str());
1282 OC_LOG_V(DEBUG, TAG, "\turi - %s", resource->uri().c_str());
1283 list = resource->getResourceTypes();
1284 OC_LOG_V(DEBUG, TAG, "\tresource type - %s", list[0].c_str());
1285 list = resource->getResourceInterfaces();
1286 OC_LOG_V(DEBUG, TAG, "\tresource interface - %s", list[0].c_str());
1290 OC_LOG(DEBUG, TAG, "Collection Resource Handle List");
1292 for (handleIt = collectionResourceHandleList.begin();
1293 handleIt != collectionResourceHandleList.end(); ++handleIt)
1295 type = handleIt->first;
1296 OC_LOG_V(DEBUG, TAG, "\t%d. collection resource type - %s", i, type.c_str());
1297 OC_LOG(DEBUG, TAG, "\tdetails");
1299 resourceHandle = handleIt->second;
1300 OC_LOG_V(DEBUG, TAG, "\turi - %s", OCGetResourceUri(resourceHandle));
1301 OC_LOG_V(DEBUG, TAG, "\tresource type - %s",
1302 OCGetResourceTypeName(resourceHandle, 0));
1303 OC_LOG_V(DEBUG, TAG, "\tresource interface - %s",
1304 OCGetResourceInterfaceName(resourceHandle, 0));
1306 childIt = childResourceHandleList.find(resourceHandle);
1307 if (childIt != childResourceHandleList.end())
1309 handleList = childIt->second;
1310 for (unsigned int j = 0; j < handleList.size(); j++)
1313 OC_LOG_V(DEBUG, TAG, "\t\t%d. child resource details", j + 1);
1315 resourceHandle = handleList.at(j);
1316 OC_LOG_V(DEBUG, TAG, "\t\turi - %s",
1317 OCGetResourceUri(resourceHandle));
1318 OC_LOG_V(DEBUG, TAG, "\t\tresource type - %s",
1319 OCGetResourceTypeName(resourceHandle, 0));
1320 OC_LOG_V(DEBUG, TAG, "\t\tresource interface - %s",
1321 OCGetResourceInterfaceName(resourceHandle, 0));