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()
79 if (pDestroyCB) pDestroyCB();
82 void HostingObject::stateChangedCB(ResourceState state)
86 case ResourceState::ALIVE:
88 if (!this->remoteObject->isCaching())
92 this->remoteObject->startCaching(pDataUpdateCB);
93 } catch (const RCSException & e)
95 OIC_HOSTING_LOG(DEBUG,
96 "[HostingObject::stateChangedCB]startCaching InvalidParameterException:%s",
102 case ResourceState::LOST_SIGNAL:
103 case ResourceState::DESTROYED:
107 this->remoteObject->stopCaching();
108 this->remoteObject->stopMonitoring();
109 } catch (const RCSException & e)
111 OIC_HOSTING_LOG(DEBUG,
112 "[HostingObject::stateChangedCB]stopWatching InvalidParameterException:%s",
115 mirroredServer.reset();
116 destroyHostingObject();
120 // not support of state
125 void HostingObject::dataChangedCB(const RCSResourceAttributes & attributes)
127 if (attributes.empty())
132 std::unique_lock<std::mutex> lock(mutexForCB);
133 if (mirroredServer == nullptr)
137 mirroredServer = createMirroredServer(this->remoteObject);
138 } catch (const RCSException & e)
140 OIC_HOSTING_LOG(DEBUG,
141 "[HostingObject::dataChangedCB]createMirroredServer Exception:%s",
148 RCSResourceObject::LockGuard guard(mirroredServer);
149 mirroredServer->getAttributes() = std::move(attributes);
152 auto HostingObject::createMirroredServer(RemoteObjectPtr rObject) -> ResourceObjectPtr
154 if (rObject == nullptr)
156 throw RCSPlatformException(OC_STACK_ERROR);
159 std::string fulluri = rObject->getUri();
160 std::string uri = fulluri.substr(0, fulluri.size() - sizeofHostingTag);
161 std::vector<std::string> types = rObject->getTypes();
162 std::vector<std::string> interfaces = rObject->getInterfaces();
165 auto resourceBuild = RCSResourceObject::Builder(uri, types[0], interfaces[0]);
166 for (unsigned int i = 1; i < types.size(); ++i)
168 resourceBuild.addType(types[i]);
170 for (unsigned int i = 1; i < interfaces.size(); ++i)
172 resourceBuild.addInterface(interfaces[i]);
174 auto retResource = resourceBuild.build();
176 retResource->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
177 retResource->setSetRequestHandler(
178 std::bind(&HostingObject::setRequestHandler, this,
179 std::placeholders::_1, std::placeholders::_2));
183 OIC_HOSTING_LOG(DEBUG, "[HostingObject::createMirroredServer] %s", "Exception");
188 RCSSetResponse HostingObject::setRequestHandler(const RCSRequest & primitiveRequest,
189 RCSResourceAttributes & resourceAttibutes)
193 RequestObject::invokeRequest(getRemoteResource(),
194 primitiveRequest, RequestObject::RequestMethod::Set, resourceAttibutes);
196 } catch (const RCSPlatformException & e)
198 OIC_HOSTING_LOG(DEBUG,
199 "[HostingObject::setRequestHandler] PlatformException:%s",
204 return RCSSetResponse::separate();
207 } /* namespace Service */
208 } /* namespace OIC */