Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / resource / examples / groupclient.cpp
old mode 100755 (executable)
new mode 100644 (file)
index 11afb80..89c5a61
 
 #include "OCPlatform.h"
 #include "OCApi.h"
+#include "logger.h"
 
 #include <functional>
 #include <pthread.h>
+#include <mutex>
+#include <condition_variable>
 #include <iostream>
+#include <mutex>
+
+#define DO_ACTION               "DoAction"
+#define GET_ACTIONSET           "GetActionSet"
+#define ACTIONSET               "ActionSet"
+#define DELETE_ACTIONSET        "DelActionSet"
 
 using namespace std;
 using namespace OC;
 namespace PH = std::placeholders;
+std::mutex resourceLock;
 
 OCResourceHandle resourceHandle;
-
 shared_ptr< OCResource > g_resource;
 vector< string > lights;
+std::mutex blocker;
+std::condition_variable cv;
 
 bool isReady = false;
 
@@ -44,6 +55,13 @@ void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, con
 
 void foundResource(std::shared_ptr< OCResource > resource)
 {
+    std::lock_guard<std::mutex> lock(resourceLock);
+    if(g_resource)
+    {
+        std::cout << "Found another resource, ignoring"<<std::endl;
+        return;
+    }
+
     std::string resourceURI;
     std::string hostAddress;
 
@@ -55,18 +73,18 @@ void foundResource(std::shared_ptr< OCResource > resource)
         {
             string resourceURI = resource->uri();
             cout << resourceURI << endl;
+            cout << "HOST :: " << resource->host() << endl;
             if (resourceURI == "/core/a/collection")
             {
                 g_resource = resource;
+                resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), onGet);
             }
-
-            g_resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), onGet);
             printf("HOST :: %s\n", resource->host().c_str());
         }
     }
     catch (std::exception& e)
     {
-        std::cout << "" << std::endl;
+        std::cerr << "Exception in foundResource: "<< e.what() << std::endl;
     }
 }
 
@@ -84,6 +102,7 @@ void onGet(const HeaderOptions& opt, const OCRepresentation &rep, const int eCod
     }
 
     isReady = true;
+    cv.notify_one();
 }
 
 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
@@ -121,8 +140,39 @@ void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, con
     }
 }
 
-int main()
+string buildActionSetDesc(unsigned int delay = 0, unsigned int type = 0)
+{
+    string actionsetDesc = "";
+    actionsetDesc = "allbulboff";
+    actionsetDesc.append("*");
+    actionsetDesc.append(std::to_string(delay));        // Set delay time.
+    actionsetDesc.append(" ");
+    actionsetDesc.append(std::to_string(type));         // Set action type.
+    actionsetDesc.append("*");
+    for (auto iter = lights.begin(); iter != lights.end(); ++iter)
+    {
+        actionsetDesc.append("uri=").append((*iter));
+        actionsetDesc.append("|");
+        actionsetDesc.append("power=");
+        actionsetDesc.append("off");
+        if ((iter + 1) != lights.end())
+        {
+            actionsetDesc.append("*");
+        }
+    }
+    return actionsetDesc;
+}
+
+bool isResourceReady()
+{
+    return isReady;
+}
+
+int main(int argc, char* argv[])
 {
+    ostringstream requestURI;
+    requestURI << OC_MULTICAST_DISCOVERY_URI << "?rt=a.collection";
+
     PlatformConfig config
     { OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos };
 
@@ -133,102 +183,82 @@ int main()
         OCPlatform::Configure(config);
 
         string resourceTypeName = "a.collection";
-        OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=a.collection", &foundResource);
 
-        isReady = false;
+        OCPlatform::findResource("", requestURI.str(),
+                                 OC_ALL, &foundResource);
+
+        //Non-intensive block until foundResource callback is called by OCPlatform
+        //and onGet gets resource.
+        //isResourceReady takes care of spurious wake-up
+
+        std::unique_lock<std::mutex> lock(blocker);
+        cv.wait(lock, isResourceReady);
 
+        isReady = false;
         while (isRun)
         {
-            usleep(100);
-            while (isReady)
-            {
-                int n;
+            int selectedMenu;
 
-                cout << endl
-                        << "1 :: CREATE ACTIONSET 2 :: EXECUTE ACTION SET 3 :: GET ACTIONSET\n";
-                cout << "4 :: DELETE ACTIONSET 5 :: Quit\n";
+            cout << endl <<  "0 :: Quit 1 :: CREATE ACTIONSET 2 :: EXECUTE ACTION SET \n";
+            cout << "3 :: GET ACTIONSET 4 :: DELETE ACTIONSET \n" << endl;
 
-                cin >> n;
-                if (n == 1)
-                {
-                    string actionsetDesc;
-                    //"movieTime*uri=coap://10.251.44.228:49858/a/light|power=10*uri=coap://10.251.44.228:49858/a/light|power=10|";
+            cin >> selectedMenu;
+            OCRepresentation rep;
+            string actionsetDesc;
 
-                    actionsetDesc = "allbulboff";
-                    actionsetDesc.append("*");
-                    for (auto iter = lights.begin(); iter != lights.end(); ++iter)
+            switch(selectedMenu)
+            {
+                case 0:
+                    isRun = false;
+                    break;
+                case 1:
+                    actionsetDesc = buildActionSetDesc();
+                    if(!actionsetDesc.empty())
                     {
-                        actionsetDesc.append("uri=").append((*iter));
-                        actionsetDesc.append("|");
-                        actionsetDesc.append("power=");
-                        actionsetDesc.append("off");
-                        if ((iter + 1) != lights.end())
-                        {
-                            actionsetDesc.append("*");
-                        }
+                        cout << "ActionSet :: " << actionsetDesc << endl;
+                        rep.setValue("ActionSet", actionsetDesc);
                     }
-
-                    cout << "ActionSet :: " << actionsetDesc << endl;
-
-                    OCRepresentation rep;
-                    rep.setValue("ActionSet", actionsetDesc);
-
                     if (g_resource)
                     {
                         g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
-                                &onPut);
+                        &onPut);
                     }
-                }
-                else if (n == 2)
-                {
-                    OCRepresentation rep;
-
-                    rep.setValue("DoAction", std::string("allbulboff"));
-
+                    break;
+                case 2:
+                    rep.setValue(DO_ACTION, std::string("allbulboff"));
                     if (g_resource)
                     {
                         g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
-                                &onPost);
-                    }
-                }
-                else if (n == 3)
-                {
-                    OCRepresentation rep;
-
-                    rep.setValue("GetActionSet", std::string("allbulboff"));
-
+                                         &onPost);
+                     }
+                     break;
+                case 3:
+                    rep.setValue(GET_ACTIONSET, std::string("allbulboff"));
                     if (g_resource)
                     {
                         g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
                                 &onPost);
                     }
-                }
-                else if (n == 4)
-                {
-                    OCRepresentation rep;
-
+                    break;
+                case 4:
                     rep.setValue("DelActionSet", std::string("allbulboff"));
-
                     if (g_resource)
                     {
                         g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
                                 &onPut);
                     }
-                }
-                else if (n == 5)
-                {
-                    isRun = false;
                     break;
-                }
-
-                fflush(stdin);
+                default:
+                    cout << "Invalid option" << endl;
+                    break;
             }
+            fflush(stdin);
         }
     }
     catch (OCException& e)
     {
         cout << e.what() << endl;
     }
-
     return 0;
 }
+