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, PCF("Hosting"), buffer);
44 std::mutex mutexForCB;
47 HostingObject::HostingObject()
48 : remoteObject(nullptr), mirroredServer(nullptr),
49 remoteState(ResourceState::NONE),
50 pStateChangedCB(nullptr), pDataUpdateCB(nullptr),
51 pDestroyCB(nullptr), pSetRequestHandler(nullptr)
55 HostingObject::~HostingObject()
63 remoteObject->stopMonitoring();
64 remoteObject->stopCaching();
68 HostingObject::RemoteObjectPtr HostingObject::getRemoteResource() const
73 void HostingObject::initializeHostingObject(RemoteObjectPtr rResource, DestroyedCallback destroyCB)
75 remoteObject = rResource;
77 pStateChangedCB = std::bind(&HostingObject::stateChangedCB, this,
78 std::placeholders::_1, remoteObject);
79 pDataUpdateCB = std::bind(&HostingObject::dataChangedCB, this,
80 std::placeholders::_1, remoteObject);
81 pDestroyCB = destroyCB;
83 pSetRequestHandler = std::bind(&HostingObject::setRequestHandler, this,
84 std::placeholders::_1, std::placeholders::_2);
88 remoteObject->startMonitoring(pStateChangedCB);
89 remoteObject->startCaching(pDataUpdateCB);
96 void HostingObject::destroyHostingObject()
101 void HostingObject::stateChangedCB(ResourceState state, RemoteObjectPtr rObject)
107 case ResourceState::ALIVE:
109 if(rObject->isCaching() == false)
113 rObject->startCaching(pDataUpdateCB);
114 }catch(InvalidParameterException &e)
116 OIC_HOSTING_LOG(DEBUG,
117 "[HostingObject::stateChangedCB]startCaching InvalidParameterException:%s",
123 case ResourceState::LOST_SIGNAL:
124 case ResourceState::DESTROYED:
126 if(rObject->isCaching() == true)
130 rObject->stopCaching();
131 }catch(InvalidParameterException &e)
133 OIC_HOSTING_LOG(DEBUG,
134 "[HostingObject::stateChangedCB]stopCaching InvalidParameterException:%s",
138 if(rObject->isMonitoring() == true)
142 rObject->stopMonitoring();
143 }catch(InvalidParameterException &e)
145 OIC_HOSTING_LOG(DEBUG,
146 "[HostingObject::stateChangedCB]stopWatching InvalidParameterException:%s",
150 mirroredServer = nullptr;
151 destroyHostingObject();
155 // not support of state
160 void HostingObject::dataChangedCB(const RCSResourceAttributes & attributes, RemoteObjectPtr rObject)
162 std::unique_lock<std::mutex> lock(mutexForCB);
163 if(attributes.empty())
168 if(mirroredServer == nullptr)
172 mirroredServer = createMirroredServer(rObject);
173 }catch(PlatformException &e)
175 OIC_HOSTING_LOG(DEBUG,
176 "[HostingObject::dataChangedCB]createMirroredServer PlatformException:%s",
178 mirroredServer = nullptr;
183 RCSResourceAttributes rData;
185 RCSResourceObject::LockGuard guard(mirroredServer);
186 rData = mirroredServer->getAttributes();
188 if(rData.empty() || rData != attributes)
191 RCSResourceObject::LockGuard guard(mirroredServer);
192 for(auto it = rData.begin(); ; ++it)
194 if(it == rData.end())
198 mirroredServer->removeAttribute(it->key());
201 for(auto it = attributes.begin();; ++it)
203 if(it == attributes.end())
207 mirroredServer->setAttribute(it->key(), it->value());
213 HostingObject::ResourceObjectPtr HostingObject::createMirroredServer(RemoteObjectPtr rObject)
215 ResourceObjectPtr retResource = nullptr;
216 if(rObject != nullptr)
218 std::string fulluri = rObject->getUri();
219 std::string uri = fulluri.substr(0, fulluri.size()-8);
220 std::vector<std::string> types = rObject->getTypes();
221 std::vector<std::string> interfaces = rObject->getInterfaces();
224 std::string type = types.begin()->c_str();
225 std::string interface = interfaces.begin()->c_str();
226 retResource = RCSResourceObject::Builder(uri, type, interface).
227 setDiscoverable(true).setObservable(true).build();
229 // TODO need to bind types and interfaces
230 retResource->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
231 retResource->setSetRequestHandler(pSetRequestHandler);
234 OIC_HOSTING_LOG(DEBUG, "[HostingObject::createMirroredServer] %s", "PlatformException");
240 throw PlatformException(OC_STACK_ERROR);
246 RCSSetResponse HostingObject::setRequestHandler(const RCSRequest & primitiveRequest,
247 RCSResourceAttributes & resourceAttibutes)
249 (void)primitiveRequest;
252 RequestObject newRequest = { };
253 newRequest.invokeRequest(remoteObject, RequestObject::RequestMethod::Setter,
255 }catch(PlatformException &e)
257 OIC_HOSTING_LOG(DEBUG,
258 "[HostingObject::setRequestHandler] PlatformException:%s",
263 return RCSSetResponse::create(resourceAttibutes);
266 } /* namespace Service */
267 } /* namespace OIC */