Add Scene Collection Resource Requestor class for scene manager client
authorMinji Park <minjii.park@samsung.com>
Thu, 4 Feb 2016 11:15:33 +0000 (20:15 +0900)
committerUze Choi <uzchoi@samsung.com>
Fri, 19 Feb 2016 14:12:35 +0000 (14:12 +0000)
It is the scene manager remote-side requestor class
which sends request for scenemember/scene creation and scene execution to local-side scene collection resource object.

Change-Id: Idbccdb7548e1f50025ddf650ea5d31520f6e0313
Signed-off-by: Minji Park <minjii.park@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/4939
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
Tested-by: Uze Choi <uzchoi@samsung.com>
service/scene-manager/src/SceneCollectionResourceRequestor.cpp [new file with mode: 0644]
service/scene-manager/src/SceneCollectionResourceRequestor.h [new file with mode: 0644]

diff --git a/service/scene-manager/src/SceneCollectionResourceRequestor.cpp b/service/scene-manager/src/SceneCollectionResourceRequestor.cpp
new file mode 100644 (file)
index 0000000..1f36e08
--- /dev/null
@@ -0,0 +1,265 @@
+//******************************************************************
+//
+// Copyright 2016 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#include "SceneCollectionResourceRequestor.h"
+#include "RemoteSceneUtils.h"
+#include "OCPlatform.h"
+
+namespace OIC
+{
+    namespace Service
+    {
+
+        SceneCollectionResourceRequestor::SceneCollectionResourceRequestor
+        (RCSRemoteResourceObject::Ptr pSceneCollection)
+            : m_SceneCollectionResourcePtr{ pSceneCollection }
+        {
+            SCENE_CLIENT_ASSERT_NOT_NULL(pSceneCollection);
+        }
+
+        SceneCollectionResourceRequestor::~SceneCollectionResourceRequestor()
+        {
+
+        }
+
+        void SceneCollectionResourceRequestor::requestSceneCreation
+        (const std::string &name, InternalSceneRequestCallback createSceneCB)
+        {
+            RCSResourceAttributes attributesToSet;
+            std::vector< std::string > vecScenes{ name };
+
+            attributesToSet[SCENE_KEY_SCENEVALUES] = vecScenes;
+
+            RCSRemoteResourceObject::RemoteAttributesSetCallback setRequestCB
+                = std::bind(&SceneCollectionResourceRequestor::onSetResponseForScene,
+                            std::placeholders::_1, std::placeholders::_2,
+                            name, std::move(createSceneCB), ADD_SCENE,
+                            SceneCollectionResourceRequestor::wPtr(shared_from_this()));
+
+            m_SceneCollectionResourcePtr->setRemoteAttributes(
+                std::move(attributesToSet), std::move(setRequestCB));
+        }
+
+        void SceneCollectionResourceRequestor::requestSceneRemoval
+        (const std::string &/* name */, InternalSceneRequestCallback /* removeSceneCB */)
+        {
+
+        }
+
+        void SceneCollectionResourceRequestor::requestSceneExecution
+        (const std::string &name, InternalSceneRequestCallback executeSceneCB)
+        {
+            RCSResourceAttributes attributesToSet;
+            attributesToSet[SCENE_KEY_LAST_SCENE] = name;
+
+            RCSRemoteResourceObject::RemoteAttributesSetCallback setRequestCB
+                = std::bind(&SceneCollectionResourceRequestor::onSetResponseForScene,
+                            std::placeholders::_1, std::placeholders::_2,
+                            name, std::move(executeSceneCB), EXECUTE_SCENE,
+                            SceneCollectionResourceRequestor::wPtr(shared_from_this()));
+
+            m_SceneCollectionResourcePtr->setRemoteAttributes
+            (std::move(attributesToSet), std::move(setRequestCB));
+        }
+
+        void SceneCollectionResourceRequestor::requestAddSceneMember
+        (RCSRemoteResourceObject::Ptr pMember, const std::string &sceneName,
+         RCSResourceAttributes &attr, InternalAddMemberCallback addMemberCB)
+        {
+            SCENE_CLIENT_ASSERT_NOT_NULL(pMember);
+
+            RCSResourceAttributes attributesToSet, linkAttrs;
+
+            linkAttrs[SCENE_KEY_HREF] = pMember->getAddress() + pMember->getUri();
+            linkAttrs[SCENE_KEY_IF] = pMember->getInterfaces();
+            linkAttrs[SCENE_KEY_RT] = pMember->getTypes();
+
+            attributesToSet[SCENE_KEY_PAYLOAD_LINK] = linkAttrs;
+
+            std::vector< RCSResourceAttributes > vecSceneMappings;
+            for (const auto &itr : attr)
+            {
+                RCSResourceAttributes sceneMappingAttrs;
+                sceneMappingAttrs[SCENE_KEY_SCENE] = sceneName;
+                sceneMappingAttrs[SCENE_KEY_MEMBERPROPERTY] = itr.key();
+                sceneMappingAttrs[SCENE_KEY_MEMBERVALUE] = itr.value();
+
+                vecSceneMappings.push_back(sceneMappingAttrs);
+            }
+
+            attributesToSet[SCENE_KEY_SCENEMAPPINGS] = vecSceneMappings;
+
+            RCSRemoteResourceObject::SetCallback setRequestCB
+                = std::bind(&SceneCollectionResourceRequestor::onSceneMemberAdded,
+                            std::placeholders::_1, std::placeholders::_2,
+                            std::placeholders::_3, std::move(addMemberCB),
+                            SceneCollectionResourceRequestor::wPtr(shared_from_this()));
+
+            RCSQueryParams queryParams;
+            queryParams.setResourceInterface(SCENE_CLIENT_CREATE_REQ_IF);
+
+            m_SceneCollectionResourcePtr->set(queryParams, std::move(attributesToSet),
+                                              std::move(setRequestCB));
+        }
+
+        void SceneCollectionResourceRequestor::requestSetName
+        (const std::string &, InternalSetNameCallback)
+        {
+
+        }
+
+        SceneMemberResourceRequestor::Ptr
+        SceneCollectionResourceRequestor::getSceneMemberResourceRequestor
+        (const std::string &memLink)
+        {
+            return m_mapMemberRequestors.find(memLink) != m_mapMemberRequestors.end() ?
+                   m_mapMemberRequestors.at(memLink) : nullptr;
+        }
+
+        void SceneCollectionResourceRequestor::onSetResponseForScene
+        (const RCSResourceAttributes &attrs, int eCode,
+         const std::string &name, const InternalSceneRequestCallback &cb,
+         REQUEST_TYPE reqType, SceneCollectionResourceRequestor::wPtr ptr)
+        {
+            SceneCollectionResourceRequestor::Ptr collectionPtr = ptr.lock();
+
+            if (collectionPtr)
+                collectionPtr->onSetResponseForScene_impl(
+                    std::move(attrs), eCode, name, std::move(cb), reqType);
+        }
+
+        void SceneCollectionResourceRequestor::onSetResponseForScene_impl
+        (const RCSResourceAttributes &attrs, int eCode, const std::string &name,
+         const InternalSceneRequestCallback &internalCB, REQUEST_TYPE reqType)
+        {
+            // TODO: error code
+            int resultCode = SCENE_CLIENT_BADREQUEST;
+
+            if (eCode == SCENE_RESPONSE_SUCCESS)
+            {
+                try
+                {
+                    switch (reqType)
+                    {
+                        case ADD_SCENE:
+                            {
+                                auto scenes
+                                    = attrs.at(SCENE_KEY_SCENEVALUES).get
+                                      < std::vector< std::string > >();
+
+                                if ((std::find(scenes.begin(), scenes.end(), name))
+                                    != scenes.end())
+                                    resultCode = SCENE_RESPONSE_SUCCESS;
+                            }
+                            break;
+
+                        case REMOVE_SCENE:
+                            break;
+
+                        case EXECUTE_SCENE:
+                            {
+                                auto lastScene
+                                    = attrs.at(SCENE_KEY_LAST_SCENE).get< std::string >();
+
+                                if (lastScene.compare(name) == 0)
+                                    resultCode = SCENE_RESPONSE_SUCCESS;
+                            }
+                            break;
+                    }
+                }
+                catch (const std::exception &e)
+                {
+                    SCENE_CLIENT_PRINT_LOG(e.what());
+                    resultCode = SCENE_SERVER_INTERNALSERVERERROR;
+                }
+            }
+
+            internalCB(reqType, name, resultCode);
+        }
+
+        void SceneCollectionResourceRequestor::onSceneMemberAdded
+        (const HeaderOpts &headOpt, const RCSRepresentation &rep, int eCode,
+         const InternalAddMemberCallback &internalCB,
+         SceneCollectionResourceRequestor::wPtr ptr)
+        {
+            SceneCollectionResourceRequestor::Ptr pSceneCollectionRequestor
+                = ptr.lock();
+
+            if (pSceneCollectionRequestor)
+                pSceneCollectionRequestor->onSceneMemberAdded_impl(
+                    headOpt, rep, eCode, std::move(internalCB));
+        }
+
+        void SceneCollectionResourceRequestor::onSceneMemberAdded_impl
+        (const HeaderOpts &, const RCSRepresentation &rep, int eCode,
+         const InternalAddMemberCallback &internalCB)
+        {
+            // TODO: error code
+            int result = SCENE_CLIENT_BADREQUEST;
+            SceneMemberResourceRequestor::Ptr pMemRequestor = nullptr;
+
+            if (eCode == SCENE_RESPONSE_SUCCESS)
+            {
+                try
+                {
+                    RCSResourceAttributes receivedAttrs = rep.getAttributes();
+
+                    std::string hostaddress, uri;
+
+                    SceneUtils::getHostUriString
+                    (receivedAttrs.at(SCENE_KEY_CREATEDLINK).get< std::string >(),
+                     &hostaddress, &uri);
+
+                    std::vector< std::string > vecRT{ SCENE_MEMBER_RT };
+                    std::vector< std::string > vecIF{ SCENE_CLIENT_REQ_IF };
+
+                    OC::OCResource::Ptr pOCResource =
+                        OC::OCPlatform::constructResourceObject
+                        (hostaddress, uri, SCENE_CONNECTIVITY, false, vecRT, vecIF);
+
+                    SCENE_CLIENT_ASSERT_NOT_NULL(pOCResource);
+
+                    RCSRemoteResourceObject::Ptr pResource
+                        = RCSRemoteResourceObject::fromOCResource(pOCResource);
+
+                    SCENE_CLIENT_ASSERT_NOT_NULL(pResource);
+
+                    pMemRequestor =
+                        std::make_shared< SceneMemberResourceRequestor >
+                        (pResource, receivedAttrs.at(SCENE_KEY_ID).get< std::string >());
+
+                    m_mapMemberRequestors[receivedAttrs.at(SCENE_KEY_PAYLOAD_LINK).
+                                          get< RCSResourceAttributes >().
+                                          at(SCENE_KEY_HREF).get< std::string >()] = pMemRequestor;
+
+                    result = SCENE_RESPONSE_SUCCESS;
+                }
+                catch (const std::exception &e)
+                {
+                    SCENE_CLIENT_PRINT_LOG(e.what());
+                    result = SCENE_SERVER_INTERNALSERVERERROR;
+                }
+            }
+
+            internalCB(pMemRequestor, result);
+        }
+
+    }
+}
\ No newline at end of file
diff --git a/service/scene-manager/src/SceneCollectionResourceRequestor.h b/service/scene-manager/src/SceneCollectionResourceRequestor.h
new file mode 100644 (file)
index 0000000..c1c7eed
--- /dev/null
@@ -0,0 +1,108 @@
+//******************************************************************
+//
+// Copyright 2016 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#ifndef SM_SCENECOLLECTION_RESOURCE_REQUESTOR_H_
+#define SM_SCENECOLLECTION_RESOURCE_REQUESTOR_H_
+
+#include <map>
+
+#include "SceneCommons.h"
+#include "RCSRemoteResourceObject.h"
+#include "RCSRepresentation.h"
+#include "SceneMemberResourceRequestor.h"
+
+namespace OIC
+{
+    namespace Service
+    {
+
+        class SceneCollectionResourceRequestor
+            : public std::enable_shared_from_this< SceneCollectionResourceRequestor >
+        {
+            public:
+                typedef std::shared_ptr< SceneCollectionResourceRequestor > Ptr;
+                typedef std::weak_ptr< SceneCollectionResourceRequestor > wPtr;
+
+                enum REQUEST_TYPE
+                {
+                    ADD_SCENE, REMOVE_SCENE, EXECUTE_SCENE
+                };
+
+                typedef std::function
+                < void(const REQUEST_TYPE, const std::string &name, int eCode) >
+                InternalSceneRequestCallback;
+
+                typedef std::function
+                < void(SceneMemberResourceRequestor::Ptr, int eCode) > InternalAddMemberCallback;
+
+                typedef std::function
+                < void(int eCode) > InternalSetNameCallback;
+
+
+            public:
+                SceneCollectionResourceRequestor(RCSRemoteResourceObject::Ptr pSceneCollection);
+                ~SceneCollectionResourceRequestor();
+
+                void requestSceneCreation(const std::string &name, InternalSceneRequestCallback);
+                void requestSceneRemoval(const std::string &name, InternalSceneRequestCallback);
+
+                void requestSceneExecution(const std::string &name, InternalSceneRequestCallback);
+
+                void requestAddSceneMember(RCSRemoteResourceObject::Ptr pMember,
+                                           const std::string &sceneName,
+                                           RCSResourceAttributes &attr,
+                                           InternalAddMemberCallback);
+
+                void requestSetName(const std::string &, InternalSetNameCallback);
+
+                SceneMemberResourceRequestor::Ptr getSceneMemberResourceRequestor(
+                    const std::string &);
+
+
+            private:
+                static void onSetResponseForScene(
+                    const RCSResourceAttributes &attrs, int eCode,
+                    const std::string &name, const InternalSceneRequestCallback &,
+                    REQUEST_TYPE, SceneCollectionResourceRequestor::wPtr);
+
+                void onSetResponseForScene_impl(
+                    const RCSResourceAttributes &attrs, int eCode,
+                    const std::string &name, const InternalSceneRequestCallback &,
+                    REQUEST_TYPE);
+
+                static void onSceneMemberAdded(
+                    const HeaderOpts &, const RCSRepresentation &, int eCode,
+                    const InternalAddMemberCallback &, SceneCollectionResourceRequestor::wPtr);
+
+                void onSceneMemberAdded_impl(
+                    const HeaderOpts &, const RCSRepresentation &, int eCode,
+                    const InternalAddMemberCallback &);
+
+
+            private:
+                RCSRemoteResourceObject::Ptr m_SceneCollectionResourcePtr;
+                std::map< std::string, SceneMemberResourceRequestor::Ptr > m_mapMemberRequestors;
+        };
+
+    }
+}
+
+#endif /* SM_SCENECOLLECTION_RESOURCE_REQUESTOR_H_ */
+