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"
28 void OIC_HOSTING_LOG(LogLevel level, const char * format, ...)
34 char buffer[MAX_LOG_V_BUFFER_SIZE] = {};
36 va_start(args, format);
37 vsnprintf(buffer, sizeof buffer - 1, format, args);
39 OCLog(level, "Hosting", buffer);
42 HostingObject::HostingObject()
43 : remoteObject(nullptr), mirroredServer(nullptr),
44 remoteState(ResourceState::NONE),
45 pStateChangedCB(nullptr), pDataUpdateCB(nullptr),
46 pDestroyCB(nullptr), pSetRequestHandler(nullptr)
50 HostingObject::RemoteObjectPtr HostingObject::getRemoteResource() const
55 void HostingObject::initializeHostingObject(RemoteObjectPtr rResource, DestroyedCallback destroyCB)
58 remoteObject = rResource;
60 pStateChangedCB = std::bind(&HostingObject::stateChangedCB, this,
61 std::placeholders::_1, remoteObject);
62 pDataUpdateCB = std::bind(&HostingObject::dataChangedCB, this,
63 std::placeholders::_1, remoteObject);
64 pDestroyCB = destroyCB;
66 pSetRequestHandler = std::bind(&HostingObject::setRequestHandler, this,
67 std::placeholders::_1, std::placeholders::_2);
71 remoteObject->startMonitoring(pStateChangedCB);
72 remoteObject->startCaching(pDataUpdateCB);
79 void HostingObject::destroyHostingObject()
84 void HostingObject::stateChangedCB(ResourceState state, RemoteObjectPtr rObject)
90 case ResourceState::ALIVE:
92 if(rObject->isCaching() == false)
96 rObject->startCaching(pDataUpdateCB);
97 }catch(InvalidParameterException &e)
99 OIC_HOSTING_LOG(DEBUG,
100 "[HostingObject::stateChangedCB]startCaching InvalidParameterException:%s",
106 case ResourceState::LOST_SIGNAL:
107 case ResourceState::DESTROYED:
109 if(rObject->isCaching() == true)
113 rObject->stopCaching();
114 }catch(InvalidParameterException &e)
116 OIC_HOSTING_LOG(DEBUG,
117 "[HostingObject::stateChangedCB]stopCaching InvalidParameterException:%s",
121 if(rObject->isMonitoring() == true)
125 rObject->stopMonitoring();
126 }catch(InvalidParameterException &e)
128 OIC_HOSTING_LOG(DEBUG,
129 "[HostingObject::stateChangedCB]stopWatching InvalidParameterException:%s",
133 mirroredServer = nullptr;
134 destroyHostingObject();
138 // not support of state
143 void HostingObject::dataChangedCB(const RCSResourceAttributes & attributes, RemoteObjectPtr rObject)
145 if(attributes.empty())
150 if(mirroredServer == nullptr)
154 mirroredServer = createMirroredServer(rObject);
155 }catch(PlatformException &e)
157 OIC_HOSTING_LOG(DEBUG,
158 "[HostingObject::dataChangedCB]createMirroredServer PlatformException:%s",
160 mirroredServer = nullptr;
165 RCSResourceAttributes rData;
167 RCSResourceObject::LockGuard guard(mirroredServer);
168 rData = mirroredServer->getAttributes();
170 if(rData.empty() || rData != attributes)
173 RCSResourceObject::LockGuard guard(mirroredServer);
174 for(auto it = rData.begin(); ; ++it)
176 if(it == rData.end())
180 mirroredServer->removeAttribute(it->key());
183 for(auto it = attributes.begin();; ++it)
185 if(it == attributes.end())
189 mirroredServer->setAttribute(it->key(), it->value());
195 HostingObject::ResourceObjectPtr HostingObject::createMirroredServer(RemoteObjectPtr rObject)
197 ResourceObjectPtr retResource = nullptr;
198 if(rObject != nullptr)
200 std::string fulluri = rObject->getUri();
201 std::string uri = fulluri.substr(0, fulluri.size()-8);
202 std::vector<std::string> types = rObject->getTypes();
203 std::vector<std::string> interfaces = rObject->getInterfaces();
206 std::string type = types.begin()->c_str();
207 std::string interface = interfaces.begin()->c_str();
208 retResource = RCSResourceObject::Builder(uri, type, interface).
209 setDiscoverable(true).setObservable(true).build();
211 // TODO need to bind types and interfaces
212 retResource->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
213 retResource->setSetRequestHandler(pSetRequestHandler);
216 OIC_HOSTING_LOG(DEBUG, "[HostingObject::createMirroredServer] %s", "PlatformException");
222 throw PlatformException(OC_STACK_ERROR);
228 RCSSetResponse HostingObject::setRequestHandler(const RCSRequest & primitiveRequest,
229 RCSResourceAttributes & resourceAttibutes)
233 RequestObject newRequest = { };
234 newRequest.invokeRequest(remoteObject, RequestObject::RequestMethod::Setter,
236 }catch(PlatformException &e)
238 OIC_HOSTING_LOG(DEBUG,
239 "[HostingObject::setRequestHandler] PlatformException:%s",
244 return RCSSetResponse::create(resourceAttibutes);
247 } /* namespace Service */
248 } /* namespace OIC */