Add SceneMemberResourceRequestor class for scene manager client
authorMinji Park <minjii.park@samsung.com>
Thu, 4 Feb 2016 11:12:49 +0000 (20:12 +0900)
committerUze Choi <uzchoi@samsung.com>
Fri, 19 Feb 2016 09:53:30 +0000 (09:53 +0000)
It is the scene manager remote-side requestor class
which sends request for scene action creation to local-side scene member resource object.

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

diff --git a/service/scene-manager/src/RemoteSceneUtils.h b/service/scene-manager/src/RemoteSceneUtils.h
new file mode 100644 (file)
index 0000000..5955b2e
--- /dev/null
@@ -0,0 +1,54 @@
+//******************************************************************
+//
+// 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 REMOTE_SCENE_UTILS_H
+#define REMOTE_SCENE_UTILS_H
+
+#include <cassert>
+
+#include "OCApi.h"
+#include "logger.h"
+
+#define SCENE_CLIENT_PRINT_LOG(strError) \
+        OIC_LOG_V(ERROR, "[SCENE_CLIENT]", "%s:%d %s", __PRETTY_FUNCTION__, __LINE__, strError);
+
+#define SCENE_CLIENT_ASSERT_NOT_NULL(Val) \
+        { \
+            if (!(Val)) \
+            { \
+                SCENE_CLIENT_PRINT_LOG("NULL value"); \
+                assert(Val); \
+                return; \
+            } \
+        }
+
+namespace OIC
+{
+    namespace Service
+    {
+
+        const OCConnectivityType SCENE_CONNECTIVITY = CT_ADAPTER_IP;
+        const std::string SCENE_CLIENT_REQ_IF = OC::DEFAULT_INTERFACE;
+        const std::string SCENE_CLIENT_CREATE_REQ_IF = OC::BATCH_INTERFACE;
+
+    }
+}
+
+#endif // REMOTE_SCENE_UTILS_H
\ No newline at end of file
diff --git a/service/scene-manager/src/SceneMemberResourceRequestor.cpp b/service/scene-manager/src/SceneMemberResourceRequestor.cpp
new file mode 100644 (file)
index 0000000..3f632e3
--- /dev/null
@@ -0,0 +1,137 @@
+//******************************************************************
+//
+// 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 "SceneMemberResourceRequestor.h"
+#include "RemoteSceneUtils.h"
+
+namespace OIC
+{
+    namespace Service
+    {
+
+        SceneMemberResourceRequestor::SceneMemberResourceRequestor
+        (RCSRemoteResourceObject::Ptr pSceneMember, const std::string &id)
+            : m_id{ id }, m_SceneMemberResourcePtr{ pSceneMember }
+        {
+            SCENE_CLIENT_ASSERT_NOT_NULL(pSceneMember);
+        }
+
+        SceneMemberResourceRequestor::~SceneMemberResourceRequestor()
+        {
+
+        }
+
+        void SceneMemberResourceRequestor::requestSceneActionCreation
+        (const std::string &sceneName, RCSResourceAttributes &attr,
+         InternalAddSceneActionCallback internalCB)
+        {
+            RCSResourceAttributes attributesToSet;
+            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::RemoteAttributesSetCallback setRequestCB
+                = std::bind(&SceneMemberResourceRequestor::onSceneActionCreated,
+                            std::placeholders::_1, std::placeholders::_2,
+                            sceneName, std::move(attr), std::move(internalCB),
+                            SceneMemberResourceRequestor::wPtr(shared_from_this()));
+
+            m_SceneMemberResourcePtr->setRemoteAttributes
+            (std::move(attributesToSet), std::move(setRequestCB));
+        }
+
+        RCSRemoteResourceObject::Ptr SceneMemberResourceRequestor::getRemoteResourceObject()
+        {
+            return m_SceneMemberResourcePtr;
+        }
+
+        void SceneMemberResourceRequestor::onSceneActionCreated
+        (const RCSResourceAttributes &attrs, int eCode, const std::string &sceneName,
+         const RCSResourceAttributes &requestedAttrs, const InternalAddSceneActionCallback &cb,
+         SceneMemberResourceRequestor::wPtr ptr)
+        {
+            SceneMemberResourceRequestor::Ptr memberPtr = ptr.lock();
+
+            if (memberPtr)
+                memberPtr->onSceneActionCreated_impl(
+                    std::move(attrs), eCode, sceneName, std::move(requestedAttrs), std::move(cb));
+        }
+
+        void SceneMemberResourceRequestor::onSceneActionCreated_impl
+        (const RCSResourceAttributes &attrs, int eCode, const std::string &sceneName,
+         const RCSResourceAttributes &requestedAttrs,
+         const InternalAddSceneActionCallback &internalCB)
+        {
+            // TODO: error code
+            int result = SCENE_CLIENT_BADREQUEST;
+
+            if (eCode == SCENE_RESPONSE_SUCCESS)
+            {
+                try
+                {
+                    auto mappings
+                        = attrs.at(SCENE_KEY_SCENEMAPPINGS).get
+                          < std::vector< RCSResourceAttributes > >();
+
+                    // check if the SCENE_MAPPINGS contains requested scene action
+                    int uncreatedActionNum = requestedAttrs.size();
+                    for (const auto &itr : mappings)
+                    {
+                        if (itr.at(SCENE_KEY_SCENE).get< std::string >().compare(sceneName) == 0)
+                        {
+                            std::string key
+                                = itr.at(SCENE_KEY_MEMBERPROPERTY).get< std::string >();
+
+                            if (requestedAttrs.contains(key)
+                                && requestedAttrs.at(key) == itr.at(SCENE_KEY_MEMBERVALUE))
+                            {
+                                uncreatedActionNum--;
+                            }
+                        }
+
+                        if (uncreatedActionNum == 0)
+                        {
+                            result = SCENE_RESPONSE_SUCCESS;
+                            break;
+                        }
+                    }
+                }
+                catch (const std::exception &e)
+                {
+                    SCENE_CLIENT_ASSERT_NOT_NULL(e.what());
+                    result = SCENE_SERVER_INTERNALSERVERERROR;
+                }
+            }
+
+            internalCB(result);
+        }
+
+    }
+}
\ No newline at end of file
diff --git a/service/scene-manager/src/SceneMemberResourceRequestor.h b/service/scene-manager/src/SceneMemberResourceRequestor.h
new file mode 100644 (file)
index 0000000..6df57a1
--- /dev/null
@@ -0,0 +1,75 @@
+//******************************************************************
+//
+// 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_SCENEMEMBER_RESOURCE_REQUESTOR_H_
+#define SM_SCENEMEMBER_RESOURCE_REQUESTOR_H_
+
+#include "SceneCommons.h"
+#include "RCSRemoteResourceObject.h"
+
+namespace OIC
+{
+    namespace Service
+    {
+
+        class SceneMemberResourceRequestor
+            : public std::enable_shared_from_this< SceneMemberResourceRequestor >
+        {
+            public:
+                typedef std::shared_ptr< SceneMemberResourceRequestor > Ptr;
+                typedef std::weak_ptr< SceneMemberResourceRequestor > wPtr;
+
+                typedef std::function < void(int eCode) > InternalAddSceneActionCallback;
+
+
+            public:
+                SceneMemberResourceRequestor(RCSRemoteResourceObject::Ptr pSceneMember,
+                                             const std::string &id);
+                ~SceneMemberResourceRequestor();
+
+                void requestSceneActionCreation(const std::string &sceneName,
+                                                RCSResourceAttributes &attr,
+                                                InternalAddSceneActionCallback);
+
+                RCSRemoteResourceObject::Ptr getRemoteResourceObject();
+
+
+            private:
+                static void onSceneActionCreated(
+                    const RCSResourceAttributes &, int eCode,
+                    const std::string &sceneName, const RCSResourceAttributes &requestedAttrs,
+                    const InternalAddSceneActionCallback &, SceneMemberResourceRequestor::wPtr);
+
+                void onSceneActionCreated_impl(
+                    const RCSResourceAttributes &, int eCode,
+                    const std::string &sceneName, const RCSResourceAttributes &requestedAttrs,
+                    const InternalAddSceneActionCallback &);
+
+
+            private:
+                std::string m_id;
+                RCSRemoteResourceObject::Ptr m_SceneMemberResourcePtr;
+        };
+
+    }
+}
+
+#endif /* SM_SCENEMEMBER_RESOURCE_REQUESTOR_H_ */
+