Improve some components for simple and performance.
authorjyong2.kim <jyong2.kim@samsung.com>
Thu, 3 Mar 2016 07:47:30 +0000 (16:47 +0900)
committerUze Choi <uzchoi@samsung.com>
Sun, 6 Mar 2016 05:29:09 +0000 (05:29 +0000)
This patch is for apply offline code review comments by JungHo.

several method is made by duplicated code.
unnecessary valiable is removed.
lamda functions is used instead std::bind for reduce std::bind.
logic simplify for performance and readable.

Change-Id: Iefb0f4b3ee5ae99201127cd3343ea819f9f7fd3d
Signed-off-by: jyong2.kim <jyong2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/5335
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/scene-manager/src/Scene.cpp
service/scene-manager/src/SceneAction.cpp
service/scene-manager/src/SceneCollectionResource.cpp
service/scene-manager/src/SceneCollectionResource.h
service/scene-manager/src/SceneList.cpp
service/scene-manager/src/SceneListResource.cpp
service/scene-manager/src/SceneListResource.h
service/scene-manager/src/SceneMemberResource.cpp
service/scene-manager/src/SceneMemberResource.h

index 752341a..c42a9a2 100755 (executable)
@@ -61,7 +61,7 @@ namespace OIC
                                 if(RCSRemoteResourceObjectPtr == member->getRemoteResourceObject())
                                 {
                                     foundMember = member;
-                                    for(const auto &it: member->getMappingInfo())
+                                    for(const auto &it: member->getMappingInfos())
                                     {
                                         isRegistered = (it.sceneName == m_name);
                                     }
@@ -120,7 +120,7 @@ namespace OIC
                                 if(RCSRemoteResourceObjectPtr == member->getRemoteResourceObject())
                                 {
                                     foundMember = member;
-                                for(const auto &it: member->getMappingInfo())
+                                for(const auto &it: member->getMappingInfos())
                                 {
                                     if(it.sceneName == m_name)
                                     {
@@ -155,7 +155,7 @@ namespace OIC
             std::function<void(const SceneMemberResource::Ptr&)> buildActionParams =
                     [&](const SceneMemberResource::Ptr & member)
                     {
-                        for(const auto &it: member->getMappingInfo())
+                        for(const auto &it: member->getMappingInfos())
                         {
                             if(it.sceneName == m_name)
                             {
index 8fc78da..6f93cdc 100755 (executable)
@@ -68,7 +68,7 @@ namespace OIC
         const RCSResourceAttributes SceneAction::getExecutionParameter()
         {
             RCSResourceAttributes attr;
-            for(const auto& it : m_sceneMemberResourceObj->getMappingInfo())
+            for(const auto& it : m_sceneMemberResourceObj->getMappingInfos())
             {
                 if(it.sceneName == m_sceneName)
                 {
index 19ae828..7585d61 100644 (file)
@@ -31,17 +31,19 @@ namespace OIC
     {
         namespace
         {
-            std::atomic_int numOfSceneCollection(0);
+            std::atomic_int g_numOfSceneCollection(0);
         }
 
-        SceneCollectionResource::Ptr SceneCollectionResource::createSceneCollectionObject()
+        SceneCollectionResource::SceneCollectionResource()
+        : m_uri(PREFIX_SCENE_COLLECTION_URI + "/" + std::to_string(g_numOfSceneCollection++)),
+          m_address(), m_sceneCollectionResourceObject(), m_requestHandler()
         {
-            SceneCollectionResource::Ptr sceneCollectionResource(new SceneCollectionResource());
-
-            sceneCollectionResource->m_uri
-                = PREFIX_SCENE_COLLECTION_URI + "/" + std::to_string(numOfSceneCollection++);
+            m_sceneCollectionResourceObject = createResourceObject();
+        }
 
-            sceneCollectionResource->createResourceObject();
+        SceneCollectionResource::Ptr SceneCollectionResource::create()
+        {
+            SceneCollectionResource::Ptr sceneCollectionResource(new SceneCollectionResource());
 
             sceneCollectionResource->setDefaultAttributes();
 
@@ -52,10 +54,10 @@ namespace OIC
             return sceneCollectionResource;
         }
 
-        SceneCollectionResource::Ptr SceneCollectionResource::createSceneCollectionObject(
+        SceneCollectionResource::Ptr SceneCollectionResource::create(
                 const RCSResourceAttributes & inputAttr)
         {
-            auto sceneCollectionResource = SceneCollectionResource::createSceneCollectionObject();
+            auto sceneCollectionResource = SceneCollectionResource::create();
             if (inputAttr.contains(SCENE_KEY_NAME))
             {
                 sceneCollectionResource->setName(inputAttr.at(SCENE_KEY_NAME).get<std::string>());
@@ -79,10 +81,9 @@ namespace OIC
             return sceneCollectionResource;
         }
 
-        void SceneCollectionResource::createResourceObject()
+        RCSResourceObject::Ptr SceneCollectionResource::createResourceObject()
         {
-            m_sceneCollectionResourceObject
-                = RCSResourceObject::Builder(
+            return RCSResourceObject::Builder(
                         m_uri, SCENE_COLLECTION_RT, OC_RSRVD_INTERFACE_DEFAULT).
                         addInterface(OC::BATCH_INTERFACE).
                         setDiscoverable(true).setObservable(false).build();
@@ -180,21 +181,18 @@ namespace OIC
                 = std::find(sceneValues.begin(), sceneValues.end(), sceneName);
             if (foundSceneValue == sceneValues.end() && executeCB && !m_sceneMembers.size())
             {
-                std::thread(executeCB, SCENE_CLIENT_BADREQUEST).detach();
+                std::thread(std::move(executeCB), SCENE_CLIENT_BADREQUEST).detach();
                 return;
             }
 
-            auto executeHandler
-                = SceneExecuteResponseHandler::createExecuteHandler(
-                        shared_from_this(), std::move(executeCB));
-
-            {
-                std::lock_guard<std::mutex> handlerlock(m_executeHandlerLock);
-                m_executeHandlers.push_back(executeHandler);
-            }
+            m_sceneCollectionResourceObject->setAttribute(
+                    SCENE_KEY_LAST_SCENE, std::move(sceneName));
 
             {
                 std::lock_guard<std::mutex> memberlock(m_sceneMemberLock);
+                auto executeHandler
+                    = SceneExecuteResponseHandler::createExecuteHandler(
+                            shared_from_this(), std::move(executeCB));
                 for (auto & it : m_sceneMembers)
                 {
                     it->execute(sceneName, std::bind(
@@ -202,19 +200,6 @@ namespace OIC
                             std::placeholders::_1, std::placeholders::_2));
                 }
             }
-
-            m_sceneCollectionResourceObject->setAttribute(SCENE_KEY_LAST_SCENE, sceneName);
-        }
-
-        void SceneCollectionResource::onExecute(
-                int errorCode, SceneExecuteCallback cb, SceneExecuteResponseHandler::Ptr ptr)
-        {
-            {
-                std::lock_guard<std::mutex> handlerlock(m_executeHandlerLock);
-                m_executeHandlers.remove(ptr);
-            }
-
-            std::thread(cb, errorCode).detach();
         }
 
         std::string SceneCollectionResource::getId() const
@@ -241,9 +226,26 @@ namespace OIC
 
         std::vector<SceneMemberResource::Ptr> SceneCollectionResource::getSceneMembers() const
         {
+            std::lock_guard<std::mutex> memberlock(m_sceneMemberLock);
             return m_sceneMembers;
         }
 
+        std::vector<SceneMemberResource::Ptr> SceneCollectionResource::findSceneMembers(
+                const std::string & sceneName) const
+        {
+            std::lock_guard<std::mutex> memberlock(m_sceneMemberLock);
+            std::vector<SceneMemberResource::Ptr> retMembers;
+            std::for_each(m_sceneMembers.begin(), m_sceneMembers.end(),
+                    [& retMembers, & sceneName](SceneMemberResource::Ptr pMember)
+                    {
+                        if(pMember->hasSceneValue(sceneName))
+                        {
+                            retMembers.push_back(pMember);
+                        }
+                    });
+            return retMembers;
+        }
+
         RCSResourceObject::Ptr SceneCollectionResource::getRCSResourceObject() const
         {
             return m_sceneCollectionResourceObject;
@@ -307,8 +309,8 @@ namespace OIC
 
             auto sizeofValues = values.size();
             unsigned int sameSize = 0;
-            std::for_each (values.begin(), values.end(),
-                    [& ptr, & sizeofValues, & sameSize](const std::string & value)
+            std::for_each(values.begin(), values.end(),
+                    [& ptr, & sameSize](const std::string & value)
                     {
                         try
                         {
@@ -342,9 +344,14 @@ namespace OIC
             auto requestKey = attributes.at(SCENE_KEY_LAST_SCENE).get<std::string>();
 
             RCSRequest req(request.getResourceObject().lock(), request.getOCRequest());
-            ptr->execute(std::string(requestKey), std::bind(
-                    &SceneCollectionResource::SceneCollectionRequestHandler::onExecute, this,
-                    std::placeholders::_1, req, attributes));
+
+            ptr->execute(std::string(requestKey),
+                    [& req](int /*eCode*/)
+                    {
+                        // TODO need to set error code.
+                        // and need to set specific attr' but this attr not to be apply to RCSResourceObject.
+                        RCSSeparateResponse(req).set();
+                    });
 
             return RCSSetResponse::separate();
         }
@@ -352,40 +359,45 @@ namespace OIC
         RCSSetResponse SceneCollectionResource::SceneCollectionRequestHandler::
         createSceneMemberRequest(const RCSRequest & /*request*/, RCSResourceAttributes & attributes)
         {
+            int eCode = SCENE_CLIENT_BADREQUEST;
             SceneCollectionResource::Ptr ptr = m_owner.lock();
-            if (ptr == nullptr || !attributes.contains(SCENE_KEY_PAYLOAD_LINK))
-            {
-                return RCSSetResponse::create(attributes, SCENE_CLIENT_BADREQUEST).
-                        setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
-            }
-
-            auto linkAtt = attributes.at(SCENE_KEY_PAYLOAD_LINK).get<RCSResourceAttributes>();
-            if (!linkAtt.contains(SCENE_KEY_HREF) ||
-                    !linkAtt.contains(SCENE_KEY_RT) || !linkAtt.contains(SCENE_KEY_IF))
+            if (!ptr)
             {
-                return RCSSetResponse::create(attributes, SCENE_CLIENT_BADREQUEST).
+                return RCSSetResponse::create(attributes, eCode).
                         setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
             }
 
-            auto memberObj = createSceneMemberFromRemote(linkAtt);
-            if (memberObj == nullptr)
+            RCSResourceAttributes responseAtt(attributes);
+            if (attributes.contains(SCENE_KEY_PAYLOAD_LINK))
             {
-                return RCSSetResponse::create(attributes, SCENE_CLIENT_BADREQUEST).
-                        setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
-            }
+                auto linkAtt = attributes.at(SCENE_KEY_PAYLOAD_LINK).get<RCSResourceAttributes>();
+                if (linkAtt.contains(SCENE_KEY_HREF) &&
+                        linkAtt.contains(SCENE_KEY_RT) && linkAtt.contains(SCENE_KEY_IF))
+                {
+                    auto memberObj = SceneMemberResource::createSceneMemberResource(linkAtt);
+                    try
+                    {
+                        ptr->addSceneMember(memberObj);
+                    }
+                    catch (...)
+                    {
+                        return RCSSetResponse::create(responseAtt, eCode).
+                                setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
+                    }
+                    eCode = SCENE_RESPONSE_SUCCESS;
 
-            if (attributes.contains(SCENE_KEY_SCENEMAPPINGS))
-            {
-                addMemberInfoFromRemote(memberObj, attributes.at(
-                        SCENE_KEY_SCENEMAPPINGS).get<std::vector<RCSResourceAttributes>>());
+                    if (attributes.contains(SCENE_KEY_SCENEMAPPINGS))
+                    {
+                        addMemberInfoFromRemote(memberObj, attributes.at(
+                                SCENE_KEY_SCENEMAPPINGS).get<std::vector<RCSResourceAttributes>>());
+                    }
+                    responseAtt[SCENE_KEY_ID] = RCSResourceAttributes::Value(memberObj->getId());
+                    responseAtt[SCENE_KEY_CREATEDLINK]
+                                = RCSResourceAttributes::Value(memberObj->getFullUri());
+                }
             }
 
-            RCSResourceAttributes responseAtt(attributes);
-            responseAtt[SCENE_KEY_ID] = RCSResourceAttributes::Value(memberObj->getId());
-            responseAtt[SCENE_KEY_CREATEDLINK]
-                        = RCSResourceAttributes::Value(memberObj->getFullUri());
-
-            return RCSSetResponse::create(responseAtt).
+            return RCSSetResponse::create(responseAtt, eCode).
                     setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
         }
 
@@ -393,58 +405,28 @@ namespace OIC
         SceneCollectionResource::SceneCollectionRequestHandler::setSceneCollectionName(
                 const RCSRequest & /*request*/, RCSResourceAttributes & attr)
         {
+            int eCode = SCENE_CLIENT_BADREQUEST;
             SceneCollectionResource::Ptr ptr = m_owner.lock();
-            if (ptr == nullptr)
+            if (ptr != nullptr)
             {
-                return RCSSetResponse::create(attr, SCENE_CLIENT_BADREQUEST).
-                        setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
+                eCode = SCENE_RESPONSE_SUCCESS;
+                ptr->setName(attr.at(SCENE_KEY_NAME).get<std::string>());
             }
 
-            ptr->setName(attr.at(SCENE_KEY_NAME).get<std::string>());
-
-            return RCSSetResponse::create(attr).
+            return RCSSetResponse::create(attr, eCode).
                     setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
         }
 
-        SceneMemberResource::Ptr
-        SceneCollectionResource::SceneCollectionRequestHandler::createSceneMemberFromRemote(
-                RCSResourceAttributes & linkAtt)
-        {
-            auto memberObj = SceneMemberResource::createSceneMemberResource(linkAtt);
-
-            try
-            {
-                SceneCollectionResource::Ptr ptr = m_owner.lock();
-                ptr->addSceneMember(memberObj);
-            } catch (...)
-            {
-                return nullptr;
-            }
-
-            return memberObj;
-        }
-
         void SceneCollectionResource::SceneCollectionRequestHandler::addMemberInfoFromRemote(
                 SceneMemberResource::Ptr memberObj, std::vector<RCSResourceAttributes> mInfo)
         {
-            std::for_each (mInfo.begin(), mInfo.end(),
+            std::for_each(mInfo.begin(), mInfo.end(),
                     [& memberObj](const RCSResourceAttributes & att)
                     {
-                        memberObj->addMappingInfo(SceneMemberResource::MappingInfo(
-                                att.at(SCENE_KEY_SCENE).get<std::string>(),
-                                att.at(SCENE_KEY_MEMBERPROPERTY).get<std::string>(),
-                                att.at(SCENE_KEY_MEMBERVALUE)));
+                        memberObj->addMappingInfo(SceneMemberResource::MappingInfo::create(att));
                     });
         }
 
-        void SceneCollectionResource::SceneCollectionRequestHandler::
-        onExecute(int /*errorCode*/, const RCSRequest & request, RCSResourceAttributes & /*att*/)
-        {
-            // TODO need to set error code.
-            // and need to set specific attr' but this attr not to be apply to RCSResourceObject.
-            RCSSeparateResponse(request).set();
-        }
-
         void SceneCollectionResource::SceneExecuteResponseHandler::
         onResponse(const RCSResourceAttributes & /*attributes*/, int errorCode)
         {
@@ -468,9 +450,11 @@ namespace OIC
             executeHandler->m_numOfMembers = ptr->m_sceneMembers.size();
             executeHandler->m_responseMembers = 0;
 
-            executeHandler->m_cb = std::bind(
-                    &SceneCollectionResource::onExecute, ptr,
-                    std::placeholders::_1, std::move(executeCB), executeHandler);
+            executeHandler->m_cb =
+                    [executeCB](int eCode)
+                    {
+                        std::thread(std::move(executeCB), eCode).detach();
+                    };
 
             executeHandler->m_owner
                 = std::weak_ptr<SceneCollectionResource>(ptr);
@@ -478,5 +462,6 @@ namespace OIC
 
             return executeHandler;
         }
+
     }
 }
index 8c57f3c..f625df6 100644 (file)
@@ -40,9 +40,8 @@ namespace OIC
 
             ~SceneCollectionResource() = default;
 
-            static SceneCollectionResource::Ptr createSceneCollectionObject();
-            static SceneCollectionResource::Ptr createSceneCollectionObject(
-                    const RCSResourceAttributes &);
+            static SceneCollectionResource::Ptr create();
+            static SceneCollectionResource::Ptr create(const RCSResourceAttributes &);
 
             void addScene(std::string &&);
             void addScene(const std::string &);
@@ -67,6 +66,9 @@ namespace OIC
 
             std::vector<SceneMemberResource::Ptr> getSceneMembers() const;
 
+            std::vector<SceneMemberResource::Ptr> findSceneMembers(
+                    const std::string & sceneName) const;
+
             RCSResourceObject::Ptr getRCSResourceObject() const;
 
         private:
@@ -100,7 +102,6 @@ namespace OIC
 
                 RCSSetResponse onSetRequest(
                         const RCSRequest &, RCSResourceAttributes &);
-                void onExecute(int, const RCSRequest &, RCSResourceAttributes &);
 
             private:
                 RCSSetResponse addSceneRequest(
@@ -112,7 +113,6 @@ namespace OIC
                 RCSSetResponse setSceneCollectionName(
                         const RCSRequest &, RCSResourceAttributes &);
 
-                SceneMemberResource::Ptr createSceneMemberFromRemote(RCSResourceAttributes &);
                 void addMemberInfoFromRemote(SceneMemberResource::Ptr,
                         std::vector<RCSResourceAttributes>);
             };
@@ -121,14 +121,12 @@ namespace OIC
             std::string m_address;
 
             RCSResourceObject::Ptr m_sceneCollectionResourceObject;
-            std::mutex m_sceneMemberLock;
+            mutable std::mutex m_sceneMemberLock;
             std::vector<SceneMemberResource::Ptr> m_sceneMembers;
 
             SceneCollectionRequestHandler m_requestHandler;
-            std::mutex m_executeHandlerLock;
-            std::list<SceneExecuteResponseHandler::Ptr> m_executeHandlers;
 
-            SceneCollectionResource() = default;
+            SceneCollectionResource();
 
             SceneCollectionResource(const SceneCollectionResource &) = delete;
             SceneCollectionResource & operator = (
@@ -138,11 +136,9 @@ namespace OIC
             SceneCollectionResource & operator = (
                     SceneCollectionResource &&) = delete;
 
-            void createResourceObject();
+            RCSResourceObject::Ptr createResourceObject();
             void setDefaultAttributes();
             void initSetRequestHandler();
-
-            void onExecute(int, SceneExecuteCallback, SceneExecuteResponseHandler::Ptr);
         };
     }
 }
index b99d3c5..5ac6d70 100755 (executable)
@@ -39,7 +39,7 @@ namespace OIC
         SceneCollection::Ptr SceneList::addNewSceneCollection()
         {
             auto sceneCollectionResObj =
-                    SceneCollectionResource::createSceneCollectionObject();
+                    SceneCollectionResource::create();
             SceneListResource::getInstance()->addSceneCollectionResource(sceneCollectionResObj);
 
             return SceneCollection::Ptr(new SceneCollection(sceneCollectionResObj));
index 8f01cdb..ef1585a 100644 (file)
@@ -28,13 +28,8 @@ namespace OIC
     namespace Service
     {
         SceneListResource::SceneListResource()
-        : m_sceneListName(), m_sceneListObj()
+        : m_sceneListObj(createResourceObject())
         {
-            m_sceneListObj = RCSResourceObject::Builder(
-                    SCENE_LIST_URI, SCENE_LIST_RT, OC_RSRVD_INTERFACE_DEFAULT).
-                            addInterface(OC::BATCH_INTERFACE).
-                            setDiscoverable(true).setObservable(false).build();
-
             m_sceneListObj->setAttribute(SCENE_KEY_NAME, SCENE_LIST_DEFAULT_NAME);
             m_sceneListObj->setAttribute(SCENE_KEY_RTS, SCENE_LIST_RT);
 
@@ -48,6 +43,14 @@ namespace OIC
             return & instance;
         }
 
+        RCSResourceObject::Ptr SceneListResource::createResourceObject()
+        {
+            return RCSResourceObject::Builder(
+                    SCENE_LIST_URI, SCENE_LIST_RT, OC_RSRVD_INTERFACE_DEFAULT).
+                            addInterface(OC::BATCH_INTERFACE).
+                            setDiscoverable(true).setObservable(false).build();
+        }
+
         void SceneListResource::addSceneCollectionResource(
                 SceneCollectionResource::Ptr newObject)
         {
@@ -58,15 +61,12 @@ namespace OIC
 
         std::string SceneListResource::getName() const
         {
-            return m_sceneListName;
+            return m_sceneListObj->getAttributeValue(SCENE_KEY_NAME).get<std::string>();
         }
 
         void SceneListResource::setName(std::string && newName)
         {
-            m_sceneListName = std::move(newName);
-
-            RCSResourceObject::LockGuard guard(m_sceneListObj);
-            m_sceneListObj->setAttribute(SCENE_KEY_NAME, m_sceneListName);
+            m_sceneListObj->setAttribute(SCENE_KEY_NAME, std::move(newName));
         }
 
         void SceneListResource::setName(const std::string & newName)
@@ -76,9 +76,15 @@ namespace OIC
 
         std::vector<SceneCollectionResource::Ptr> SceneListResource::getSceneCollections() const
         {
+            std::lock_guard<std::mutex> collectionlock(m_sceneCollectionLock);
             return m_sceneCollections;
         }
 
+        RCSResourceObject::Ptr SceneListResource::getResourceObject() const
+        {
+            return m_sceneListObj;
+        }
+
         std::vector<RCSResourceAttributes> SceneListResource::getChildrenAttributes() const
         {
             std::vector<RCSResourceAttributes> childrenAttrs;
@@ -119,10 +125,13 @@ namespace OIC
         RCSSetResponse SceneListResource::SceneListRequestHandler::onSetRequest(
                 const RCSRequest & request, RCSResourceAttributes & attributes)
         {
+            RCSResourceAttributes responseAttr;
+            int eCode = SCENE_CLIENT_BADREQUEST;
+
             if(request.getInterface() == LINK_BATCH)
             {
                 auto newObject
-                    = SceneCollectionResource::createSceneCollectionObject(attributes);
+                    = SceneCollectionResource::create(attributes);
 
                 SceneListResource::getInstance()->addSceneCollectionResource(newObject);
 
@@ -134,24 +143,30 @@ namespace OIC
                 responseAtt[SCENE_KEY_PAYLOAD_LINK]
                             = RCSResourceAttributes::Value(uri);
 
-                return RCSSetResponse::create(responseAtt, SCENE_RESPONSE_SUCCESS).
-                        setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
+                responseAttr = responseAtt;
+                eCode = SCENE_RESPONSE_SUCCESS;
             }
 
-            if (attributes.contains(SCENE_KEY_NAME))
+            else if (attributes.contains(SCENE_KEY_NAME))
             {
                 SceneListResource::getInstance()->setName(
                         attributes.at(SCENE_KEY_NAME).get<std::string>());
-                return RCSSetResponse::create(attributes, SCENE_RESPONSE_SUCCESS).
-                        setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
+
+                responseAttr = attributes;
+                eCode = SCENE_RESPONSE_SUCCESS;
+            }
+            else
+            {
+                responseAttr = attributes;
+                eCode = SCENE_CLIENT_BADREQUEST;
             }
 
-            return RCSSetResponse::create(attributes, SCENE_CLIENT_BADREQUEST).
+            return RCSSetResponse::create(responseAttr, eCode).
                     setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
         }
 
         RCSGetResponse SceneListResource::SceneListRequestHandler::onGetRequest(
-                const RCSRequest & request, RCSResourceAttributes & attributes)
+                const RCSRequest & request, RCSResourceAttributes & /*attributes*/)
         {
 
             if(request.getInterface() != OC::DEFAULT_INTERFACE)
@@ -161,7 +176,14 @@ namespace OIC
 
             auto childrenAttrs = SceneListResource::getInstance()->getChildrenAttributes();
 
-            RCSResourceAttributes retAttr =  attributes;
+            RCSResourceAttributes retAttr;
+
+            {
+                RCSResourceObject::LockGuard lock(
+                        SceneListResource::getInstance()->getResourceObject());
+                retAttr = SceneListResource::getInstance()->getResourceObject()->getAttributes();
+            }
+
             if (childrenAttrs.size())
             {
                 retAttr[SCENE_KEY_CHILD] = childrenAttrs;
@@ -169,5 +191,6 @@ namespace OIC
 
             return RCSGetResponse::create(retAttr);
         }
+
     }
 }
index f54dfb4..d9e0d03 100644 (file)
@@ -76,6 +76,8 @@ namespace OIC
 
             std::vector<RCSResourceAttributes> getChildrenAttributes() const;
 
+            RCSResourceObject::Ptr getResourceObject() const;
+
         private:
             class SceneListRequestHandler
             {
@@ -87,9 +89,8 @@ namespace OIC
                 static RCSGetResponse onGetRequest(const RCSRequest &, RCSResourceAttributes &);
             };
 
-            std::string m_sceneListName;
             RCSResourceObject::Ptr m_sceneListObj;
-            std::mutex m_sceneCollectionLock;
+            mutable std::mutex m_sceneCollectionLock;
             std::vector<SceneCollectionResource::Ptr> m_sceneCollections;
 
             SceneListResource();
@@ -100,6 +101,8 @@ namespace OIC
 
             SceneListResource(SceneListResource &&) = delete;
             SceneListResource & operator = (SceneListResource &&) = delete;
+
+            RCSResourceObject::Ptr createResourceObject();
         };
     }
 }
index 9aea585..0de8d6b 100644 (file)
@@ -29,7 +29,7 @@ namespace OIC
     {
         namespace
         {
-            std::atomic_int numOfSceneMember(0);
+            std::atomic_int g_numOfSceneMember(0);
         }
 
         SceneMemberResource::Ptr
@@ -39,7 +39,7 @@ namespace OIC
             SceneMemberResource::Ptr sceneMemberResource(new SceneMemberResource());
 
             sceneMemberResource->m_uri = PREFIX_SCENE_MEMBER_URI + "/" +
-                std::to_string(numOfSceneMember++);
+                std::to_string(g_numOfSceneMember++);
 
             sceneMemberResource->m_remoteMemberObj = remoteObject;
 
@@ -132,9 +132,9 @@ namespace OIC
                 mappingInfo.erase(foundMInfo);
             }
             RCSResourceAttributes newMapInfo;
-            newMapInfo[SCENE_KEY_SCENE] = RCSResourceAttributes::Value(mInfo.sceneName);
-            newMapInfo[SCENE_KEY_MEMBERPROPERTY] = RCSResourceAttributes::Value(mInfo.key);
-            newMapInfo[SCENE_KEY_MEMBERVALUE] = mInfo.value;
+            newMapInfo[SCENE_KEY_SCENE] = RCSResourceAttributes::Value(std::move(mInfo.sceneName));
+            newMapInfo[SCENE_KEY_MEMBERPROPERTY] = RCSResourceAttributes::Value(std::move(mInfo.key));
+            newMapInfo[SCENE_KEY_MEMBERVALUE] = std::move(mInfo.value);
             mappingInfo.push_back(newMapInfo);
 
             m_sceneMemberResourceObj->setAttribute(SCENE_KEY_SCENEMAPPINGS, mappingInfo);
@@ -145,7 +145,7 @@ namespace OIC
             addMappingInfo(MappingInfo(mInfo));
         }
 
-        std::vector<SceneMemberResource::MappingInfo> SceneMemberResource::getMappingInfo()
+        std::vector<SceneMemberResource::MappingInfo> SceneMemberResource::getMappingInfos() const
         {
             std::vector<MappingInfo> retMInfo;
 
@@ -154,10 +154,7 @@ namespace OIC
             std::for_each(mInfo.begin(), mInfo.end(),
                     [& retMInfo](const RCSResourceAttributes & att)
                     {
-                        MappingInfo info(att.at(SCENE_KEY_SCENE).get<std::string>(),
-                                att.at(SCENE_KEY_MEMBERPROPERTY).get<std::string>(),
-                                att.at(SCENE_KEY_MEMBERVALUE));
-                        retMInfo.push_back(info);
+                        retMInfo.push_back(MappingInfo::create(att));
                     });
 
             return retMInfo;
@@ -202,8 +199,8 @@ namespace OIC
         {
             RCSResourceAttributes setAtt;
 
-            auto mInfo = getMappingInfo();
-            std::for_each (mInfo.begin(), mInfo.end(),
+            auto mInfo = getMappingInfos();
+            std::for_each(mInfo.begin(), mInfo.end(),
                     [& setAtt, & sceneName](const MappingInfo & info)
                     {
                         if(info.sceneName == sceneName)
@@ -241,6 +238,45 @@ namespace OIC
             return m_sceneMemberResourceObj->getAttributeValue(SCENE_KEY_NAME).toString();
         }
 
+        std::vector<SceneMemberResource::MappingInfo> SceneMemberResource::findMappingInfos(
+                const std::string & sceneValue) const
+        {
+            auto mInfo = getMappingInfos();
+            std::vector<MappingInfo> retMInfo;
+
+            std::for_each(mInfo.begin(), mInfo.end(),
+                    [& retMInfo, & sceneValue](const MappingInfo & info)
+                    {
+                        if(info.sceneName == sceneValue)
+                        {
+                            retMInfo.push_back(MappingInfo(info));
+                        }
+                    });
+            return retMInfo;
+        }
+
+        bool SceneMemberResource::hasSceneValue(const std::string & sceneValue) const
+        {
+            auto mInfo = getMappingInfos();
+            if (std::find_if(mInfo.begin(), mInfo.end(),
+                    [& sceneValue](const MappingInfo & info) -> bool
+                    {
+                        return info.sceneName == sceneValue;
+                    }) != mInfo.end())
+            {
+                return true;
+            }
+            return false;
+        }
+
+        SceneMemberResource::MappingInfo
+        SceneMemberResource::MappingInfo::create(const RCSResourceAttributes & att)
+        {
+            return MappingInfo(att.at(SCENE_KEY_SCENE).get<std::string>(),
+                    att.at(SCENE_KEY_MEMBERPROPERTY).get<std::string>(),
+                    att.at(SCENE_KEY_MEMBERVALUE));
+        }
+
         RCSSetResponse SceneMemberResource::SceneMemberRequestHandler::
         onSetRequest(const RCSRequest & request, RCSResourceAttributes & attributes)
         {
@@ -262,25 +298,24 @@ namespace OIC
         SceneMemberResource::SceneMemberRequestHandler::addMappingInfos(
                 const RCSRequest & /*request*/, RCSResourceAttributes & attributes)
         {
+            int eCode = SCENE_RESPONSE_SUCCESS;
             auto ptr = m_owner.lock();
             if (!ptr)
             {
-                return RCSSetResponse::create(attributes, SCENE_CLIENT_BADREQUEST).
-                        setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
+                eCode = SCENE_CLIENT_BADREQUEST;
+            }
+            else
+            {
+                auto mInfo = attributes.at(SCENE_KEY_SCENEMAPPINGS).
+                        get<std::vector<RCSResourceAttributes>>();
+                std::for_each(mInfo.begin(), mInfo.end(),
+                        [& ptr](const RCSResourceAttributes & att)
+                        {
+                            ptr->addMappingInfo(SceneMemberResource::MappingInfo::create(att));
+                        });
             }
 
-            auto mInfo = attributes.at(SCENE_KEY_SCENEMAPPINGS).
-                    get<std::vector<RCSResourceAttributes>>();
-            std::for_each (mInfo.begin(), mInfo.end(),
-                    [& ptr](const RCSResourceAttributes & att)
-                    {
-                        ptr->addMappingInfo(SceneMemberResource::MappingInfo(
-                                att.at(SCENE_KEY_SCENE).get<std::string>(),
-                                att.at(SCENE_KEY_MEMBERPROPERTY).get<std::string>(),
-                                att.at(SCENE_KEY_MEMBERVALUE)));
-                    });
-
-            return RCSSetResponse::create(attributes).
+            return RCSSetResponse::create(attributes, eCode).
                     setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
         }
 
@@ -288,16 +323,18 @@ namespace OIC
         SceneMemberResource::SceneMemberRequestHandler::setSceneMemberName(
                 const RCSRequest & /*request*/, RCSResourceAttributes & attributes)
         {
+            int eCode = SCENE_RESPONSE_SUCCESS;
             auto ptr = m_owner.lock();
             if (!ptr)
             {
-                return RCSSetResponse::create(attributes, SCENE_CLIENT_BADREQUEST).
-                        setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
+                eCode = SCENE_CLIENT_BADREQUEST;
+            }
+            else
+            {
+                ptr->setName(attributes.at(SCENE_KEY_NAME).get<std::string>());
             }
 
-            ptr->setName(attributes.at(SCENE_KEY_NAME).get<std::string>());
-
-            return RCSSetResponse::create(attributes).
+            return RCSSetResponse::create(attributes, eCode).
                     setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
         }
     }
index 8883393..cac6ebf 100644 (file)
@@ -66,6 +66,8 @@ namespace OIC
                 MappingInfo(MappingInfo &&) = default;
                 MappingInfo(const MappingInfo &) = default;
 
+                static MappingInfo create(const RCSResourceAttributes &);
+
                 std::string sceneName;              ///< name of scene value
                 std::string key;                    ///< key to set at attributes of remote resource
                 RCSResourceAttributes::Value value; ///< val to set at attributes of remote resource
@@ -112,7 +114,11 @@ namespace OIC
             /**
              * Returns all Mapping information of a scene member resource.
              */
-            std::vector<MappingInfo> getMappingInfo();
+            std::vector<MappingInfo> getMappingInfos() const;
+
+            std::vector<MappingInfo> findMappingInfos(const std::string & sceneValue) const;
+
+            bool hasSceneValue(const std::string &) const;
 
             /**
              * Returns ID of a Scene member resource.