Update snapshot(2017-12-14)
[platform/upstream/iotivity.git] / resource / examples / groupclient.cpp
index e6724a0..cde8eb2 100644 (file)
 #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;
@@ -74,15 +79,16 @@ void foundResource(std::shared_ptr< OCResource > resource)
                 g_resource = resource;
                 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");
 
@@ -99,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");
 
@@ -134,13 +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 = "";
-    //"movieTime*uri=coap://10.251.44.228:49858/a/light|power=10*uri=coap:
-    //10.251.44.228:49858/a/light|power=10|";
     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));
@@ -159,8 +169,12 @@ bool isResourceReady()
 {
     return isReady;
 }
-int main()
+
+int main(int /*argc*/, char** /*argv[]*/)
 {
+    ostringstream requestURI;
+    requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=a.collection";
+
     PlatformConfig config
     { OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos };
 
@@ -171,28 +185,29 @@ int main()
         OCPlatform::Configure(config);
 
         string resourceTypeName = "a.collection";
-        OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=a.collection", &foundResource);
 
-        //Non-intensive block util foundResource callback is called by OCPlatform
+        OCPlatform::findResource("", requestURI.str(),
+                                 CT_DEFAULT, &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);
             int selectedMenu;
 
-            cout << endl
-                 << "0 :: Quit 1 :: CREATE ACTIONSET 2 :: EXECUTE ACTION SET \n";
+            cout << endl <<  "0 :: Quit 1 :: CREATE ACTIONSET 2 :: EXECUTE ACTION SET \n";
             cout << "3 :: GET ACTIONSET 4 :: DELETE ACTIONSET \n" << endl;
 
             cin >> selectedMenu;
-
             OCRepresentation rep;
             string actionsetDesc;
+
             switch(selectedMenu)
             {
                 case 0:
@@ -200,33 +215,31 @@ int main()
                     break;
                 case 1:
                     actionsetDesc = buildActionSetDesc();
-
                     if(!actionsetDesc.empty())
                     {
                         cout << "ActionSet :: " << actionsetDesc << endl;
                         rep.setValue("ActionSet", actionsetDesc);
                     }
-
                     if (g_resource)
                     {
                         g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
-                                &onPut);
+                        &onPut);
                     }
                     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(),
-                            &onPost);
-                    }
-                    break;
+                                         &onPost);
+                     }
+                     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(),
-                            &onPost);
+                                &onPost);
                     }
                     break;
                 case 4:
@@ -234,21 +247,19 @@ int main()
                     if (g_resource)
                     {
                         g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
-                            &onPut);
+                                &onPut);
                     }
                     break;
                 default:
-                    cout << "Incorrect option" << endl;
+                    cout << "Invalid option" << endl;
                     break;
-
             }
-            fflush(stdin);
         }
     }
     catch (OCException& e)
     {
         cout << e.what() << endl;
     }
-
     return 0;
 }
+