[TIZEN] RESampleClient application resource discovery fix.
authorarya.k <arya.kumar@samsung.com>
Tue, 29 Sep 2015 09:40:09 +0000 (15:10 +0530)
committerMadan Lanka <lanka.madan@samsung.com>
Tue, 29 Sep 2015 14:18:17 +0000 (14:18 +0000)
Change-Id: I80c2e436bbfac92420cb67fc405e2786c081b432
Signed-off-by: arya.k <arya.kumar@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/3265
Reviewed-by: Abhishek Pandey <abhi.siso@samsung.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
service/resource-encapsulation/examples/tizen/RESampleClientApp/edc_resource/re_client_control.edc
service/resource-encapsulation/examples/tizen/RESampleClientApp/src/reclient.cpp

index e936bbb..8146661 100644 (file)
@@ -28,13 +28,24 @@ group
     {
         part
         {
-            name: "find_button";
+            name: "button1";
             type: SWALLOW;
             scale: 1;
             description
             {
                 rel1.relative: 0.01 0.01;
-                rel2.relative: 0.7 0.09;
+                rel2.relative: 0.5 0.09;
+            }
+        }
+        part
+        {
+            name: "button2";
+            type: SWALLOW;
+            scale: 1;
+            description
+            {
+                rel1.relative: 0.51 0.01;
+                rel2.relative: 1.0 0.09;
             }
         }
         part
index bfd0755..7097230 100644 (file)
@@ -21,8 +21,6 @@
 #include "reclient.h"
 
 #include<iostream>
-#include "mutex"
-#include "condition_variable"
 
 #include "reclientmain.h"
 
@@ -44,13 +42,12 @@ constexpr int INCORRECT_INPUT = 2;
 constexpr int QUIT_INPUT = 3;
 
 std::shared_ptr<RCSRemoteResourceObject>  resource;
+std::vector<RCSRemoteResourceObject::Ptr> resourceList;
+std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> discoveryTask;
 
 const std::string defaultKey = "Temperature";
 const std::string resourceType = "oic.r.temperaturesensor";
 
-std::mutex mtx;
-std::condition_variable cond;
-
 static Evas_Object *log_entry = NULL;
 static Evas_Object *list = NULL;
 static Evas_Object *naviframe = NULL;
@@ -74,6 +71,7 @@ void *updateGroupLog(void *data)
 static void onDestroy()
 {
     dlog_print(DLOG_INFO, LOG_TAG, "#### Destroy sequence called");
+    resourceList.clear();
     resource = nullptr;
 }
 
@@ -84,21 +82,19 @@ void onResourceDiscovered(std::shared_ptr<RCSRemoteResourceObject> foundResource
     std::string resourceURI = foundResource->getUri();
     std::string hostAddress = foundResource->getAddress();
 
-    dlog_print(DLOG_INFO, LOG_TAG, "#### Resource URI :  %s", resourceURI.c_str());
-    dlog_print(DLOG_INFO, LOG_TAG, "#### Resource Host : %S", hostAddress.c_str());
-
-    string logMessage = "Resource Found <br>";
+    int resourceSize = resourceList.size() + 1;
+    string logMessage = "Resource Found : " + std::to_string(resourceSize) + "<br>";
     logMessage = logMessage + "URI: " + resourceURI + "<br>";
     logMessage = logMessage + "Host:" + hostAddress + "<br>";
     logMessage += "----------------------<br>";
-    dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
+    dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
                                           &logMessage);
 
-    resource = foundResource;
+    resourceList.push_back(foundResource);
 
-    ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))showClientAPIs, NULL);
-    cond.notify_all();
+    if ("/a/TempSensor" == resourceURI)
+        resource = foundResource;
 }
 
 void onResourceStateChanged(const ResourceState &resourceState)
@@ -158,7 +154,7 @@ void onCacheUpdated(const RCSResourceAttributes &attributes)
                                           &logMessage);
 }
 
-void onRemoteAttributesReceived(const RCSResourceAttributesattributes, int)
+void onRemoteAttributesReceived(const RCSResourceAttributes &attributes, int)
 {
     dlog_print(DLOG_INFO, LOG_TAG, "#### onRemoteAttributesReceived entry");
 
@@ -478,17 +474,57 @@ static void stopCaching(void *data, Evas_Object *obj, void *event_info)
 
 void discoverResource()
 {
-    dlog_print(DLOG_INFO, LOG_TAG, "#### Wait 2 seconds until discovered");
-
-    RCSDiscoveryManager::getInstance()->discoverResourceByType(RCSAddress::multicast(), resourceType,
-            &onResourceDiscovered);
+    dlog_print(DLOG_INFO, LOG_TAG, "#### discovery started");
 
-    std::unique_lock<std::mutex> lck(mtx);
-    cond.wait_for(lck, std::chrono::seconds(2));
+    while (!discoveryTask)
+    {
+        try
+        {
+            discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
+                                RCSAddress::multicast(), resourceType, &onResourceDiscovered);
+        }
+        catch (const RCSPlatformException &e)
+        {
+            std::cout << e.what() << std::endl;
+        }
+    }
 
     dlog_print(DLOG_INFO, LOG_TAG, "#### Discovery over");
 }
 
+void cancelDiscoverResource()
+{
+    dlog_print(DLOG_INFO, LOG_TAG, "#### cancelDiscoverResource entry");
+    string logMessage = "";
+
+    if (!discoveryTask)
+    {
+        logMessage += "There is no discovery request <br>";
+    }
+    else
+    {
+        discoveryTask->cancel();
+
+        logMessage += "Discovery canceled <br>";
+
+        int resourceSize = resourceList.size();
+        if (!resourceSize)
+        {
+            logMessage += "No Resource Discovered <br>";
+        }
+        else
+        {
+            logMessage += std::to_string(resourceSize) + " : Resource Discovered <br>";
+            ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))showClientAPIs, NULL);
+        }
+
+    }
+
+    dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
+    ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
+                                          &logMessage);
+}
+
 static void
 popup_cancel_clicked_cb(void *data, Evas_Object *obj, void *event_info)
 {
@@ -595,7 +631,7 @@ list_scheduled_actionset_cb(void *data, Evas_Object *obj, void *event_info)
     evas_object_show(popup);
 }
 
-// Method to be called when the Discover Resource UI Button is selected
+// Method to be called when the Start Discovery UI Button is selected
 static void
 find_resource_cb(void *data, Evas_Object *obj, void *event_info)
 {
@@ -609,6 +645,20 @@ find_resource_cb(void *data, Evas_Object *obj, void *event_info)
     }
 }
 
+// Method to be called when the Cancel Discovery UI Button is selected
+static void
+cancel_resource_cb(void *data, Evas_Object *obj, void *event_info)
+{
+    if (NULL != list)
+    {
+        cancelDiscoverResource();
+    }
+    else
+    {
+        dlog_print(DLOG_ERROR, "cancel_resource_cb", "list is NULL - So unable to add items!!!");
+    }
+}
+
 void *showClientAPIs(void *data)
 {
     // Add items to the list only if the list is empty
@@ -672,7 +722,8 @@ void group_cb(void *data, Evas_Object *obj, void *event_info)
     Evas_Object *layout;
     Evas_Object *scroller;
     Evas_Object *nf = (Evas_Object *)data;
-    Evas_Object *find_button;
+    Evas_Object *button1;
+    Evas_Object *button2;
     Elm_Object_Item *nf_it;
 
     naviframe = nf;
@@ -689,11 +740,17 @@ void group_cb(void *data, Evas_Object *obj, void *event_info)
 
     elm_object_content_set(scroller, layout);
 
-    // Button
-    find_button = elm_button_add(layout);
-    elm_object_part_content_set(layout, "find_button", find_button);
-    elm_object_text_set(find_button, "Discover Resource");
-    evas_object_smart_callback_add(find_button, "clicked", find_resource_cb, NULL);
+    // Start Discovery Button
+    button1 = elm_button_add(layout);
+    elm_object_part_content_set(layout, "button1", button1);
+    elm_object_text_set(button1, "Start Discovery");
+    evas_object_smart_callback_add(button1, "clicked", find_resource_cb, NULL);
+
+    // Cancel Discovery Button
+    button2 = elm_button_add(layout);
+    elm_object_part_content_set(layout, "button2", button2);
+    elm_object_text_set(button2, "Cancel Discovery");
+    evas_object_smart_callback_add(button2, "clicked", cancel_resource_cb, NULL);
 
     // List
     list = elm_list_add(layout);