Update snapshot(2017-12-14)
[platform/upstream/iotivity.git] / resource / examples / groupclient.cpp
index 88a92db..cde8eb2 100644 (file)
 #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;
 
@@ -50,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;
 
@@ -61,22 +73,22 @@ 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;
     }
 }
 
-void onGet(const HeaderOptions& opt, const OCRepresentation &rep, const int eCode)
+void onGet(const HeaderOptions& /*opt*/, const OCRepresentation &rep, const int /*eCode*/)
 {
     // printf("onGet\n");
 
@@ -93,12 +105,14 @@ void onGet(const HeaderOptions& opt, const OCRepresentation &rep, const int eCod
     cv.notify_one();
 }
 
-void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
+void onPut(const HeaderOptions& /*headerOptions*/,
+        const OCRepresentation& /*rep*/, const int /*eCode*/)
 {
     printf("\nonPut\n");
 }
 
-void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
+void onPost(const HeaderOptions& /*headerOptions*/,
+        const OCRepresentation& rep, const int /*eCode*/)
 {
     printf("\nonPost\n");
 
@@ -128,11 +142,15 @@ void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, con
     }
 }
 
-string buildActionSetDesc()
+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));
@@ -152,53 +170,10 @@ bool isResourceReady()
     return isReady;
 }
 
-int main(int argc, char* argv[])
+int main(int /*argc*/, char** /*argv[]*/)
 {
     ostringstream requestURI;
-    requestURI << OC_WELL_KNOWN_QUERY << "?rt=a.collection";
-
-    OCConnectivityType connectivityType = OC_WIFI;
-
-    if(argc == 2)
-    {
-        try
-        {
-            std::size_t inputValLen;
-            int optionSelected = stoi(argv[1], &inputValLen);
-
-            if(inputValLen == strlen(argv[1]))
-            {
-                if(optionSelected == 0)
-                {
-                    connectivityType = OC_ETHERNET;
-                }
-                else if(optionSelected == 1)
-                {
-                    connectivityType = OC_WIFI;
-                }
-                else
-                {
-                    std::cout << "Invalid connectivity type selected. Using default WIFI"
-                        << std::endl;
-                }
-            }
-            else
-            {
-                std::cout << "Invalid connectivity type selected. Using default WIFI" << std::endl;
-            }
-        }
-        catch(exception& e)
-        {
-            std::cout << "Invalid input argument. Using WIFI as connectivity type" << std::endl;
-        }
-    }
-    else
-    {
-        std::cout<<"Usage: groupclient <ConnectivityType(0|1)>\n";
-        std::cout<<"ConnectivityType: Default WIFI\n";
-        std::cout<<"ConnectivityType 0: ETHERNET\n";
-        std::cout<<"ConnectivityType 1: WIFI\n";
-    }
+    requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=a.collection";
 
     PlatformConfig config
     { OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos };
@@ -212,7 +187,7 @@ int main(int argc, char* argv[])
         string resourceTypeName = "a.collection";
 
         OCPlatform::findResource("", requestURI.str(),
-                                 connectivityType, &foundResource);
+                                 CT_DEFAULT, &foundResource);
 
         //Non-intensive block until foundResource callback is called by OCPlatform
         //and onGet gets resource.
@@ -252,7 +227,7 @@ int main(int argc, char* argv[])
                     }
                     break;
                 case 2:
-                    rep.setValue("DoAction", std::string("allbulboff"));
+                    rep.setValue(DO_ACTION, std::string("allbulboff"));
                     if (g_resource)
                     {
                         g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
@@ -260,7 +235,7 @@ int main(int argc, char* argv[])
                      }
                      break;
                 case 3:
-                    rep.setValue("GetActionSet", std::string("allbulboff"));
+                    rep.setValue(GET_ACTIONSET, std::string("allbulboff"));
                     if (g_resource)
                     {
                         g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
@@ -279,7 +254,6 @@ int main(int argc, char* argv[])
                     cout << "Invalid option" << endl;
                     break;
             }
-            fflush(stdin);
         }
     }
     catch (OCException& e)
@@ -288,3 +262,4 @@ int main(int argc, char* argv[])
     }
     return 0;
 }
+