IoTivity C++ SDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RCSResourceObject.h
Go to the documentation of this file.
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 
26 #ifndef SERVER_RCSRESOURCEOBJECT_H
27 #define SERVER_RCSRESOURCEOBJECT_H
28 
29 #include <string>
30 #include <mutex>
31 #include <thread>
32 #include <map>
33 
34 #include "RCSResourceAttributes.h"
35 #include "RCSResponse.h"
36 
37 namespace OC
38 {
39  class OCResourceRequest;
40 }
41 
42 namespace OIC
43 {
44  namespace Service
45  {
46 
47  class RCSRequest;
48  class RCSRepresentation;
49  class InterfaceHandler;
50 
58  {
59  public:
60  NoLockException(std::string what) : RCSException { std::move(what) } {}
61  };
62 
64  template < typename T >
65  class AtomicWrapper;
67 
84  {
85  private:
86 
87  typedef AtomicWrapper< std::thread::id > AtomicThreadId;
88 
90  class WeakGuard
91  {
92  public:
93  WeakGuard(const RCSResourceObject&);
94  ~WeakGuard();
95 
96  WeakGuard(const WeakGuard&) = delete;
97  WeakGuard(WeakGuard&&) = delete;
98 
99  WeakGuard& operator=(const WeakGuard&) = delete;
100  WeakGuard& operator=(WeakGuard&&) = delete;
101 
102  bool hasLocked() const;
103 
104  private:
105  bool m_isOwningLock;
106  const RCSResourceObject& m_resourceObject;
107  };
109 
110  public:
111 
125  enum class AutoNotifyPolicy
126  {
127  NEVER,
128  ALWAYS,
129  UPDATED
130  };
131 
138  {
139  NEVER,
142  ACCEPTANCE
144  };
145 
146  typedef std::shared_ptr< RCSResourceObject > Ptr;
147  typedef std::shared_ptr< const RCSResourceObject > ConstPtr;
148 
158  class Builder
159  {
160  public:
169  Builder(std::string uri, std::string type, std::string interface);
170 
176  Builder& addInterface(std::string interface);
177 
183  Builder& addType(std::string type);
184 
192  Builder& setDefaultInterface(std::string interface);
193 
200  Builder& setDiscoverable(bool discoverable);
201 
208  Builder& setObservable(bool observable);
209 
216  Builder& setSecureFlag(bool secureFlag);
217 
224  Builder& setAttributes(const RCSResourceAttributes &attributes);
225 
230 
238 
239  private:
240  std::string m_uri;
241  std::vector< std::string > m_types;
242  std::vector< std::string > m_interfaces;
243  std::string m_defaultInterface;
244  uint8_t m_properties;
245  RCSResourceAttributes m_resourceAttributes;
246  };
247 
248  class LockGuard;
249 
264  typedef std::function < RCSGetResponse(const RCSRequest& request,
266 
283  typedef std::function < RCSSetResponse(const RCSRequest& request,
285 
292  typedef std::function < void(const RCSResourceAttributes::Value& oldValue,
294 
295  public:
297  RCSResourceObject(const RCSResourceObject&) = delete;
298 
301 
302  virtual ~RCSResourceObject();
303 
312  void setAttribute(const std::string& key, const RCSResourceAttributes::Value& value);
313 
317  void setAttribute(const std::string& key, RCSResourceAttributes::Value&& value);
318 
322  void setAttribute(std::string&& key, const RCSResourceAttributes::Value& value);
323 
327  void setAttribute(std::string&& key, RCSResourceAttributes::Value&& value);
328 
338  RCSResourceAttributes::Value getAttributeValue(const std::string& key) const;
339 
350  template< typename T >
351  T getAttribute(const std::string& key) const
352  {
353  RCSResourceObject::WeakGuard lock(*this);
354  return m_resourceAttributes.at(key).get< T >();
355  }
356 
366  bool removeAttribute(const std::string& key);
367 
377  bool containsAttribute(const std::string& key) const;
378 
400 
404  const RCSResourceAttributes& getAttributes() const;
405 
409  virtual bool isObservable() const;
410 
414  virtual bool isDiscoverable() const;
415 
427  virtual void setGetRequestHandler(GetRequestHandler handler);
428 
440  virtual void setSetRequestHandler(SetRequestHandler handler);
441 
449  virtual void addAttributeUpdatedListener(const std::string& key,
450  AttributeUpdatedListener listener);
451 
455  virtual void addAttributeUpdatedListener(std::string&& key,
456  AttributeUpdatedListener listener);
457 
466  virtual bool removeAttributeUpdatedListener(const std::string& key);
467 
473  virtual void notify() const;
474 
482 
488 
496 
502 
515  void bindResource(const RCSResourceObject::Ptr& resource);
516 
528  void unbindResource(const RCSResourceObject::Ptr& resource);
529 
533  std::vector< RCSResourceObject::Ptr > getBoundResources() const;
534 
538  std::string getUri() const;
539 
545  std::string getDefaultInterface() const;
546 
552  std::vector< std::string > getInterfaces() const;
553 
559  std::vector< std::string > getTypes() const;
560 
565 
566  private:
567  RCSResourceObject(const std::string&, uint8_t, RCSResourceAttributes&&);
568 
569  void init(OCResourceHandle, const std::vector< std::string >&,
570  const std::vector< std::string >&, const std::string&);
571 
572  static OCEntityHandlerResult entityHandler(const std::weak_ptr< RCSResourceObject >&,
573  const std::shared_ptr< OC::OCResourceRequest >&);
574 
575  OCEntityHandlerResult handleRequest(const RCSRequest&);
576  OCEntityHandlerResult handleRequestGet(const RCSRequest&);
577  OCEntityHandlerResult handleRequestSet(const RCSRequest&);
578  OCEntityHandlerResult handleObserve(const RCSRequest&);
579 
580  template <typename RESPONSE, typename RESPONSE_BUILDER>
582  const RESPONSE&, const RESPONSE_BUILDER&);
583 
584  void expectOwnLock() const;
585 
586  std::thread::id getLockOwner() const noexcept;
587 
588  void setLockOwner(std::thread::id&&) const noexcept;
589 
590  void autoNotify(bool, AutoNotifyPolicy) const;
591  void autoNotify(bool) const;
592 
593  bool testValueUpdated(const std::string&, const RCSResourceAttributes::Value&) const;
594 
595  template< typename K, typename V >
596  void setAttributeInternal(K&&, V&&);
597 
598  bool applyAcceptanceMethod(const RCSSetResponse&, const RCSResourceAttributes&);
599 
600  InterfaceHandler findInterfaceHandler(const std::string&) const;
601 
602  RCSRepresentation getRepresentation(const RCSRequest&) const;
603 
604  private:
605  const uint8_t m_properties;
606 
607  const std::string m_uri;
608  std::vector< std::string > m_interfaces;
609  std::vector< std::string > m_types;
610  std::string m_defaultInterface;
611 
612  OCResourceHandle m_resourceHandle;
613 
614  RCSResourceAttributes m_resourceAttributes;
615 
616  std::shared_ptr< GetRequestHandler > m_getRequestHandler;
617  std::shared_ptr< SetRequestHandler > m_setRequestHandler;
618 
619  AutoNotifyPolicy m_autoNotifyPolicy;
620  SetRequestHandlerPolicy m_setRequestHandlerPolicy;
621 
622  std::unordered_map< std::string, std::shared_ptr< AttributeUpdatedListener > >
623  m_attributeUpdatedListeners;
624 
625  mutable std::unique_ptr< AtomicThreadId > m_lockOwner;
626  mutable std::mutex m_mutex;
627 
628  std::mutex m_mutexAttributeUpdatedListeners;
629 
630  mutable std::mutex m_mutexForBoundResources;
631 
632  std::vector< RCSResourceObject::Ptr > m_boundResources;
633 
634  std::map< std::string, InterfaceHandler > m_interfaceHandlers;
635 
636  friend class RCSSeparateResponse;
637  };
638 
651  {
652  public:
653  LockGuard(const RCSResourceObject& rcsResourceObject);
654 
655  LockGuard(const RCSResourceObject::Ptr);
656 
665  LockGuard(const RCSResourceObject& object, AutoNotifyPolicy autoNotifyPolicy);
666 
670  LockGuard(const RCSResourceObject::Ptr, AutoNotifyPolicy);
671 
677  ~LockGuard() noexcept(false);
678 
679  LockGuard(const LockGuard&) = delete;
680  LockGuard(LockGuard&&) = delete;
681 
682  LockGuard& operator=(const LockGuard&) = delete;
683  LockGuard& operator=(LockGuard&&) = delete;
684 
685  private:
686  void init();
687 
688  private:
689  const RCSResourceObject& m_resourceObject;
690 
691  AutoNotifyPolicy m_autoNotifyPolicy;
692 
693  bool m_isOwningLock;
694 
695  std::function<void()> m_autoNotifyFunc;
696  };
697 
698  }
699 }
700 
701 #endif // SERVER_RCSRESOURCEOBJECT_H
virtual void addAttributeUpdatedListener(const std::string &key, AttributeUpdatedListener listener)
Adds a listener for a particular attribute updated.
virtual bool isDiscoverable() const
Checks whether the resource is discoverable or not.
std::string getUri() const
Returns the uri of the resource.
OCEntityHandlerResult
Possible returned values from entity handler.
Definition: octypes.h:1229
RCSResourceObject::Ptr build()
Register a resource and returns a RCSResourceObject.
Thrown when lock has not been acquired.
Definition: RCSResourceObject.h:57
OCStackResult sendResponse(const std::shared_ptr< OCResourceResponse > pResponse)
Allows application entity handler to send response to an incoming request.
SetRequestHandlerPolicy getSetRequestHandlerPolicy() const
Returns the current policy.
The base exception class for resource encapsulation.
Definition: RCSException.h:44
void bindResource(const RCSResourceObject::Ptr &resource)
Bind a resource to this resource.
std::vector< std::string > getInterfaces() const
Returns all interfaces added for the resource.
This class provides factory methods to create the response for a received get request.
Definition: RCSResponse.h:47
virtual void notify() const
Notifies all observers of the current attributes.
std::shared_ptr< RCSResourceObject > Ptr
Definition: RCSResourceObject.h:146
virtual const char * what() const BOOST_NOEXCEPT
Returns the exception description.
std::function< void(const RCSResourceAttributes::Value &oldValue, const RCSResourceAttributes::Value &newValue) > AttributeUpdatedListener
Callback definition to be invoked when an attribute is updated.
Definition: RCSResourceObject.h:293
void * OCResourceHandle
Handle to an OCResource object owned by the OCStack.
Definition: octypes.h:1152
Requests will be ignored if attributes of the request contain a new key or a value that has different...
OCResourceHandle getResourceHandle() const
Returns the resource handle of the resource.
RCSResourceAttributes & getAttributes()
Returns reference to the attributes of the RCSResourceObject.
This file contains the declaration of classes and its members related to RCSResourceAttributes.
This class represents a resource and handles any requests from clients automatically with attributes...
Definition: RCSResourceObject.h:83
This class describes a resource representation.
Definition: RCSRepresentation.h:42
Builder(std::string uri, std::string type, std::string interface)
Constructs a Builder.
bool removeAttribute(const std::string &key)
Removes a particular attribute of the resource.
SetRequestHandlerPolicy
Represents the policy of set-request handler.
Definition: RCSResourceObject.h:137
Value holds a value among various types at a time.
Definition: RCSResourceAttributes.h:432
Builder & setAttributes(const RCSResourceAttributes &attributes)
Sets attributes for the resource.
The class provides a convenient RAII-style mechanism for the attributes of a RCSResourceObject.
Definition: RCSResourceObject.h:650
Builder & setSecureFlag(bool secureFlag)
Sets whether the resource should be secure or not.
virtual bool isObservable() const
Checks whether the resource is observable or not.
This file contains the declaration of classes and its members related to RCSResponse.
std::string getDefaultInterface() const
Returns the default interface of the resource.
This class describes the request.
Definition: RCSRequest.h:47
This represents the attributes for a resource.
Definition: RCSResourceAttributes.h:195
std::function< RCSGetResponse(const RCSRequest &request, RCSResourceAttributes &attributes) > GetRequestHandler
Callback definition for a handler to be invoked when a get request is received.
Definition: RCSResourceObject.h:248
void unbindResource(const RCSResourceObject::Ptr &resource)
Unbind a resource from this resource.
Builder & addInterface(std::string interface)
Add an interface for the resource.
virtual void setGetRequestHandler(GetRequestHandler handler)
Sets the get request handler.
void setSetRequestHandlerPolicy(SetRequestHandlerPolicy policy)
Sets the policy for handling a set request.
NoLockException(std::string what)
Definition: RCSResourceObject.h:60
This is a builder to create resource with properties and attributes.
Definition: RCSResourceObject.h:158
AutoNotifyPolicy
Represents the policy of auto-notify function.
Definition: RCSResourceObject.h:125
Builder & setDefaultInterface(std::string interface)
Sets the default interface.
Builder & setObservable(bool observable)
Sets the observable property of the resource.
This class provides factory methods to create the response for a received set request.
Definition: RCSResponse.h:168
void setAutoNotifyPolicy(AutoNotifyPolicy policy)
Sets auto notify policy.
virtual void setSetRequestHandler(SetRequestHandler handler)
Sets the set request handler.
std::function< RCSSetResponse(const RCSRequest &request, RCSResourceAttributes &attributes) > SetRequestHandler
Callback definition for a handler to be invoked when a set request is received.
Definition: RCSResourceObject.h:284
std::vector< RCSResourceObject::Ptr > getBoundResources() const
Returns all bound resources to this resource.
std::vector< std::string > getTypes() const
Returns all types added for the resource.
RCSResourceAttributes::Value getAttributeValue(const std::string &key) const
Returns an attribute value corresponding to a key.
std::shared_ptr< const RCSResourceObject > ConstPtr
Definition: RCSResourceObject.h:147
virtual bool removeAttributeUpdatedListener(const std::string &key)
Removes a listener for a particular attribute updated.
void setAttribute(const std::string &key, const RCSResourceAttributes::Value &value)
Sets a particular attribute value.
Value & at(const std::string &key)
Accesses a value.
std::add_lvalue_reference< const T >::type get() const
Returns the underlying value as T.
Definition: RCSResourceAttributes.h:474
Builder & setDiscoverable(bool discoverable)
Sets whether the resource is discoverable.
The attributes of the request will be applied unconditionally even if there are new name or type conf...
RCSResourceObject(RCSResourceObject &&)=delete
AutoNotifyPolicy getAutoNotifyPolicy() const
Returns the current policy.
T getAttribute(const std::string &key) const
Returns the attribute value as T.
Definition: RCSResourceObject.h:351
bool containsAttribute(const std::string &key) const
Checks whether a particular attribute exists or not.
Builder & addType(std::string type)
Add a type for the resource.
RCSResourceObject & operator=(RCSResourceObject &&)=delete
This class is to send a delayed response for request handlers of the RCSResourceObject.
Definition: RCSSeparateResponse.h:39