//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-#ifndef __PRIMITIVEREQUEST__H
-#define __PRIMITIVEREQUEST__H
+#ifndef __PRIMITIVEREQUEST_H
+#define __PRIMITIVEREQUEST_H
#include <string>
-class PrimitiveRequest
+namespace OIC
{
-public:
- explicit PrimitiveRequest(const std::string& resourceUri) :
- m_resourceUri(resourceUri)
+ namespace Service
{
- }
- PrimitiveRequest& operator=(PrimitiveRequest&) = delete;
+ class PrimitiveRequest
+ {
+ public:
+ explicit PrimitiveRequest(const std::string& resourceUri) :
+ m_resourceUri(resourceUri)
+ {
+ }
- std::string getResourceUri() const
- {
- return m_resourceUri;
- }
+ PrimitiveRequest& operator=(PrimitiveRequest&) = delete;
-private:
- std::string m_resourceUri;
-};
+ std::string getResourceUri() const
+ {
+ return m_resourceUri;
+ }
+
+ private:
+ std::string m_resourceUri;
+ };
+
+ }
+}
-#endif
+#endif // __PRIMITIVEREQUEST_H
#include <octypes.h>
class ResourceAttributes;
-class RequestHandler;
-class PrimitiveGetResponse
+namespace OIC
{
-public:
- static PrimitiveGetResponse defaultAction();
+ namespace Service
+ {
+ class RequestHandler;
- static PrimitiveGetResponse create(const OCEntityHandlerResult&, int errorCode);
+ class PrimitiveGetResponse
+ {
+ public:
+ static PrimitiveGetResponse defaultAction();
- static PrimitiveGetResponse create(const ResourceAttributes&);
- static PrimitiveGetResponse create(const ResourceAttributes&,
- const OCEntityHandlerResult&, int errorCode);
+ static PrimitiveGetResponse create(const OCEntityHandlerResult&, int errorCode);
- static PrimitiveGetResponse create(ResourceAttributes&&);
- static PrimitiveGetResponse create(ResourceAttributes&&, const OCEntityHandlerResult&,
- int errorCode);
+ static PrimitiveGetResponse create(const ResourceAttributes&);
+ static PrimitiveGetResponse create(const ResourceAttributes&,
+ const OCEntityHandlerResult&, int errorCode);
- RequestHandler* getHandler() const;
+ static PrimitiveGetResponse create(ResourceAttributes&&);
+ static PrimitiveGetResponse create(ResourceAttributes&&, const OCEntityHandlerResult&,
+ int errorCode);
-private:
- PrimitiveGetResponse(RequestHandler*);
+ RequestHandler* getHandler() const;
-private:
- std::shared_ptr< RequestHandler > m_handler;
-};
+ private:
+ PrimitiveGetResponse(RequestHandler*);
-class PrimitiveSetResponse
-{
-public:
- static PrimitiveSetResponse defaultAction();
+ private:
+ std::shared_ptr< RequestHandler > m_handler;
+ };
+
+ class PrimitiveSetResponse
+ {
+ public:
+ static PrimitiveSetResponse defaultAction();
+
+ static PrimitiveSetResponse create(const OCEntityHandlerResult&, int errorCode);
- static PrimitiveSetResponse create(const OCEntityHandlerResult&, int errorCode);
+ static PrimitiveSetResponse create(const ResourceAttributes&);
+ static PrimitiveSetResponse create(const ResourceAttributes&,
+ const OCEntityHandlerResult&, int errorCode);
- static PrimitiveSetResponse create(const ResourceAttributes&);
- static PrimitiveSetResponse create(const ResourceAttributes&,
- const OCEntityHandlerResult&, int errorCode);
+ static PrimitiveSetResponse create(ResourceAttributes&&);
+ static PrimitiveSetResponse create(ResourceAttributes&&, const OCEntityHandlerResult&,
+ int errorCode);
- static PrimitiveSetResponse create(ResourceAttributes&&);
- static PrimitiveSetResponse create(ResourceAttributes&&, const OCEntityHandlerResult&,
- int errorCode);
+ RequestHandler* getHandler() const;
- RequestHandler* getHandler() const;
+ private:
+ PrimitiveSetResponse(RequestHandler*);
-private:
- PrimitiveSetResponse(RequestHandler*);
+ static PrimitiveSetResponse withProxy(RequestHandler*);
-private:
- std::shared_ptr< RequestHandler > m_handler;
-};
+ private:
+ std::shared_ptr< RequestHandler > m_handler;
+ };
+ }
+}
#endif
class OCResourceRequest;
}
-class NoLockException : public PrimitiveException
+namespace OIC
{
-};
+ namespace Service
+ {
-class DeadLockException : public PrimitiveException
-{
-};
+ class NoLockException : public PrimitiveException
+ {
+ };
-class PrimitiveServerResource
-{
-private:
- class WeakGuard;
+ class DeadLockException : public PrimitiveException
+ {
+ };
-public:
-// enum class AutoNotifyPolicy {
-// NEVER,
-// ALWAYS,
-// UPDATED
-// };
+ class PrimitiveServerResource
+ {
+ private:
+ class WeakGuard;
- using Ptr = std::shared_ptr<PrimitiveServerResource>;
- using ConstPtr = std::shared_ptr<const PrimitiveServerResource>;
+ public:
+ // enum class AutoNotifyPolicy {
+ // NEVER,
+ // ALWAYS,
+ // UPDATED
+ // };
- class Builder
- {
- public:
- Builder(const std::string& uri, const std::string& type, const std::string& interface);
+ using Ptr = std::shared_ptr<PrimitiveServerResource>;
+ using ConstPtr = std::shared_ptr<const PrimitiveServerResource>;
- Builder& setDiscoverable(bool discoverable);
- Builder& setObservable(bool observable);
+ class Builder
+ {
+ public:
+ Builder(const std::string& uri, const std::string& type,
+ const std::string& interface);
- Builder& setAttributes(const ResourceAttributes&);
- Builder& setAttributes(ResourceAttributes&&);
+ Builder& setDiscoverable(bool discoverable);
+ Builder& setObservable(bool observable);
- /**
- * @throw PlatformException
- */
- PrimitiveServerResource::Ptr create();
+ Builder& setAttributes(const ResourceAttributes&);
+ Builder& setAttributes(ResourceAttributes&&);
- private:
- std::string m_uri;
- std::string m_type;
- std::string m_interface;
+ /**
+ * @throw PlatformException
+ */
+ PrimitiveServerResource::Ptr create();
- uint8_t m_properties;
+ private:
+ std::string m_uri;
+ std::string m_type;
+ std::string m_interface;
- ResourceAttributes m_resourceAttributes;
- };
+ uint8_t m_properties;
- class LockGuard;
+ ResourceAttributes m_resourceAttributes;
+ };
- typedef std::function< PrimitiveGetResponse(const PrimitiveRequest&, const ResourceAttributes&) > GetRequestHandler;
- typedef std::function< PrimitiveSetResponse(const PrimitiveRequest&, const ResourceAttributes&) > SetRequestHandler;
+ class LockGuard;
-public:
- PrimitiveServerResource(PrimitiveServerResource&&) = delete;
- PrimitiveServerResource(const PrimitiveServerResource&) = delete;
+ typedef std::function< PrimitiveGetResponse(const PrimitiveRequest&,
+ ResourceAttributes&) > GetRequestHandler;
+ typedef std::function< PrimitiveSetResponse(const PrimitiveRequest&,
+ ResourceAttributes&) > SetRequestHandler;
- PrimitiveServerResource& operator=(PrimitiveServerResource&&) = delete;
- PrimitiveServerResource& operator=(const PrimitiveServerResource&) = delete;
+ public:
+ PrimitiveServerResource(PrimitiveServerResource&&) = delete;
+ PrimitiveServerResource(const PrimitiveServerResource&) = delete;
- template< typename T >
- void setAttribute(const std::string& key, const T &value)
- {
- WeakGuard lock(*this);
- m_resourceAttributes[key] = value;
- }
+ PrimitiveServerResource& operator=(PrimitiveServerResource&&) = delete;
+ PrimitiveServerResource& operator=(const PrimitiveServerResource&) = delete;
- template< typename T >
- T getAttribute(const std::string& key) const
- {
- WeakGuard lock(*this);
- return m_resourceAttributes.at(key).get< T >();
- }
+ template< typename T >
+ void setAttribute(const std::string& key, const T &value)
+ {
+ WeakGuard lock(*this);
+ m_resourceAttributes[key] = value;
+ }
- bool hasAttribute(const std::string& key) const;
+ template< typename T >
+ T getAttribute(const std::string& key) const
+ {
+ WeakGuard lock(*this);
+ return m_resourceAttributes.at(key).get< T >();
+ }
- ResourceAttributes& getAttributes();
- const ResourceAttributes& getAttributes() const;
+ bool hasAttribute(const std::string& key) const;
-// bool isObservable() const;
-// bool isDiscoverable() const;
+ ResourceAttributes& getAttributes();
+ const ResourceAttributes& getAttributes() const;
- void setGetRequestHandler(GetRequestHandler);
- void setSetRequestHandler(SetRequestHandler);
+ bool isObservable() const;
+ bool isDiscoverable() const;
- // void notify();
+ void setGetRequestHandler(GetRequestHandler);
+ void setSetRequestHandler(SetRequestHandler);
-// void setAutoNotifyPolicy(AutoNotifyPolicy);
+ void notify() const;
-private:
- PrimitiveServerResource(ResourceAttributes&&);
+ // void setAutoNotifyPolicy(AutoNotifyPolicy);
- OCEntityHandlerResult entityHandler(std::shared_ptr< OC::OCResourceRequest >);
+ private:
+ PrimitiveServerResource(uint8_t, ResourceAttributes&&);
- OCEntityHandlerResult handleRequest(std::shared_ptr< OC::OCResourceRequest >);
- OCEntityHandlerResult handleRequestGet(std::shared_ptr< OC::OCResourceRequest >);
- OCEntityHandlerResult handleRequestSet(std::shared_ptr< OC::OCResourceRequest >);
- OCEntityHandlerResult handleObserve(std::shared_ptr< OC::OCResourceRequest >);
+ OCEntityHandlerResult entityHandler(std::shared_ptr< OC::OCResourceRequest >);
- void assertOwnLock() const;
+ OCEntityHandlerResult handleRequest(std::shared_ptr< OC::OCResourceRequest >);
+ OCEntityHandlerResult handleRequestGet(std::shared_ptr< OC::OCResourceRequest >);
+ OCEntityHandlerResult handleRequestSet(std::shared_ptr< OC::OCResourceRequest >);
+ OCEntityHandlerResult handleObserve(std::shared_ptr< OC::OCResourceRequest >);
-private:
- OCResourceHandle m_resourceHandle;
- ResourceAttributes m_resourceAttributes;
+ void expectOwnLock() const;
- GetRequestHandler m_getRequestHandler;
- SetRequestHandler m_setRequestHandler;
+ private:
+ const uint8_t m_properties;
- mutable std::atomic<std::thread::id> m_lockOwner;
- mutable std::mutex m_mutex;
-};
+ OCResourceHandle m_resourceHandle;
+ ResourceAttributes m_resourceAttributes;
-class PrimitiveServerResource::LockGuard
-{
-public:
- LockGuard(const PrimitiveServerResource&);
- ~LockGuard();
+ GetRequestHandler m_getRequestHandler;
+ SetRequestHandler m_setRequestHandler;
- LockGuard(const LockGuard&) = delete;
- LockGuard(LockGuard&&) = delete;
+ mutable std::atomic< std::thread::id > m_lockOwner;
+ mutable std::mutex m_mutex;
+ };
- LockGuard& operator=(const LockGuard&) = delete;
- LockGuard& operator=(LockGuard&&) = delete;
+ class PrimitiveServerResource::LockGuard
+ {
+ public:
+ LockGuard(const PrimitiveServerResource&);
+ ~LockGuard();
-private:
- const PrimitiveServerResource& m_serverResource;
-};
+ LockGuard(const LockGuard&) = delete;
+ LockGuard(LockGuard&&) = delete;
-class PrimitiveServerResource::WeakGuard
-{
-public:
- WeakGuard(const PrimitiveServerResource&);
- ~WeakGuard();
+ LockGuard& operator=(const LockGuard&) = delete;
+ LockGuard& operator=(LockGuard&&) = delete;
+
+ private:
+ const PrimitiveServerResource& m_serverResource;
+ };
- WeakGuard(const WeakGuard&) = delete;
- WeakGuard(WeakGuard&&) = delete;
+ class PrimitiveServerResource::WeakGuard
+ {
+ public:
+ WeakGuard(const PrimitiveServerResource&);
+ ~WeakGuard();
- WeakGuard& operator=(const WeakGuard&) = delete;
- WeakGuard& operator=(WeakGuard&&) = delete;
+ WeakGuard(const WeakGuard&) = delete;
+ WeakGuard(WeakGuard&&) = delete;
-private:
- const PrimitiveServerResource& m_serverResource;
- bool m_hasLocked;
-};
+ WeakGuard& operator=(const WeakGuard&) = delete;
+ WeakGuard& operator=(WeakGuard&&) = delete;
+
+ private:
+ const PrimitiveServerResource& m_serverResource;
+ bool m_hasLocked;
+ };
+ }
+}
#endif
#define __REQUESTHANDLER_H
#include <PrimitiveServerResource.h>
-
#include <internal/ResourceAtrributesConverter.h>
#include <OCResourceResponse.h>
-class RequestHandler
-{
-public:
- virtual ~RequestHandler()
- {
- }
-
- virtual std::shared_ptr< OC::OCResourceResponse > buildResponse(PrimitiveServerResource&) = 0;
-};
-
-class SimpleRequestHandler: public RequestHandler
-{
-public:
- SimpleRequestHandler(const OCEntityHandlerResult& result = OC_EH_OK, int errorCode = 200) :
- m_result{ result }, m_errorCode{ errorCode }
- {
- }
-
- std::shared_ptr< OC::OCResourceResponse > buildResponse(PrimitiveServerResource& resource) override
- {
- auto response = std::make_shared< OC::OCResourceResponse >();
-
- response->setErrorCode(getErrorCode(resource));
- response->setResponseResult(getResponseResult(resource));
- response->setResourceRepresentation(getOCRepresentation(resource));
-
- return response;
- }
-
-protected:
- virtual int getErrorCode(PrimitiveServerResource&)
- {
- return m_errorCode;
- }
-
- virtual OCEntityHandlerResult getResponseResult(PrimitiveServerResource&)
- {
- return m_result;
- }
-
- virtual OC::OCRepresentation getOCRepresentation(PrimitiveServerResource& resource)
- {
- PrimitiveServerResource::LockGuard lock{ resource };
- return ResourceAttributesConverter::toOCRepresentation(resource.getAttributes());
- }
-
-private:
- OCEntityHandlerResult m_result;
- int m_errorCode;
-};
-
-
-class CustomAttrRequestHandler: public SimpleRequestHandler
+namespace OIC
{
-public:
- template <typename T>
- CustomAttrRequestHandler(T&& attrs,
- const OCEntityHandlerResult& result = OC_EH_OK, int errorCode = 200) :
- SimpleRequestHandler{ result, errorCode }, m_attrs{ std::forward<T>(attrs) }
+ namespace Service
{
+ class RequestHandler
+ {
+ public:
+ using Ptr = std::shared_ptr<RequestHandler>;
+
+ virtual ~RequestHandler()
+ {
+ }
+
+ virtual std::shared_ptr< OC::OCResourceResponse > buildResponse(
+ PrimitiveServerResource&, const ResourceAttributes& requestAttrs) = 0;
+ };
+
+ class SimpleRequestHandler: public RequestHandler
+ {
+ public:
+ SimpleRequestHandler(const OCEntityHandlerResult& result = OC_EH_OK,
+ int errorCode = 200);
+
+ std::shared_ptr< OC::OCResourceResponse > buildResponse(
+ PrimitiveServerResource&, const ResourceAttributes&) override;
+
+ protected:
+ virtual int getErrorCode(PrimitiveServerResource&);
+ virtual OCEntityHandlerResult getResponseResult(PrimitiveServerResource&);
+ virtual OC::OCRepresentation getOCRepresentation(PrimitiveServerResource& resource);
+
+ private:
+ OCEntityHandlerResult m_result;
+ int m_errorCode;
+ };
+
+
+ class CustomAttrRequestHandler: public SimpleRequestHandler
+ {
+ public:
+ template <typename T>
+ CustomAttrRequestHandler(T&& attrs,
+ const OCEntityHandlerResult& result = OC_EH_OK, int errorCode = 200) :
+ SimpleRequestHandler{ result, errorCode }, m_attrs{ std::forward<T>(attrs) }
+ {
+ }
+
+ protected:
+ OC::OCRepresentation getOCRepresentation(PrimitiveServerResource& resource) override;
+
+ private:
+ ResourceAttributes m_attrs;
+ };
+
+ class SetRequestProxyHandler: public RequestHandler
+ {
+ public:
+ SetRequestProxyHandler(RequestHandler::Ptr requestHandler);
+
+ std::shared_ptr< OC::OCResourceResponse > buildResponse(
+ PrimitiveServerResource& resource,
+ const ResourceAttributes& requestAttrs) override;
+
+ private:
+ RequestHandler::Ptr m_requestHandler;
+ };
}
-
-protected:
- virtual OC::OCRepresentation getOCRepresentation(PrimitiveServerResource& resource) override
- {
- return ResourceAttributesConverter::toOCRepresentation(m_attrs);
- }
-
-private:
- ResourceAttributes m_attrs;
-};
+}
#endif // __REQUESTHANDLER_H
#include <internal/RequestHandler.h>
-PrimitiveGetResponse PrimitiveGetResponse::defaultAction()
-{
- static PrimitiveGetResponse defaultRes { new SimpleRequestHandler };
-
- return defaultRes;
-}
-
-PrimitiveGetResponse PrimitiveGetResponse::create(const OCEntityHandlerResult& result,
- int errorCode)
-{
- return PrimitiveGetResponse { new SimpleRequestHandler { result, errorCode } };
-}
-
-PrimitiveGetResponse PrimitiveGetResponse::create(const ResourceAttributes& attrs)
-{
- return PrimitiveGetResponse { new CustomAttrRequestHandler { attrs } };
-}
-
-PrimitiveGetResponse PrimitiveGetResponse::create(const ResourceAttributes& attrs,
- const OCEntityHandlerResult& result, int errorCode)
-{
- return PrimitiveGetResponse { new CustomAttrRequestHandler { attrs, result, errorCode } };
-}
-
-PrimitiveGetResponse PrimitiveGetResponse::create(ResourceAttributes&& result)
-{
- return PrimitiveGetResponse { new CustomAttrRequestHandler { std::move(result) } };
-}
-
-PrimitiveGetResponse PrimitiveGetResponse::create(ResourceAttributes&& attrs,
- const OCEntityHandlerResult& result, int errorCode)
-{
- return PrimitiveGetResponse { new CustomAttrRequestHandler { std::move(attrs), result, errorCode } };
-}
-
-PrimitiveGetResponse::PrimitiveGetResponse(RequestHandler* handler) :
- m_handler{ handler }
-{
-}
-
-RequestHandler* PrimitiveGetResponse::getHandler() const
-{
- return m_handler.get();
-}
-
-
-PrimitiveSetResponse PrimitiveSetResponse::defaultAction()
-{
- static PrimitiveSetResponse defaultRes { new SimpleRequestHandler };
-
- return defaultRes;
-}
-
-PrimitiveSetResponse PrimitiveSetResponse::create(const OCEntityHandlerResult& result,
- int errorCode)
-{
- return PrimitiveSetResponse { new SimpleRequestHandler { result, errorCode } };
-}
-
-PrimitiveSetResponse PrimitiveSetResponse::create(const ResourceAttributes& attrs)
-{
- return PrimitiveSetResponse { new CustomAttrRequestHandler { attrs } };
-}
-
-PrimitiveSetResponse PrimitiveSetResponse::create(const ResourceAttributes& attrs,
- const OCEntityHandlerResult& result, int errorCode)
-{
- return PrimitiveSetResponse { new CustomAttrRequestHandler { attrs, result, errorCode } };
-}
-
-PrimitiveSetResponse PrimitiveSetResponse::create(ResourceAttributes&& result)
-{
- return PrimitiveSetResponse { new CustomAttrRequestHandler { std::move(result) } };
-}
-
-PrimitiveSetResponse PrimitiveSetResponse::create(ResourceAttributes&& attrs,
- const OCEntityHandlerResult& result, int errorCode)
-{
- return PrimitiveSetResponse { new CustomAttrRequestHandler { std::move(attrs), result, errorCode } };
-}
-
-PrimitiveSetResponse::PrimitiveSetResponse(RequestHandler* handler) :
- m_handler{ handler }
-{
-}
-
-RequestHandler* PrimitiveSetResponse::getHandler() const
-{
- return m_handler.get();
+namespace OIC
+{
+ namespace Service
+ {
+ PrimitiveGetResponse PrimitiveGetResponse::defaultAction()
+ {
+ static PrimitiveGetResponse defaultRes { new SimpleRequestHandler };
+
+ return defaultRes;
+ }
+
+ PrimitiveGetResponse PrimitiveGetResponse::create(const OCEntityHandlerResult& result,
+ int errorCode)
+ {
+ return PrimitiveGetResponse { new SimpleRequestHandler { result, errorCode } };
+ }
+
+ PrimitiveGetResponse PrimitiveGetResponse::create(const ResourceAttributes& attrs)
+ {
+ return PrimitiveGetResponse { new CustomAttrRequestHandler { attrs } };
+ }
+
+ PrimitiveGetResponse PrimitiveGetResponse::create(const ResourceAttributes& attrs,
+ const OCEntityHandlerResult& result, int errorCode)
+ {
+ return PrimitiveGetResponse {
+ new CustomAttrRequestHandler { attrs, result, errorCode } };
+ }
+
+ PrimitiveGetResponse PrimitiveGetResponse::create(ResourceAttributes&& result)
+ {
+ return PrimitiveGetResponse { new CustomAttrRequestHandler { std::move(result) } };
+ }
+
+ PrimitiveGetResponse PrimitiveGetResponse::create(ResourceAttributes&& attrs,
+ const OCEntityHandlerResult& result, int errorCode)
+ {
+ return PrimitiveGetResponse { new CustomAttrRequestHandler {
+ std::move(attrs), result, errorCode } };
+ }
+
+ PrimitiveGetResponse::PrimitiveGetResponse(RequestHandler* handler) :
+ m_handler{ handler }
+ {
+ assert(m_handler);
+ }
+
+ RequestHandler* PrimitiveGetResponse::getHandler() const
+ {
+ return m_handler.get();
+ }
+
+
+ PrimitiveSetResponse PrimitiveSetResponse::defaultAction()
+ {
+ static PrimitiveSetResponse defaultRes { withProxy(new SimpleRequestHandler) };
+
+ return defaultRes;
+ }
+
+ PrimitiveSetResponse PrimitiveSetResponse::create(const OCEntityHandlerResult& result,
+ int errorCode)
+ {
+ return withProxy(new SimpleRequestHandler { result, errorCode });
+ }
+
+ PrimitiveSetResponse PrimitiveSetResponse::create(const ResourceAttributes& attrs)
+ {
+ return withProxy(new CustomAttrRequestHandler { attrs });
+ }
+
+ PrimitiveSetResponse PrimitiveSetResponse::create(const ResourceAttributes& attrs,
+ const OCEntityHandlerResult& result, int errorCode)
+ {
+ return withProxy(new CustomAttrRequestHandler { attrs, result, errorCode });
+ }
+
+ PrimitiveSetResponse PrimitiveSetResponse::create(ResourceAttributes&& result)
+ {
+ return withProxy(new CustomAttrRequestHandler { std::move(result) });
+ }
+
+ PrimitiveSetResponse PrimitiveSetResponse::create(ResourceAttributes&& attrs,
+ const OCEntityHandlerResult& result, int errorCode)
+ {
+ return withProxy(
+ new CustomAttrRequestHandler { std::move(attrs), result, errorCode });
+ }
+
+ PrimitiveSetResponse::PrimitiveSetResponse(RequestHandler* handler) :
+ m_handler{ handler }
+ {
+ assert(m_handler);
+ }
+
+ PrimitiveSetResponse PrimitiveSetResponse::withProxy(RequestHandler* handler)
+ {
+ assert(handler);
+ return PrimitiveSetResponse{ new SetRequestProxyHandler {
+ RequestHandler::Ptr{ handler } } };
+ }
+
+ RequestHandler* PrimitiveSetResponse::getHandler() const
+ {
+ return m_handler.get();
+ }
+ }
}
#include <vector>
#include <internal/RequestHandler.h>
+#include <internal/AssertUtils.h>
#include <OCPlatform.h>
namespace
{
- uint8_t makePropertyFlags(uint8_t base, uint8_t target, bool add)
+ namespace Detail
{
- if (add)
+ template <typename RESPONSE>
+ OCEntityHandlerResult sendResponse(OIC::Service::PrimitiveServerResource& resource,
+ std::shared_ptr< OC::OCResourceRequest > ocRequest,
+ const ResourceAttributes& requestAttrs, RESPONSE&& response)
{
- return base | target;
- }
+ auto ocResponse = response.getHandler()->buildResponse(resource, requestAttrs);
- return base & ~target;
+ ocResponse->setRequestHandle(ocRequest->getRequestHandle());
+ ocResponse->setResourceHandle(ocRequest->getResourceHandle());
+
+ if (OC::OCPlatform::sendResponse(ocResponse) == OC_STACK_OK)
+ {
+ return OC_EH_OK;
+ }
+ return OC_EH_ERROR;
+ }
}
- template <typename RESPONSE>
- OCEntityHandlerResult sendResponse(PrimitiveServerResource& resource,
- std::shared_ptr< OC::OCResourceRequest > ocRequest, RESPONSE&& response)
+ inline bool hasProperty(uint8_t base, uint8_t target)
{
- auto ocResponse = response.getHandler()->buildResponse(resource);
-
- ocResponse->setRequestHandle(ocRequest->getRequestHandle());
- ocResponse->setResourceHandle(ocRequest->getResourceHandle());
+ return (base & target) == target;
+ }
- if (OC::OCPlatform::sendResponse(ocResponse) == OC_STACK_OK)
+ inline uint8_t makePropertyFlags(uint8_t base, uint8_t target, bool add)
+ {
+ if (add)
{
- return OC_EH_OK;
+ return base | target;
}
- return OC_EH_ERROR;
+
+ return base & ~target;
}
template< typename HANDLER, typename RESPONSE = typename std::decay<HANDLER>::type::result_type>
- OCEntityHandlerResult handleRequest(PrimitiveServerResource& resource,
+ OCEntityHandlerResult handleRequest(OIC::Service::PrimitiveServerResource& resource,
std::shared_ptr< OC::OCResourceRequest > ocRequest, HANDLER&& handler)
{
+ ResourceAttributes attrs{ ResourceAttributesConverter::fromOCRepresentation(
+ ocRequest->getResourceRepresentation()) };
+
if (handler)
{
- ResourceAttributes attrs{ ResourceAttributesConverter::fromOCRepresentation(
- ocRequest->getResourceRepresentation()) };
-
- return sendResponse(resource, ocRequest, handler(
- PrimitiveRequest{ ocRequest->getResourceUri() }, attrs));
+ return Detail::sendResponse(resource, ocRequest, attrs, handler(
+ OIC::Service::PrimitiveRequest{ ocRequest->getResourceUri() }, attrs));
}
- return sendResponse(resource, ocRequest, RESPONSE::defaultAction());
+ return Detail::sendResponse(resource, ocRequest, attrs, RESPONSE::defaultAction());
}
} // unnamed namespace
-PrimitiveServerResource::PrimitiveServerResource(ResourceAttributes&& attrs) :
- m_resourceHandle{}, m_resourceAttributes{ std::move(attrs) },
- m_getRequestHandler{}, m_setRequestHandler{},
- m_mutex { }
+namespace OIC
{
-}
-
-PrimitiveServerResource::Builder::Builder(const std::string &uri, const std::string &type,
- const std::string &interface) :
- m_uri{ uri }, m_type{ type }, m_interface{ interface }, m_properties{ 0 }
-{
-}
+ namespace Service
+ {
+ PrimitiveServerResource::PrimitiveServerResource(uint8_t properties,
+ ResourceAttributes&& attrs) :
+ m_properties { properties }, m_resourceHandle{},
+ m_resourceAttributes{ std::move(attrs) }, m_getRequestHandler{ },
+ m_setRequestHandler{ }, m_mutex { }
+ {
+ }
-PrimitiveServerResource::Builder& PrimitiveServerResource::Builder::setDiscoverable(
- bool discoverable)
-{
- m_properties = ::makePropertyFlags(m_properties, OC_DISCOVERABLE, discoverable);
- return *this;
-}
+ PrimitiveServerResource::Builder::Builder(const std::string& uri, const std::string& type,
+ const std::string& interface) :
+ m_uri{ uri }, m_type{ type }, m_interface{ interface }, m_properties{ 0 }
+ {
+ }
-PrimitiveServerResource::Builder& PrimitiveServerResource::Builder::setObservable(bool observable)
-{
- m_properties = ::makePropertyFlags(m_properties, OC_OBSERVABLE, observable);
- return *this;
-}
+ PrimitiveServerResource::Builder& PrimitiveServerResource::Builder::setDiscoverable(
+ bool discoverable)
+ {
+ m_properties = ::makePropertyFlags(m_properties, OC_DISCOVERABLE, discoverable);
+ return *this;
+ }
-PrimitiveServerResource::Builder& PrimitiveServerResource::Builder::setAttributes(
- const ResourceAttributes &attrs)
-{
- m_resourceAttributes = attrs;
- return *this;
-}
+ PrimitiveServerResource::Builder& PrimitiveServerResource::Builder::setObservable(
+ bool observable)
+ {
+ m_properties = ::makePropertyFlags(m_properties, OC_OBSERVABLE, observable);
+ return *this;
+ }
-PrimitiveServerResource::Builder& PrimitiveServerResource::Builder::setAttributes(
- ResourceAttributes &&attrs)
-{
- m_resourceAttributes = std::move(attrs);
- return *this;
-}
+ PrimitiveServerResource::Builder& PrimitiveServerResource::Builder::setAttributes(
+ const ResourceAttributes& attrs)
+ {
+ m_resourceAttributes = attrs;
+ return *this;
+ }
-PrimitiveServerResource::Ptr PrimitiveServerResource::Builder::create()
-{
- OCResourceHandle handle{ nullptr };
- PrimitiveServerResource::Ptr server { new PrimitiveServerResource{ std::move(m_resourceAttributes) } };
- OC::EntityHandler entityHandler{ std::bind(&PrimitiveServerResource::entityHandler, server.get(),
- std::placeholders::_1) };
+ PrimitiveServerResource::Builder& PrimitiveServerResource::Builder::setAttributes(
+ ResourceAttributes&& attrs)
+ {
+ m_resourceAttributes = std::move(attrs);
+ return *this;
+ }
- OCStackResult result = OC::OCPlatform::registerResource(handle, m_uri, m_type, m_interface, entityHandler,
- m_properties);
+ PrimitiveServerResource::Ptr PrimitiveServerResource::Builder::create()
+ {
+ OCResourceHandle handle{ nullptr };
- if (OC_STACK_OK != result)
- {
- throw PlatformException(result);
- }
+ PrimitiveServerResource::Ptr server {
+ new PrimitiveServerResource{ m_properties, std::move(m_resourceAttributes) } };
- server->m_resourceHandle = handle;
+ OC::EntityHandler entityHandler{ std::bind(&PrimitiveServerResource::entityHandler,
+ server.get(), std::placeholders::_1) };
- return server;
-}
+ expectOCStackResultOK(
+ OC::OCPlatform::registerResource(handle, m_uri, m_type, m_interface,
+ entityHandler, m_properties));
-bool PrimitiveServerResource::hasAttribute(const std::string& key) const
-{
- WeakGuard lock(*this);
- return m_resourceAttributes.contains(key);
-}
+ server->m_resourceHandle = handle;
-ResourceAttributes& PrimitiveServerResource::getAttributes()
-{
- assertOwnLock();
- return m_resourceAttributes;
-}
+ return server;
+ }
-const ResourceAttributes& PrimitiveServerResource::getAttributes() const
-{
- assertOwnLock();
- return m_resourceAttributes;
-}
+ bool PrimitiveServerResource::hasAttribute(const std::string& key) const
+ {
+ WeakGuard lock(*this);
+ return m_resourceAttributes.contains(key);
+ }
-void PrimitiveServerResource::assertOwnLock() const
-{
- if (m_lockOwner != std::this_thread::get_id())
- {
- throw NoLockException{ };
- }
-}
+ ResourceAttributes& PrimitiveServerResource::getAttributes()
+ {
+ expectOwnLock();
+ return m_resourceAttributes;
+ }
-//bool PrimitiveServerResource::isObservable() const
-//{
-// // TODO : fill
-//}
-//
-//bool PrimitiveServerResource::isDiscoverable() const
-//{
-// // TODO : fill
-//}
+ const ResourceAttributes& PrimitiveServerResource::getAttributes() const
+ {
+ expectOwnLock();
+ return m_resourceAttributes;
+ }
-void PrimitiveServerResource::setGetRequestHandler(GetRequestHandler h)
-{
- m_getRequestHandler = h;
-}
+ void PrimitiveServerResource::expectOwnLock() const
+ {
+ if (m_lockOwner != std::this_thread::get_id())
+ {
+ throw NoLockException{ };
+ }
+ }
-void PrimitiveServerResource::setSetRequestHandler(SetRequestHandler h)
-{
- m_setRequestHandler = h;
-}
+ bool PrimitiveServerResource::isObservable() const
+ {
+ return ::hasProperty(m_properties, OC_OBSERVABLE);
+ }
-//void PrimitiveServerResource::notify()
-//{
-// // TODO : fill
-//}
+ bool PrimitiveServerResource::isDiscoverable() const
+ {
+ return ::hasProperty(m_properties, OC_DISCOVERABLE);
+ }
-OCEntityHandlerResult PrimitiveServerResource::entityHandler(
- std::shared_ptr< OC::OCResourceRequest > request)
-{
- if (!request)
- {
- return OC_EH_ERROR;
- }
+ void PrimitiveServerResource::setGetRequestHandler(GetRequestHandler h)
+ {
+ m_getRequestHandler = h;
+ }
- try
- {
- if (request->getRequestHandlerFlag() & OC::RequestHandlerFlag::RequestFlag)
+ void PrimitiveServerResource::setSetRequestHandler(SetRequestHandler h)
{
- return handleRequest(request);
+ m_setRequestHandler = h;
}
- if (request->getRequestHandlerFlag() & OC::RequestHandlerFlag::ObserverFlag)
+ void PrimitiveServerResource::notify() const
{
- return handleObserve(request);
+ expectOCStackResultOK(OC::OCPlatform::notifyAllObservers(m_resourceHandle));
}
- }
- catch (...)
- {
- // TODO : how to notify the error?
- }
- return OC_EH_ERROR;
-}
+ OCEntityHandlerResult PrimitiveServerResource::entityHandler(
+ std::shared_ptr< OC::OCResourceRequest > request)
+ {
+ if (!request)
+ {
+ return OC_EH_ERROR;
+ }
+
+ try
+ {
+ if (request->getRequestHandlerFlag() & OC::RequestHandlerFlag::RequestFlag)
+ {
+ return handleRequest(request);
+ }
+
+ if (request->getRequestHandlerFlag() & OC::RequestHandlerFlag::ObserverFlag)
+ {
+ return handleObserve(request);
+ }
+ }
+ catch (...)
+ {
+ // TODO : how to notify the error?
+ }
+
+ return OC_EH_ERROR;
+ }
-OCEntityHandlerResult PrimitiveServerResource::handleRequest(
- std::shared_ptr< OC::OCResourceRequest > request)
-{
- if (request->getRequestType() == "GET")
- {
- return handleRequestGet(request);
- }
+ OCEntityHandlerResult PrimitiveServerResource::handleRequest(
+ std::shared_ptr< OC::OCResourceRequest > request)
+ {
+ if (request->getRequestType() == "GET")
+ {
+ return handleRequestGet(request);
+ }
- if (request->getRequestType() == "PUT" || request->getRequestType() == "POST")
- {
- return handleRequestSet(request);
- }
+ if (request->getRequestType() == "PUT" || request->getRequestType() == "POST")
+ {
+ return handleRequestSet(request);
+ }
- return OC_EH_ERROR;
-}
+ return OC_EH_ERROR;
+ }
-OCEntityHandlerResult PrimitiveServerResource::handleRequestGet(
- std::shared_ptr< OC::OCResourceRequest > request)
-{
- return ::handleRequest(*this, request, m_getRequestHandler);
-}
+ OCEntityHandlerResult PrimitiveServerResource::handleRequestGet(
+ std::shared_ptr< OC::OCResourceRequest > request)
+ {
+ return ::handleRequest(*this, request, m_getRequestHandler);
+ }
-OCEntityHandlerResult PrimitiveServerResource::handleRequestSet(
- std::shared_ptr< OC::OCResourceRequest > request)
-{
- return ::handleRequest(*this, request, m_setRequestHandler);
-}
+ OCEntityHandlerResult PrimitiveServerResource::handleRequestSet(
+ std::shared_ptr< OC::OCResourceRequest > request)
+ {
+ return ::handleRequest(*this, request, m_setRequestHandler);
+ }
-OCEntityHandlerResult PrimitiveServerResource::handleObserve(
- std::shared_ptr< OC::OCResourceRequest > request)
-{
-// if (!isObservable())
-// {
-// return OC_EH_ERROR;
-// }
+ OCEntityHandlerResult PrimitiveServerResource::handleObserve(
+ std::shared_ptr< OC::OCResourceRequest > request)
+ {
+ if (!isObservable())
+ {
+ return OC_EH_ERROR;
+ }
- return OC_EH_OK;
-}
+ return OC_EH_OK;
+ }
-PrimitiveServerResource::LockGuard::LockGuard(const PrimitiveServerResource& serverResource) :
- m_serverResource(serverResource)
-{
- if (m_serverResource.m_lockOwner == std::this_thread::get_id())
- {
- throw DeadLockException{ };
- }
+ PrimitiveServerResource::LockGuard::LockGuard(
+ const PrimitiveServerResource& serverResource) :
+ m_serverResource(serverResource)
+ {
+ if (m_serverResource.m_lockOwner == std::this_thread::get_id())
+ {
+ throw DeadLockException{ };
+ }
- m_serverResource.m_mutex.lock();
- m_serverResource.m_lockOwner = std::this_thread::get_id();
-}
+ m_serverResource.m_mutex.lock();
+ m_serverResource.m_lockOwner = std::this_thread::get_id();
+ }
-PrimitiveServerResource::LockGuard::~LockGuard()
-{
- m_serverResource.m_lockOwner = std::thread::id();
- m_serverResource.m_mutex.unlock();
-}
+ PrimitiveServerResource::LockGuard::~LockGuard()
+ {
+ m_serverResource.m_lockOwner = std::thread::id();
+ m_serverResource.m_mutex.unlock();
+ }
-PrimitiveServerResource::WeakGuard::WeakGuard(const PrimitiveServerResource& serverResource) :
- m_serverResource(serverResource), m_hasLocked{ false }
-{
- if (m_serverResource.m_lockOwner != std::this_thread::get_id())
- {
- m_serverResource.m_mutex.lock();
- m_hasLocked = true;
- }
-}
+ PrimitiveServerResource::WeakGuard::WeakGuard(
+ const PrimitiveServerResource& serverResource) :
+ m_serverResource(serverResource), m_hasLocked{ false }
+ {
+ if (m_serverResource.m_lockOwner != std::this_thread::get_id())
+ {
+ m_serverResource.m_mutex.lock();
+ m_hasLocked = true;
+ }
+ }
-PrimitiveServerResource::WeakGuard::~WeakGuard()
-{
- if (m_hasLocked)
- {
- m_serverResource.m_mutex.unlock();
+ PrimitiveServerResource::WeakGuard::~WeakGuard()
+ {
+ if (m_hasLocked)
+ {
+ m_serverResource.m_mutex.unlock();
+ }
+ }
}
}
-
--- /dev/null
+//******************************************************************
+//
+// Copyright 2015 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#include <internal/RequestHandler.h>
+
+#include <internal/ResourceAttributesUtils.h>
+
+namespace OIC
+{
+ namespace Service
+ {
+
+ SimpleRequestHandler::SimpleRequestHandler(const OCEntityHandlerResult& result,
+ int errorCode) :
+ m_result{ result }, m_errorCode{ errorCode }
+ {
+ }
+
+ std::shared_ptr< OC::OCResourceResponse > SimpleRequestHandler::buildResponse(
+ PrimitiveServerResource& resource, const ResourceAttributes&)
+ {
+ auto response = std::make_shared< OC::OCResourceResponse >();
+
+ response->setErrorCode(getErrorCode(resource));
+ response->setResponseResult(getResponseResult(resource));
+ response->setResourceRepresentation(getOCRepresentation(resource));
+
+ return response;
+ }
+
+ int SimpleRequestHandler::getErrorCode(PrimitiveServerResource&)
+ {
+ return m_errorCode;
+ }
+
+ OCEntityHandlerResult SimpleRequestHandler::getResponseResult(PrimitiveServerResource&)
+ {
+ return m_result;
+ }
+
+ OC::OCRepresentation SimpleRequestHandler::getOCRepresentation(
+ PrimitiveServerResource& resource)
+ {
+ PrimitiveServerResource::LockGuard lock{ resource };
+ return ResourceAttributesConverter::toOCRepresentation(resource.getAttributes());
+ }
+
+
+ OC::OCRepresentation CustomAttrRequestHandler::getOCRepresentation(
+ PrimitiveServerResource& resource)
+
+ {
+ return ResourceAttributesConverter::toOCRepresentation(m_attrs);
+ }
+
+
+ SetRequestProxyHandler::SetRequestProxyHandler(RequestHandler::Ptr requestHandler) :
+ m_requestHandler{ requestHandler }
+ {
+ assert(m_requestHandler);
+ }
+
+ std::shared_ptr< OC::OCResourceResponse > SetRequestProxyHandler::buildResponse(
+ PrimitiveServerResource& resource, const ResourceAttributes& requestAttrs)
+ {
+ {
+ PrimitiveServerResource::LockGuard lock(resource);
+
+ if (!acceptableAttributes(resource.getAttributes(), requestAttrs))
+ {
+ throw PrimitiveException("Resource can't accept request Attributes!");
+ }
+
+ replaceAttributesRecursively(resource.getAttributes(), requestAttrs);
+ }
+ return m_requestHandler->buildResponse(resource, requestAttrs);
+ }
+ }
+}