[IOT-1065][IOT-1069] RC - Tizen: sample application fix
[platform/upstream/iotivity.git] / service / scene-manager / src / RemoteScene.cpp
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "RemoteScene.h"
22
23 #include <utility>
24 #include <cassert>
25
26 #include "SceneCommons.h"
27 #include "SceneCollectionResourceRequestor.h"
28 #include "SceneMemberResourceRequestor.h"
29 #include "OCPlatform.h"
30
31 namespace OIC
32 {
33     namespace Service
34     {
35
36         RemoteScene::RemoteScene(
37             const std::string &name, std::shared_ptr< SceneCollectionResourceRequestor > requestor)
38             : m_name{ name }, m_requestor{ requestor }
39         {
40             assert(requestor);
41         }
42
43         void RemoteScene::addNewSceneAction(
44             RCSRemoteResourceObject::Ptr targetResource, const RCSResourceAttributes &attrs,
45             AddNewSceneActionCallback clientCB)
46         {
47             if (targetResource == nullptr)
48             {
49                 throw RCSInvalidParameterException("RCSRemoteResoureObject value is null");
50             }
51
52             if (!clientCB)
53             {
54                 throw RCSInvalidParameterException{ "addNewSceneAction : Callback is NULL" };
55             }
56
57             SceneCollectionResourceRequestor::InternalAddMemberCallback internalCB
58                 = std::bind(&RemoteScene::onSceneActionAdded, this,
59                 std::placeholders::_1, targetResource, attrs, std::move(clientCB));
60
61             m_requestor->requestAddSceneMember(
62                 targetResource, m_name, attrs, internalCB);
63         }
64
65         void RemoteScene::addNewSceneAction(
66             RCSRemoteResourceObject::Ptr targetResource,
67             const std::string &key, const RCSResourceAttributes::Value &value,
68             AddNewSceneActionCallback clientCB)
69         {
70             RCSResourceAttributes attrs;
71             attrs[key] = RCSResourceAttributes::Value(value);
72
73             addNewSceneAction(targetResource, attrs, clientCB);
74         }
75
76         std::vector< RemoteSceneAction::Ptr > RemoteScene::getRemoteSceneActions() const
77         {
78             std::lock_guard< std::mutex > actionlock(m_sceneActionLock);
79             std::vector< RemoteSceneAction::Ptr > sceneActionList;
80
81             for (auto itrMap : m_remoteSceneActions)
82             {
83                 sceneActionList.push_back(itrMap.second);
84             }
85
86             return sceneActionList;
87         }
88
89         RemoteSceneAction::Ptr RemoteScene::getRemoteSceneAction(
90             const RCSRemoteResourceObject::Ptr targetResource) const
91         {
92             if (targetResource == nullptr)
93             {
94                 throw RCSInvalidParameterException("RCSRemoteResoureObject value is null");
95             }
96
97             std::lock_guard< std::mutex > actionlock(m_sceneActionLock);
98             auto itr = m_remoteSceneActions.find(
99                 targetResource->getAddress() + targetResource->getUri());
100
101             if (itr == m_remoteSceneActions.end())
102             {
103                 throw RCSInvalidParameterException("Invalid RCSRemoteResoureObject");
104             }
105
106             return itr->second;
107         }
108
109         std::string RemoteScene::getName() const
110         {
111             return m_name;
112         }
113
114         void RemoteScene::execute(RemoteSceneExecuteCallback clientCB)
115         {
116             if (!clientCB)
117             {
118                 throw RCSInvalidParameterException{ "execute : Callback is NULL" };
119             }
120
121             SceneCollectionResourceRequestor::InternalSceneRequestCallback internalCB
122                 = std::bind(&RemoteScene::onSceneExecuted, this, std::placeholders::_2,
123                             std::placeholders::_3, std::move(clientCB));
124
125             m_requestor->requestSceneExecution(m_name, internalCB);
126         }
127
128         RemoteSceneAction::Ptr RemoteScene::createRemoteSceneAction(
129             const std::string &targetHref, const RCSResourceAttributes &attrs)
130         {
131             SceneMemberResourceRequestor::Ptr memRequestor
132                 = m_requestor->getSceneMemberResourceRequestor(targetHref);
133
134             if (memRequestor == nullptr)
135             {
136                 return nullptr;
137             }
138
139             RemoteSceneAction::Ptr newAction(new RemoteSceneAction(memRequestor, m_name, attrs));
140
141             {
142                 std::lock_guard< std::mutex > actionlock(m_sceneActionLock);
143                 m_remoteSceneActions.insert(
144                     std::make_pair(targetHref, newAction));
145             }
146
147             return newAction;
148         }
149
150         void RemoteScene::addExistingRemoteSceneAction(
151             const std::string &href, const std::string &id, RCSRemoteResourceObject::Ptr target,
152             const std::string &key, const RCSResourceAttributes::Value &value)
153         {
154             std::string targetHref = target->getAddress() + target->getUri();
155
156             SceneMemberResourceRequestor::Ptr foundMemberRequestor
157                 = m_requestor->getSceneMemberResourceRequestor(targetHref);
158
159             if (foundMemberRequestor == nullptr)
160                 m_requestor->createSceneMemberResourceRequestor(href, id, target);
161
162             RCSResourceAttributes attrs;
163             attrs[key] = RCSResourceAttributes::Value(value);
164
165             createRemoteSceneAction(targetHref, attrs);
166         }
167
168         void RemoteScene::onSceneActionAdded(
169             int eCode, RCSRemoteResourceObject::Ptr target, const RCSResourceAttributes &attrs,
170             const AddNewSceneActionCallback &clientCB)
171         {
172             int result = SCENE_CLIENT_BADREQUEST;
173             RemoteSceneAction::Ptr newAction = nullptr;
174
175             if (eCode == SCENE_RESPONSE_SUCCESS)
176             {
177                 std::string targetLink = target->getAddress() + target->getUri();
178
179                 newAction = createRemoteSceneAction(targetLink, attrs);
180
181                 if (newAction)
182                     result = SCENE_RESPONSE_SUCCESS;
183             }
184
185             clientCB(newAction, result);
186         }
187
188         void RemoteScene::onSceneExecuted(const std::string &sceneName, const int eCode,
189             const RemoteSceneExecuteCallback &clientCB)
190         {
191             clientCB(sceneName, eCode);
192         }
193
194     }
195 }