From 92d1f40911116f06e1412bf596d0e4aa2066ba7c Mon Sep 17 00:00:00 2001 From: Heewon Park Date: Thu, 4 Jun 2015 15:01:28 +0900 Subject: [PATCH] Update server builder Initial Version This will update the intial files for resource-manipulation branch. As a sample, the skeletons of server builder moulde are included. Change-Id: I49a76b19e3683f735b7f2974611045c46e94bba2 Signed-off-by: Heewon Park Reviewed-on: https://gerrit.iotivity.org/gerrit/1165 Tested-by: jenkins-iotivity Reviewed-by: Uze Choi --- .../basis/serverBuilder/include/PrimitiveRequest.h | 33 +++ .../serverBuilder/include/PrimitiveResponse.h | 60 +++++ .../include/PrimitiveServerResource.h | 85 +++++++ .../basis/serverBuilder/include/ServerBuilder.h | 102 +++++++++ .../basis/serverBuilder/src/PrimitiveRequest.cpp | 21 ++ .../basis/serverBuilder/src/PrimitiveResponse.cpp | 21 ++ .../serverBuilder/src/PrimitiveServerResource.cpp | 246 +++++++++++++++++++++ service/basis/serverBuilder/src/ServerBuilder.cpp | 189 ++++++++++++++++ 8 files changed, 757 insertions(+) create mode 100644 service/basis/serverBuilder/include/PrimitiveRequest.h create mode 100644 service/basis/serverBuilder/include/PrimitiveResponse.h create mode 100644 service/basis/serverBuilder/include/PrimitiveServerResource.h create mode 100644 service/basis/serverBuilder/include/ServerBuilder.h create mode 100644 service/basis/serverBuilder/src/PrimitiveRequest.cpp create mode 100644 service/basis/serverBuilder/src/PrimitiveResponse.cpp create mode 100644 service/basis/serverBuilder/src/PrimitiveServerResource.cpp create mode 100644 service/basis/serverBuilder/src/ServerBuilder.cpp diff --git a/service/basis/serverBuilder/include/PrimitiveRequest.h b/service/basis/serverBuilder/include/PrimitiveRequest.h new file mode 100644 index 0000000..025c7fc --- /dev/null +++ b/service/basis/serverBuilder/include/PrimitiveRequest.h @@ -0,0 +1,33 @@ +//****************************************************************** +// +// 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef __PRIMITIVEREQUEST__H +#define __PRIMITIVEREQUEST__H + + +#include + +class PrimitiveRequest +{ + std::string getResourceUri() const; +}; + + +#endif diff --git a/service/basis/serverBuilder/include/PrimitiveResponse.h b/service/basis/serverBuilder/include/PrimitiveResponse.h new file mode 100644 index 0000000..7fb9733 --- /dev/null +++ b/service/basis/serverBuilder/include/PrimitiveResponse.h @@ -0,0 +1,60 @@ +//****************************************************************** +// +// 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef __PRIMITIVERESPONSE_H +#define __PRIMITIVERESPONSE_H + +class Handler; + +class PrimitiveResponse +{ + public: + static PrimitiveResponse DEFAULT; + + static PrimitiveResponse createResponse(const ResourceAttributes &attrs); + + shared_ptr m_handler; +}; + +class GetDefaultHandler +{ + public: + OCResponse handle(ServerResource &resource) + { + // build response + } +}; + +GetRequestHandler m_getRequestHandler; + +void onGet() +{ + Request request; + Attributes attributes; + + if (m_getRequestHandler) + { + GetPrimitiveResponse response = m_getRequestHandler(request, attributes); + + return response->getHandler()->handle(*this); + } +} + +#endif diff --git a/service/basis/serverBuilder/include/PrimitiveServerResource.h b/service/basis/serverBuilder/include/PrimitiveServerResource.h new file mode 100644 index 0000000..fa9d85d --- /dev/null +++ b/service/basis/serverBuilder/include/PrimitiveServerResource.h @@ -0,0 +1,85 @@ +//****************************************************************** +// +// 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef __PRIMITIVESERVERRESOURCE_H +#define __PRIMITIVESERVERRESOURCE_H + +class PrimitiveServerResource +{ + private: + PrimitiveServerResource(); + PrimitiveServerResource(OCResourceHandle baseResourceHandle); + OCEntityHandlerResult entityHandler(std::shared_ptr request); + + public: + class Builder + { + public: + Builder(const std::string &uri, const std::string &type, + const std::string &interface); + + Builder &setDiscoverable(bool discoverable); + Builder &setObservable(bool observable); + + Builder &setAttributes(const ResourceAttributes &attrs); + + PrimitiveServerResource create() const; + + private: + std::string m_uri; + std::string m_type; + std::string m_interface; + uint8_t m_property; + }; + + typedef std::function < + PrimitiveResponse(const PrimitiveRequest &, const ResourceAttributes &) > GetRequestHandler; + typedef std::function < + PrimitiveResponse(const PrimitiveRequest &, const ResourceAttributes &) > SetRequestHandler; + + template + void setAttribute(const std::string &key, const T &value); + + template + T getAttribute(T &key) const; + + bool hasAttribute(const std::string &key) const; + + const ResourceAttributes &getAttributes() const; + ResourceAttributes &getAttributes(); + + bool isObservable() const; + bool isDiscoverable() const; + + void setGetRequestHandler(GetRequestHandler); + void setSetRequestHandler(SetRequestHandler); + + void notify(); + + std::string getUri() const; + std::vector getTypes() const; + std::vector getInterfaces() const; + + private: + OCResourceHandle m_resourceHandle; +}; + + +#endif diff --git a/service/basis/serverBuilder/include/ServerBuilder.h b/service/basis/serverBuilder/include/ServerBuilder.h new file mode 100644 index 0000000..0b42daf --- /dev/null +++ b/service/basis/serverBuilder/include/ServerBuilder.h @@ -0,0 +1,102 @@ +//****************************************************************** +// +// 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 +#include +#include +#include +#include + +class ResourceAttributes +{ + public: + /* + * @brief Value manipulation helper class. + * You may need to wrap OCRepresent::AttributeItem class. + * Do not expose the class as the part of API. + */ + class Item + { + public: + /* + * @brief Get the value as the type of T + * + * @throw BadCastException if the type is not correct. + */ + template + T get() const; + + /** + * @brief Get the value as the type of T + * + * @return True if the type is correct. + */ + template + bool get(T &var) const; + + // ... + // we need other helper methods for this class. + // please refer OCRepresent::AttributeItem class. + }; + + /** + * @brief There will be container in this class, and we need iterator for that. + * Implement one or just define alias. + */ + typedef std::unordered_map::const_iterator const_iterator; + + /** + * @brief std::map style get method. + */ + Item &operator[](const std::string &key); + + /** + * @brief std::map style get method. + * + * @throw InvalidKeyException If no value for the key. + */ + Item &at(const std::string &key); + + /** + * @brief std::map style get method. + * + * @throw InvalidKeyException If no value for the key. + */ + const Item &at(const std::string &key) const; + + /** + * @return True if there is a value for the key. + */ + bool contains(const std::string &key) const; + + const_iterator cbegin() const; + const_iterator cend() const; + + size_t getSize() const; + bool isEmpty() const; + + template + void insert(const std::string &key, const T &value); + + void remove(const std::string &key); + + void clear(); +}; + diff --git a/service/basis/serverBuilder/src/PrimitiveRequest.cpp b/service/basis/serverBuilder/src/PrimitiveRequest.cpp new file mode 100644 index 0000000..ade31eb --- /dev/null +++ b/service/basis/serverBuilder/src/PrimitiveRequest.cpp @@ -0,0 +1,21 @@ +//****************************************************************** +// +// 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 "PrimitiveRequest.h" diff --git a/service/basis/serverBuilder/src/PrimitiveResponse.cpp b/service/basis/serverBuilder/src/PrimitiveResponse.cpp new file mode 100644 index 0000000..bb2e273 --- /dev/null +++ b/service/basis/serverBuilder/src/PrimitiveResponse.cpp @@ -0,0 +1,21 @@ +//****************************************************************** +// +// 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 "PrimitiveResponse.h" diff --git a/service/basis/serverBuilder/src/PrimitiveServerResource.cpp b/service/basis/serverBuilder/src/PrimitiveServerResource.cpp new file mode 100644 index 0000000..8235b36 --- /dev/null +++ b/service/basis/serverBuilder/src/PrimitiveServerResource.cpp @@ -0,0 +1,246 @@ +//****************************************************************** +// +// 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 + +#include +#include +#include + +#include "OCPlatform.h" + +using namespace OC; + +PrimitiveServerResource::PrimitiveServerResource(OCResourceHandle &baseResourceHandle) +{ + m_resourceHandle = baseResourceHandle; +} + +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_property = 0; +} + +PrimitiveServerResource::Builder &PrimitiveServerResource::Builder::setDiscoverable( + bool discoverable) +{ + //set flag + if (discoverable) + m_property |= OC_DISCOVERABLE; + else + m_property ^ = OC_DISCOVERABLE; + return *this; +} + +PrimitiveServerResource::Builder &PrimitiveServerResource::Builder::setObservable(bool observable) +{ + //set flag + if (observable) + m_property |= OC_OBSERVABLE; + else + m_property ^ = OC_OBSERVABLE; + return *this; +} + +PrimitiveServerResource::Builder &PrimitiveServerResource::Builder::setAttributes( + const ResourceAttributes &attrs) +{ + //set Attributemap + return *this; +} + +PrimitiveServerResource PrimitiveServerResource::Builder::create() const +{ + //TODO: EntityHandler param change + OCResourceHandle handle = NULL; + EntityHandler cb = std::bind(&PrimitiveServerResource::entityHandler, this, std::placeholders::_1); + OCStackResult result = OCPlatform::registerResource( handle, m_uri, + m_type, m_interface, cb, m_property); + + if (OC_STACK_OK != result) + { + //TODO: Throw error exception. + } + return PrimitiveServerResource(handle); +} + +OCEntityHandlerResult PrimitiveServerResource::entityHandler(std::shared_ptr + request) +{ + OCEntityHandlerResult ehResult = OC_EH_ERROR; + if (request) + { + //TODO: simplify Code. + + // Get the request type and request flag + std::string requestType = request->getRequestType(); + int requestFlag = request->getRequestHandlerFlag(); + + if (requestFlag & RequestHandlerFlag::RequestFlag) + { + auto pResponse = std::make_shared(); + pResponse->setRequestHandle(request->getRequestHandle()); + pResponse->setResourceHandle(request->getResourceHandle()); + + // If the request type is GET + if (requestType == "GET") + { + cout << "\t\t\trequestType : GET\n"; + //TODO: implementation sevral method for "default" or "custom" +// pResponse->setErrorCode(200); +// pResponse->setResponseResult(OC_EH_OK); +// pResponse->setResourceRepresentation(get()); +// if(OC_STACK_OK == OCPlatform::sendResponse(pResponse)) +// { +// ehResult = OC_EH_OK; +// } + } + else if (requestType == "PUT") + { + cout << "\t\t\trequestType : PUT\n"; + //TODO: implementation sevral method for "default" or "custom" + +// OCRepresentation rep = request->getResourceRepresentation(); +// +// // Do related operations related to PUT request +// // Update the lightResource +// put(rep); +// pResponse->setErrorCode(200); +// pResponse->setResponseResult(OC_EH_OK); +// pResponse->setResourceRepresentation(get()); +// if(OC_STACK_OK == OCPlatform::sendResponse(pResponse)) +// { +// ehResult = OC_EH_OK; +// } + } + } + + if (requestFlag & RequestHandlerFlag::ObserverFlag) + { + ObservationInfo observationInfo = request->getObservationInfo(); + if (ObserveAction::ObserveRegister == observationInfo.action) + { + m_interestedObservers.push_back(observationInfo.obsId); + } + else if (ObserveAction::ObserveUnregister == observationInfo.action) + { + m_interestedObservers.erase(std::remove( + m_interestedObservers.begin(), + m_interestedObservers.end(), + observationInfo.obsId), + m_interestedObservers.end()); + } + + pthread_t threadId; + + cout << "\t\trequestFlag : Observer\n"; + gObservation = 1; + static int startedThread = 0; + + // Observation happens on a different thread in ChangeLightRepresentation function. + // If we have not created the thread already, we will create one here. + if (!startedThread) + { + pthread_create (&threadId, NULL, ChangeLightRepresentation, (void *)this); + startedThread = 1; + } + ehResult = OC_EH_OK; + } + } + else + { + std::cout << "Request invalid" << std::endl; + } + + return ehResult; +} + + +template +void PrimitiveServerResource::setAttribute(const std::string &key, const T &value) +{ + +} + +template +T PrimitiveServerResource::getAttribute(T &key) const +{ + +} + +bool PrimitiveServerResource::hasAttribute(const std::string &key) const +{ + +} + +const ResourceAttributes &PrimitiveServerResource::getAttributes() const +{ + +} +ResourceAttributes &PrimitiveServerResource::getAttributes() +{ + +} + +bool PrimitiveServerResource:: isObservable() const +{ + +} + +bool PrimitiveServerResource::isDiscoverable() const +{ + +} + +void PrimitiveServerResource::setGetRequestHandler(GetRequestHandler) +{ + +} + +void PrimitiveServerResource::setSetRequestHandler(SetRequestHandler) +{ + +} + +void PrimitiveServerResource::notify() +{ + +} + +std::string PrimitiveServerResource::getUri() const +{ + +} + +std::vector PrimitiveServerResource::getTypes() const +{ + +} + +std::vector PrimitiveServerResource::getInterfaces() const +{ + +} + + diff --git a/service/basis/serverBuilder/src/ServerBuilder.cpp b/service/basis/serverBuilder/src/ServerBuilder.cpp new file mode 100644 index 0000000..c44f5d7 --- /dev/null +++ b/service/basis/serverBuilder/src/ServerBuilder.cpp @@ -0,0 +1,189 @@ +//****************************************************************** +// +// 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 +#include +#include +#include +#include + +class ResourceAttributes +{ + public: + /* + * @brief Value manipulation helper class. + * You may need to wrap OCRepresent::AttributeItem class. + * Do not expose the class as the part of API. + */ + class Item + { + public: + /* + * @brief Get the value as the type of T + * + * @throw BadCastException if the type is not correct. + */ + template + T get() const; + + /** + * @brief Get the value as the type of T + * + * @return True if the type is correct. + */ + template + bool get(T &var) const; + + // ... + // we need other helper methods for this class. + // please refer OCRepresent::AttributeItem class. + }; + + /** + * @brief There will be container in this class, and we need iterator for that. + * Implement one or just define alias. + */ + typedef std::unordered_map::const_iterator const_iterator; + + /** + * @brief std::map style get method. + */ + Item &operator[](const std::string &key); + + /** + * @brief std::map style get method. + * + * @throw InvalidKeyException If no value for the key. + */ + Item &at(const std::string &key); + + /** + * @brief std::map style get method. + * + * @throw InvalidKeyException If no value for the key. + */ + const Item &at(const std::string &key) const; + + /** + * @return True if there is a value for the key. + */ + bool contains(const std::string &key) const; + + const_iterator cbegin() const; + const_iterator cend() const; + + size_t getSize() const; + bool isEmpty() const; + + template + void insert(const std::string &key, const T &value); + + void remove(const std::string &key); + + void clear(); +}; + +class PrimitiveRequest +{ + std::string getResourceUri() const; +}; + +class PrimitiveResponse +{ + public: + static PrimitiveResponse DEFAULT; + + static PrimitiveResponse createResponse(const ResourceAttributes &attrs); +}; + +class PrimitiveServerResource +{ + private: + PrimitiveServerResource(); + public: + class Builder + { + public: + Builder(const std::string &uri, const std::string &type, + const std::string &interface); + + Builder &setProperties(uint8_t properties); + Builder &setDiscoverable(bool discoverable); + Builder &setObservable(bool observable); + + Builder &setAttributes(const ResourceAttributes &attrs); + + PrimitiveServerResource create() const; + }; + + typedef std::function < + PrimitiveResponse(const PrimitiveRequest &, const ResourceAttributes &) > GetRequestHandler; + typedef std::function < + PrimitiveResponse(const PrimitiveRequest &, const ResourceAttributes &) > SetRequestHandler; + + template + void setAttribute(const std::string &key, const T &value); + + template + T getAttribute() const; + + bool hasAttribute(const std::string &key) const; + + const ResourceAttributes &getAttributes() const; + ResourceAttributes &getAttributes(); + + uint8_t getProperties() const; + bool isObservable() const; + bool isDiscoverable() const; + + void setGetRequestHandler(GetRequestHandler); + void setSetRequestHandler(SetRequestHandler); + + void notify(); + + std::string getUri() const; + std::vector getTypes() const; + std::vector getInterfaces() const; +}; + +void example() +{ + PrimitiveServerResource server = PrimitiveServerResource::Builder("uri", "type", + "interface").setObservable(true).create(); + + server.setAttribute("key1", "value"); + server.setAttribute("key2", 0); + + server.setGetRequestHandler( + [](const PrimitiveRequest &, const ResourceAttributes &) -> PrimitiveResponse + { + // If you want to let the default handler handle the request which sends attributes in the server as the response, return DEFAULT. + return PrimitiveResponse::DEFAULT; + + // or if you want to send back with user defined response + ResourceAttributes responseAttrs; + responseAttrs["key"] = 0; + + return PrimitiveResponse::createResponse(responseAttrs); + }); + + // If you want to notify observers of attributes changed. + server.notify(); +} -- 2.7.4