void HostingObject::dataChangedCB(const RCSResourceAttributes & attributes) { if (attributes.empty()) { return; } std::unique_lock<std::mutex> lock(mutexForCB); if (mirroredServer == nullptr) { try { mirroredServer = createMirroredServer(this->remoteObject); } catch (const RCSException & e) { OIC_HOSTING_LOG(DEBUG, "[HostingObject::dataChangedCB]createMirroredServer Exception:%s", e.what()); return; } } lock.unlock(); RCSResourceObject::LockGuard guard(mirroredServer); mirroredServer->getAttributes() = std::move(attributes); }
void ResourceHosting::discoveryHandler(RemoteObjectPtr remoteResource) { std::cout << "ResourceHosting: Discovered device with uri: " << remoteResource->getUri() << std::endl; auto discoverdUri = remoteResource->getUri(); if (discoverdUri.compare( discoverdUri.size()-HOSTING_TAG_SIZE, HOSTING_TAG_SIZE, HOSTING_TAG) != 0) { return; } auto foundHostingObject = findRemoteResource(remoteResource); if (foundHostingObject != nullptr) return; try { HostingObjectKey key = generateHostingObjectKey(remoteResource); foundHostingObject = HostingObject::createHostingObject(remoteResource, std::bind(&ResourceHosting::destroyedHostingObject, this, key)); RHLock lock(m_mutexForList); m_hostingObjects.insert(std::make_pair(key, foundHostingObject)); } catch (const RCSException & e) { OIC_HOSTING_LOG(DEBUG, "[ResourceHosting::discoverHandler]InvalidParameterException:%s", e.what()); } }
void HostingObject::stateChangedCB(ResourceState state) { switch (state) { case ResourceState::ALIVE: { if (!this->remoteObject->isCaching()) { try { this->remoteObject->startCaching(pDataUpdateCB); } catch (const RCSException & e) { OIC_HOSTING_LOG(DEBUG, "[HostingObject::stateChangedCB]startCaching InvalidParameterException:%s", e.what()); } } break; } case ResourceState::LOST_SIGNAL: case ResourceState::DESTROYED: { try { this->remoteObject->stopCaching(); this->remoteObject->stopMonitoring(); } catch (const RCSException & e) { OIC_HOSTING_LOG(DEBUG, "[HostingObject::stateChangedCB]stopWatching InvalidParameterException:%s", e.what()); } mirroredServer.reset(); destroyHostingObject(); break; } default: // not support of state break; } }
void ResourceHosting::startHosting() { try { requestMulticastDiscovery(); }catch(const RCSPlatformException &e) { OIC_HOSTING_LOG(DEBUG, "[ResourceHosting::startHosting]PlatformException:%s", e.what()); throw; }catch(const RCSInvalidParameterException &e) { OIC_HOSTING_LOG(DEBUG, "[ResourceHosting::startHosting]InvalidParameterException:%s", e.what()); throw; }catch(const std::exception &e) { OIC_HOSTING_LOG(DEBUG, "[ResourceHosting::startHosting]std::exception:%s", e.what()); throw; } }
void HostingObject::dataChangedCB(const RCSResourceAttributes & attributes, RemoteObjectPtr rObject) { if(attributes.empty()) { return; } if(mirroredServer == nullptr) { try { mirroredServer = createMirroredServer(rObject); }catch(PlatformException &e) { OIC_HOSTING_LOG(DEBUG, "[HostingObject::dataChangedCB]createMirroredServer PlatformException:%s", e.what()); mirroredServer = nullptr; return; } } RCSResourceAttributes rData; { RCSResourceObject::LockGuard guard(mirroredServer); rData = mirroredServer->getAttributes(); } if(rData.empty() || rData != attributes) { { RCSResourceObject::LockGuard guard(mirroredServer); for(auto it = rData.begin(); ; ++it) { if(it == rData.end()) { break; } mirroredServer->removeAttribute(it->key()); } for(auto it = attributes.begin();; ++it) { if(it == attributes.end()) { break; } mirroredServer->setAttribute(it->key(), it->value()); } } } }
RCSSetResponse HostingObject::setRequestHandler(const RCSRequest & primitiveRequest, RCSResourceAttributes & resourceAttibutes) { try { RequestObject newRequest = { }; newRequest.invokeRequest(remoteObject, RequestObject::RequestMethod::Setter, resourceAttibutes); }catch(PlatformException &e) { OIC_HOSTING_LOG(DEBUG, "[HostingObject::setRequestHandler] PlatformException:%s", e.what()); throw; } return RCSSetResponse::create(resourceAttibutes); }
RCSSetResponse HostingObject::setRequestHandler(const RCSRequest & primitiveRequest, RCSResourceAttributes & resourceAttibutes) { try { RequestObject::invokeRequest(getRemoteResource(), primitiveRequest, RequestObject::RequestMethod::Set, resourceAttibutes); } catch (const RCSPlatformException & e) { OIC_HOSTING_LOG(DEBUG, "[HostingObject::setRequestHandler] PlatformException:%s", e.what()); throw; } return RCSSetResponse::separate(); }