From 75c3344217e8d77520a74ace29852f6c05c684e0 Mon Sep 17 00:00:00 2001 From: HyunJun Kim Date: Fri, 20 Mar 2015 11:35:46 +0900 Subject: [PATCH] Fix and Add join/leaveGroup function for stability. We provided leaveGroup in Thingsmanager. But it has obscurities. So, we have to separate two leaveGroup functions which it had different arguments. Now, We can select join/leaveGroup function to match our situation. Change-Id: I0a570a4685e833e4e55ced50e1f37971b7076b26 Signed-off-by: HyunJun Kim Reviewed-on: https://gerrit.iotivity.org/gerrit/522 Tested-by: jenkins-iotivity Reviewed-by: Uze Choi --- .../sampleapp/linux/groupsyncaction/phone.cpp | 2 +- service/things-manager/sdk/inc/ThingsManager.h | 18 +++ .../sdk/src/GroupSynchronization.cpp | 164 +++++++++++---------- .../things-manager/sdk/src/GroupSynchronization.h | 3 + service/things-manager/sdk/src/ThingsManager.cpp | 7 + 5 files changed, 115 insertions(+), 79 deletions(-) diff --git a/service/things-manager/sampleapp/linux/groupsyncaction/phone.cpp b/service/things-manager/sampleapp/linux/groupsyncaction/phone.cpp index 57494d0..177399d 100644 --- a/service/things-manager/sampleapp/linux/groupsyncaction/phone.cpp +++ b/service/things-manager/sampleapp/linux/groupsyncaction/phone.cpp @@ -433,7 +433,7 @@ int main(int argc, char* argv[]) continue; } - result = gThingManager->leaveGroup(collectionResourceType, gPhoneResourceHandle); + result = gThingManager->leaveGroup(gFindGroup, collectionResourceType, gPhoneResourceHandle); if (OC_STACK_OK == result) { cout << "Leaving group was successful\n"; diff --git a/service/things-manager/sdk/inc/ThingsManager.h b/service/things-manager/sdk/inc/ThingsManager.h index a1058b5..6bc0f92 100644 --- a/service/things-manager/sdk/inc/ThingsManager.h +++ b/service/things-manager/sdk/inc/ThingsManager.h @@ -190,6 +190,24 @@ namespace OIC OCResourceHandle resourceHandle); /** + * API for leaving a joined group. + * + * @param resource - group resource pointer to join. + * It can be the callback result of findGroup(). + * + * @param collectionResourceType - resource type of a group to leave. + * @param resourceHandle - resource handle to leave a group. + * + * @return OCStackResult - return value of this API. + * It returns OC_STACK_OK if success. + * + * NOTE: OCStackResult is defined in ocstack.h. + */ + OCStackResult leaveGroup(const std::shared_ptr< OCResource > resource, + std::string collectionResourceType, + OCResourceHandle resourceHandle); + + /** * API for deleting a group. * * @param collectionResourceType - resource type of a group to delete. diff --git a/service/things-manager/sdk/src/GroupSynchronization.cpp b/service/things-manager/sdk/src/GroupSynchronization.cpp index 50d032a..775fcb1 100644 --- a/service/things-manager/sdk/src/GroupSynchronization.cpp +++ b/service/things-manager/sdk/src/GroupSynchronization.cpp @@ -198,7 +198,6 @@ namespace OIC } OCResourceHandle collectionResHandle = resIt->second; - try{ OCStackResult result = OCPlatform::bindResource(collectionResHandle, resourceHandle); if (result != OC_STACK_OK) @@ -210,7 +209,7 @@ namespace OIC } catch(OCException &e) { return OC_STACK_INVALID_PARAM; - + } cout << "GroupSynchronization::joinGroup : " << "To bind collectionResHandle and resourceHandle" << endl; @@ -387,7 +386,7 @@ namespace OIC << endl; return result; } - } catch(OCException &e) + } catch(OCException &e) { cout << "ERROR : " << e.reason() << endl; return OC_STACK_NO_RESOURCE; @@ -421,70 +420,79 @@ namespace OIC debugGroupSync(); } - else // requesting to unbind this resourceHandle to the remote collection resource + + return OC_STACK_OK; + } + +OCStackResult GroupSynchronization::leaveGroup( + const std::shared_ptr resource, + std::string collectionResourceType, OCResourceHandle resourceHandle) + { + if ((!resource) || (!resourceHandle)) { - auto resourceIt = groupSyncResourceList.find(collectionResourceType); + cout << "GroupSynchronization::joinGroup : Error! Input params are wrong." << endl; + return OC_STACK_INVALID_PARAM; + } - if (resourceIt == groupSyncResourceList.end()) - { - cout << "GroupSynchronization::leaveGroup : " - << "Error! There is no collectin resource type to leave." << endl; - return OC_STACK_INVALID_PARAM; - } + cout << "GroupSynchronization::joinGroup" << endl; - std::shared_ptr< OCResource > resource = resourceIt->second; - if(resource == NULL) - return OC_STACK_NO_RESOURCE; -// cout << "GroupSynchronization::leaveGroup : group sync resource uri - " -// << resource->uri() << endl; + // making representation to join group + std::vector< std::string > type = resource->getResourceTypes(); + std::string host = resource->host(); + std::string uri = resource->uri() + "/groupsync"; - handleIt = collectionResourceHandleList.find(collectionResourceType); - if (handleIt == collectionResourceHandleList.end()) - { - cout << "GroupSynchronization::leaveGroup : " - << "Error! There is no collection resource handle to leave." << endl; - return OC_STACK_INVALID_PARAM; - } + std::vector< std::string > resourceTypes; + std::string temp; + for (unsigned int i = 0; i < type.size(); ++i) + { + temp = type[0] + ".groupsync"; + resourceTypes.push_back(temp); + } - collectionResHandle = handleIt->second; + std::vector< std::string > resourceInterface; + resourceInterface.push_back(DEFAULT_INTERFACE); - // making representation to leave group - std::string method = "leaveGroup"; - std::string type = OCGetResourceTypeName(collectionResHandle, 0); - std::string resourceType; - resourceType.append(OCGetResourceTypeName(resourceHandle, 0)); + OCResource::Ptr groupSyncResource; +#ifdef CA_INT + groupSyncResource = OCPlatform::constructResourceObject(host, uri, + OC_ETHERNET | OC_WIFI, 1, resourceTypes, resourceInterface); +#else + groupSyncResource = OCPlatform::constructResourceObject(host, uri, 1, + resourceTypes, resourceInterface); +#endif - OCRepresentation rep; - rep.setValue("method", method); - rep.setValue("collectionResourceType", type); - rep.setValue("resourceType", resourceType); + // making representation to leave group + std::string method = "leaveGroup"; +// std::string type = OCGetResourceTypeName(collectionResourceType, 0); + std::string resourceType; + resourceType.append(OCGetResourceTypeName(resourceHandle, 0)); - cout << "\tmethod - " << method << endl; - cout << "\tcollectionResourceType - " << type << endl; - cout << "\tresourceType - " << resourceType << endl; + OCRepresentation rep; + rep.setValue("method", method); + rep.setValue("collectionResourceType", collectionResourceType); + rep.setValue("resourceType", resourceType); - QueryParamsMap queryParamsMap; + cout << "\tmethod - " << method << endl; + cout << "\tcollectionResourceType - " << collectionResourceType << endl; + cout << "\tresourceType - " << resourceType << endl; - // request to leave group to the remote group sync resource - OCStackResult result = resource->put(rep, queryParamsMap, - std::bind(&GroupSynchronization::onLeaveGroup, this, std::placeholders::_1, - std::placeholders::_2, std::placeholders::_3)); - if (OC_STACK_OK == result) - { - cout << "GroupSynchronization::leaveGroup : " - << "groupSyncResource->put was successful." << endl; - } - else - { - cout << "GroupSynchronization::leaveGroup : " - << "groupSyncResource->put was unsuccessful. result - " << result << endl; - } + QueryParamsMap queryParamsMap; - // deleting all remote resources. These are copied in onGetJoinedRemoteChild() - deleteGroup(collectionResourceType); + // request to leave group to the remote group sync resource + OCStackResult result = groupSyncResource->put(rep, queryParamsMap, + std::bind(&GroupSynchronization::onLeaveGroup, this, std::placeholders::_1, + std::placeholders::_2, std::placeholders::_3)); + if (OC_STACK_OK == result) + { + cout << "GroupSynchronization::leaveGroup : " + << "groupSyncResource->put was successful." << endl; } - - return OC_STACK_OK; + else + { + cout << "GroupSynchronization::leaveGroup : " + << "groupSyncResource->put was unsuccessful. result - " << result << endl; + } + return result; } void GroupSynchronization::deleteGroup(std::string collectionResourceType) @@ -933,29 +941,29 @@ namespace OIC return; } - cout << "GroupSynchronization::onJoinGroup : " << endl; - - if (remoteCollectionResource) - { - std::string resourceInterface = DEFAULT_INTERFACE; - QueryParamsMap queryParamsMap; - - OCStackResult result = remoteCollectionResource->get("", resourceInterface, - queryParamsMap, - std::bind(&GroupSynchronization::onGetJoinedRemoteChild, this, - std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); - if (OC_STACK_OK == result) - { - cout << "GroupSynchronization::onJoinGroup : " - << "remoteCollectionResource->get was successful." << endl; - } - else - { - cout << "GroupSynchronization::onJoinGroup : " - << "remoteCollectionResource->get was unsuccessful. result - " << result - << endl; - } - } +// cout << "GroupSynchronization::onJoinGroup : " << endl; +// +// if (remoteCollectionResource) +// { +// std::string resourceInterface = DEFAULT_INTERFACE; +// QueryParamsMap queryParamsMap; +// +// OCStackResult result = remoteCollectionResource->get("", resourceInterface, +// queryParamsMap, +// std::bind(&GroupSynchronization::onGetJoinedRemoteChild, this, +// std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); +// if (OC_STACK_OK == result) +// { +// cout << "GroupSynchronization::onJoinGroup : " +// << "remoteCollectionResource->get was successful." << endl; +// } +// else +// { +// cout << "GroupSynchronization::onJoinGroup : " +// << "remoteCollectionResource->get was unsuccessful. result - " << result +// << endl; +// } +// } } void GroupSynchronization::onFindResource(std::shared_ptr< OCResource > resource) diff --git a/service/things-manager/sdk/src/GroupSynchronization.h b/service/things-manager/sdk/src/GroupSynchronization.h index 6bd8a8d..6c0252b 100644 --- a/service/things-manager/sdk/src/GroupSynchronization.h +++ b/service/things-manager/sdk/src/GroupSynchronization.h @@ -110,6 +110,9 @@ public: OCResourceHandle resourceHandle); OCStackResult leaveGroup(std::string collectionResourceType, OCResourceHandle resourceHandle); + OCStackResult leaveGroup(const std::shared_ptr< OCResource > resource, + std::string collectionResourceType, + OCResourceHandle resourceHandle); void deleteGroup(std::string collectionResourceType); std::map< std::string, OCResourceHandle > getGroupList(); diff --git a/service/things-manager/sdk/src/ThingsManager.cpp b/service/things-manager/sdk/src/ThingsManager.cpp index 024104f..3c0178f 100644 --- a/service/things-manager/sdk/src/ThingsManager.cpp +++ b/service/things-manager/sdk/src/ThingsManager.cpp @@ -125,6 +125,13 @@ namespace OIC return result; } + OCStackResult ThingsManager::leaveGroup(const std::shared_ptr< OCResource > resource, + std::string collectionResourceType, + OCResourceHandle resourceHandle) + { + return g_groupSync->leaveGroup(resource, collectionResourceType, resourceHandle); + } + void ThingsManager::deleteGroup(std::string collectionResourceType) { g_groupSync->deleteGroup(collectionResourceType); -- 2.7.4