X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=service%2Fthings-manager%2Fsdk%2Fsrc%2FGroupSynchronization.cpp;h=b60c045b748fb207ba7d1f17cb8d58d76993e438;hb=935fdb9b67b6c10d007e652e9e2e028fd6ccfe09;hp=dd2c96cfcdfbdecbe296b000d91da5c43e6875a4;hpb=00b3660e45c56cb3db35dc2596a054f801b5591a;p=platform%2Fupstream%2Fiotivity.git diff --git a/service/things-manager/sdk/src/GroupSynchronization.cpp b/service/things-manager/sdk/src/GroupSynchronization.cpp index dd2c96c..b60c045 100644 --- a/service/things-manager/sdk/src/GroupSynchronization.cpp +++ b/service/things-manager/sdk/src/GroupSynchronization.cpp @@ -18,183 +18,211 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -/// @file GroupSynchronization.cpp -/// @brief - #include "GroupSynchronization.h" +#include "logger.h" using namespace OC; +using namespace std; + +#define TAG "GROUP_SYNC" namespace OIC { + GroupSynchronization* GroupSynchronization::groupSyncnstance = NULL; + bool GroupSynchronization::bIsFinding = false; -GroupSynchronization* GroupSynchronization::groupSyncnstance = NULL; - -GroupSynchronization* GroupSynchronization::getInstance() -{ - if (groupSyncnstance == NULL) + GroupSynchronization* GroupSynchronization::getInstance() { - groupSyncnstance = new GroupSynchronization(); + if (groupSyncnstance == NULL) + { + groupSyncnstance = new GroupSynchronization(); + } + return groupSyncnstance; } - return groupSyncnstance; -} -void GroupSynchronization::deleteInstance() -{ - if (groupSyncnstance) + void GroupSynchronization::deleteInstance() { - delete groupSyncnstance; - groupSyncnstance = NULL; + if (groupSyncnstance) + { + delete groupSyncnstance; + groupSyncnstance = NULL; + } } -} -OCStackResult GroupSynchronization::findGroup( - std::vector< std::string > collectionResourceTypes, FindCallback callback) -{ - cout << "GroupSynchronization::findGroup" << endl; + OCStackResult GroupSynchronization::findGroup( + std::vector< std::string > collectionResourceTypes, FindCallback callback) + { + OC_LOG(DEBUG, TAG, "GroupSynchronization::findGroup"); - foundGroupResourceList.clear(); + if(bIsFinding) + { + OC_LOG(DEBUG, TAG, "It was searching already."); + return OC_STACK_ERROR; + } - findCallback = callback; + std::lock_guard < std::mutex > guard(foundGroupMutex); + foundGroupResourceList.clear(); + findCallback = callback; - for (unsigned int i = 0; i < collectionResourceTypes.size(); ++i) - { - std::string query = "coap://224.0.1.187/oc/core?rt="; - query.append(collectionResourceTypes.at(i)); - cout << "GroupSynchronization::findGroup - " << query << endl; + if (findCallback == NULL) + { + OC_LOG(DEBUG, TAG, "Find callback is NULL."); + return OC_STACK_ERROR; + } - OCPlatform::findResource("", query, - std::bind(&GroupSynchronization::onFindGroup, this, std::placeholders::_1)); - } + for (unsigned int i = 0; i < collectionResourceTypes.size(); ++i) + { - // thread to check if GroupSynchronization::onFoundGroup is called or not. - std::thread t(std::bind(&GroupSynchronization::checkFindGroup, this)); - t.detach(); + std::string query = OC_MULTICAST_DISCOVERY_URI; + query.append("?rt="); + query.append(collectionResourceTypes.at(i)); - return OC_STACK_OK; -} + OCPlatform::findResource("", query, + OC_ALL, + std::bind(&GroupSynchronization::onFindGroup, this, + std::placeholders::_1)); + } -OCStackResult GroupSynchronization::createGroup(std::string collectionResourceType) -{ - foundGroupResourceList.clear(); + bIsFinding = true; - OCResourceHandle collectionResHandle = NULL; - OCResourceHandle groupSyncResHandle = NULL; + // thread to check if GroupSynchronization::onFoundGroup is called or not. + std::thread t(std::bind(&GroupSynchronization::checkFindGroup, this)); + t.detach(); + + return OC_STACK_OK; + } - if (0 != collectionResourceType.length()) + OCStackResult GroupSynchronization::createGroup(std::string collectionResourceType) { - cout << "GroupSynchronization::createGroup - The created group is added." << endl; + std::lock_guard < std::mutex > guard(foundGroupMutex); + foundGroupResourceList.clear(); + + OCResourceHandle collectionResHandle = NULL; + OCResourceHandle groupSyncResHandle = NULL; + + if (0 == collectionResourceType.length()) + { + OC_LOG(DEBUG, TAG, + "GroupSynchronization::createGroup : Error! Input params are wrong."); + return OC_STACK_INVALID_PARAM; + } + + OC_LOG(DEBUG, TAG, "GroupSynchronization::createGroup - The created group is added."); OCStackResult result; // creating master collection resource std::string collectionUri = "/" + collectionResourceType; - int i; + std::size_t i; while ((i = collectionUri.find(".")) != std::string::npos) { collectionUri.replace(i, 1, "/"); } - cout << "GroupSynchronization::createGroup : collection uri - " << collectionUri - << ", type - " << collectionResourceType << endl; + + OC_LOG_V(DEBUG, TAG, "GroupSynchronization::createGroup : collection uri - %s, type - %s", + collectionUri.c_str(), collectionResourceType.c_str()); std::string resourceInterface = DEFAULT_INTERFACE; result = OCPlatform::registerResource(collectionResHandle, collectionUri, - collectionResourceType, resourceInterface, NULL, - OC_DISCOVERABLE | OC_OBSERVABLE); + collectionResourceType, resourceInterface, NULL, OC_DISCOVERABLE | OC_OBSERVABLE); if (result != OC_STACK_OK) { - cout << "To register resource (" << collectionUri << ") was unsuccessful. result - " - << result << endl; + OC_LOG_V(DEBUG, TAG, "To register resource (%s) was unsuccessful. result - %d", + collectionUri.c_str(), result); goto Error; } - OCPlatform::bindInterfaceToResource(collectionResHandle, GROUP_INTERFACE); - if (result != OC_STACK_OK) { - cout << "To bind Interface (collection) was unsuccessful. result - " << result - << endl; - } - - collectionResourceHandleList[collectionResourceType] = collectionResHandle; + OCPlatform::bindInterfaceToResource(collectionResHandle, GROUP_INTERFACE); + if (result != OC_STACK_OK) + { + OC_LOG_V(DEBUG, TAG, + "To bind Interface (collection) was unsuccessful. result - %d", + result); + } - // creating master group sync resource - std::string groupSyncUri = collectionUri + "/groupsync"; - std::string groupSyncResType = collectionResourceType + ".groupsync"; + collectionResourceHandleList[collectionResourceType] = collectionResHandle; -// cout << "GroupSynchronization::createGroup : groupSync uri - " << groupSyncUri -// << ", type - " << collectionResourceType << endl; + // creating master group sync resource + std::string groupSyncUri = collectionUri + "/groupsync"; + std::string groupSyncResType = collectionResourceType + ".groupsync"; - result = OCPlatform::registerResource(groupSyncResHandle, groupSyncUri, - groupSyncResType, resourceInterface, - std::bind(&GroupSynchronization::groupEntityHandler, this, - std::placeholders::_1), OC_DISCOVERABLE | OC_OBSERVABLE); - if (result != OC_STACK_OK) - { - cout << "To register resource (groupsync) was unsuccessful. result - " << result - << endl; - goto Error; - } + result = OCPlatform::registerResource(groupSyncResHandle, groupSyncUri, + groupSyncResType, resourceInterface, + std::bind(&GroupSynchronization::groupEntityHandler, this, + std::placeholders::_1), OC_DISCOVERABLE | OC_OBSERVABLE); + if (result != OC_STACK_OK) + { + OC_LOG_V(DEBUG, TAG, + "To register resource (groupsync) was unsuccessful. result - %d", result); + goto Error; + } - groupSyncResourceHandleList[collectionResourceType] = groupSyncResHandle; + groupSyncResourceHandleList[collectionResourceType] = groupSyncResHandle; - return OC_STACK_OK; - } - else - { - cout << "GroupSynchronization::createGroup : Error! Input params are wrong." << endl; - return OC_STACK_INVALID_PARAM; - } + return OC_STACK_OK; + } + Error: - Error: + if (collectionResHandle) + { + OCPlatform::unregisterResource(collectionResHandle); + auto iterator = collectionResourceHandleList.find(collectionResourceType); + if (iterator != collectionResourceHandleList.end()) + { + collectionResourceHandleList.erase(iterator); + } + } - if (collectionResHandle) - { - OCPlatform::unregisterResource(collectionResHandle); - auto iterator = collectionResourceHandleList.find(collectionResourceType); - if (iterator != collectionResourceHandleList.end()) + if (groupSyncResHandle) { - collectionResourceHandleList.erase(iterator); + OCPlatform::unregisterResource(groupSyncResHandle); + auto iterator = groupSyncResourceHandleList.find(collectionResourceType); + if (iterator != groupSyncResourceHandleList.end()) + { + groupSyncResourceHandleList.erase(iterator); + } } + + return OC_STACK_NO_RESOURCE; } - if (groupSyncResHandle) + OCStackResult GroupSynchronization::joinGroup(std::string collectionResourceType, + OCResourceHandle resourceHandle) { - OCPlatform::unregisterResource(groupSyncResHandle); - auto iterator = groupSyncResourceHandleList.find(collectionResourceType); - if (iterator != groupSyncResourceHandleList.end()) + std::lock_guard < std::mutex > guard(foundGroupMutex); + if ((0 == collectionResourceType.length()) || (!resourceHandle)) { - groupSyncResourceHandleList.erase(iterator); + OC_LOG(DEBUG, TAG, + "GroupSynchronization::joinGroup : Error! input params are wrong."); + return OC_STACK_INVALID_PARAM; } - } - - return OC_STACK_NO_RESOURCE; -} -OCStackResult GroupSynchronization::joinGroup(std::string collectionResourceType, - OCResourceHandle resourceHandle) -{ - if ((0 != collectionResourceType.length()) && (resourceHandle)) - { auto resIt = collectionResourceHandleList.find(collectionResourceType); - if (resIt == groupSyncResourceHandleList.end()) + if (resIt == collectionResourceHandleList.end()) { - cout << "GroupSynchronization::joinGroup : error! There is no collection to join" - << endl; + OC_LOG(DEBUG, TAG, + "GroupSynchronization::joinGroup : error! There is no collection to join"); return OC_STACK_INVALID_PARAM; } OCResourceHandle collectionResHandle = resIt->second; - - OCStackResult result = OCPlatform::bindResource(collectionResHandle, resourceHandle); - if (result != OC_STACK_OK) - { - cout << "GroupSynchronization::joinGroup : To bind resource was unsuccessful." - << "result - " << result << endl; - return OC_STACK_ERROR; + try{ + OCStackResult result = OCPlatform::bindResource(collectionResHandle, resourceHandle); + if (result != OC_STACK_OK) + { + OC_LOG_V(DEBUG, TAG, + "GroupSynchronization::joinGroup : To bind resource was unsuccessful." \ + " result - %d", result); + return OC_STACK_ERROR; + } + } catch(OCException &e) { + return OC_STACK_INVALID_PARAM; } - cout << "GroupSynchronization::joinGroup : " - << "To bind collectionResHandle and resourceHandle" << endl; + + OC_LOG(DEBUG, TAG, + "GroupSynchronization::joinGroup : To bind collectionResHandle and resourceHandle"); std::vector< OCResourceHandle > childHandleList; @@ -210,22 +238,21 @@ OCStackResult GroupSynchronization::joinGroup(std::string collectionResourceType deviceResourceHandleList.push_back(resourceHandle); debugGroupSync(); - } - else - { - cout << "GroupSynchronization::joinGroup : Error! input params are wrong." << endl; - return OC_STACK_INVALID_PARAM; - } - return OC_STACK_OK; -} + return OC_STACK_OK; + } -OCStackResult GroupSynchronization::joinGroup(const std::shared_ptr< OCResource > resource, - OCResourceHandle resourceHandle) -{ - if ((resource) && (resourceHandle)) + OCStackResult GroupSynchronization::joinGroup(const std::shared_ptr< OCResource > resource, + OCResourceHandle resourceHandle) { - cout << "GroupSynchronization::joinGroup" << endl; + std::lock_guard < std::mutex > guard(foundGroupMutex); + if ((!resource) || (!resourceHandle)) + { + OC_LOG(DEBUG, TAG, "GroupSynchronization::joinGroup : Error! Input params are wrong."); + return OC_STACK_INVALID_PARAM; + } + + OC_LOG(DEBUG, TAG, "GroupSynchronization::joinGroup"); // making representation to join group std::string method = "joinGroup"; @@ -238,9 +265,9 @@ OCStackResult GroupSynchronization::joinGroup(const std::shared_ptr< OCResource rep.setValue("collectionResourceType", type[0]); rep.setValue("resourceType", resourceType); - cout << "\tmethod - " << method << endl; - cout << "\tcollectionResourceType - " << type[0] << endl; - cout << "\tresourceType - " << resourceType << endl; + OC_LOG_V(DEBUG, TAG, "\tmethod - %s", method.c_str()); + OC_LOG_V(DEBUG, TAG, "\tcollectionResourceType - %s", type[0].c_str()); + OC_LOG_V(DEBUG, TAG, "\tresourceType - %s", resourceType.c_str()); // creating group sync resource with the received collection resource. // entity handler of group sync is used to join group. @@ -258,11 +285,18 @@ OCStackResult GroupSynchronization::joinGroup(const std::shared_ptr< OCResource std::vector< std::string > resourceInterface; resourceInterface.push_back(DEFAULT_INTERFACE); - OCResource::Ptr groupSyncResource = OCPlatform::constructResourceObject(host, uri, 1, - resourceTypes, resourceInterface); + OCResource::Ptr groupSyncResource = + OCPlatform::constructResourceObject(host, uri, + + OC_ALL, false, + resourceTypes, resourceInterface); + + // OCResource::Ptr groupSyncResource = OCPlatform::constructResourceObject(host, uri, + // OC_WIFI, false, resourceTypes, resourceInterface); + groupSyncResourceList[type[0]] = groupSyncResource; - cout << "GroupSynchronization::joinGroup : creating groupSyncResource." << endl; + OC_LOG(DEBUG, TAG, "GroupSynchronization::joinGroup : creating groupSyncResource."); // Create QueryParameters Map and add query params (if any) QueryParamsMap queryParamsMap; @@ -273,13 +307,14 @@ OCStackResult GroupSynchronization::joinGroup(const std::shared_ptr< OCResource std::placeholders::_2, std::placeholders::_3)); if (OC_STACK_OK == result) { - cout << "GroupSynchronization::joinGroup : groupSyncResource->put was successful." - << endl; + OC_LOG(DEBUG, TAG, + "GroupSynchronization::joinGroup : groupSyncResource->put was successful."); } else { - cout << "GroupSynchronization::joinGroup : " - << "groupSyncResource->put was unsuccessful. result - " << result << endl; + OC_LOG_V(DEBUG, TAG, + "GroupSynchronization::joinGroup : groupSyncResource->put was unsuccessful." \ + "result - %d", result); } // saving the remote collection resource. @@ -291,23 +326,22 @@ OCStackResult GroupSynchronization::joinGroup(const std::shared_ptr< OCResource return OC_STACK_OK; } - else - { - cout << "GroupSynchronization::joinGroup : Error! Input params are wrong." << endl; - return OC_STACK_INVALID_PARAM; - } -} -OCStackResult GroupSynchronization::leaveGroup(std::string collectionResourceType, - OCResourceHandle resourceHandle) -{ - if ((0 != collectionResourceType.length()) && (resourceHandle)) + OCStackResult GroupSynchronization::leaveGroup(std::string collectionResourceType, + OCResourceHandle resourceHandle) { - cout << "GroupSynchronization::leaveGroup : collectionResourceType - " - << collectionResourceType << endl; + if ((0 == collectionResourceType.length()) || (!resourceHandle)) + { + OC_LOG(DEBUG, TAG, + "GroupSynchronization::leaveGroup : Error! Input params are wrong."); + return OC_STACK_INVALID_PARAM; + } - OCResourceHandle collectionResHandle; + OC_LOG_V(DEBUG, TAG, + "GroupSynchronization::leaveGroup : collectionResourceType - %s", + collectionResourceType.c_str()); + OCResourceHandle collectionResHandle; auto handleIt = groupSyncResourceHandleList.find(collectionResourceType); // if groupSyncResourceHandleList has resourceType, @@ -317,57 +351,71 @@ OCStackResult GroupSynchronization::leaveGroup(std::string collectionResourceTyp handleIt = collectionResourceHandleList.find(collectionResourceType); if (handleIt == collectionResourceHandleList.end()) { - cout << "GroupSynchronization::leaveGroup : " - << "Error! There is no collection resource handle to leave." << endl; + OC_LOG(DEBUG, TAG, "GroupSynchronization::leaveGroup : Error!" \ + "There is no collection resource handle to leave."); return OC_STACK_INVALID_PARAM; } collectionResHandle = handleIt->second; -// cout << "GroupSynchronization::leaveGroup : collection handle uri - " -// << OCGetResourceUri(collectionResHandle) << endl; + if(collectionResHandle == NULL) + return OC_STACK_INVALID_PARAM; - OCStackResult result = OCPlatform::unbindResource(collectionResHandle, - resourceHandle); - if (OC_STACK_OK == result) + OCStackResult result; + try { - cout << "GroupSynchronization::leaveGroup : " - << "To unbind resource was successful." << endl; - } - else - { - cout << "GroupSynchronization::leaveGroup : " - << "To unbind resource was unsuccessful. result - " << result << endl; + result = OCPlatform::unbindResource(collectionResHandle, resourceHandle); + if (OC_STACK_OK == result) + { + OC_LOG(DEBUG, TAG, + "GroupSynchronization::leaveGroup : To unbind resource was successful."); + } + else + { + OC_LOG_V(DEBUG, TAG, "GroupSynchronization::leaveGroup : " \ + "To unbind resource was unsuccessful. result - %d", result); + return result; + } + } catch(OCException &e) { + OC_LOG_V(DEBUG, TAG, "ERROR : %s", e.reason().c_str()); + return OC_STACK_NO_RESOURCE; } - auto It = std::find(deviceResourceHandleList.begin(), - deviceResourceHandleList.end(), resourceHandle); + auto It = std::find(deviceResourceHandleList.begin(), deviceResourceHandleList.end(), + resourceHandle); if (It == deviceResourceHandleList.end()) // there is no resource handle to find { - result = OCPlatform::unregisterResource(resourceHandle); - if (OC_STACK_OK == result) + try { - cout << "GroupSynchronization::leaveGroup : " - << "To unregister resource was successful." << endl; - } - else + result = OCPlatform::unregisterResource(resourceHandle); + if (OC_STACK_OK == result) + { + OC_LOG(DEBUG, TAG, "GroupSynchronization::leaveGroup : " \ + "To unregister resource was successful."); + } + else + { + OC_LOG_V(DEBUG, TAG, "GroupSynchronization::leaveGroup : " \ + "To unregister resource was unsuccessful. result - %d", result); + return result; + } + } catch(OCException &e) { - cout << "GroupSynchronization::leaveGroup : " - << "To unregister resource was unsuccessful. result - " << result - << endl; + OC_LOG_V(DEBUG, TAG, "ERROR : %s", e.reason().c_str()); + return OC_STACK_NO_RESOURCE; } } else { - cout << "GroupSynchronization::leaveGroup : " - << "This resource cannot be unregistered." << endl; + OC_LOG(DEBUG, TAG, "GroupSynchronization::leaveGroup : " \ + "This resource cannot be unregistered."); deviceResourceHandleList.erase(It); } auto handleListIt = childResourceHandleList.find(collectionResHandle); if (handleListIt == childResourceHandleList.end()) { - cout << "GroupSynchronization::leaveGroup : " - << "Error! There is no child resource list to delete." << endl; + OC_LOG(DEBUG, TAG, "GroupSynchronization::leaveGroup : " \ + "Error! There is no child resource list to delete."); return OC_STACK_INVALID_PARAM; } @@ -375,8 +423,8 @@ OCStackResult GroupSynchronization::leaveGroup(std::string collectionResourceTyp auto childIt = std::find(childList.begin(), childList.end(), resourceHandle); if (childIt != childList.end()) { - cout << "GroupSynchronization::groupEntityHandler : " - << "Found! The resource to leave is found." << endl; + OC_LOG(DEBUG, TAG, "GroupSynchronization::groupEntityHandler : " \ + "Found! The resource to leave is found."); childList.erase(childIt); } @@ -384,90 +432,127 @@ OCStackResult GroupSynchronization::leaveGroup(std::string collectionResourceTyp 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); + OC_LOG(DEBUG, TAG, "GroupSynchronization::joinGroup : Error! Input params are wrong."); + 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; - } + OC_LOG(DEBUG, TAG, "GroupSynchronization::joinGroup"); - std::shared_ptr< OCResource > resource = resourceIt->second; -// 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); + + OCResource::Ptr groupSyncResource; + groupSyncResource = OCPlatform::constructResourceObject(host, uri, + OC_ALL, false, + resourceTypes, resourceInterface); + // groupSyncResource = OCPlatform::constructResourceObject(host, uri, + // OC_WIFI, false, resourceTypes, resourceInterface); + + // making representation to leave group + std::string method = "leaveGroup"; +// std::string type = OCGetResourceTypeName(collectionResourceType, 0); + std::string resourceType; + resourceType.append(OCGetResourceTypeName(resourceHandle, 0)); + + OCRepresentation rep; + rep.setValue("method", method); + rep.setValue("collectionResourceType", collectionResourceType); + rep.setValue("resourceType", resourceType); + + OC_LOG_V(DEBUG, TAG, "\tmethod - %s", method.c_str()); + OC_LOG_V(DEBUG, TAG, "\tcollectionResourceType - %s", collectionResourceType.c_str()); + OC_LOG_V(DEBUG, TAG, "\tresourceType - %s", resourceType.c_str()); + + QueryParamsMap queryParamsMap; + + // 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) + { + OC_LOG(DEBUG, TAG, "GroupSynchronization::leaveGroup : " \ + "groupSyncResource->put was successful."); + } + else + { + OC_LOG_V(DEBUG, TAG, "GroupSynchronization::leaveGroup : " \ + "groupSyncResource->put was unsuccessful. result - %d", result); + } + return result; + } - // making representation to leave group - std::string method = "leaveGroup"; - std::string type = OCGetResourceTypeName(collectionResHandle, 0); - std::string resourceType; - resourceType.append(OCGetResourceTypeName(resourceHandle, 0)); + void GroupSynchronization::deleteGroup(std::string collectionResourceType) + { + if (0 == collectionResourceType.length()) + { + OC_LOG(DEBUG, TAG, + "GroupSynchronization::deleteGroup : Error! Input params are wrong."); + return; + } - OCRepresentation rep; - rep.setValue("method", method); - rep.setValue("collectionResourceType", type); - rep.setValue("resourceType", resourceType); + OC_LOG(DEBUG, TAG, "GroupSynchronization::deleteGroup"); - cout << "\tmethod - " << method << endl; - cout << "\tcollectionResourceType - " << type << endl; - cout << "\tresourceType - " << resourceType << endl; + OCStackResult result; + OCResourceHandle resourceHandle; - QueryParamsMap queryParamsMap; + auto handleIt = groupSyncResourceHandleList.find(collectionResourceType); - // 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 groupSyncResourceHandleList has resourceType, + // group sync of this app created collection resource. + if (handleIt != groupSyncResourceHandleList.end()) + { + resourceHandle = handleIt->second; // group sync resource handle + result = OCPlatform::unregisterResource(resourceHandle); if (OC_STACK_OK == result) { - cout << "GroupSynchronization::leaveGroup : " - << "groupSyncResource->put was successful." << endl; + OC_LOG(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \ + "To unregister group sync resource handle was successful."); } else { - cout << "GroupSynchronization::leaveGroup : " - << "groupSyncResource->put was unsuccessful. result - " << result - << endl; + OC_LOG_V(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \ + "To unregister group sync resource handle was unsuccessful. " \ + "result - %d", result); } - // deleting all remote resources. These are copied in onGetJoinedRemoteChild() - deleteGroup(collectionResourceType); + groupSyncResourceHandleList.erase(handleIt); } - } - else - { - cout << "GroupSynchronization::leaveGroup : Error! Input params are wrong." << endl; - return OC_STACK_INVALID_PARAM; - } - - return OC_STACK_OK; -} - -void GroupSynchronization::deleteGroup(std::string collectionResourceType) -{ - if (0 != collectionResourceType.length()) - { - cout << "GroupSynchronization::deleteGroup" << endl; - OCStackResult result; + auto resourceIt = groupSyncResourceList.find(collectionResourceType); + if (resourceIt != groupSyncResourceList.end()) + { + groupSyncResourceList.erase(resourceIt); + } - auto handleIt = collectionResourceHandleList.find(collectionResourceType); + handleIt = collectionResourceHandleList.find(collectionResourceType); if (handleIt == collectionResourceHandleList.end()) { - cout << "GroupSynchronization::deleteGroup : " - << "Error! There is no collection resource handle to delete." << endl; + OC_LOG(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \ + "Error! There is no collection resource handle to delete."); return; } OCResourceHandle collectionResHandle = handleIt->second; @@ -477,10 +562,26 @@ void GroupSynchronization::deleteGroup(std::string collectionResourceType) auto handleListIt = childResourceHandleList.find(collectionResHandle); if (handleListIt == childResourceHandleList.end()) { - cout << "GroupSynchronization::deleteGroup : " - << "Error! There is no child resource list to delete." << endl; + OC_LOG(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \ + "There is no child resource list to delete."); + + result = OCPlatform::unregisterResource(collectionResHandle); + if (result == OC_STACK_OK) + { + OC_LOG(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \ + "To unregister collection resource handle was successful."); + } + else + { + OC_LOG_V(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \ + " To unregister collection resource handle was unsuccessful. result - %d", + result); + } + + debugGroupSync(); return; } + std::vector< OCResourceHandle > childList = handleListIt->second; childResourceHandleList.erase(handleListIt); @@ -488,29 +589,28 @@ void GroupSynchronization::deleteGroup(std::string collectionResourceType) result = OCPlatform::unbindResources(collectionResHandle, childList); if (OC_STACK_OK == result) { - cout << "GroupSynchronization::deleteGroup : " - << "To unbind resources was successful." << endl; + OC_LOG(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \ + "To unbind resources was successful."); } else { - cout << "GroupSynchronization::deleteGroup : " - << "To unbind resources was unsuccessful. result - " << result << endl; + OC_LOG_V(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \ + "To unbind resources was unsuccessful. result - %d", result); } result = OCPlatform::unregisterResource(collectionResHandle); - if (result != OC_STACK_OK) + if (result == OC_STACK_OK) { - cout << "GroupSynchronization::deleteGroup : " - << "To unregister collection resource handle was successful." << endl; + OC_LOG(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \ + "To unregister collection resource handle was successful."); } else { - cout << "GroupSynchronization::deleteGroup : " - << " To unregister collection resource handle was unsuccessful. result - " - << result << endl; + OC_LOG_V(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \ + " To unregister collection resource handle was unsuccessful. result - %d", + result); } - OCResourceHandle resourceHandle; std::vector< OCResourceHandle >::iterator It; for (unsigned int i = 0; i < childList.size(); i++) @@ -528,556 +628,513 @@ void GroupSynchronization::deleteGroup(std::string collectionResourceType) result = OCPlatform::unregisterResource(resourceHandle); if (OC_STACK_OK == result) { - cout << "GroupSynchronization::deleteGroup : UnregisterResource(" << i + 1 - << ") was successful." << endl; + OC_LOG_V(DEBUG, TAG, + "GroupSynchronization::deleteGroup : UnregisterResource(%d)" \ + " was successful.", i + 1); } else { - cout << "GroupSynchronization::deleteGroup : UnregisterResource(" << i + 1 - << ") was unsuccessful. result - " << result << endl; + OC_LOG_V(DEBUG, TAG, + "GroupSynchronization::deleteGroup : UnregisterResource(%d)" \ + " was unsuccessful. result - ", i + 1, result); } } } - handleIt = groupSyncResourceHandleList.find(collectionResourceType); - - // if groupSyncResourceHandleList has resourceType, - // group sync of this app created collection resource. - if (handleIt != groupSyncResourceHandleList.end()) - { - resourceHandle = handleIt->second; // group sync resource handle - result = OCPlatform::unregisterResource(resourceHandle); - if (OC_STACK_OK == result) - { - cout << "GroupSynchronization::deleteGroup : " - << "To unregister group sync resource handle was successful." << endl; - } - else - { - cout << "GroupSynchronization::deleteGroup : " - << "To unregister group sync resource handle was unsuccessful. " - << "result - " << result << endl; - } - - groupSyncResourceHandleList.erase(handleIt); - } - - auto resourceIt = groupSyncResourceList.find(collectionResourceType); - if (resourceIt != groupSyncResourceList.end()) - { - groupSyncResourceList.erase(resourceIt); - } - debugGroupSync(); } - else + + std::map< std::string, OCResourceHandle > GroupSynchronization::getGroupList() { - cout << "GroupSynchronization::deleteGroup : Error! Input params are wrong." << endl; + return collectionResourceHandleList; } -} -std::map< std::string, OCResourceHandle > GroupSynchronization::getGroupList() -{ - return collectionResourceHandleList; -} - -OCEntityHandlerResult GroupSynchronization::groupEntityHandler( - const std::shared_ptr< OCResourceRequest > request) -{ - cout << "GroupSynchronization::groupEntityHandler\n"; - - if (request) + OCEntityHandlerResult GroupSynchronization::groupEntityHandler( + const std::shared_ptr< OCResourceRequest > request) { - // Get the request type and request flag - std::string requestType = request->getRequestType(); - int requestFlag = request->getRequestHandlerFlag(); + OC_LOG(DEBUG, TAG, "GroupSynchronization::groupEntityHandler"); - if (requestFlag == RequestHandlerFlag::InitFlag) + if (request) { - cout << "\trequestFlag : Init\n"; + // Get the request type and request flag + std::string requestType = request->getRequestType(); + int requestFlag = request->getRequestHandlerFlag(); - // entity handler to perform resource initialization operations - } - else if (requestFlag == RequestHandlerFlag::RequestFlag) - { - cout << "\trequestFlag : Request\n"; - - // If the request type is GET - if (requestType == "GET") - { - cout << "\t\trequestType : GET\n"; - } - else if (requestType == "PUT") + if (requestFlag == RequestHandlerFlag::RequestFlag) { - cout << "\t\trequestType : PUT\n"; - - //get method name, group resource type and resource type to join group - OCRepresentation rp = request->getResourceRepresentation(); - std::string methodType = rp.getValue< std::string >("method"); - std::string collectionResourceType = rp.getValue< std::string >( - "collectionResourceType"); - std::string resourceType = rp.getValue< std::string >("resourceType"); - - cout << "\t\t\tmethod : " << methodType << endl; - cout << "\t\t\tcollection resourceType : " << collectionResourceType << endl; - cout << "\t\t\tresourceType : " << resourceType << endl; - - auto handleIt = collectionResourceHandleList.find(collectionResourceType); - if (handleIt == collectionResourceHandleList.end()) - { - cout << "GroupSynchronization::groupEntityHandler : " - << "Error! There is no collection resource handle to delete." - << endl; - return OC_EH_ERROR; - } - collectionResourceHandle = handleIt->second; - // in case of join group it is used in onFindResource() + OC_LOG(DEBUG, TAG, "\trequestFlag : Request"); - if (methodType == "joinGroup") + // If the request type is GET + if (requestType == "GET") { - std::string resourceName = "coap://224.0.1.187/oc/core?rt="; - resourceName += resourceType; - cout << "\t\t\tresourceName : " << resourceName << endl; - - resourceRequest = request; - - OCPlatform::findResource("", resourceName, - std::bind(&GroupSynchronization::onFindResource, this, - std::placeholders::_1)); + OC_LOG(DEBUG, TAG, "\t\trequestType : GET"); } - else if (methodType == "leaveGroup") + else if (requestType == "PUT") { - auto it = childResourceHandleList.find(collectionResourceHandle); - if (it == childResourceHandleList.end()) + OC_LOG(DEBUG, TAG, "\t\trequestType : PUT"); + + //get method name, group resource type and resource type to join group + OCRepresentation rp = request->getResourceRepresentation(); + std::string methodType = rp.getValue< std::string >("method"); + std::string collectionResourceType = rp.getValue< std::string >( + "collectionResourceType"); + std::string resourceType = rp.getValue< std::string >("resourceType"); + + OC_LOG_V(DEBUG, TAG, "\t\t\tmethod : %s", methodType.c_str()); + OC_LOG_V(DEBUG, TAG, "\t\t\tcollection resourceType : %s", + collectionResourceType.c_str()); + OC_LOG_V(DEBUG, TAG, "\t\t\tresourceType : %s", resourceType.c_str()); + + auto handleIt = collectionResourceHandleList.find(collectionResourceType); + if (handleIt == collectionResourceHandleList.end()) { - cout << "GroupSynchronization::groupEntityHandler : " - << "Error! There is no child resource list." << endl; + OC_LOG(DEBUG, TAG, "GroupSynchronization::groupEntityHandler : " \ + "Error! There is no collection resource handle to delete."); return OC_EH_ERROR; } + collectionResourceHandle = handleIt->second; + // in case of join group it is used in onFindResource() - std::vector< OCResourceHandle > childList = it->second; - OCResourceHandle resourceHandle; - for (auto childIt = childList.begin(); childIt != childList.end();) + if (methodType == "joinGroup") { - resourceHandle = (*childIt); - char* type = (char*) OCGetResourceTypeName(resourceHandle, 0); + std::string resourceName = OC_MULTICAST_DISCOVERY_URI; + resourceName.append("?rt="); + resourceName.append(resourceType); + OC_LOG_V(DEBUG, TAG, "\t\t\tresourceName : %s", resourceName.c_str()); - if (0 == resourceType.compare(type)) - { - cout << "GroupSynchronization::groupEntityHandler : " - << "Found! The resource to leave is found. - " << type - << endl; + resourceRequest = request; - childIt = childList.erase(childIt++); + OCPlatform::findResource("", resourceName, + OC_ALL, + std::bind(&GroupSynchronization::onFindResource, this, + std::placeholders::_1)); - OCStackResult result = OCPlatform::unbindResource( - collectionResourceHandle, resourceHandle); - if (OC_STACK_OK == result) - { - cout << "GroupSynchronization::groupEntityHandler : " - << "To unbind resource was successful." << endl; - } - else - { - cout << "GroupSynchronization::groupEntityHandler : " - << "To unbind resource was unsuccessful. result - " - << result << endl; - } + return OC_EH_SLOW; + } + else if (methodType == "leaveGroup") + { + auto it = childResourceHandleList.find(collectionResourceHandle); + if (it == childResourceHandleList.end()) + { + OC_LOG(DEBUG, TAG, "GroupSynchronization::groupEntityHandler : " \ + "Error! There is no child resource list."); + return OC_EH_ERROR; + } - result = OCPlatform::unregisterResource(resourceHandle); - if (OC_STACK_OK == result) + std::vector< OCResourceHandle > childList = it->second; + OCResourceHandle resourceHandle; + for (auto childIt = childList.begin(); childIt != childList.end();) + { + resourceHandle = (*childIt); + char* type = (char*) OCGetResourceTypeName(resourceHandle, 0); + + if (0 == resourceType.compare(type)) { - cout << "GroupSynchronization::groupEntityHandler : " - << "To unregister resource was successful." << endl; + OC_LOG_V(DEBUG, TAG, + "GroupSynchronization::groupEntityHandler : " \ + "Found! The resource to leave is found. - %s", type); + + childIt = childList.erase(childIt++); + + OCStackResult result = OCPlatform::unbindResource( + collectionResourceHandle, resourceHandle); + if (OC_STACK_OK == result) + { + OC_LOG(DEBUG, TAG, + "GroupSynchronization::groupEntityHandler : " \ + "To unbind resource was successful."); + } + else + { + OC_LOG_V(DEBUG, TAG, + "GroupSynchronization::groupEntityHandler : " \ + "To unbind resource was unsuccessful. result - %d", + result); + } + + result = OCPlatform::unregisterResource(resourceHandle); + if (OC_STACK_OK == result) + { + OC_LOG(DEBUG, TAG, + "GroupSynchronization::groupEntityHandler : " \ + "To unregister resource was successful."); + } + else + { + OC_LOG_V(DEBUG, TAG, + "GroupSynchronization::groupEntityHandler : " + "To unregister resource was unsuccessful. result - %d", + result); + } } else { - cout << "GroupSynchronization::groupEntityHandler : " - << "To unregister resource was unsuccessful. result - " - << result << endl; + ++childIt; } -// break; - } - else - { - ++childIt; } - } + childResourceHandleList[collectionResourceHandle] = childList; - childResourceHandleList[collectionResourceHandle] = childList; + debugGroupSync(); - debugGroupSync(); + auto pResponse = std::make_shared< OC::OCResourceResponse >(); + pResponse->setRequestHandle(request->getRequestHandle()); + pResponse->setResourceHandle(request->getResourceHandle()); + pResponse->setErrorCode(200); + pResponse->setResponseResult(OC_EH_OK); - auto pResponse = std::make_shared< OC::OCResourceResponse >(); - pResponse->setRequestHandle(request->getRequestHandle()); - pResponse->setResourceHandle(request->getResourceHandle()); - pResponse->setErrorCode(200); - pResponse->setResponseResult(OC_EH_OK); + OCRepresentation rep = request->getResourceRepresentation(); + pResponse->setResourceRepresentation(rep, DEFAULT_INTERFACE); + if (OC_STACK_OK == OCPlatform::sendResponse(pResponse)) + { + OC_LOG(DEBUG, TAG, "GroupSynchronization::groupEntityHandler : " \ + "sendResponse is successful."); + } + } - OCRepresentation rep = request->getResourceRepresentation(); - pResponse->setResourceRepresentation(rep, DEFAULT_INTERFACE); - if (OC_STACK_OK == OCPlatform::sendResponse(pResponse)) + if (methodType != "") //TODO: Check groupmethodtype NULL { - cout << "GroupSynchronization::groupEntityHandler : " - << "sendResponse is successful." << endl; } } - - if (methodType != "") //TODO: Check groupmethodtype NULL + else if (requestType == "POST") { + // POST request operations + } + else if (requestType == "DELETE") + { + // DELETE request operations } } - else if (requestType == "POST") - { - // POST request operations - } - else if (requestType == "DELETE") + else if (requestFlag == RequestHandlerFlag::ObserverFlag) { - // DELETE request operations + OC_LOG(DEBUG, TAG, "\trequestFlag : Observer"); } } - else if (requestFlag == RequestHandlerFlag::ObserverFlag) + else { - cout << "\trequestFlag : Observer\n"; + OC_LOG(DEBUG, TAG, "Request invalid"); } - } - else - { - std::cout << "Request invalid" << std::endl; - } - return OC_EH_OK; -} - -void GroupSynchronization::onFindGroup(std::shared_ptr< OCResource > resource) -{ - cout << "GroupSynchronization::onFindGroup" << endl; + return OC_EH_OK; + } - try + void GroupSynchronization::onFindGroup(std::shared_ptr< OCResource > resource) { - if (resource) + OC_LOG(DEBUG, TAG, "GroupSynchronization::onFindGroup"); + + try { -////////////////////////////////////////////////////////////////////////////////////////////////// -////////////debugging - std::string resourceURI; - std::string hostAddress; + if (resource) + { + // Debugging + std::string resourceURI; + std::string hostAddress; - // Get the resource URI - resourceURI = resource->uri(); - cout << "\tURI of the resource: " << resourceURI << endl; + // Get the resource URI + resourceURI = resource->uri(); + OC_LOG_V(DEBUG, TAG, "\tURI of the resource: %s", resourceURI.c_str()); - // Get the resource host address - hostAddress = resource->host(); - cout << "\tHost address of the resource: " << hostAddress << endl; + // Get the resource host address + hostAddress = resource->host(); + OC_LOG_V(DEBUG, TAG, "\tHost address of the resource: %s", hostAddress.c_str()); - hostAddress.append(resourceURI); + hostAddress.append(resourceURI); - // Get the resource types - cout << "\tList of resource types: " << endl; +#ifndef NDEBUG + // Get the resource types + OC_LOG(DEBUG, TAG, "\tList of resource types: "); + for (auto &resourceTypes : resource->getResourceTypes()) + { + OC_LOG_V(DEBUG, TAG, "\t\t", resourceTypes.c_str()); + } - for (auto &resourceTypes : resource->getResourceTypes()) - { - cout << "\t\t" << resourceTypes << endl; - } + // Get the resource interfaces + OC_LOG(DEBUG, TAG, "\tList of resource interfaces: "); + for (auto &resourceInterfaces : resource->getResourceInterfaces()) + { + OC_LOG_V(DEBUG, TAG, "\t\t", resourceInterfaces.c_str()); + } +#endif - // Get the resource interfaces - cout << "\tList of resource interfaces: " << endl; - for (auto &resourceInterfaces : resource->getResourceInterfaces()) - { - cout << "\t\t" << resourceInterfaces << endl; + if (false == IsSameGroup(resource)) + { + saveGroup(resource); + findCallback(resource); + } } -////////////////////////////////////////////////////////////////////////////////////////////////// - - if (false == IsSameGroup(resource)) + else { - saveGroup(resource); - findCallback(resource); + // Resource is invalid + OC_LOG(DEBUG, TAG, "Resource is invalid"); + findCallback(NULL); } + + bIsFinding = false; } - else + catch (std::exception& e) { - // Resource is invalid - cout << "Resource is invalid" << endl; - findCallback(NULL); + //log(e.what()); } - } - catch (std::exception& e) + + void GroupSynchronization::checkFindGroup(void) { - //log(e.what()); - } -} + OC_LOG(DEBUG, TAG, "GroupSynchronization::checkFindGroup"); -void GroupSynchronization::checkFindGroup(void) -{ - cout << "GroupSynchronization::checkFindGroup" << endl; + for (int i = 0; i < 15; i++) + { + std::chrono::milliseconds workTime(300); + std::this_thread::sleep_for(workTime); - for (int i = 0; i < 15; i++) - { - std::chrono::milliseconds workTime(300); - std::this_thread::sleep_for(workTime); + std::lock_guard < std::mutex > guard(foundGroupMutex); + + if (false == foundGroupResourceList.empty()) + { + OC_LOG(DEBUG, TAG, "GroupSynchronization::checkFoundGroup : " \ + "Some group is received."); + return; + } + } + OC_LOG(DEBUG, TAG, "GroupSynchronization::checkFoundGroup : " \ + "It is failed to find resource within 3s."); + + onFindGroup(NULL); + return; + } + + bool GroupSynchronization::IsSameGroup(std::shared_ptr< OCResource > resource) + { std::lock_guard < std::mutex > guard(foundGroupMutex); - if (false == foundGroupResourceList.empty()) + if (true == foundGroupResourceList.empty()) { - cout << "GroupSynchronization::checkFoundGroup : " << "Some group is received." - << endl; - return; + OC_LOG(DEBUG, TAG, "GroupSynchronization::IsSameGroup : There is no found group."); + return false; } - } - cout << "GroupSynchronization::checkFoundGroup : " - << "It is failed to find resource within 3s." << endl; + std::string foundHostAddress, savedHostAddress; + std::string foundHostUri, savedHostUri; + foundHostAddress = resource->host(); + foundHostUri = resource->uri(); - onFindGroup(NULL); - return; -} + for (unsigned int i = 0; i < foundGroupResourceList.size(); ++i) + { + savedHostAddress = (foundGroupResourceList.at(i))->host(); + savedHostUri = (foundGroupResourceList.at(i))->uri(); -bool GroupSynchronization::IsSameGroup(std::shared_ptr< OCResource > resource) -{ - std::lock_guard < std::mutex > guard(foundGroupMutex); + if (0 == foundHostAddress.compare(savedHostAddress.c_str())) + { + if( 0 == foundHostUri.compare(savedHostAddress) ) + { + OC_LOG(DEBUG, TAG, + "GroupSynchronization::IsSameGroup : Found! The same group is found."); + return true; + } + } + } - if (true == foundGroupResourceList.empty()) - { - cout << "GroupSynchronization::IsSameGroup : There is no found group." << endl; + OC_LOG(DEBUG, TAG, "GroupSynchronization::IsSameGroup : There is no same group."); return false; } - std::string foundHostAddress, savedHostAddress; - foundHostAddress = resource->host(); -// foundHostAddress.append (resource->uri()); - - for (unsigned int i = 0; i < foundGroupResourceList.size(); ++i) + void GroupSynchronization::saveGroup(std::shared_ptr< OCResource > resource) { - savedHostAddress = (foundGroupResourceList.at(i))->host(); -// savedHostAddress.append ((foundGroupResourceList.at(i))->uri()); -// cout << "GroupSynchronization::IsSameGroup : foundHostAddress - " << foundHostAddress -// << ", savedHostAddress - " << savedHostAddress << endl; + OC_LOG(DEBUG, TAG, "GroupSynchronization::saveGroup"); - if (0 == foundHostAddress.compare(savedHostAddress.c_str())) + std::lock_guard < std::mutex > guard(foundGroupMutex); + + foundGroupResourceList.push_back(resource); + } + + void GroupSynchronization::onJoinGroup(const HeaderOptions& headerOptions, + const OCRepresentation& rep, const int eCode) + { + if (eCode != OC_STACK_OK) { - cout << "GroupSynchronization::IsSameGroup : Found! The same group is found." - << endl; - return true; + OC_LOG_V(DEBUG, TAG, "GroupSynchronization::onJoinGroup : error - %d", eCode); + return; } } - cout << "GroupSynchronization::IsSameGroup : There is no same group." << endl; - return false; -} + void GroupSynchronization::onFindResource(std::shared_ptr< OCResource > resource) + { + OC_LOG(DEBUG, TAG, "GroupSynchronization::onFindResource"); -void GroupSynchronization::saveGroup(std::shared_ptr< OCResource > resource) -{ - cout << "GroupSynchronization::saveGroup" << endl; + if (resource) + { + if(NULL == collectionResourceHandle) + return; - std::lock_guard < std::mutex > guard(foundGroupMutex); + // start of debugging + std::string resourceURI; + std::string hostAddress; - foundGroupResourceList.push_back(resource); -} + // Get the resource URI + resourceURI = resource->uri(); + OC_LOG_V(DEBUG, TAG, "\tURI of the resource: %s", resourceURI.c_str()); -void GroupSynchronization::onJoinGroup(const HeaderOptions& headerOptions, - const OCRepresentation& rep, const int eCode) -{ - if (eCode == OC_STACK_OK) - { - cout << "GroupSynchronization::onJoinGroup : " << endl; + // Get the resource host address + hostAddress = resource->host(); + OC_LOG_V(DEBUG, TAG, "\tHost address of the resource: %s", hostAddress.c_str()); - 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) + hostAddress.append(resourceURI); + +#ifndef NDEBUG + // Get the resource types + OC_LOG(DEBUG, TAG, "\tList of resource types: "); + for (auto &resourceTypes : resource->getResourceTypes()) { - cout << "GroupSynchronization::onJoinGroup : " - << "remoteCollectionResource->get was successful." << endl; + OC_LOG_V(DEBUG, TAG, "\t\t%s", resourceTypes.c_str()); } - else + + // Get the resource interfaces + OC_LOG(DEBUG, TAG, "\tList of resource interfaces: "); + for (auto &resourceInterfaces : resource->getResourceInterfaces()) { - cout << "GroupSynchronization::onJoinGroup : " - << "remoteCollectionResource->get was unsuccessful. result - " << result - << endl; + OC_LOG_V(DEBUG, TAG, "\t\t%s", resourceInterfaces.c_str()); } - } - } - else - { - cout << "GroupSynchronization::onJoinGroup : error - " << eCode << endl; - } -} + // end of debugging +#endif -void GroupSynchronization::onFindResource(std::shared_ptr< OCResource > resource) -{ - cout << "GroupSynchronization::onFindResource" << endl; + OCResourceHandle resourceHandle; + OCStackResult result = OCPlatform::registerResource(resourceHandle, resource); + if (result != OC_STACK_OK) + { + OC_LOG_V(DEBUG, TAG, "GroupSynchronization::" \ + "onFindResource - Resource to join creation was unsuccessful. result - %d", + result); + return; + } - if (resource) - { -////////////////////////////////////////////////////////////////////////////////////////////////// -////////// debugging - std::string resourceURI; - std::string hostAddress; + result = OCPlatform::bindResource(collectionResourceHandle, resourceHandle); + if (result != OC_STACK_OK) + { + OC_LOG_V(DEBUG, TAG, "GroupSynchronization::onFindResource : " \ + "To bind resource was unsuccessful. result - %d", result); + return; + } - // Get the resource URI - resourceURI = resource->uri(); - cout << "\tURI of the resource: " << resourceURI << endl; + OC_LOG(DEBUG, TAG, "GroupSynchronization::onFindResource : " \ + "To bind joinGroupHandle and resourceHandle was successful."); - // Get the resource host address - hostAddress = resource->host(); - cout << "\tHost address of the resource: " << hostAddress << endl; + auto it = childResourceHandleList.find(collectionResourceHandle); + std::vector< OCResourceHandle > childHandleList; + if (it != childResourceHandleList.end()) + { + childHandleList = it->second; + } - hostAddress.append(resourceURI); + childHandleList.push_back(resourceHandle); + childResourceHandleList[collectionResourceHandle] = childHandleList; - // Get the resource types - cout << "\tList of resource types: " << endl; + auto pResponse = std::make_shared< OC::OCResourceResponse >(); + pResponse->setRequestHandle(resourceRequest->getRequestHandle()); + pResponse->setResourceHandle(resourceRequest->getResourceHandle()); + pResponse->setErrorCode(200); + pResponse->setResponseResult(OC_EH_OK); - for (auto &resourceTypes : resource->getResourceTypes()) - { - cout << "\t\t" << resourceTypes << endl; + OCRepresentation rep = resourceRequest->getResourceRepresentation(); + pResponse->setResourceRepresentation(rep); + try{ + if (OC_STACK_OK == OCPlatform::sendResponse(pResponse)) + { + OC_LOG(DEBUG, TAG, + "GroupSynchronization::onFindResource : sendResponse is successful."); + } + } + catch( OCException &e ) + { + // OC_LOG(DEBUG, TAG, e.what); + return; + } } - - // Get the resource interfaces - cout << "\tList of resource interfaces: " << endl; - for (auto &resourceInterfaces : resource->getResourceInterfaces()) + else { - cout << "\t\t" << resourceInterfaces << endl; + OC_LOG(DEBUG, TAG, "GroupSynchronization::onFindResource : " \ + "Resource is invalid. So a new Group Resource has to be created."); } -////////////////////////////////////////////////////////////////////////////////////////////////// - OCResourceHandle resourceHandle; - OCStackResult result = OCPlatform::registerResource(resourceHandle, resource); - if (result != OC_STACK_OK) - { - cout << "GroupSynchronization::" - << "onFindResource - Resource to join creation was unsuccessful. result - " - << result << endl; - return; - } -// cout << "GroupSynchronization::onFindResource : creating resourceHandle. resource type - " -// << OCGetResourceTypeName(resourceHandle, 0) << endl; + debugGroupSync(); + } - result = OCPlatform::bindResource(collectionResourceHandle, resourceHandle); - if (result != OC_STACK_OK) + void GroupSynchronization::onGetJoinedRemoteChild(const HeaderOptions& headerOptions, + const OCRepresentation& rep, const int eCode) + { + if (eCode != OC_STACK_OK) { - cout << "GroupSynchronization::onFindResource : " - << "To bind resource was unsuccessful. result - " << result << endl; + OC_LOG_V(DEBUG, TAG, + "GroupSynchronization::onGetJoinedRemoteChild : error - %d", eCode); return; } - cout << "GroupSynchronization::onFindResource : " - << "To bind joinGroupHandle and resourceHandle was successful." << endl; - - auto it = childResourceHandleList.find(collectionResourceHandle); - std::vector< OCResourceHandle > childHandleList; - if (it != childResourceHandleList.end()) - { - childHandleList = it->second; - } - childHandleList.push_back(resourceHandle); - childResourceHandleList[collectionResourceHandle] = childHandleList; - - auto pResponse = std::make_shared< OC::OCResourceResponse >(); - pResponse->setRequestHandle(resourceRequest->getRequestHandle()); - pResponse->setResourceHandle(resourceRequest->getResourceHandle()); - pResponse->setErrorCode(200); - pResponse->setResponseResult(OC_EH_OK); - - OCRepresentation rep = resourceRequest->getResourceRepresentation(); - pResponse->setResourceRepresentation(rep, DEFAULT_INTERFACE); - if (OC_STACK_OK == OCPlatform::sendResponse(pResponse)) - { - cout << "GroupSynchronization::onFindResource : sendResponse is successful." - << endl; - } - } - else - { - cout << "GroupSynchronization::onFindResource : " - << "Resource is invalid. So a new Group Resource has to be created." << endl; - } - - debugGroupSync(); -} - -void GroupSynchronization::onGetJoinedRemoteChild(const HeaderOptions& headerOptions, - const OCRepresentation& rep, const int eCode) -{ - if (eCode == OC_STACK_OK) - { - cout << "GroupSynchronization::onGetJoinedRemoteChild" << endl; - -////////////////////////////////////////////////////////////////////////////////////////////////// -////////// debugging std::string resourceURI; + std::vector< OCRepresentation > childList; + OCRepresentation child; + OC_LOG(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild"); +#ifndef NDEBUG + // debugging // Get the resource URI resourceURI = rep.getUri(); - cout << "\tURI of the resource: " << resourceURI << endl; + OC_LOG_V(DEBUG, TAG, "\tURI of the resource: %s", resourceURI.c_str()); // Get the resource types - cout << "\tList of resource types: " << endl; + OC_LOG(DEBUG, TAG, "\tList of resource types: "); for (auto &resourceTypes : rep.getResourceTypes()) { - cout << "\t\t" << resourceTypes << endl; + OC_LOG_V(DEBUG, TAG, "\t\t%s", resourceTypes.c_str()); } // Get the resource interfaces - cout << "\tList of resource interfaces: " << endl; + OC_LOG(DEBUG, TAG, "\tList of resource interfaces: "); for (auto &resourceInterfaces : rep.getResourceInterfaces()) { - cout << "\t\t" << resourceInterfaces << endl; + OC_LOG_V(DEBUG, TAG, "\t\t%s", resourceInterfaces.c_str()); } - std::vector< OCRepresentation > childList = rep.getChildren(); - OCRepresentation child; + childList = rep.getChildren(); for (unsigned int i = 0; i < childList.size(); ++i) { - cout << "\n\tchild resource - " << i + 1 << endl; + OC_LOG_V(DEBUG, TAG, "\tchild resource - %d", i + 1); child = childList.at(i); resourceURI = child.getUri(); - cout << "\t\tURI of the resource: " << resourceURI << endl; + OC_LOG_V(DEBUG, TAG, "\t\tURI of the resource: %s", resourceURI.c_str()); - cout << "\t\tList of resource types: " << endl; + OC_LOG_V(DEBUG, TAG, "\t\tList of resource types: "); for (auto &types : child.getResourceTypes()) { - cout << "\t\t\t" << types << endl; + OC_LOG_V(DEBUG, TAG, "\t\t\t%s", types.c_str()); } - cout << "\tList of resource interfaces: " << endl; + OC_LOG(DEBUG, TAG, "\tList of resource interfaces: "); for (auto &interfaces : child.getResourceInterfaces()) { - cout << "\t\t\t" << interfaces << endl; + OC_LOG_V(DEBUG, TAG, "\t\t\t%s", interfaces.c_str()); } } -////////////////////////////////////////////////////////////////////////////////////////////////// +#endif // creating remote collection resource handle OCResourceHandle remoteCollectionResourceHandle; resourceURI = remoteCollectionResource->uri(); std::vector< std::string > types = remoteCollectionResource->getResourceTypes(); - std::vector< std::string > interfaces = - remoteCollectionResource->getResourceInterfaces(); + std::vector< std::string > interfaces = remoteCollectionResource->getResourceInterfaces(); OCStackResult result = OCPlatform::registerResource(remoteCollectionResourceHandle, resourceURI, types[0], interfaces[0], NULL, OC_OBSERVABLE); if (result != OC_STACK_OK) { - cout << "GroupSynchronization::onGetJoinedRemoteChild - " - << "To register remoteCollectionResourceHandle" - << " was unsuccessful. result - " << result << endl; + OC_LOG_V(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild - " \ + "To register remoteCollectionResourceHandle" \ + " was unsuccessful. result - %d", result); return; } - cout << "GroupSynchronization::onGetJoinedRemoteChild : " - "To register remoteCollectionResourceHandle was successful." << endl; + OC_LOG(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild : " \ + "To register remoteCollectionResourceHandle was successful."); // binding remote collection resource handle and resource handle to join collectionResourceHandleList[types[0]] = remoteCollectionResourceHandle; @@ -1085,15 +1142,14 @@ void GroupSynchronization::onGetJoinedRemoteChild(const HeaderOptions& headerOpt result = OCPlatform::bindResource(remoteCollectionResourceHandle, deviceResourceHandle); if (OC_STACK_OK == result) { - cout << "GroupSynchronization::onGetJoinedRemoteChild : " - << "binding remoteCollectionResourceHandle and deviceResourceHandle" - << endl; + OC_LOG(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild : " \ + "binding remoteCollectionResourceHandle and deviceResourceHandle"); } else { - cout << "GroupSynchronization::onGetJoinedRemoteChild - " - << "To bind remoteCollectionResourceHandle and deviceResourceHandle " - << "was unsuccessful. result - " << result << endl; + OC_LOG_V(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild - " \ + "To bind remoteCollectionResourceHandle and deviceResourceHandle " \ + "was unsuccessful. result - %d", result); } std::vector< OCResourceHandle > childHandleList; @@ -1104,7 +1160,7 @@ void GroupSynchronization::onGetJoinedRemoteChild(const HeaderOptions& headerOpt OCResourceHandle resourceHandle; for (unsigned int i = 0; i < childList.size(); ++i) { - cout << "\tremote resource - " << i + 1 << endl; + OC_LOG_V(DEBUG, TAG, "\tremote resource - %d", i + 1); child = childList.at(i); resourceURI = child.getUri(); @@ -1113,8 +1169,8 @@ void GroupSynchronization::onGetJoinedRemoteChild(const HeaderOptions& headerOpt if (0 == types[0].compare(OCGetResourceTypeName(deviceResourceHandle, 0))) { - cout << "GroupSynchronization::onGetJoinedRemoteChild : " << types[0] - << " is bind already." << endl; + OC_LOG_V(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild : %s" \ + " is bind already.", types[0].c_str()); continue; } @@ -1122,156 +1178,151 @@ void GroupSynchronization::onGetJoinedRemoteChild(const HeaderOptions& headerOpt interfaces[0], NULL, OC_OBSERVABLE); if (OC_STACK_OK == result) { - result = OCPlatform::bindResource(remoteCollectionResourceHandle, - resourceHandle); + result = OCPlatform::bindResource(remoteCollectionResourceHandle, resourceHandle); if (result != OC_STACK_OK) { - cout << "GroupSynchronization::onGetJoinedRemoteChild - " - << "binding remoteCollectionResourceHandle and resourceHandle " - << "was unsuccessful. result - " << result << endl; + OC_LOG_V(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild - " \ + "binding remoteCollectionResourceHandle and resourceHandle " \ + "was unsuccessful. result - %d", result); OCPlatform::unregisterResource(resourceHandle); } childHandleList.push_back(resourceHandle); - cout << "GroupSynchronization::onGetJoinedRemoteChild : " - << "binding remoteCollectionResourceHandle and resourceHandle" << endl; + OC_LOG(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild : " \ + "binding remoteCollectionResourceHandle and resourceHandle"); } else { - cout << "GroupSynchronization::onGetJoinedRemoteChild - " - << "To register remoteCollectionResourceHandle was unsuccessful." - << " result - " << result << endl; + OC_LOG_V(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild - " \ + "To register remoteCollectionResourceHandle was unsuccessful." \ + " result - %d", result); } } childResourceHandleList[remoteCollectionResourceHandle] = childHandleList; // this handle list is used to leave group - } - else - { - cout << "GroupSynchronization::onGetJoinedRemoteChild : error - " << eCode << endl; - } - - debugGroupSync(); -} -void GroupSynchronization::onLeaveGroup(const HeaderOptions& headerOptions, - const OCRepresentation& rep, const int eCode) -{ - if (eCode == OC_STACK_OK) - { - cout << "GroupSynchronization::onLeaveGroup" << endl; - } - else - { - cout << "GroupSynchronization::onLeaveGroup : error - " << eCode << endl; + debugGroupSync(); } - debugGroupSync(); -} -void GroupSynchronization::debugGroupSync(void) -{ - cout << "GroupSynchronization::debugGroupSync" << endl; - - unsigned int i; - std::map< std::string, OCResourceHandle >::iterator handleIt; - std::map< OCResourceHandle, std::vector< OCResourceHandle > >::iterator childIt; - std::string type; - OCResourceHandle resourceHandle; - std::vector< OCResourceHandle > handleList; - std::shared_ptr< OCResource > resource; - - cout << "Resource Handle Created by App" << endl; - for (i = 0; i < deviceResourceHandleList.size(); i++) + void GroupSynchronization::onLeaveGroup(const HeaderOptions& headerOptions, + const OCRepresentation& rep, const int eCode) { - resourceHandle = deviceResourceHandleList.at(i); + if (eCode != OC_STACK_OK) + { + OC_LOG_V(DEBUG, TAG, "GroupSynchronization::onLeaveGroup : error - %d", eCode); + return; + } - cout << i + 1 << ". details" << endl; - cout << " uri - " << OCGetResourceUri(resourceHandle) << endl; - cout << " resource type - " << OCGetResourceTypeName(resourceHandle, 0) << endl; - cout << " resource interface - " << OCGetResourceInterfaceName(resourceHandle, 0) - << endl << endl; + OC_LOG(DEBUG, TAG, "GroupSynchronization::onLeaveGroup"); + debugGroupSync(); } - cout << "\nGroup Sync Resource Handle List. The number is " - << groupSyncResourceHandleList.size() << endl; - i = 1; - for (handleIt = groupSyncResourceHandleList.begin(); - handleIt != groupSyncResourceHandleList.end(); ++handleIt) + void GroupSynchronization::debugGroupSync(void) { - type = handleIt->first; - cout << "\t" << i << ". group sync resource type - " << type << endl; - cout << "\t details" << endl; - - resourceHandle = handleIt->second; - cout << "\t uri - " << OCGetResourceUri(resourceHandle) << endl; - cout << "\t resource type - " << OCGetResourceTypeName(resourceHandle, 0) << endl; - cout << "\t resource interface - " << OCGetResourceInterfaceName(resourceHandle, 0) - << endl << endl; - ; - i++; - } +#ifndef NDEBUG + OC_LOG(DEBUG, TAG, "GroupSynchronization::debugGroupSync"); - cout << "Copied Remote Group Sync Resource List. The number is " - << groupSyncResourceList.size() << endl; - std::vector< std::string > list; - i = 1; - for (auto resourceIt = groupSyncResourceList.begin(); - resourceIt != groupSyncResourceList.end(); ++resourceIt) - { - type = resourceIt->first; - cout << "\t" << i << ". group sync resource type - " << type << endl; - cout << "\t details" << endl; - - resource = resourceIt->second; - cout << "\t host - " << resource->host() << endl; - cout << "\t uri - " << resource->uri() << endl; - list = resource->getResourceTypes(); - cout << "\t resource type - " << list[0] << endl; - list = resource->getResourceInterfaces(); - cout << "\t resource interface - " << list[0] << endl << endl; - i++; - } + unsigned int i; + std::map< std::string, OCResourceHandle >::iterator handleIt; + std::map< OCResourceHandle, std::vector< OCResourceHandle > >::iterator childIt; + std::string type; + OCResourceHandle resourceHandle; + std::vector< OCResourceHandle > handleList; + std::shared_ptr< OCResource > resource; -// cout << "The number of collection Resource Handle is " << collectionResourceHandleList.size() -// << endl; -// cout << "The number of child resource handle list is " << childResourceHandleList.size() -// << endl; + OC_LOG(DEBUG, TAG, "Resource Handle Created by App"); + for (i = 0; i < deviceResourceHandleList.size(); i++) + { + resourceHandle = deviceResourceHandleList.at(i); + + OC_LOG_V(DEBUG, TAG, "%d. details", i + 1); + OC_LOG_V(DEBUG, TAG, "\turi - %s", OCGetResourceUri(resourceHandle)); + OC_LOG_V(DEBUG, TAG, "\tresource type - %s", + OCGetResourceTypeName(resourceHandle, 0)); + OC_LOG_V(DEBUG, TAG, "\tresource interface - %s", + OCGetResourceInterfaceName(resourceHandle, 0)); + } - cout << "Collection Resource Handle List" << endl; - i = 1; - for (handleIt = collectionResourceHandleList.begin(); - handleIt != collectionResourceHandleList.end(); ++handleIt) - { - type = handleIt->first; - cout << "\t" << i << ". collection resource type - " << type << endl; - cout << "\t details" << endl; + OC_LOG_V(DEBUG, TAG, "Group Sync Resource Handle List. The number is %d", + groupSyncResourceHandleList.size()); - resourceHandle = handleIt->second; - cout << "\t uri - " << OCGetResourceUri(resourceHandle) << endl; - cout << "\t resource type - " << OCGetResourceTypeName(resourceHandle, 0) << endl; - cout << "\t resource interface - " << OCGetResourceInterfaceName(resourceHandle, 0) - << endl << endl; + i = 1; + for (handleIt = groupSyncResourceHandleList.begin(); + handleIt != groupSyncResourceHandleList.end(); ++handleIt) + { + type = handleIt->first; + OC_LOG_V(DEBUG, TAG, "\t%d. group sync resource type - %s", i, type.c_str()); + OC_LOG(DEBUG, TAG, "\tdetails"); + + resourceHandle = handleIt->second; + OC_LOG_V(DEBUG, TAG, "\turi - %s", + OCGetResourceUri(resourceHandle)); + OC_LOG_V(DEBUG, TAG, "\tresource type - %s", + OCGetResourceTypeName(resourceHandle, 0)); + OC_LOG_V(DEBUG, TAG, "\tresource interface - %s", + OCGetResourceInterfaceName(resourceHandle, 0)); + + i++; + } - childIt = childResourceHandleList.find(resourceHandle); - if (childIt != childResourceHandleList.end()) + OC_LOG_V(DEBUG, TAG, "Copied Remote Group Sync Resource List. The number is %d", + groupSyncResourceList.size()); + std::vector< std::string > list; + i = 1; + for (auto resourceIt = groupSyncResourceList.begin(); + resourceIt != groupSyncResourceList.end(); ++resourceIt) { - handleList = childIt->second; - for (unsigned int j = 0; j < handleList.size(); j++) + type = resourceIt->first; + OC_LOG_V(DEBUG, TAG, "\t%d. group sync resource type - %s", i, type.c_str()); + OC_LOG(DEBUG, TAG, "\tdetails"); + + resource = resourceIt->second; + OC_LOG_V(DEBUG, TAG, "\thost - %s", resource->host().c_str()); + OC_LOG_V(DEBUG, TAG, "\turi - %s", resource->uri().c_str()); + list = resource->getResourceTypes(); + OC_LOG_V(DEBUG, TAG, "\tresource type - %s", list[0].c_str()); + list = resource->getResourceInterfaces(); + OC_LOG_V(DEBUG, TAG, "\tresource interface - %s", list[0].c_str()); + i++; + } + + OC_LOG(DEBUG, TAG, "Collection Resource Handle List"); + i = 1; + for (handleIt = collectionResourceHandleList.begin(); + handleIt != collectionResourceHandleList.end(); ++handleIt) + { + type = handleIt->first; + OC_LOG_V(DEBUG, TAG, "\t%d. collection resource type - %s", i, type.c_str()); + OC_LOG(DEBUG, TAG, "\tdetails"); + + resourceHandle = handleIt->second; + OC_LOG_V(DEBUG, TAG, "\turi - %s", OCGetResourceUri(resourceHandle)); + OC_LOG_V(DEBUG, TAG, "\tresource type - %s", + OCGetResourceTypeName(resourceHandle, 0)); + OC_LOG_V(DEBUG, TAG, "\tresource interface - %s", + OCGetResourceInterfaceName(resourceHandle, 0)); + + childIt = childResourceHandleList.find(resourceHandle); + if (childIt != childResourceHandleList.end()) { + handleList = childIt->second; + for (unsigned int j = 0; j < handleList.size(); j++) + { - cout << "\t\t" << j + 1 << ". child resource details" << endl; + OC_LOG_V(DEBUG, TAG, "\t\t%d. child resource details", j + 1); - resourceHandle = handleList.at(j); - cout << "\t\t uri - " << OCGetResourceUri(resourceHandle) << endl; - cout << "\t\t resource type - " << OCGetResourceTypeName(resourceHandle, 0) - << endl; - cout << "\t\t resource interface - " - << OCGetResourceInterfaceName(resourceHandle, 0) << endl << endl; + resourceHandle = handleList.at(j); + OC_LOG_V(DEBUG, TAG, "\t\turi - %s", + OCGetResourceUri(resourceHandle)); + OC_LOG_V(DEBUG, TAG, "\t\tresource type - %s", + OCGetResourceTypeName(resourceHandle, 0)); + OC_LOG_V(DEBUG, TAG, "\t\tresource interface - %s", + OCGetResourceInterfaceName(resourceHandle, 0)); + } } + i++; } - - i++; +#endif } } -}