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"
30 const auto sizeofHostingTag = strlen("/hosting");
33 HostingObject::HostingObject()
34 : remoteObject(nullptr), mirroredServer(nullptr),
35 pDataUpdateCB(nullptr), pDestroyCB(nullptr)
39 HostingObject::~HostingObject()
45 remoteObject->stopMonitoring();
46 remoteObject->stopCaching();
50 auto HostingObject::getRemoteResource() const -> RemoteObjectPtr
55 auto HostingObject::createHostingObject(const RemoteObjectPtr & rResource,
56 DestroyedCallback destroyCB) -> Ptr
58 auto newObject = std::make_shared<HostingObject>();
60 newObject->remoteObject = rResource;
61 newObject->pDestroyCB = destroyCB;
63 newObject->pDataUpdateCB = std::bind(&HostingObject::dataChangedCB, newObject,
64 std::placeholders::_1);
66 newObject->remoteObject->startMonitoring(
67 std::bind(&HostingObject::stateChangedCB, newObject,
68 std::placeholders::_1));
69 newObject->remoteObject->startCaching(newObject->pDataUpdateCB);
74 void HostingObject::destroyHostingObject()
76 if(pDestroyCB) pDestroyCB();
79 void HostingObject::stateChangedCB(ResourceState state)
83 case ResourceState::ALIVE:
85 if(!this->remoteObject->isCaching())
89 this->remoteObject->startCaching(pDataUpdateCB);
90 }catch(const RCSException &e)
92 OIC_HOSTING_LOG(DEBUG,
93 "[HostingObject::stateChangedCB]startCaching InvalidParameterException:%s",
99 case ResourceState::LOST_SIGNAL:
100 case ResourceState::DESTROYED:
104 this->remoteObject->stopCaching();
105 this->remoteObject->stopMonitoring();
107 catch(const RCSException &e)
109 OIC_HOSTING_LOG(DEBUG,
110 "[HostingObject::stateChangedCB]stopWatching InvalidParameterException:%s",
113 mirroredServer.reset();
114 destroyHostingObject();
118 // not support of state
123 void HostingObject::dataChangedCB(const RCSResourceAttributes & attributes)
125 if(attributes.empty())
131 std::unique_lock<std::mutex> lock(mutexForCB);
132 if(mirroredServer == nullptr)
136 mirroredServer = createMirroredServer(this->remoteObject);
137 }catch(const RCSException &e)
139 OIC_HOSTING_LOG(DEBUG,
140 "[HostingObject::dataChangedCB]createMirroredServer Exception:%s",
147 RCSResourceObject::LockGuard guard(mirroredServer);
148 mirroredServer->getAttributes() = std::move(attributes);
151 auto HostingObject::createMirroredServer(RemoteObjectPtr rObject) -> ResourceObjectPtr
153 if(rObject == nullptr)
155 throw RCSPlatformException(OC_STACK_ERROR);
158 std::string fulluri = rObject->getUri();
159 std::string uri = fulluri.substr(0, fulluri.size() - sizeofHostingTag);
160 std::vector<std::string> types = rObject->getTypes();
161 std::vector<std::string> interfaces = rObject->getInterfaces();
164 auto retResource = RCSResourceObject::Builder(uri, types[0], interfaces[0]).build();
166 // TODO need to bind types and interfaces
167 retResource->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
168 retResource->setSetRequestHandler(
169 std::bind(&HostingObject::setRequestHandler, this,
170 std::placeholders::_1, std::placeholders::_2));
174 OIC_HOSTING_LOG(DEBUG, "[HostingObject::createMirroredServer] %s", "Exception");
179 RCSSetResponse HostingObject::setRequestHandler(const RCSRequest & primitiveRequest,
180 RCSResourceAttributes & resourceAttibutes)
182 (void)primitiveRequest;
185 RequestObject newRequest = { };
186 newRequest.invokeRequest(remoteObject, RequestObject::RequestMethod::Set,
188 }catch(const RCSPlatformException &e)
190 OIC_HOSTING_LOG(DEBUG,
191 "[HostingObject::setRequestHandler] PlatformException:%s",
196 return RCSSetResponse::defaultAction();
199 } /* namespace Service */
200 } /* namespace OIC */