From 3ed6a0b57c3e8b899062f1684bb5898063725199 Mon Sep 17 00:00:00 2001 From: coderhyme Date: Tue, 1 Mar 2016 20:08:57 -0800 Subject: [PATCH] Modified RCSSeparateResponse to send the response built by interface handlers. RCSResourceObject has interface handlers in order to build proper responses by the interfaces of the requests. Separate response should use them to build the response. Change-Id: Ic2b9af279acbecf025a06b493347d6d5cbdea20c Signed-off-by: coderhyme Reviewed-on: https://gerrit.iotivity.org/gerrit/5285 Tested-by: jenkins-iotivity Reviewed-by: Uze Choi --- .../include/RCSResourceObject.h | 725 +++++++++++---------- .../src/serverBuilder/include/InterfaceHandler.h | 4 +- .../src/serverBuilder/src/InterfaceHandler.cpp | 28 +- .../src/serverBuilder/src/RCSResourceObject.cpp | 58 +- .../src/serverBuilder/src/RCSSeparateResponse.cpp | 3 +- 5 files changed, 437 insertions(+), 381 deletions(-) diff --git a/service/resource-encapsulation/include/RCSResourceObject.h b/service/resource-encapsulation/include/RCSResourceObject.h index e913293..505e43a 100644 --- a/service/resource-encapsulation/include/RCSResourceObject.h +++ b/service/resource-encapsulation/include/RCSResourceObject.h @@ -66,7 +66,7 @@ namespace OIC //! @endcond /** - * RCSResourceObject represents a resource and handles any requests from clients + * This class represents a resource and handles any requests from clients * automatically with attributes. * * It also provides an auto notification mechanism that notifies to the observers. @@ -81,405 +81,460 @@ namespace OIC */ class RCSResourceObject : public std::enable_shared_from_this< RCSResourceObject > { - private: - class WeakGuard; + private: + class WeakGuard; + + typedef AtomicWrapper< std::thread::id > AtomicThreadId; + + public: + /** + * Represents the policy of auto-notify function. + * In accord with this policy, observers are notified of attributes + * when the attributes are set. + * + * @note Attributes are set according to the execution of some functions which + * modify attributes or receipt of set requests. + * + * @see RCSResourceObject::setAttribute + * @see RCSResourceObject::removeAttribute + * @see RCSResourceObject::getAttributes + * @see RCSResourceObject::LockGuard + */ + enum class AutoNotifyPolicy + { + NEVER, /**< Never*/ + ALWAYS, /**< Always*/ + UPDATED /**< Only when attributes are changed*/ + }; - typedef AtomicWrapper< std::thread::id > AtomicThreadId; + /** + * Represents the policy of set-request handler. + * In accord with this, the RCSResourceObject decides whether a set-request is + * acceptable or not. + */ + enum class SetRequestHandlerPolicy + { + NEVER, /**< Requests will be ignored if attributes of the request contain + a new key or a value that has different type from the current + value of the key. */ + ACCEPTANCE /**< The attributes of the request will be applied unconditionally + even if there are new name or type conflicts. */ + }; + + typedef std::shared_ptr< RCSResourceObject > Ptr; + typedef std::shared_ptr< const RCSResourceObject > ConstPtr; + /** + * This is a builder to create resource with properties and attributes. + * + * The resource will be observable and discoverable by default, to make them disable + * set these properties explicitly with setDiscoverable and setObservable. + * + * "oic.if.baseline" is an interface that a resource always holds, by default, + * even it is not added manually. + */ + class Builder + { public: /** - * Represents the policy of auto-notify function. - * In accord with this policy, observers are notified of attributes - * when the attributes are set. + * Constructs a Builder. * - * @note Attributes are set according to the execution of some functions which - * modify attributes or receipt of set requests. + * @param uri Resource uri + * @param type Resource type + * @param interface Resource interface * - * @see RCSResourceObject::setAttribute - * @see RCSResourceObject::removeAttribute - * @see RCSResourceObject::getAttributes - * @see RCSResourceObject::LockGuard */ - enum class AutoNotifyPolicy - { - NEVER, /**< Never*/ - ALWAYS, /**< Always*/ - UPDATED /**< Only when attributes are changed*/ - }; + Builder(std::string uri, std::string type, std::string interface); /** - * Represents the policy of set-request handler. - * In accord with this, the RCSResourceObject decides whether a set-request is - * acceptable or not. + * Add an interface for the resource. + * + * @param interface new interface. */ - enum class SetRequestHandlerPolicy - { - NEVER, /**< Requests will be ignored if attributes of the request contain - a new key or a value that has different type from the current - value of the key. */ - ACCEPTANCE /**< The attributes of the request will be applied unconditionally - even if there are new name or type conflicts. */ - }; - - typedef std::shared_ptr< RCSResourceObject > Ptr; - typedef std::shared_ptr< const RCSResourceObject > ConstPtr; + Builder& addInterface(std::string interface); /** - * This is a builder to create resource with properties and attributes. + * Add a type for the resource. * - * The resource will be observable and discoverable by default, to make them disable - * set these properties explicitly with setDiscoverable and setObservable. + * @param type new type. */ - class Builder - { - public: - /** - * Constructs a Builder. - * - * @param uri Resource uri - * @param type Resource type - * @param interface Resource interface - * - */ - Builder(const std::string& uri, const std::string& type, - const std::string& interface); - - Builder& addInterface(const std::string& interface); - Builder& addInterface(std::string&& interface); - - Builder& addType(const std::string& type); - Builder& addType(std::string&& type); - - /** - * Sets the default interface - * - * @param interface default interface name - * - */ - Builder& setDefaultInterface(std::string interface); - - /** - * Sets whether the resource is discoverable. - * - * @param discoverable whether to be discoverable. - * - */ - Builder& setDiscoverable(bool discoverable); - - /** - * Sets the observable property of the resource. - * - * @param observable whether to be observable. - * - */ - Builder& setObservable(bool observable); - - /** - * Sets whether the resource should be secure or not. - * - * @param secureFlag whether to be secure or not. - * - */ - Builder& setSecureFlag(bool secureFlag); - - /** - * Sets attributes for the resource. - * - * @param attributes attributes to set - * - */ - Builder& setAttributes(const RCSResourceAttributes &attributes); - - /** - * @overload - */ - Builder& setAttributes(RCSResourceAttributes &&attributes); - - /** - * Register a resource and returns a RCSResourceObject. - * - * @throw RCSPlatformException if resource registration is failed. - * - */ - RCSResourceObject::Ptr build(); - - private: - std::string m_uri; - std::vector< std::string > m_types; - std::vector< std::string > m_interfaces; - std::string m_defaultInterface; - uint8_t m_properties; - RCSResourceAttributes m_resourceAttributes; - }; - - class LockGuard; + Builder& addType(std::string type); /** - * Callback definition for a handler to be invoked when a get request is received. + * Sets the default interface. + * If the default interface is not sepcified, "oic.if.baseline" * - * The handler will be called first when a get request is received, before the - * RCSResourceObject handles. + * @param interface default interface name * - * @param request the request information - * @param attributes attributes of the request - * - * @return response to be sent and that indicates how the request to be handled by - * the RCSResourceObject. - * - * @see setGetRequestHandler */ - typedef std::function < RCSGetResponse(const RCSRequest& request, - RCSResourceAttributes& attributes) > GetRequestHandler; + Builder& setDefaultInterface(std::string interface); /** - * Callback definition for a handler to be invoked when a set request is received. - * - * The handler will be called first when a get request is received, before the - * RCSResourceObject handles. If the attributes are modified in the callback, - * the modified attributes will be set in the RCSResourceObject if the request is - * not ignored. - * - * @param request the request information - * @param attributes attributes of the request + * Sets whether the resource is discoverable. * - * @return response to be sent and that indicates how the request to be handled by - * the RCSResourceObject. + * @param discoverable whether to be discoverable. * - * @see setGetRequestHandler */ - typedef std::function < RCSSetResponse(const RCSRequest& request, - RCSResourceAttributes& attributes) > SetRequestHandler; + Builder& setDiscoverable(bool discoverable); /** - * Callback definition to be invoked when an attribute is updated. + * Sets the observable property of the resource. + * + * @param observable whether to be observable. * - * @param oldValue the value before being changed - * @param newValue changed value */ - typedef std::function < void(const RCSResourceAttributes::Value& oldValue, - const RCSResourceAttributes::Value& newValue) > AttributeUpdatedListener; - - public: - RCSResourceObject(RCSResourceObject&&) = delete; - RCSResourceObject(const RCSResourceObject&) = delete; - - RCSResourceObject& operator=(RCSResourceObject&&) = delete; - RCSResourceObject& operator=(const RCSResourceObject&) = delete; - - virtual ~RCSResourceObject(); + Builder& setObservable(bool observable); /** - * Sets a particular attribute value. + * Sets whether the resource should be secure or not. * - * @param key key of attribute - * @param value value to be mapped against the key + * @param secureFlag whether to be secure or not. * - * @note Thread-safety is guaranteed for the attributes. */ - void setAttribute(const std::string& key, const RCSResourceAttributes::Value& value); + Builder& setSecureFlag(bool secureFlag); /** - * @overload - */ - void setAttribute(const std::string& key, RCSResourceAttributes::Value&& value); - - /** - * @overload + * Sets attributes for the resource. + * + * @param attributes attributes to set + * */ - void setAttribute(std::string&& key, const RCSResourceAttributes::Value& value); + Builder& setAttributes(const RCSResourceAttributes &attributes); /** * @overload */ - void setAttribute(std::string&& key, RCSResourceAttributes::Value&& value); + Builder& setAttributes(RCSResourceAttributes &&attributes); /** - * Returns an attribute value corresponding to a key. - * - * @param key key of the attribute + * Register a resource and returns a RCSResourceObject. * - * @throws RCSInvalidKeyException If key is invalid. + * @throw RCSPlatformException if resource registration is failed. * - * @note Thread-safety is guaranteed for the attributes. */ - RCSResourceAttributes::Value getAttributeValue(const std::string& key) const; + RCSResourceObject::Ptr build(); - /** - * Returns the attribute value as T. - * - * @param key key of the attribute - * - * @throws RCSBadGetException If type of the underlying value is not T. - * @throws RCSInvalidKeyException If @a key doesn't match the key of any value. - * - * @note Thread-safety is guaranteed for the attributes. - */ - template< typename T > - T getAttribute(const std::string& key) const - { - WeakGuard lock(*this); - return m_resourceAttributes.at(key).get< T >(); - } + private: + std::string m_uri; + std::vector< std::string > m_types; + std::vector< std::string > m_interfaces; + std::string m_defaultInterface; + uint8_t m_properties; + RCSResourceAttributes m_resourceAttributes; + }; - /** - * Removes a particular attribute of the resource. - * - * @param key key of the attribute. - * - * @return True if the key exists and matched attribute is removed, otherwise false. - * - * @note Thread-safety is guaranteed for the attributes. - */ - bool removeAttribute(const std::string& key); + class LockGuard; - /** - * Checks whether a particular attribute exists or not. - * - * @param key key of the attribute - * - * @return True if the key exists, otherwise false. - * - * @note Thread-safety is guaranteed for the attributes. - */ - bool containsAttribute(const std::string& key) const; + /** + * Callback definition for a handler to be invoked when a get request is received. + * + * The handler will be called first when a get request is received, before the + * RCSResourceObject handles. + * + * @param request the request information + * @param attributes attributes of the request + * + * @return response to be sent and that indicates how the request to be handled by + * the RCSResourceObject. + * + * @see setGetRequestHandler + */ + typedef std::function < RCSGetResponse(const RCSRequest& request, + RCSResourceAttributes& attributes) > GetRequestHandler; - /** - * Returns reference to the attributes of the RCSResourceObject. - * - * @pre The call must be guarded by LockGuard. - * - * - * @return Reference to the attributes - * - * @throws NoLockException If the call is not guarded by LockGuard. - * - * @note Here is the standard idiom for LockGuard: - * @code - { - RCSResourceObject::LockGuard lock(rcsResourceObject); - - auto &attributes = server->getAttributes(); - ... - } - * @endcode - */ - RCSResourceAttributes& getAttributes(); + /** + * Callback definition for a handler to be invoked when a set request is received. + * + * The handler will be called first when a get request is received, before the + * RCSResourceObject handles. If the attributes are modified in the callback, + * the modified attributes will be set in the RCSResourceObject if the request is + * not ignored. + * + * @param request the request information + * @param attributes attributes of the request + * + * @return response to be sent and that indicates how the request to be handled by + * the RCSResourceObject. + * + * @see setGetRequestHandler + */ + typedef std::function < RCSSetResponse(const RCSRequest& request, + RCSResourceAttributes& attributes) > SetRequestHandler; - /** - * @overload - */ - const RCSResourceAttributes& getAttributes() const; + /** + * Callback definition to be invoked when an attribute is updated. + * + * @param oldValue the value before being changed + * @param newValue changed value + */ + typedef std::function < void(const RCSResourceAttributes::Value& oldValue, + const RCSResourceAttributes::Value& newValue) > AttributeUpdatedListener; - /** - * Checks whether the resource is observable or not. - */ - virtual bool isObservable() const; + public: + RCSResourceObject(RCSResourceObject&&) = delete; + RCSResourceObject(const RCSResourceObject&) = delete; - /** - * Checks whether the resource is discoverable or not. - */ - virtual bool isDiscoverable() const; + RCSResourceObject& operator=(RCSResourceObject&&) = delete; + RCSResourceObject& operator=(const RCSResourceObject&) = delete; - /** - * Sets the get request handler. - * To remove handler, pass empty handler or nullptr. - * - * Default behavior is RCSGetResponse::defaultAction(). - * - * @param handler a get request handler - * - * @see RCSGetResponse - * - */ - virtual void setGetRequestHandler(GetRequestHandler handler); + virtual ~RCSResourceObject(); - /** - * Sets the set request handler. - * To remove handler, pass empty handler or nullptr. - * - * Default behavior is RCSSetResponse::defaultAction(). - * - * @param handler a set request handler - * - * @see RCSSetResponse - * - */ - virtual void setSetRequestHandler(SetRequestHandler handler); + /** + * Sets a particular attribute value. + * + * @param key key of attribute + * @param value value to be mapped against the key + * + * @note Thread-safety is guaranteed for the attributes. + */ + void setAttribute(const std::string& key, const RCSResourceAttributes::Value& value); - /** - * Adds a listener for a particular attribute updated. - * - * @param key the interested attribute's key - * @param listener listener to be invoked - * - */ - virtual void addAttributeUpdatedListener(const std::string& key, - AttributeUpdatedListener listener); + /** + * @overload + */ + void setAttribute(const std::string& key, RCSResourceAttributes::Value&& value); - /** - * @overload - */ - virtual void addAttributeUpdatedListener(std::string&& key, - AttributeUpdatedListener listener); + /** + * @overload + */ + void setAttribute(std::string&& key, const RCSResourceAttributes::Value& value); - /** - * Removes a listener for a particular attribute updated. - * - * @param key the key associated with the listener to be removed - * - * @return True if the listener added with same key exists and is removed. - * - */ - virtual bool removeAttributeUpdatedListener(const std::string& key); + /** + * @overload + */ + void setAttribute(std::string&& key, RCSResourceAttributes::Value&& value); - /** - * Notifies all observers of the current attributes. - * - * @throws RCSPlatformException If the operation failed. - */ - virtual void notify() const; + /** + * Returns an attribute value corresponding to a key. + * + * @param key key of the attribute + * + * @throws RCSInvalidKeyException If key is invalid. + * + * @note Thread-safety is guaranteed for the attributes. + */ + RCSResourceAttributes::Value getAttributeValue(const std::string& key) const; - /** - * Sets auto notify policy - * - * @param policy policy to be set - * - */ - void setAutoNotifyPolicy(AutoNotifyPolicy policy); + /** + * Returns the attribute value as T. + * + * @param key key of the attribute + * + * @throws RCSBadGetException If type of the underlying value is not T. + * @throws RCSInvalidKeyException If @a key doesn't match the key of any value. + * + * @note Thread-safety is guaranteed for the attributes. + */ + template< typename T > + T getAttribute(const std::string& key) const + { + WeakGuard lock(*this); + return m_resourceAttributes.at(key).get< T >(); + } - /** - * Returns the current policy - * - */ - AutoNotifyPolicy getAutoNotifyPolicy() const; + /** + * Removes a particular attribute of the resource. + * + * @param key key of the attribute. + * + * @return True if the key exists and matched attribute is removed, otherwise false. + * + * @note Thread-safety is guaranteed for the attributes. + */ + bool removeAttribute(const std::string& key); - /** - * Sets the policy for handling a set request. - * - * @param policy policy to be set - * - */ - void setSetRequestHandlerPolicy(SetRequestHandlerPolicy policy); + /** + * Checks whether a particular attribute exists or not. + * + * @param key key of the attribute + * + * @return True if the key exists, otherwise false. + * + * @note Thread-safety is guaranteed for the attributes. + */ + bool containsAttribute(const std::string& key) const; - /** - * Returns the current policy. - * - */ - SetRequestHandlerPolicy getSetRequestHandlerPolicy() const; + /** + * Returns reference to the attributes of the RCSResourceObject. + * + * @pre The call must be guarded by LockGuard. + * + * + * @return Reference to the attributes + * + * @throws NoLockException If the call is not guarded by LockGuard. + * + * @note Here is the standard idiom for LockGuard: + * @code + { + RCSResourceObject::LockGuard lock(rcsResourceObject); + + auto &attributes = server->getAttributes(); + ... + } + * @endcode + */ + RCSResourceAttributes& getAttributes(); + + /** + * @overload + */ + const RCSResourceAttributes& getAttributes() const; + + /** + * Checks whether the resource is observable or not. + */ + virtual bool isObservable() const; + + /** + * Checks whether the resource is discoverable or not. + */ + virtual bool isDiscoverable() const; + + /** + * Sets the get request handler. + * To remove handler, pass empty handler or nullptr. + * + * Default behavior is RCSGetResponse::defaultAction(). + * + * @param handler a get request handler + * + * @see RCSGetResponse + * + */ + virtual void setGetRequestHandler(GetRequestHandler handler); + + /** + * Sets the set request handler. + * To remove handler, pass empty handler or nullptr. + * + * Default behavior is RCSSetResponse::defaultAction(). + * + * @param handler a set request handler + * + * @see RCSSetResponse + * + */ + virtual void setSetRequestHandler(SetRequestHandler handler); + + /** + * Adds a listener for a particular attribute updated. + * + * @param key the interested attribute's key + * @param listener listener to be invoked + * + */ + virtual void addAttributeUpdatedListener(const std::string& key, + AttributeUpdatedListener listener); + + /** + * @overload + */ + virtual void addAttributeUpdatedListener(std::string&& key, + AttributeUpdatedListener listener); + + /** + * Removes a listener for a particular attribute updated. + * + * @param key the key associated with the listener to be removed + * + * @return True if the listener added with same key exists and is removed. + * + */ + virtual bool removeAttributeUpdatedListener(const std::string& key); + + /** + * Notifies all observers of the current attributes. + * + * @throws RCSPlatformException If the operation failed. + */ + virtual void notify() const; + + /** + * Sets auto notify policy + * + * @param policy policy to be set + * + */ + void setAutoNotifyPolicy(AutoNotifyPolicy policy); + + /** + * Returns the current policy + * + */ + AutoNotifyPolicy getAutoNotifyPolicy() const; + + /** + * Sets the policy for handling a set request. + * + * @param policy policy to be set + * + */ + void setSetRequestHandlerPolicy(SetRequestHandlerPolicy policy); + + /** + * Returns the current policy. + * + */ + SetRequestHandlerPolicy getSetRequestHandlerPolicy() const; - void bindResource(const RCSResourceObject::Ptr&); + /** + * Bind a resource to this resource. + * Binding another resource makes this resource work as a collection resource, + * by default. + * + * @param resource a resource to be bound to this resource. + * + * @throws RCSInvalidParameterException If resource is nullptr or itself. + * @throws RCSPlatformException If the operation failed. + * + * @see unbindResource + */ + void bindResource(const RCSResourceObject::Ptr& resource); + + /** + * Unbind a resource from this resource. + * If there is no bound resource left, the resource will run as a normal resource. + * + * @param resource a resource to be unbound from this resource. + * + * @throws RCSInvalidParameterException If resource is nullptr or itself. + * @throws RCSPlatformException If the operation failed. + * + * @see bindResource + */ + void unbindResource(const RCSResourceObject::Ptr& resource); - void unbindResource(const RCSResourceObject::Ptr&); + /** + * Returns all bound resources to this resource. + */ + std::vector< RCSResourceObject::Ptr > getBoundResources() const; - std::vector< RCSResourceObject::Ptr > getBoundResources() const; + /** + * Returns the uri of the resource. + */ + std::string getUri() const; - std::string getUri() const; - std::string getDefaultInterface() const; + /** + * Returns the default interface of the resource + * + * @see Builder::setDefaultInterface + */ + std::string getDefaultInterface() const; - std::vector< std::string > getInterfaces() const; - std::vector< std::string > getTypes() const; + /** + * Returns all interfaces added for the resource. + * + * @see Builder::addInterface + */ + std::vector< std::string > getInterfaces() const; - RCSRepresentation toRepresentation() const; + /** + * Returns all types added for the resource. + * + * @see Builder::addType + */ + std::vector< std::string > getTypes() const; private: RCSResourceObject(const std::string&, uint8_t, RCSResourceAttributes&&); @@ -513,11 +568,12 @@ namespace OIC template< typename K, typename V > void setAttributeInternal(K&&, V&&); - RCSResourceAttributes applyAcceptanceMethod(const RCSSetResponse&, - const RCSResourceAttributes&); + bool applyAcceptanceMethod(const RCSSetResponse&, const RCSResourceAttributes&); InterfaceHandler findInterfaceHandler(const std::string&) const; + RCSRepresentation getRepresentation(const RCSRequest&) const; + private: const uint8_t m_properties; @@ -550,6 +606,7 @@ namespace OIC std::map< std::string, InterfaceHandler > m_interfaceHandlers; + friend class RCSSeparateResponse; }; /** diff --git a/service/resource-encapsulation/src/serverBuilder/include/InterfaceHandler.h b/service/resource-encapsulation/src/serverBuilder/include/InterfaceHandler.h index 2f8150f..0049785 100644 --- a/service/resource-encapsulation/src/serverBuilder/include/InterfaceHandler.h +++ b/service/resource-encapsulation/src/serverBuilder/include/InterfaceHandler.h @@ -41,10 +41,10 @@ namespace OIC class InterfaceHandler { public: - typedef std::function< RCSRepresentation(RCSRequest, RCSResourceObject&) > + typedef std::function< RCSRepresentation(RCSRequest, const RCSResourceObject&) > GetResponseBuilder; - typedef std::function< RCSRepresentation(RCSRequest, RCSResourceObject&) > + typedef std::function< RCSRepresentation(RCSRequest, const RCSResourceObject&) > SetResponseBuilder; public: diff --git a/service/resource-encapsulation/src/serverBuilder/src/InterfaceHandler.cpp b/service/resource-encapsulation/src/serverBuilder/src/InterfaceHandler.cpp index 21422ba..1825246 100644 --- a/service/resource-encapsulation/src/serverBuilder/src/InterfaceHandler.cpp +++ b/service/resource-encapsulation/src/serverBuilder/src/InterfaceHandler.cpp @@ -1,6 +1,5 @@ #include "InterfaceHandler.h" -#include "OCApi.h" #include "OCResourceRequest.h" #include "RCSRepresentation.h" @@ -15,32 +14,39 @@ namespace { using namespace OIC::Service; - RCSRepresentation buildGetBaselineResponse(const RCSRequest&, RCSResourceObject& resource) + RCSRepresentation toRepresentation(const RCSResourceObject& resource) { - RCSResourceObject::LockGuard lock(resource); + RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER }; return RCSRepresentation{ resource.getUri(), resource.getInterfaces(), resource.getTypes(), resource.getAttributes()}; } - RCSRepresentation buildSetBaselineResponse(const RCSRequest& rcsRequest, RCSResourceObject& resource) + RCSRepresentation buildGetBaselineResponse(const RCSRequest&, const RCSResourceObject& resource) + { + return toRepresentation(resource); + } + + RCSRepresentation buildSetBaselineResponse(const RCSRequest& rcsRequest, + const RCSResourceObject& resource) { return buildGetBaselineResponse(rcsRequest, resource); } - RCSRepresentation buildGetRequestResponse(const RCSRequest&, RCSResourceObject& resource) + RCSRepresentation buildGetRequestResponse(const RCSRequest&, const RCSResourceObject& resource) { - RCSResourceObject::LockGuard lock(resource); + RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER }; - return RCSRepresentation{resource.getAttributes()}; + return RCSRepresentation{ resource.getAttributes() }; } - RCSRepresentation buildSetRequestResponse(const RCSRequest& rcsRequest, RCSResourceObject& resource) + RCSRepresentation buildSetRequestResponse(const RCSRequest& rcsRequest, + const RCSResourceObject& resource) { auto requestAttr = ResourceAttributesConverter::fromOCRepresentation( (rcsRequest.getOCRequest())->getResourceRepresentation()); - RCSResourceObject::LockGuard lock(resource); + RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER }; const RCSResourceAttributes& updatedAttr = resource.getAttributes(); @@ -60,13 +66,13 @@ namespace return RCSRepresentation{ requestAttr }; } - RCSRepresentation buildGetBatchResponse(RCSRequest, RCSResourceObject& resource) + RCSRepresentation buildGetBatchResponse(RCSRequest, const RCSResourceObject& resource) { RCSRepresentation rcsRep; for (const auto& bound : resource.getBoundResources()) { - rcsRep.addChild(bound->toRepresentation()); + rcsRep.addChild(toRepresentation(*bound)); } return rcsRep; diff --git a/service/resource-encapsulation/src/serverBuilder/src/RCSResourceObject.cpp b/service/resource-encapsulation/src/serverBuilder/src/RCSResourceObject.cpp index ac49c84..0e52fdd 100644 --- a/service/resource-encapsulation/src/serverBuilder/src/RCSResourceObject.cpp +++ b/service/resource-encapsulation/src/serverBuilder/src/RCSResourceObject.cpp @@ -130,36 +130,24 @@ namespace OIC namespace Service { - RCSResourceObject::Builder::Builder(const std::string& uri, const std::string& type, - const std::string& interface) : - m_uri{ uri }, - m_types{ type }, - m_interfaces{ interface }, + RCSResourceObject::Builder::Builder(std::string uri, std::string type, + std::string interface) : + m_uri{ std::move(uri) }, + m_types{ std::move(type) }, + m_interfaces{ std::move(interface) }, m_defaultInterface { BASELINE_INTERFACE }, m_properties{ OC_DISCOVERABLE | OC_OBSERVABLE }, m_resourceAttributes{ } { } - RCSResourceObject::Builder& RCSResourceObject::Builder::addInterface( - const std::string& interface) - { - return addInterface(std::string{ interface }); - } - - RCSResourceObject::Builder& RCSResourceObject::Builder::addInterface( - std::string&& interface) + RCSResourceObject::Builder& RCSResourceObject::Builder::addInterface(std::string interface) { m_interfaces.push_back(std::move(interface)); return *this; } - RCSResourceObject::Builder& RCSResourceObject::Builder::addType(const std::string& type) - { - return addType(std::string{ type }); - } - - RCSResourceObject::Builder& RCSResourceObject::Builder::addType(std::string&& type) + RCSResourceObject::Builder& RCSResourceObject::Builder::addType(std::string type) { m_types.push_back(std::move(type)); return *this; @@ -289,7 +277,7 @@ namespace OIC { try { - OC::OCPlatform::unregisterResource(m_resourceHandle); + invokeOCFunc(OC::OCPlatform::unregisterResource, m_resourceHandle); } catch (...) { @@ -540,10 +528,21 @@ namespace OIC return m_types; } - RCSRepresentation RCSResourceObject::toRepresentation() const + RCSRepresentation RCSResourceObject::getRepresentation(const RCSRequest& request) const { - WeakGuard lock{*this}; - return RCSRepresentation{ m_uri, m_interfaces, m_types, m_resourceAttributes }; + if (request.getOCRequest()->getRequestType() == "GET") + { + return findInterfaceHandler( + request.getInterface()).getGetResponseBuilder()(request, *this); + } + + if (request.getOCRequest()->getRequestType() == "POST") + { + return findInterfaceHandler( + request.getInterface()).getSetResponseBuilder()(request, *this); + } + + throw RCSBadRequestException{ "Unsupported request type!" }; } void RCSResourceObject::autoNotify(bool isAttributesChanged) const @@ -645,7 +644,7 @@ namespace OIC findInterfaceHandler(request.getInterface()).getGetResponseBuilder()); } - RCSResourceAttributes RCSResourceObject::applyAcceptanceMethod( + bool RCSResourceObject::applyAcceptanceMethod( const RCSSetResponse& response, const RCSResourceAttributes& requestAttrs) { auto requestHandler = response.getHandler(); @@ -675,11 +674,9 @@ namespace OIC { (*foundListener)(attrKeyValPair.second, requestAttrs.at(attrKeyValPair.first)); } - - result[attrKeyValPair.first] = attrKeyValPair.second; } - return result; + return !replaced.empty(); } OCEntityHandlerResult RCSResourceObject::handleRequestSet( @@ -697,9 +694,7 @@ namespace OIC if (response.isSeparate()) return OC_EH_SLOW; - auto replaced = applyAcceptanceMethod(response, attrs); - - autoNotify(!replaced.empty(), m_autoNotifyPolicy); + autoNotify(applyAcceptanceMethod(response, attrs), m_autoNotifyPolicy); return sendResponse(request, response, findInterfaceHandler(request.getInterface()).getSetResponseBuilder()); @@ -746,8 +741,7 @@ namespace OIC else { ocResponse->setResourceRepresentation( - RCSRepresentation::toOCRepresentation( - resBuilder(request, *this))); + RCSRepresentation::toOCRepresentation(resBuilder(request, *this))); } return ::sendResponse(request.getOCRequest(), ocResponse); diff --git a/service/resource-encapsulation/src/serverBuilder/src/RCSSeparateResponse.cpp b/service/resource-encapsulation/src/serverBuilder/src/RCSSeparateResponse.cpp index 69cc22a..8a9e1c0 100755 --- a/service/resource-encapsulation/src/serverBuilder/src/RCSSeparateResponse.cpp +++ b/service/resource-encapsulation/src/serverBuilder/src/RCSSeparateResponse.cpp @@ -83,9 +83,8 @@ namespace OIC response->setResponseResult(OC_EH_OK); - // TODO the response should be different by the request interface. response->setResourceRepresentation( - RCSRepresentation::toOCRepresentation(resObj->toRepresentation())); + RCSRepresentation::toOCRepresentation(resObj->getRepresentation(m_request))); invokeOCFunc(OC::OCPlatform::sendResponse, response); -- 2.7.4