//! @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.
*/
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&&);
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;
std::map< std::string, InterfaceHandler > m_interfaceHandlers;
+ friend class RCSSeparateResponse;
};
/**