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"
27 #define HOSTING_LOG_TAG PCF("Hosting")
28 #define OIC_HOSTING_LOG(level, tag, ...) OCLogv((level), (HOSTING_LOG_TAG), __VA_ARGS__)
30 HostingObject::HostingObject()
31 : remoteObject(nullptr), mirroredServer(nullptr),
32 remoteState(ResourceState::NOT_MONITORING),
33 pStateChangedCB(nullptr), pDataUpdateCB(nullptr),
34 pDestroyCB(nullptr), pSetRequestHandler(nullptr)
38 HostingObject::RemoteObjectPtr HostingObject::getRemoteResource() const
43 void HostingObject::initializeHostingObject(RemoteObjectPtr rResource, DestroyedCallback destroyCB)
46 remoteObject = rResource;
48 pStateChangedCB = std::bind(&HostingObject::stateChangedCB, this,
49 std::placeholders::_1, remoteObject);
50 pDataUpdateCB = std::bind(&HostingObject::dataChangedCB, this,
51 std::placeholders::_1, remoteObject);
52 pDestroyCB = destroyCB;
54 pSetRequestHandler = std::bind(&HostingObject::setRequestHandler, this,
55 std::placeholders::_1, std::placeholders::_2);
59 remoteObject->startMonitoring(pStateChangedCB);
60 remoteObject->startCaching(pDataUpdateCB);
67 void HostingObject::destroyHostingObject()
72 void HostingObject::stateChangedCB(ResourceState state, RemoteObjectPtr rObject)
78 case ResourceState::ALIVE:
80 if(rObject->isCaching() == false)
84 rObject->startCaching(pDataUpdateCB);
85 }catch(InvalidParameterException &e)
87 OIC_HOSTING_LOG(DEBUG,
88 "[HostingObject::stateChangedCB]startCaching InvalidParameterException:%s",
94 case ResourceState::LOST_SIGNAL:
95 case ResourceState::DESTROYED:
97 if(rObject->isCaching() == true)
101 rObject->stopCaching();
102 }catch(InvalidParameterException &e)
104 OIC_HOSTING_LOG(DEBUG,
105 "[HostingObject::stateChangedCB]stopCaching InvalidParameterException:%s",
109 if(rObject->isMonitoring() == true)
113 rObject->stopMonitoring();
114 }catch(InvalidParameterException &e)
116 OIC_HOSTING_LOG(DEBUG,
117 "[HostingObject::stateChangedCB]stopWatching InvalidParameterException:%s",
121 mirroredServer = nullptr;
122 destroyHostingObject();
126 // not support of state
131 void HostingObject::dataChangedCB(const ResourceAttributes & attributes, RemoteObjectPtr rObject)
133 if(attributes.empty())
138 if(mirroredServer == nullptr)
142 mirroredServer = createMirroredServer(rObject);
143 }catch(PlatformException &e)
145 OIC_HOSTING_LOG(DEBUG,
146 "[HostingObject::dataChangedCB]createMirroredServer PlatformException:%s",
148 mirroredServer = nullptr;
153 ResourceAttributes rData;
155 ResourceObject::LockGuard guard(mirroredServer);
156 rData = mirroredServer->getAttributes();
158 if(rData.empty() || rData != attributes)
161 ResourceObject::LockGuard guard(mirroredServer);
162 for(auto it = rData.begin(); ; ++it)
164 if(it == rData.end())
168 mirroredServer->removeAttribute(it->key());
171 for(auto it = attributes.begin();; ++it)
173 if(it == attributes.end())
177 mirroredServer->setAttribute(it->key(), it->value());
183 HostingObject::ResourceObjectPtr HostingObject::createMirroredServer(RemoteObjectPtr rObject)
185 ResourceObjectPtr retResource = nullptr;
186 if(rObject != nullptr)
188 std::string fulluri = rObject->getUri();
189 std::string uri = fulluri.substr(0, fulluri.size()-8);
190 std::vector<std::string> types = rObject->getTypes();
191 std::vector<std::string> interfaces = rObject->getInterfaces();
194 std::string type = types.begin()->c_str();
195 std::string interface = interfaces.begin()->c_str();
196 retResource = ResourceObject::Builder(uri, type, interface).
197 setDiscoverable(true).setObservable(true).build();
199 // TODO need to bind types and interfaces
200 retResource->setAutoNotifyPolicy(ResourceObject::AutoNotifyPolicy::UPDATED);
201 retResource->setSetRequestHandler(pSetRequestHandler);
204 OIC_HOSTING_LOG(DEBUG, "[HostingObject::createMirroredServer] %s", "PlatformException");
210 throw PlatformException(OC_STACK_ERROR);
216 RCSSetResponse HostingObject::setRequestHandler(const RCSRequest & primitiveRequest,
217 ResourceAttributes & resourceAttibutes)
221 RequestObject newRequest = { };
222 newRequest.invokeRequest(remoteObject, RequestObject::RequestMethod::Setter,
223 primitiveRequest, resourceAttibutes);
224 }catch(PlatformException &e)
226 OIC_HOSTING_LOG(DEBUG,
227 "[HostingObject::setRequestHandler] PlatformException:%s",
232 return RCSSetResponse::create(resourceAttibutes);
235 } /* namespace Service */
236 } /* namespace OIC */