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 <h_w.park@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1165
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
--- /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.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#ifndef __PRIMITIVEREQUEST__H
+#define __PRIMITIVEREQUEST__H
+
+
+#include <string>
+
+class PrimitiveRequest
+{
+ std::string getResourceUri() const;
+};
+
+
+#endif
--- /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.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#ifndef __PRIMITIVERESPONSE_H
+#define __PRIMITIVERESPONSE_H
+
+class Handler;
+
+class PrimitiveResponse
+{
+ public:
+ static PrimitiveResponse DEFAULT;
+
+ static PrimitiveResponse createResponse(const ResourceAttributes &attrs);
+
+ shared_ptr<Handler> 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
--- /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.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#ifndef __PRIMITIVESERVERRESOURCE_H
+#define __PRIMITIVESERVERRESOURCE_H
+
+class PrimitiveServerResource
+{
+ private:
+ PrimitiveServerResource();
+ PrimitiveServerResource(OCResourceHandle baseResourceHandle);
+ OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> 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<typename T>
+ void setAttribute(const std::string &key, const T &value);
+
+ template<typename T>
+ 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<std::string> getTypes() const;
+ std::vector<std::string> getInterfaces() const;
+
+ private:
+ OCResourceHandle m_resourceHandle;
+};
+
+
+#endif
--- /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 <memory>
+#include <string>
+#include <functional>
+#include <unordered_map>
+#include <vector>
+
+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<typename T>
+ T get() const;
+
+ /**
+ * @brief Get the value as the type of T
+ *
+ * @return True if the type is correct.
+ */
+ template<typename T>
+ 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<std::string, Item>::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<typename T>
+ void insert(const std::string &key, const T &value);
+
+ void remove(const std::string &key);
+
+ void clear();
+};
+
--- /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 "PrimitiveRequest.h"
--- /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 "PrimitiveResponse.h"
--- /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 <PrimitiveServerResource.h>
+
+#include <string>
+#include <functional>
+#include <vector>
+
+#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<OCResourceRequest>
+ 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<OC::OCResourceResponse>();
+ 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<typename T>
+void PrimitiveServerResource::setAttribute(const std::string &key, const T &value)
+{
+
+}
+
+template<typename T>
+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<std::string> PrimitiveServerResource::getTypes() const
+{
+
+}
+
+std::vector<std::string> PrimitiveServerResource::getInterfaces() const
+{
+
+}
+
+
--- /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 <memory>
+#include <string>
+#include <functional>
+#include <unordered_map>
+#include <vector>
+
+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<typename T>
+ T get() const;
+
+ /**
+ * @brief Get the value as the type of T
+ *
+ * @return True if the type is correct.
+ */
+ template<typename T>
+ 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<std::string, Item>::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<typename T>
+ 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<typename T>
+ void setAttribute(const std::string &key, const T &value);
+
+ template<typename T>
+ 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<std::string> getTypes() const;
+ std::vector<std::string> 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();
+}