Implementaion of APIs for sending GET, PUT and POST requests.
authorHarish Kumara Marappa <h.marappa@samsung.com>
Wed, 12 Aug 2015 09:20:46 +0000 (14:50 +0530)
committerMadan Lanka <lanka.madan@samsung.com>
Wed, 12 Aug 2015 10:09:04 +0000 (10:09 +0000)
1. APIs for sending GET, PUT, POST and Observe reaquets and handling
responses.
2. Modified the sample test application for testing new APIs.

Change-Id: Ieaa473b7367340c50415055db274feb1731a21d1
Signed-off-by: Harish Kumara Marappa <h.marappa@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2134
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
service/simulator/examples/client-controller/client_controller.cpp
service/simulator/src/simulator_remote_resource.cpp
service/simulator/src/simulator_remote_resource.h
service/simulator/src/simulator_resource_model.h

index 94c788e..f48a2a4 100644 (file)
@@ -42,7 +42,7 @@ class ClientController
                 int choice = -1;
                 std::cout << "Enter your choice: ";
                 std::cin >> choice;
-                if (choice < 0 || choice > 3)
+                if (choice < 0 || choice > 8)
                 {
                     std::cout << "Invaild choice !" << std::endl; continue;
                 }
@@ -51,7 +51,12 @@ class ClientController
                 {
                     case 1: findResource(); break;
                     case 2: displayResource(); break;
-                    case 3: printMenu(); break;
+                    case 3: observeResource(); break;
+                    case 4: cancelObserving(); break;
+                    case 5: sendGet(); break;
+                    case 6: sendPut(); break;
+                    case 7: sendPost(); break;
+                    case 8: printMenu(); break;
                     case 0: cont = false;
                 }
             }
@@ -63,7 +68,12 @@ class ClientController
             std::cout << "########### SIMULATOR CLIENT CONTROLLER ###########" << std::endl;
             std::cout << "1. Find resource" << std::endl;
             std::cout << "2. Display resource information" << std::endl;
-            std::cout << "3: Help" << std::endl;
+            std::cout << "3. Observe for resource change" << std::endl;
+            std::cout << "4. Cancel observation" << std::endl;
+            std::cout << "5. Send GET message" << std::endl;
+            std::cout << "6. Send PUT message" << std::endl;
+            std::cout << "7. Send POST message" << std::endl;
+            std::cout << "8: Help" << std::endl;
             std::cout << "0. Exit" << std::endl;
             std::cout << "###################################################" << std::endl;
         }
@@ -79,7 +89,7 @@ class ClientController
             int index = 1;
             for (auto & resource : resourceList)
             {
-                std::cout << index++ << ": " << resource->getURI().c_str() << std::endl;
+                std::cout << index++ << ": " << resource->getURI() << "[" << resource->getHost()  << "]" << std::endl;
             }
 
             int choice = -1;
@@ -129,6 +139,7 @@ class ClientController
         {
             std::cout << "#############################" << std::endl;
             std::cout << "URI: " << resource->getURI().c_str() << std::endl;
+            std::cout << "Host: " << resource->getHost().c_str() << std::endl;
             std::cout << "Resource Types: ";
             for (auto &type : resource->getResourceTypes())
                 std::cout << type << " ";
@@ -138,6 +149,170 @@ class ClientController
             std::cout << std::boolalpha << "\nisObservable : " << resource->isObservable() << std::endl;
             std::cout << "#############################" << std::endl;
         }
+
+        void observeResource()
+        {
+            std::vector<SimulatorRemoteResourcePtr> resourceList =
+                SimulatorManager::getInstance()->getFoundResources();
+
+            int index = selectResource(resourceList);
+            if (-1 == index)
+                return;
+
+            SimulatorRemoteResourcePtr resource = resourceList[index - 1];
+
+            // callback implementaion
+            SimulatorRemoteResource::RepresentationChangeCallback callback =
+            [](int errorCode, const SimulatorResourceModel &rep, int seq)
+            {
+                std::cout << "\nObserve notificatoin received ###[errorcode:  " << errorCode << " seq:  " << seq << "]" << std::endl;
+                std::map<std::string, SimulatorResourceModel::Attribute> attributes = rep.getAttributes();
+                for (auto & attribute : attributes)
+                {
+                    std::cout << (attribute.second).getName() << " :  {" << std::endl;
+                    std::cout << "value: " << (attribute.second).valueToString().c_str() << std::endl;
+                    std::cout << "}" << std::endl;
+                }
+                std::cout << std::endl;
+            };
+
+            std::map <std::string, std::string> queryParams;
+            SimulatorResult result = resource->observe(SimulatorRemoteResource::OBSERVE, queryParams, callback);
+            if ( SIMULATOR_SUCCESS == result)
+                std::cout << "Observe is successfull!" << std::endl;
+            else
+                std::cout << "Observe is failed!error: " << result << std::endl;
+        }
+
+        void cancelObserving()
+        {
+            std::vector<SimulatorRemoteResourcePtr> resourceList =
+                SimulatorManager::getInstance()->getFoundResources();
+
+            int index = selectResource(resourceList);
+            if (-1 == index)
+                return;
+
+            SimulatorRemoteResourcePtr resource = resourceList[index - 1];
+            SimulatorResult result = resource->cancelObserve();
+            if ( SIMULATOR_SUCCESS == result)
+                std::cout << "Cancelling observe is successfull!" << std::endl;
+            else
+                std::cout << "Cancelling observe is failed!error: " << result << std::endl;
+        }
+
+        void sendGet()
+        {
+            std::vector<SimulatorRemoteResourcePtr> resourceList =
+                SimulatorManager::getInstance()->getFoundResources();
+
+            int index = selectResource(resourceList);
+            if (-1 == index)
+                return;
+
+            SimulatorRemoteResourcePtr resource = resourceList[index - 1];
+
+            // callback implementaion
+            SimulatorRemoteResource::ResponseCallback callback =
+            [](int errorCode, const SimulatorResourceModel & rep)
+            {
+                std::cout << "\nGET Response received ### [errorcode:  " << errorCode << "]" << std::endl;
+                std::cout << "Representation is: " << std::endl;
+                std::map<std::string, SimulatorResourceModel::Attribute> attributes = rep.getAttributes();
+                for (auto & attribute : attributes)
+                {
+                    std::cout << (attribute.second).getName() << " :  {" << std::endl;
+                    std::cout << "value: " << (attribute.second).valueToString().c_str() << std::endl;
+                    std::cout << "}" << std::endl;
+                }
+                std::cout << std::endl;
+            };
+
+            std::map <std::string, std::string> queryParams;
+            SimulatorResult result = resource->get(queryParams, callback);
+            if ( SIMULATOR_SUCCESS == result)
+                std::cout << "GET is successfull!" << std::endl;
+            else
+                std::cout << "GET is failed!error: " << result << std::endl;
+        }
+
+        void sendPut()
+        {
+            std::vector<SimulatorRemoteResourcePtr> resourceList =
+                SimulatorManager::getInstance()->getFoundResources();
+
+            int index = selectResource(resourceList);
+            if (-1 == index)
+                return;
+
+            SimulatorRemoteResourcePtr resource = resourceList[index - 1];
+
+            // callback implementaion
+            SimulatorRemoteResource::ResponseCallback callback =
+            [](int errorCode, const SimulatorResourceModel & rep)
+            {
+                std::cout << "\nPUT Response received ![errorcode:  " << errorCode << "]" << std::endl;
+                std::cout << "Representation is: " << std::endl;
+                std::map<std::string, SimulatorResourceModel::Attribute> attributes = rep.getAttributes();
+                for (auto & attribute : attributes)
+                {
+                    std::cout << (attribute.second).getName() << " :  {" << std::endl;
+                    std::cout << "value: " << (attribute.second).valueToString().c_str() << std::endl;
+                    std::cout << "}" << std::endl;
+                }
+                std::cout << std::endl;
+            };
+
+            std::map <std::string, std::string> queryParams;
+            SimulatorResourceModel rep;
+            rep.addAttribute("power", "off");
+            rep.addAttribute("intensity", 5);
+
+            SimulatorResult result = resource->put(rep, queryParams, callback);
+            if ( SIMULATOR_SUCCESS == result)
+                std::cout << "PUT is successfull!" << std::endl;
+            else
+                std::cout << "PUT is failed!error: " << result << std::endl;
+        }
+
+        void sendPost()
+        {
+            std::vector<SimulatorRemoteResourcePtr> resourceList =
+                SimulatorManager::getInstance()->getFoundResources();
+
+            int index = selectResource(resourceList);
+            if (-1 == index)
+                return;
+
+            SimulatorRemoteResourcePtr resource = resourceList[index - 1];
+
+            // callback implementaion
+            SimulatorRemoteResource::ResponseCallback callback =
+            [](int errorCode, const SimulatorResourceModel & rep)
+            {
+                std::cout << "\nPOST Response received ![errorcode:  " << errorCode << "]" << std::endl;
+                std::cout << "Representation is: " << std::endl;
+                std::map<std::string, SimulatorResourceModel::Attribute> attributes = rep.getAttributes();
+                for (auto & attribute : attributes)
+                {
+                    std::cout << (attribute.second).getName() << " :  {" << std::endl;
+                    std::cout << "value: " << (attribute.second).valueToString().c_str() << std::endl;
+                    std::cout << "}" << std::endl;
+                }
+                std::cout << std::endl;
+            };
+
+            std::map <std::string, std::string> queryParams;
+            SimulatorResourceModel rep;
+            rep.addAttribute("power", "on");
+            rep.addAttribute("intensity", 7);
+
+            SimulatorResult result = resource->post(rep, queryParams, callback);
+            if ( SIMULATOR_SUCCESS == result)
+                std::cout << "POST is successfull!" << std::endl;
+            else
+                std::cout << "POST is failed!error: " << result << std::endl;
+        }
 };
 
 void printMainMenu()
index eb3cf90..cdd8fd1 100644 (file)
@@ -59,3 +59,143 @@ bool SimulatorRemoteResource::isObservable() const
     return m_ocResource->isObservable();
 }
 
+SimulatorResult SimulatorRemoteResource::observe(SimulatorRemoteResource::ObserveType type,
+        const QueryParamsMap &queryParams, RepresentationChangeCallback callback)
+{
+    std::lock_guard<std::mutex> lock(m_observeMutex);
+
+    static int observeState = false;
+    if (!observeState)
+    {
+        OC::ObserveCallback observeCallback = [callback](const OC::HeaderOptions & headerOptions,
+                                              const OC::OCRepresentation & rep, const int errorCode,
+                                              const int sequenceNum)
+        {
+            // Convert OCRepresentation to SimulatorResourceModel
+            SimulatorResourceModel resModel = SimulatorResourceModel::create(rep);
+            callback(errorCode, resModel, sequenceNum);
+        };
+
+        OC::ObserveType observeType =
+            (type == SimulatorRemoteResource::OBSERVE_ALL) ? OC::ObserveType::ObserveAll :
+            OC::ObserveType::Observe;
+        OCStackResult error = m_ocResource->observe(observeType, queryParams, observeCallback);
+        if (OC_STACK_OK == error)
+        {
+            observeState = true;
+            return SIMULATOR_SUCCESS;
+        }
+        else
+        {
+            return SIMULATOR_ERROR;
+        }
+    }
+
+    return SIMULATOR_RESOURCE_ALREADY_OBSERVING;
+}
+
+SimulatorResult SimulatorRemoteResource::cancelObserve()
+{
+    OCStackResult error = m_ocResource->cancelObserve();
+    if (OC_STACK_OK != error)
+        return SIMULATOR_ERROR;
+    return SIMULATOR_SUCCESS;
+}
+
+SimulatorResult SimulatorRemoteResource::get(const std::string &resourceType,
+        const std::string &interfaceType,
+        const QueryParamsMap &queryParams, ResponseCallback callback)
+{
+    OC::GetCallback getCallback = [callback](const OC::HeaderOptions & headerOptions,
+                                  const OC::OCRepresentation & rep, const int errorCode)
+    {
+        // Convert OCRepresentation to SimulatorResourceModel
+        SimulatorResourceModel resModel = SimulatorResourceModel::create(rep);
+        callback(errorCode, resModel);
+    };
+
+    // Convert SimulatorResourceModel to OcRepresentation
+    OCStackResult result = m_ocResource->get(resourceType, interfaceType, queryParams,
+                           getCallback);
+    if (OC_STACK_OK != result)
+    {
+        return SIMULATOR_ERROR;
+    }
+
+    return SIMULATOR_SUCCESS;
+}
+
+SimulatorResult SimulatorRemoteResource::get(const QueryParamsMap &queryParams,
+        ResponseCallback callback)
+{
+    std::string resourceType;
+    std::string interfaceType;
+    return get(resourceType, interfaceType, queryParams, callback);
+}
+
+SimulatorResult SimulatorRemoteResource::put(const std::string &resourceType,
+        const std::string &interfaceType,
+        const SimulatorResourceModel &representation,
+        const QueryParamsMap &queryParams, ResponseCallback callback)
+{
+    OC::PutCallback putCallback = [callback](const OC::HeaderOptions & headerOptions,
+                                  const OC::OCRepresentation & rep, const int errorCode)
+    {
+        // Convert OCRepresentation to SimulatorResourceModel
+        SimulatorResourceModel resModel = SimulatorResourceModel::create(rep);
+        callback(errorCode, resModel);
+    };
+
+    // Convert SimulatorResourceModel to OcRepresentation
+    OC::OCRepresentation ocRep = representation.getOCRepresentation();
+    OCStackResult result = m_ocResource->put(resourceType, interfaceType, ocRep, queryParams,
+                           putCallback);
+    if (OC_STACK_OK != result)
+    {
+        return SIMULATOR_ERROR;
+    }
+
+    return SIMULATOR_SUCCESS;
+}
+
+SimulatorResult SimulatorRemoteResource::put(const SimulatorResourceModel &representation,
+        const QueryParamsMap &queryParams, ResponseCallback callback)
+{
+    std::string resourceType;
+    std::string interfaceType;
+    return put(resourceType, interfaceType, representation, queryParams, callback);
+}
+
+SimulatorResult SimulatorRemoteResource::post(const std::string &resourceType,
+        const std::string &interfaceType,
+        const SimulatorResourceModel &representation,
+        const QueryParamsMap &queryParams, ResponseCallback callback)
+{
+    OC::PostCallback postCallback = [callback](const OC::HeaderOptions & headerOptions,
+                                    const OC::OCRepresentation & rep, const int errorCode)
+    {
+        // Convert OCRepresentation to SimulatorResourceModel
+        SimulatorResourceModel resModel = SimulatorResourceModel::create(rep);
+        callback(errorCode, resModel);
+    };
+
+    // Convert SimulatorResourceModel to OcRepresentation
+    OC::OCRepresentation ocRep = representation.getOCRepresentation();
+    OCStackResult result = m_ocResource->post(resourceType, interfaceType, ocRep, queryParams,
+                           postCallback);
+    if (OC_STACK_OK != result)
+    {
+        return SIMULATOR_ERROR;
+    }
+
+    return SIMULATOR_SUCCESS;
+}
+
+SimulatorResult SimulatorRemoteResource::post(const SimulatorResourceModel &representation,
+        const QueryParamsMap &queryParams, ResponseCallback callback)
+{
+    std::string resourceType;
+    std::string interfaceType;
+    return post(resourceType, interfaceType, representation, queryParams, callback);
+}
+
index 662cca7..6104569 100644 (file)
 
 /**
  * @class   SimulatorRemoteResource
- * @brief   This class provides a set of functions for the client to hande the resources currently running on the servers.
+ * @brief   This class represents the resource discovered in the network and provides APIs
+ *          for sending requests to discovered resource.
  */
 class SimulatorRemoteResource
 {
     public:
+        typedef enum
+        {
+            OBSERVE,
+            OBSERVE_ALL
+        } ObserveType;
+
+        // Typedef for query parameter map
+        typedef std::map<std::string, std::string> QueryParamsMap;
+
+        typedef std::function<void (int, const SimulatorResourceModel &, int)>
+        RepresentationChangeCallback;
+
+        typedef std::function<void (int, const SimulatorResourceModel &)>
+        ResponseCallback;
+
         SimulatorRemoteResource(std::shared_ptr<OC::OCResource> resource);
 
         std::string getURI() const;
@@ -59,9 +75,35 @@ class SimulatorRemoteResource
 
         bool isObservable() const;
 
+        SimulatorResult observe(ObserveType type,
+                                const QueryParamsMap &queryParams, RepresentationChangeCallback callback);
+
+        SimulatorResult cancelObserve();
+
+        SimulatorResult get(const QueryParamsMap &queryParams,
+                            ResponseCallback callback);
+
+        SimulatorResult get(const std::string &resourceType, const std::string &interfaceType,
+                            const QueryParamsMap &queryParams, ResponseCallback callback);
+
+        SimulatorResult put(const SimulatorResourceModel &representation,
+                            const QueryParamsMap &queryParams, ResponseCallback callback);
+
+        SimulatorResult put(const std::string &resourceType, const std::string &interfaceType,
+                            const SimulatorResourceModel &representation,
+                            const QueryParamsMap &queryParams, ResponseCallback callback);
+
+        SimulatorResult post(const SimulatorResourceModel &representation,
+                             const QueryParamsMap &queryParams, ResponseCallback callback);
+
+        SimulatorResult post(const std::string &resourceType, const std::string &interfaceType,
+                             const SimulatorResourceModel &representation,
+                             const QueryParamsMap &queryParams, ResponseCallback callback);
+
     private:
 
         std::shared_ptr<OC::OCResource> m_ocResource;
+        std::mutex m_observeMutex;
 };
 
 typedef std::shared_ptr<SimulatorRemoteResource> SimulatorRemoteResourcePtr;
index 439082e..07f9fdb 100644 (file)
@@ -34,6 +34,7 @@
 #include "OCApi.h"
 
 class SimulatorResourceServer;
+class SimulatorRemoteResource;
 /**
  * @class   SimulatorResourceModel
  * @brief   This class provides a set of functions for accessing and manipulating the resource model.
@@ -41,6 +42,7 @@ class SimulatorResourceServer;
 class SimulatorResourceModel
 {
         friend class SimulatorResourceServer;
+        friend class SimulatorRemoteResource;
 
     public: