From: Erich Keane Date: Thu, 18 Sep 2014 22:56:30 +0000 (-0700) Subject: Fridge example which shows a nice set of the std::bind functionality as well as how... X-Git-Tag: 1.2.0+RC1~2220 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8a66783f5eb6a2ed7de7c4c91f14846844e6d7b9;p=platform%2Fupstream%2Fiotivity.git Fridge example which shows a nice set of the std::bind functionality as well as how to use the ResourceObject creation. Note that this will not function correctly without the csdk fix for resource commas. Patch 2: Resolve Sudarshan's fixes Patch 3: rebase & remove previous version of code Change-Id: Ic7c5f3ae5da40cd8e6e82692e0f5e3b878f34489 --- diff --git a/examples/fridgeclient.cpp b/examples/fridgeclient.cpp new file mode 100644 index 000000000..81f360f74 --- /dev/null +++ b/examples/fridgeclient.cpp @@ -0,0 +1,149 @@ +//****************************************************************** +// +// Copyright 2014 Intel Mobile Communications GmbH 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +/// This fridgeclient represents a client trying to discover the associated +/// fridgeserver. The device resource is the only one available for discovery +/// on the server, so we have to take the fact that we know the device tag +/// to then generate a Resource object + +#include +#include +#include +#include +#include "OCPlatform.h" +#include "OCApi.h" + +using namespace OC; +namespace PH = std::placeholders; + +class ClientFridge +{ + public: + ClientFridge(PlatformConfig &cfg) : m_platform(cfg) + { + std::cout << "Fridge Client has started " < lk(m_mutex); + m_cv.wait(lk); + } + } + + private: + void foundDevice(std::shared_ptr resource) + { + if(resource && resource->uri() == "/device") + { + std::cout << "Discovered a device object"<host()<uri() < lightTypes = {"intel.fridge.light"}; + std::vector ifaces = {DEFAULT_INTERFACE}; + OCResource::Ptr light = m_platform.constructResourceObject(resource->host(), + "/light", false, lightTypes, ifaces); + + std::vector doorTypes = {"intel.fridge.door"}; + + OCResource::Ptr leftdoor = m_platform.constructResourceObject(resource->host(), + "/door/left", false, doorTypes, ifaces); + OCResource::Ptr rightdoor = m_platform.constructResourceObject(resource->host(), + "/door/right", false, doorTypes, ifaces); + + light->get(QueryParamsMap(), GetCallback( + std::bind(&ClientFridge::getResponse, this, "Fridge Light", PH::_1, + PH::_2, light, 1) + )); + leftdoor->get(QueryParamsMap(), GetCallback( + std::bind(&ClientFridge::getResponse, this, "Left Door", PH::_1, + PH::_2, leftdoor, 2) + )); + rightdoor->get(QueryParamsMap(), GetCallback( + std::bind(&ClientFridge::getResponse, this, "Right Door", PH::_1, + PH::_2, rightdoor, 3) + )); + } + + // Note that resourceName, resource, and getId are all bound via the std::bind mechanism. + // it is possible to attach ANY arbitrary data to do whatever you would like here. It may, + // however be a better fit to wrap each call in an object so a fuller context (and additional + // requests) can be easily made inside of a simple context + void getResponse(const std::string& resourceName, const OCRepresentation rep, const int eCode, + OCResource::Ptr resource, int getId) + { + std::cout << "Got a response from get from the "<uri()< +#include +#include +#include + +#include "OCPlatform.h" +#include "OCApi.h" + +using namespace OC; +namespace PH = std::placeholders; + +class Resource +{ + protected: + OCResourceHandle m_resourceHandle; + OCRepresentation m_rep; + virtual void entityHandler(std::shared_ptr request, + std::shared_ptr response)=0; +}; + +class DeviceResource : public Resource +{ + public: + DeviceResource(OCPlatform& platform) + { + std::string resourceURI = "/device"; + std::string resourceTypeName = "intel.fridge"; + std::string resourceInterface = DEFAULT_INTERFACE; + RegisterCallback cb = std::bind(&DeviceResource::entityHandler, this,PH::_1, PH::_2); + uint8_t resourceProperty = OC_DISCOVERABLE; + OCStackResult result = platform.registerResource(m_resourceHandle, + resourceURI, + resourceTypeName, + resourceInterface, + cb, + resourceProperty); + + if(OC_STACK_OK != result) + { + throw std::runtime_error( + std::string("Device Resource failed to start")+std::to_string(result)); + } + } + private: + OCRepresentation get() + { + m_rep.setValue("device_name", "Intel Powered 2 door, 1 light refridgerator"); + return m_rep; + } + + std::string m_modelName; + protected: + virtual void entityHandler(std::shared_ptr request, + std::shared_ptr response) + { + if(request) + { + if(request->getRequestHandlerFlag() == RequestHandlerFlag::RequestFlag) + { + if(request->getRequestType() == "GET") + { + if(response) + { + std::cout<<"DeviceResource Get Request"<setErrorCode(200); + response->setResourceRepresentation(get(), ""); + } + } + else + { + std::cout <<"DeviceResource unsupported request type " + << request->getRequestType() << std::endl; + } + } + else + { + std::cout << "DeviceResource unsupported request flag" < request, + std::shared_ptr response) + { + if(request) + { + if(request->getRequestHandlerFlag() == RequestHandlerFlag::RequestFlag) + { + if(request->getRequestType() == "GET") + { + if(response) + { + std::cout<<"Light Get Request"<setErrorCode(200); + response->setResourceRepresentation(get(), ""); + } + } + else if(request->getRequestType() == "PUT") + { + if(response) + { + std::cout <<"Light Put Request"<getResourceRepresentation()); + + response->setErrorCode(200); + response->setResourceRepresentation(get(),""); + } + } + else + { + std::cout <<"Light unsupported request type " + << request->getRequestType() << std::endl; + } + } + else + { + std::cout << "Light unsupported request flag" < request, + std::shared_ptr response) + { + if(request) + { + if(request->getRequestHandlerFlag() == RequestHandlerFlag::RequestFlag) + { + if(request->getRequestType() == "GET") + { + if(response) + { + // note that we know the side because std::bind gives us the + // appropriate object + std::cout<setErrorCode(200); + response->setResourceRepresentation(get(), ""); + } + } + else if(request->getRequestType() == "PUT") + { + if(response) + { + std::cout <getResourceRepresentation()); + response->setErrorCode(200); + response->setResourceRepresentation(get(),""); + } + } + else + { + std::cout <getRequestType() << std::endl; + } + } + else + { + std::cout << "Door unsupported request flag" <