1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "HostingObject.h"
23 #include "RCSSeparateResponse.h"
24 #include "RequestObject.h"
33 const auto sizeofHostingTag = strlen("/hosting");
36 HostingObject::HostingObject()
37 : remoteObject(nullptr), mirroredServer(nullptr),
38 pDataUpdateCB(nullptr), pDestroyCB(nullptr)
42 HostingObject::~HostingObject()
48 remoteObject->stopMonitoring();
49 remoteObject->stopCaching();
53 auto HostingObject::getRemoteResource() const -> RemoteObjectPtr
58 auto HostingObject::createHostingObject(const RemoteObjectPtr & rResource,
59 DestroyedCallback destroyCB) -> Ptr
61 auto newObject = std::make_shared<HostingObject>();
63 newObject->remoteObject = rResource;
64 newObject->pDestroyCB = destroyCB;
66 newObject->pDataUpdateCB = std::bind(&HostingObject::dataChangedCB, newObject,
67 std::placeholders::_1);
69 newObject->remoteObject->startMonitoring(
70 std::bind(&HostingObject::stateChangedCB, newObject,
71 std::placeholders::_1));
72 newObject->remoteObject->startCaching(newObject->pDataUpdateCB);
77 void HostingObject::destroyHostingObject()
85 void HostingObject::stateChangedCB(ResourceState state)
89 case ResourceState::ALIVE:
91 if (!this->remoteObject->isCaching())
95 this->remoteObject->startCaching(pDataUpdateCB);
96 } catch (const RCSException & e)
98 OIC_HOSTING_LOG(DEBUG,
99 "[HostingObject::stateChangedCB]startCaching InvalidParameterException:%s",
105 case ResourceState::LOST_SIGNAL:
106 case ResourceState::DESTROYED:
110 this->remoteObject->stopCaching();
111 this->remoteObject->stopMonitoring();
112 } catch (const RCSException & e)
114 OIC_HOSTING_LOG(DEBUG,
115 "[HostingObject::stateChangedCB]stopWatching InvalidParameterException:%s",
118 mirroredServer.reset();
119 destroyHostingObject();
124 // not support of state
130 void HostingObject::dataChangedCB(const RCSResourceAttributes & attributes)
132 if (attributes.empty())
137 std::unique_lock<std::mutex> lock(mutexForCB);
138 if (mirroredServer == nullptr)
142 mirroredServer = createMirroredServer(this->remoteObject);
143 } catch (const RCSException & e)
145 OIC_HOSTING_LOG(DEBUG,
146 "[HostingObject::dataChangedCB]createMirroredServer Exception:%s",
153 RCSResourceObject::LockGuard guard(mirroredServer);
154 mirroredServer->getAttributes() = std::move(attributes);
157 auto HostingObject::createMirroredServer(RemoteObjectPtr rObject) -> ResourceObjectPtr
159 if (rObject == nullptr)
161 throw RCSPlatformException(OC_STACK_ERROR);
164 std::string fulluri = rObject->getUri();
165 std::string uri = fulluri.substr(0, fulluri.size() - sizeofHostingTag);
166 std::vector<std::string> types = rObject->getTypes();
167 std::vector<std::string> interfaces = rObject->getInterfaces();
170 auto resourceBuild = RCSResourceObject::Builder(uri, types[0], interfaces[0]);
171 for (unsigned int i = 1; i < types.size(); ++i)
173 resourceBuild.addType(types[i]);
175 for (unsigned int i = 1; i < interfaces.size(); ++i)
177 resourceBuild.addInterface(interfaces[i]);
179 auto retResource = resourceBuild.build();
181 retResource->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
182 retResource->setSetRequestHandler(
183 std::bind(&HostingObject::setRequestHandler, this,
184 std::placeholders::_1, std::placeholders::_2));
189 OIC_HOSTING_LOG(DEBUG, "[HostingObject::createMirroredServer] %s", "Exception");
194 RCSSetResponse HostingObject::setRequestHandler(const RCSRequest & primitiveRequest,
195 RCSResourceAttributes & resourceAttibutes)
199 RequestObject::invokeRequest(getRemoteResource(),
200 primitiveRequest, RequestObject::RequestMethod::Set, resourceAttibutes);
203 catch (const RCSPlatformException & e)
205 OIC_HOSTING_LOG(DEBUG,
206 "[HostingObject::setRequestHandler] PlatformException:%s",
211 return RCSSetResponse::separate();
214 } /* namespace Service */
215 } /* namespace OIC */