modify resouce-encapsulation sample server and client
authorChaJiwon <jw_wonny.cha@samsung.com>
Fri, 18 Sep 2015 05:46:30 +0000 (14:46 +0900)
committerMadan Lanka <lanka.madan@samsung.com>
Fri, 18 Sep 2015 07:26:51 +0000 (07:26 +0000)
sampleResourceClient
- add the function that discover resouce
- add the function that cancel discover resource

sampleResourceServer
- select presence mode
- select resource type

Change-Id: I9e5867569be687fc25a9d1eca7a2cdff585b2963
Signed-off-by: ChaJiwon <jw_wonny.cha@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2631
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
service/resource-encapsulation/examples/linux/SampleResourceClient.cpp
service/resource-encapsulation/examples/linux/SampleResourceServer.cpp

index b905977..55a41ff 100644 (file)
@@ -18,7 +18,9 @@
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
-#include<iostream>
+#include <iostream>
+#include <functional>
+
 #include "mutex"
 #include "condition_variable"
 
@@ -26,7 +28,6 @@
 #include "RCSRemoteResourceObject.h"
 #include "RCSResourceAttributes.h"
 #include "RCSAddress.h"
-
 #include "OCPlatform.h"
 
 using namespace OC;
@@ -36,11 +37,15 @@ constexpr int CORRECT_INPUT = 1;
 constexpr int INCORRECT_INPUT = 2;
 constexpr int QUIT_INPUT = 3;
 
+constexpr int REQUEST_TEMP = 1;
+constexpr int REQUEST_LIGHT = 2;
+
+std::unique_ptr<RCSDiscoveryTask> discoveryTask = nullptr;
 std::shared_ptr<RCSRemoteResourceObject>  resource;
 
-const std::string defaultKey = "Temperature";
-const std::string resourceType = "core.TemperatureSensor";
-const std::string relativetUri = OC_RSRVD_WELL_KNOWN_URI;
+std::string defaultKey = "Temperature";
+const std::string relativeUri = OC_RSRVD_WELL_KNOWN_URI;
+const std::string multicastAdd = "multi";
 
 std::mutex mtx;
 std::condition_variable cond;
@@ -56,6 +61,9 @@ void getResourceCacheState();
 void getCachedAttributes();
 void getCachedAttribute();
 void stopCaching();
+void discoverResource();
+void cancelDiscovery();
+int processUserInput();
 
 enum Menu
 {
@@ -69,6 +77,8 @@ enum Menu
     GET_CACHED_ATTRIBUTES,
     GET_CACHED_ATTRIBUTE,
     STOP_CACHING,
+    DISCOVERY_RESOURCE,
+    CANCEL_DISCOVERY,
     QUIT,
     END_OF_MENU
 };
@@ -94,23 +104,22 @@ ClientMenu clientMenu[] = {
         {Menu::GET_CACHED_ATTRIBUTES, getCachedAttributes, CORRECT_INPUT},
         {Menu::GET_CACHED_ATTRIBUTE, getCachedAttribute, CORRECT_INPUT},
         {Menu::STOP_CACHING, stopCaching, CORRECT_INPUT},
+        {Menu::DISCOVERY_RESOURCE, discoverResource, CORRECT_INPUT},
+        {Menu::CANCEL_DISCOVERY, cancelDiscovery, CORRECT_INPUT},
         {Menu::QUIT, [](){}, QUIT_INPUT},
         {Menu::END_OF_MENU, nullptr, INCORRECT_INPUT}
     };
 
-void onResourceDiscovered(std::shared_ptr<RCSRemoteResourceObject> foundResource)
+void onResourceDiscovered(std::shared_ptr<RCSRemoteResourceObject> discoveredResource)
 {
-    std::cout << "onResourceDiscovered callback" << std::endl;
-
-    std::string resourceURI = foundResource->getUri();
-    std::string hostAddress = foundResource->getAddress();
+    std::cout << "onResourceDiscovered callback :: " << std::endl;
+    std::string resourceURI = discoveredResource->getUri();
+    std::string hostAddress = discoveredResource->getAddress();
 
-    std::cout << "\t\tResource URI : " << resourceURI << std::endl;
-    std::cout << "\t\tResource Host : " << hostAddress << std::endl;
+    std::cout << resourceURI << std::endl;
+    std::cout << hostAddress << std::endl;
 
-    resource = foundResource;
-
-    cond.notify_all();
+    resource = discoveredResource;
 }
 
 void onResourceStateChanged(const ResourceState& resourceState)
@@ -189,7 +198,9 @@ void displayMenu()
     std::cout << "8 :: Get Cached Attributes" << std::endl;
     std::cout << "9 :: Get Cached Attribute"  << std::endl;
     std::cout << "10 :: Stop Caching" << std::endl;
-    std::cout << "11 :: Stop Server" << std::endl;
+    std::cout << "11 :: Discover Resource" << std::endl;
+    std::cout << "12 :: Cancel Discovery" << std::endl;
+    std::cout << "13 :: Stop Server" << std::endl;
 }
 
 int processUserInput()
@@ -393,29 +404,63 @@ void platFormConfigure()
     OCPlatform::Configure(config);
 }
 
-bool discoverResource()
+void discoverResource()
 {
-    std::cout << "Wait 2 seconds until discovered." << std::endl;
+    std::string resourceType;
 
-    RCSDiscoveryManager::getInstance()->discoverResourceByType(RCSAddress::multicast(),
-            relativetUri, resourceType, &onResourceDiscovered);
+    std::cout << "========================================================" << std::endl;
+    std::cout << "1. Temperature Resource Discovery" << std::endl;
+    std::cout << "2. Light Resource Discovery" << std::endl;
+    std::cout << "========================================================" << std::endl;
 
-    std::unique_lock<std::mutex> lck(mtx);
-    cond.wait_for(lck,std::chrono::seconds(2));
+    switch (processUserInput())
+    {
+    case REQUEST_TEMP:
+        resourceType = "core.TemperatureSensor";
+        break;
+    case REQUEST_LIGHT:
+        resourceType = "core.light";
+        defaultKey = "Light";
+        break;
+    default :
+        std::cout << "Invalid input, please try again" << std::endl;
+        return;
+    }
+
+    std::string addressInput;
+    std::cout << "========================================================" << std::endl;
+    std::cout << "Please input address" << std::endl;
+    std::cout << "(want to use multicast -> please input 'multi')" << std::endl;
+    std::cout << "========================================================" << std::endl;
+
+    std::cin >> addressInput;
+
+    if(addressInput == multicastAdd)
+    {
+        discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(RCSAddress::multicast(),
+                relativeUri, resourceType, &onResourceDiscovered);
+    }
+    else
+    {
+        discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(RCSAddress::unicast
+                (addressInput), relativeUri, resourceType, &onResourceDiscovered);
+    }
+}
 
-    return resource != nullptr;
+void cancelDiscovery()
+{
+    if(!discoveryTask)
+    {
+        std::cout << "There isn't discovery request..." << std::endl;
+        return;
+    }
+    discoveryTask->cancel();
 }
 
 int main()
 {
     platFormConfigure();
 
-    if (!discoverResource())
-    {
-        std::cout << "Can't discovered Server... Exiting the Client." << std::endl;
-        return -1;
-    }
-
     try
     {
         process();
@@ -429,4 +474,3 @@ int main()
 
     return 0;
 }
-
index f7c946c..b1c67cf 100644 (file)
@@ -29,25 +29,36 @@ using namespace OIC::Service;
 
 constexpr int DEFALUT_VALUE = 0;
 
+constexpr int REQUEST_TEMP = 1;
+constexpr int REQUEST_LIGHT = 2;
+
+constexpr int PRESENCE_ON = 1;
+constexpr int PRESENCE_OFF = 2;
+
 constexpr int DEFALUT_SERVER = 1;
 constexpr int CUSTOM_SERVER = 2;
 constexpr int STOP = 3;
 
 constexpr int INCREASE_TEMPERATURE = 1;
 constexpr int DECREASE_TEMPERATURE = 2;
-constexpr int STOP_SENSOR = 3;
+constexpr int STOP_TEMPERATURE_SENSOR = 3;
+constexpr int STOP_LIGHT_SENSOR = 1;
 
 constexpr int CORRECT_INPUT = 1;
 constexpr int INCORRECT_INPUT = 2;
 constexpr int QUIT = 3;
 
+
 std::string resourceUri = "/a/TempSensor";
 std::string resourceType = "core.TemperatureSensor";
 std::string resourceInterface = "oic.if.";
 std::string attributeKey = "Temperature";
+int isPresenceOn = PRESENCE_ON;
 
 RCSResourceObject::Ptr server;
 
+int processUserInput();
+
 enum class Control{
     INCREASE,
     DECREASE
@@ -74,6 +85,13 @@ void displayControlTemperatureMenu()
     std::cout << "========================================================" << std::endl;
 }
 
+void displayControlLightMenu()
+{
+    std::cout << "========================================================" << std::endl;
+    std::cout << "1. Stop the Sensor" << std::endl;
+    std::cout << "========================================================" << std::endl;
+}
+
 void printAttribute(const RCSResourceAttributes& attrs)
 {
     for(const auto& attr : attrs)
@@ -88,7 +106,6 @@ RCSGetResponse requestHandlerForGet(const RCSRequest& request,
         RCSResourceAttributes& attrs)
 {
     std::cout << "Recieved a Get request from Client" << std::endl;
-
     RCSResourceObject::LockGuard lock(*server);
     RCSResourceAttributes attributes = server->getAttributes();
 
@@ -112,12 +129,33 @@ RCSSetResponse requestHandlerForSet(const RCSRequest& request,
 
 void createResource()
 {
-    server = RCSResourceObject::Builder(resourceUri, resourceType,
-                             resourceInterface).setDiscoverable(true).setObservable(true).build();
+    server = RCSResourceObject::Builder(resourceUri, resourceType,resourceInterface).
+            setDiscoverable(true).setObservable(true).build();
 }
 
 void initServer()
 {
+    std::cout << "========================================================" << std::endl;
+    std::cout << "Select Resource Type" << std::endl;
+    std::cout << "1. Temperature" << std::endl;
+    std::cout << "2. Light" << std::endl;
+    std::cout << "========================================================" << std::endl;
+
+    switch(processUserInput())
+    {
+    case REQUEST_TEMP:
+        resourceUri = "/a/TempSensor";
+        resourceType = "core.TemperatureSensor";
+        break;
+    case REQUEST_LIGHT:
+        resourceUri = "/a/light";
+        resourceType = "core.light";
+        break;
+    default :
+        std::cout << "Invalid input, please try again" << std::endl;
+        return;
+    }
+
     try
     {
         createResource();
@@ -164,6 +202,27 @@ int processUserInput()
     return userInput;
 }
 
+int selectPresenceMenu()
+{
+    std::cout << "========================================================" << std::endl;
+    std::cout << "1. Presence On" << std::endl;
+    std::cout << "2. Presence Off" << std::endl;
+    std::cout << "========================================================" << std::endl;
+
+    switch(processUserInput())
+        {
+        case PRESENCE_ON:
+            isPresenceOn = PRESENCE_ON;
+            startPresence(3);
+            return CORRECT_INPUT;
+        case PRESENCE_OFF:
+            isPresenceOn = PRESENCE_OFF;
+            return CORRECT_INPUT;
+        default :
+            std::cout << "Invalid input, please try again" << std::endl;
+            return INCORRECT_INPUT;
+        }
+}
 int selectServerMenu()
 {
     switch (processUserInput())
@@ -201,7 +260,20 @@ int selectControlTemperatureMenu()
            changeTemperature(Control::DECREASE);
            return CORRECT_INPUT;
 
-       case STOP_SENSOR:
+       case STOP_TEMPERATURE_SENSOR:
+           return QUIT;
+
+       default:
+           std::cout << "Invalid input. Please try again." << std::endl;
+           return INCORRECT_INPUT;
+   }
+}
+
+int selectControlLightMenu()
+{
+   switch (processUserInput())
+   {
+       case STOP_LIGHT_SENSOR:
            return QUIT;
 
        default:
@@ -214,8 +286,13 @@ void process()
 {
     while(true)
     {
-        displayMenu();
+        int ret = selectPresenceMenu();
+        if(ret == CORRECT_INPUT) break;
+    }
 
+    while(true)
+    {
+        displayMenu();
         int ret = selectServerMenu();
 
         if(ret == QUIT) return;
@@ -224,16 +301,21 @@ void process()
 
     while(true)
     {
-        displayControlTemperatureMenu();
-
-        if (selectControlTemperatureMenu() == QUIT) return;
+        if(resourceType == "core.TemperatureSensor")
+        {
+            displayControlTemperatureMenu();
+            if (selectControlTemperatureMenu() == QUIT) return;
+        }
+        else if(resourceType == "core.light")
+        {
+            displayControlLightMenu();
+            if (selectControlLightMenu() == QUIT) return;
+        }
     }
 }
 
 int main(void)
 {
-    startPresence(3);
-
     try
     {
         process();
@@ -244,7 +326,9 @@ int main(void)
         std::cout << "main exception  : " << e.what() << std::endl;
     }
 
+    if(isPresenceOn == PRESENCE_ON)
+    {
+        stopPresence();
+    }
     std::cout << "Stopping the Server" << std::endl;
 }
-
-